24 require_once
CORE_PATH .
'libs/db/adapters/pdo/interface.php';
96 if (!extension_loaded(
'pdo')) {
97 throw new KumbiaException(
'Debe cargar la extensión de PHP llamada php_pdo');
101 $this->pdo =
new PDO(
$config[
'type'] .
":" .
$config[
'dsn'], $config[
'username'], $config[
'password']);
103 throw new KumbiaException(
"No se pudo realizar la conexion con $this->db_rbdm");
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);
111 if ($config[
'type'] ==
'mysql' and isset($config[
'charset'])) {
112 $this->pdo->exec(
'set character set ' . $config[
'charset']);
116 }
catch (PDOException $e) {
129 $this->
debug($sql_query);
134 throw new KumbiaException(
'No hay conexión para realizar esta acción');
136 $this->last_query = $sql_query;
137 $this->pdo_statement = null;
145 }
catch (PDOException $e) {
146 throw new KumbiaException($this->
error($e->getMessage() .
" al ejecutar <em>\"$sql_query\"</em>"));
156 public function exec($sql_query)
158 $this->
debug(
">" . $sql_query);
163 throw new KumbiaException(
'No hay conexión para realizar esta acción');
165 $this->last_query = $sql_query;
166 $this->pdo_statement = null;
168 $result = $this->pdo->exec($sql_query);
170 if ($result ===
false) {
174 }
catch (PDOException $e) {
202 $opt = self::DB_BOTH;
205 throw new KumbiaException(
'No hay conexión para realizar esta acción');
216 }
catch (PDOException $e) {
242 return count(
$pdo->fetchAll(PDO::FETCH_NUM));
258 throw new KumbiaException(
'No hay conexión para realizar esta acción');
268 return $meta[
'name'];
269 }
catch (PDOException $e) {
296 throw new KumbiaException(
'No hay conexión para realizar esta acción');
301 if ($row_count ===
false) {
305 }
catch (PDOException $e) {
321 $error = $this->pdo->errorInfo();
326 $this->last_error.= $error .
" [" . $err .
"]";
341 $error = $this->pdo->errorInfo();
354 return $this->pdo->lastInsertId();
363 return $this->pdo->beginTransaction();
372 return $this->pdo->rollBack();
381 return $this->pdo->commit();
391 return "'" . addslashes($value) .
"'";
402 public function insert($table, $values, $fields=null)
405 if (is_array($values)) {
406 if (!count($values)) {
407 throw new KumbiaException(
"Imposible realizar inserción en $table sin datos");
409 if (is_array($fields)) {
410 $insert_sql =
"INSERT INTO $table (" . join(
",", $fields) .
") VALUES (" . join(
",", $values) .
")";
412 $insert_sql =
"INSERT INTO $table VALUES (" . join(
",", $values) .
")";
414 return $this->
exec($insert_sql);
416 throw new KumbiaException(
'El segundo parametro para insert no es un Array');
429 public function update($table, $fields, $values, $where_condition=null)
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');
436 $update_values = array();
437 foreach ($fields as $field) {
438 $update_values[] = $field .
' = ' . $values[$i];
441 $update_sql.= join(
',', $update_values);
442 if ($where_condition != null) {
443 $update_sql.=
" WHERE $where_condition";
445 return $this->
exec($update_sql);
454 public function delete($table, $where_condition)
456 if ($where_condition) {
457 return $this->
exec(
"DELETE FROM $table WHERE $where_condition");
459 return $this->
exec(
"DELETE FROM $table");