KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
pdo.php
Ir a la documentación de este archivo.
1 <?php
24 require_once CORE_PATH . 'libs/db/adapters/pdo/interface.php';
25 
33 abstract class DbPDO extends DbBase implements DbPDOInterface
34 {
35 
41  protected $pdo;
53  protected $last_query;
59  protected $last_error;
63  protected $affected_rows;
67  protected $db_rbdm;
68 
73  const DB_ASSOC = PDO::FETCH_ASSOC;
74 
79  const DB_BOTH = PDO::FETCH_BOTH;
80 
85  const DB_NUM = PDO::FETCH_NUM;
86 
93  public function connect($config)
94  {
95 
96  if (!extension_loaded('pdo')) {
97  throw new KumbiaException('Debe cargar la extensión de PHP llamada php_pdo');
98  }
99 
100  try {
101  $this->pdo = new PDO($config['type'] . ":" . $config['dsn'], $config['username'], $config['password']);
102  if (!$this->pdo) {
103  throw new KumbiaException("No se pudo realizar la conexion con $this->db_rbdm");
104  }
105  if ($this->db_rbdm != 'odbc') {
106  $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
107  $this->pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
108  $this->pdo->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY);
109  }
110  //Selecciona charset
111  if ($config['type'] == 'mysql' and isset($config['charset'])) {
112  $this->pdo->exec('set character set ' . $config['charset']);
113  }
114  $this->initialize();
115  return true;
116  } catch (PDOException $e) {
117  throw new KumbiaException($this->error($e->getMessage()));
118  }
119  }
120 
127  public function query($sql_query)
128  {
129  $this->debug($sql_query);
130  if ($this->logger) {
131  Logger::debug($sql_query);
132  }
133  if (!$this->pdo) {
134  throw new KumbiaException('No hay conexión para realizar esta acción');
135  }
136  $this->last_query = $sql_query;
137  $this->pdo_statement = null;
138  try {
139  if ($pdo_statement = $this->pdo->query($sql_query)) {
140  $this->pdo_statement = $pdo_statement;
141  return $pdo_statement;
142  } else {
143  return false;
144  }
145  } catch (PDOException $e) {
146  throw new KumbiaException($this->error($e->getMessage() . " al ejecutar <em>\"$sql_query\"</em>"));
147  }
148  }
149 
156  public function exec($sql_query)
157  {
158  $this->debug(">" . $sql_query);
159  if ($this->logger) {
160  Logger::debug($sql_query);
161  }
162  if (!$this->pdo) {
163  throw new KumbiaException('No hay conexión para realizar esta acción');
164  }
165  $this->last_query = $sql_query;
166  $this->pdo_statement = null;
167  try {
168  $result = $this->pdo->exec($sql_query);
169  $this->affected_rows = $result;
170  if ($result === false) {
171  throw new KumbiaException($this->error(" al ejecutar <em>\"$sql_query\"</em>"));
172  }
173  return $result;
174  } catch (PDOException $e) {
175  throw new KumbiaException($this->error(" al ejecutar <em>\"$sql_query\"</em>"));
176  }
177  }
178 
183  public function close()
184  {
185  if ($this->pdo) {
186  unset($this->pdo);
187  return true;
188  }
189  return false;
190  }
191 
199  public function fetch_array($pdo_statement=NULL, $opt='')
200  {
201  if ($opt === '') {
202  $opt = self::DB_BOTH;
203  }
204  if (!$this->pdo) {
205  throw new KumbiaException('No hay conexión para realizar esta acción');
206  }
207  if (!$pdo_statement) {
209  if (!$pdo_statement) {
210  return false;
211  }
212  }
213  try {
214  $pdo_statement->setFetchMode($opt);
215  return $pdo_statement->fetch();
216  } catch (PDOException $e) {
217  throw new KumbiaException($this->error($e->getMessage()));
218  }
219  }
220 
226  public function __construct($config)
227  {
228  $this->connect($config);
229  }
230 
238  public function num_rows($pdo_statement='')
239  {
240  if ($pdo_statement) {
241  $pdo = clone $pdo_statement;
242  return count($pdo->fetchAll(PDO::FETCH_NUM));
243  } else {
244  return 0;
245  }
246  }
247 
255  public function field_name($number, $pdo_statement=NULL)
256  {
257  if (!$this->pdo) {
258  throw new KumbiaException('No hay conexión para realizar esta acción');
259  }
260  if (!$pdo_statement) {
262  if (!$pdo_statement) {
263  return false;
264  }
265  }
266  try {
267  $meta = $pdo_statement->getColumnMeta($number);
268  return $meta['name'];
269  } catch (PDOException $e) {
270  throw new KumbiaException($this->error($e->getMessage()));
271  }
272  }
273 
281  public function data_seek($number, $pdo_statement=NULL)
282  {
283  return false;
284  }
285 
293  public function affected_rows($pdo_statement=NULL)
294  {
295  if (!$this->pdo) {
296  throw new KumbiaException('No hay conexión para realizar esta acción');
297  }
298  if ($pdo_statement) {
299  try {
300  $row_count = $pdo_statement->rowCount();
301  if ($row_count === false) {
302  throw new KumbiaException($this->error(" al ejecutar <em>\"$sql_query\"</em>"));
303  }
304  return $row_count;
305  } catch (PDOException $e) {
306  throw new KumbiaException($this->error($e->getMessage()));
307  }
308  } else {
309  return $this->affected_rows;
310  }
311  }
312 
318  public function error($err='')
319  {
320  if ($this->pdo) {
321  $error = $this->pdo->errorInfo();
322  $error = $error[2];
323  } else {
324  $error = "";
325  }
326  $this->last_error.= $error . " [" . $err . "]";
327  if ($this->logger) {
328  Logger::error($this->last_error);
329  }
330  return $this->last_error;
331  }
332 
338  public function no_error($number=0)
339  {
340  if ($this->pdo) {
341  $error = $this->pdo->errorInfo();
342  $number = $error[1];
343  }
344  return $number;
345  }
346 
352  public function last_insert_id($table='', $primary_key='')
353  {
354  return $this->pdo->lastInsertId();
355  }
356 
361  public function begin()
362  {
363  return $this->pdo->beginTransaction();
364  }
365 
370  public function rollback()
371  {
372  return $this->pdo->rollBack();
373  }
374 
379  public function commit()
380  {
381  return $this->pdo->commit();
382  }
383 
389  static public function add_quotes($value)
390  {
391  return "'" . addslashes($value) . "'";
392  }
393 
402  public function insert($table, $values, $fields=null)
403  {
404  //$insert_sql = "";
405  if (is_array($values)) {
406  if (!count($values)) {
407  throw new KumbiaException("Imposible realizar inserción en $table sin datos");
408  }
409  if (is_array($fields)) {
410  $insert_sql = "INSERT INTO $table (" . join(",", $fields) . ") VALUES (" . join(",", $values) . ")";
411  } else {
412  $insert_sql = "INSERT INTO $table VALUES (" . join(",", $values) . ")";
413  }
414  return $this->exec($insert_sql);
415  } else {
416  throw new KumbiaException('El segundo parametro para insert no es un Array');
417  }
418  }
419 
429  public function update($table, $fields, $values, $where_condition=null)
430  {
431  $update_sql = "UPDATE $table SET ";
432  if (count($fields) != count($values)) {
433  throw new KumbiaException('El número de valores a actualizar no es el mismo de los campos');
434  }
435  $i = 0;
436  $update_values = array();
437  foreach ($fields as $field) {
438  $update_values[] = $field . ' = ' . $values[$i];
439  $i++;
440  }
441  $update_sql.= join(',', $update_values);
442  if ($where_condition != null) {
443  $update_sql.= " WHERE $where_condition";
444  }
445  return $this->exec($update_sql);
446  }
447 
454  public function delete($table, $where_condition)
455  {
456  if ($where_condition) {
457  return $this->exec("DELETE FROM $table WHERE $where_condition");
458  } else {
459  return $this->exec("DELETE FROM $table");
460  }
461  }
462 
468  public function last_sql_query()
469  {
470  return $this->last_query;
471  }
472 
473 }