00001 <?php
00027 require_once CORE_PATH.'extensions/db/adapters/pdo/interface.php';
00028
00029 abstract class DbPDO extends DbBase implements DbPDOInterface {
00030
00036 protected $pdo;
00037
00043 public $pdo_statement;
00044
00050 protected $last_query;
00056 protected $last_error;
00057
00061 protected $affected_rows;
00062
00067 const DB_ASSOC = PDO::FETCH_ASSOC;
00068
00073 const DB_BOTH = PDO::FETCH_BOTH;
00074
00079 const DB_NUM = PDO::FETCH_NUM;
00086 public function connect($config){
00087
00088 if(!extension_loaded('pdo')){
00089 throw new KumbiaException('Debe cargar la extensión de PHP llamada php_pdo');
00090 return false;
00091 }
00092
00093 try {
00094 $this->pdo = new PDO($config['type'] . ":" . $config['dsn'], $config['username'], $config['password']);
00095 if(!$this->pdo){
00096 throw new KumbiaException("No se pudo realizar la conexion con $this->db_rbdm", 0, false);
00097 }
00098 if($this->db_rbdm!='odbc'){
00099 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
00100 $this->pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
00101 $this->pdo->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY);
00102 }
00103 $this->initialize();
00104 return true;
00105 } catch(PDOException $e) {
00106 throw new KumbiaException($this->error($e->getMessage()), $this->no_error($e->getCode()), false);
00107 }
00108
00109 }
00110
00117 public function query($sql_query){
00118 $this->debug($sql_query);
00119 if($this->logger){
00120 Logger::debug($sql_query);
00121 }
00122 if(!$this->pdo){
00123 throw new KumbiaException("No hay conexión para realizar esta acción:", 0);
00124 }
00125 $this->last_query = $sql_query;
00126 $this->pdo_statement = null;
00127 try {
00128 if($pdo_statement = $this->pdo->query($sql_query)){
00129 $this->pdo_statement = $pdo_statement;
00130 return $pdo_statement;
00131 } else {
00132 return false;
00133 }
00134 }
00135 catch(PDOException $e) {
00136 throw new KumbiaException($this->error($e->getMessage()." al ejecutar <i>\"$sql_query\"</i>"), $this->no_error($e->getCode()));
00137 }
00138 }
00139
00146 public function exec($sql_query){
00147 $this->debug(">".$sql_query);
00148 if($this->logger){
00149 Logger::debug($sql_query);
00150 }
00151 if(!$this->pdo){
00152 throw new KumbiaException("No hay conexión para realizar esta acción:", 0);
00153 }
00154 $this->last_query = $sql_query;
00155 $this->pdo_statement = null;
00156 try {
00157 $result = $this->pdo->exec($sql_query);
00158 $this->affected_rows = $result;
00159 if($result===false){
00160 throw new KumbiaException($this->error(" al ejecutar <i>\"$sql_query\"</i>"), $this->no_error());
00161 }
00162 return $result;
00163 }
00164 catch(PDOException $e) {
00165 throw new KumbiaException($this->error(" al ejecutar <i>\"$sql_query\"</i>"), $this->no_error());
00166 }
00167 }
00168
00172 public function close(){
00173 if($this->pdo) {
00174 unset($this->pdo);
00175 return true;
00176 }
00177 return false;
00178 }
00179
00187 public function fetch_array($pdo_statement='', $opt=''){
00188 if($opt==='') {
00189 $opt = db::DB_BOTH;
00190 }
00191 if(!$this->pdo){
00192 throw new KumbiaException("No hay conexión para realizar esta acción:", 0);
00193 return false;
00194 }
00195 if(!$pdo_statement){
00196 $pdo_statement = $this->pdo_statement;
00197 if(!$pdo_statement){
00198 return false;
00199 }
00200 }
00201 try {
00202 $pdo_statement->setFetchMode($opt);
00203 return $pdo_statement->fetch();
00204 }
00205 catch(PDOException $e) {
00206 throw new KumbiaException($this->error($e->getMessage()), $this->no_error($e->getCode()));
00207 }
00208 }
00209
00215 public function __construct($config){
00216 $this->connect($config);
00217 }
00218
00226 public function num_rows($pdo_statement=''){
00227 if($pdo_statement){
00228 $pdo = clone $pdo_statement;
00229 return count($pdo->fetchAll(PDO::FETCH_NUM));
00230 } else {
00231 return 0;
00232 }
00233 }
00234
00242 public function field_name($number, $pdo_statement=''){
00243 if(!$this->pdo){
00244 throw new KumbiaException("No hay conexión para realizar esta acción:", 0);
00245 return false;
00246 }
00247 if(!$pdo_statement){
00248 $pdo_statement = $this->pdo_statement;
00249 if(!$pdo_statement){
00250 return false;
00251 }
00252 }
00253 try {
00254 $meta = $pdo_statement->getColumnMeta($number);
00255 return $meta['name'];
00256 }
00257 catch(PDOException $e) {
00258 throw new KumbiaException($this->error($e->getMessage()), $this->no_error($e->getCode()));
00259 }
00260 return false;
00261 }
00262
00263
00271 public function data_seek($number, $pdo_statement=''){
00272 return false;
00273 }
00274
00282 public function affected_rows($pdo_statement=''){
00283 if(!$this->pdo){
00284 throw new KumbiaException("No hay conexión para realizar esta acción:", 0);
00285 return false;
00286 }
00287 if($pdo_statement){
00288 try {
00289 $row_count = $pdo_statement->rowCount();
00290 if($row_count===false){
00291 throw new KumbiaException($this->error(" al ejecutar <i>\"$sql_query\"</i>"), $this->no_error());
00292 }
00293 return $row_count;
00294 }
00295 catch(PDOException $e) {
00296 throw new KumbiaException($this->error($e->getMessage()), $this->no_error($e->getCode()));
00297 }
00298 } else {
00299 return $this->affected_rows;
00300 }
00301 return false;
00302 }
00303
00309 public function error($err=''){
00310 if($this->pdo){
00311 $error = $this->pdo->errorInfo();
00312 $error = $error[2];
00313 } else {
00314 $error = "";
00315 }
00316 $this->last_error.= $error." [".$err."]";
00317 if($this->logger){
00318 Logger::error($this->last_error);
00319 }
00320 return $this->last_error;
00321 }
00322
00328 public function no_error($number=0){
00329 if($this->pdo){
00330 $error = $this->pdo->errorInfo();
00331 $number = $error[1];
00332 }
00333 return $number;
00334 }
00335
00341 public function last_insert_id($table='', $primary_key=''){
00342 if(!$this->pdo){
00343 return false;
00344 }
00345 return $this->pdo->lastInsertId();
00346 }
00347
00352 public function begin(){
00353 return $this->pdo->beginTransaction();
00354 }
00355
00356
00361 public function rollback(){
00362 return $this->pdo->rollBack();
00363 }
00364
00369 public function commit(){
00370 return $this->pdo->commit();
00371 }
00372
00378 static public function add_quotes($value){
00379 return "'".addslashes($value)."'";
00380 }
00381
00390 public function insert($table, $values, $fields=null){
00391 $insert_sql = "";
00392 if(is_array($values)){
00393 if(!count($values)){
00394 new KumbiaException("Imposible realizar inserción en $table sin datos");
00395 }
00396 if(is_array($fields)){
00397 $insert_sql = "INSERT INTO $table (".join(",", $fields).") VALUES (".join(",", $values).")";
00398 } else {
00399 $insert_sql = "INSERT INTO $table VALUES (".join(",", $values).")";
00400 }
00401 return $this->exec($insert_sql);
00402 } else{
00403 throw new KumbiaException("El segundo parametro para insert no es un Array");
00404 }
00405 }
00406
00416 public function update($table, $fields, $values, $where_condition=null){
00417 $update_sql = "UPDATE $table SET ";
00418 if(count($fields)!=count($values)){
00419 throw new KumbiaException('Los número de valores a actualizar no es el mismo de los campos');
00420 }
00421 $i = 0;
00422 $update_values = array();
00423 foreach($fields as $field){
00424 $update_values[] = $field.' = '.$values[$i];
00425 $i++;
00426 }
00427 $update_sql.= join(',', $update_values);
00428 if($where_condition!=null){
00429 $update_sql.= " WHERE $where_condition";
00430 }
00431 return $this->exec($update_sql);
00432 }
00433
00440 public function delete($table, $where_condition){
00441 if($where_condition){
00442 return $this->exec("DELETE FROM $table WHERE $where_condition");
00443 } else {
00444 return $this->exec("DELETE FROM $table");
00445 }
00446 }
00447
00448 }