KumbiaPHP beta2-dev
Framework PHP en español
pdo.php
Ir a la documentación de este archivo.
00001 <?php
00027 require_once CORE_PATH.'libs/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                 }
00091 
00092                 try {
00093                         $this->pdo = new PDO($config['type'] . ":" . $config['dsn'], $config['username'], $config['password']);
00094                         if(!$this->pdo){
00095                                 throw new KumbiaException("No se pudo realizar la conexion con $this->db_rbdm");
00096                         }
00097                         if($this->db_rbdm!='odbc'){
00098                                 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
00099                                 $this->pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
00100                                 $this->pdo->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY);
00101                         }
00102                         //Selecciona charset
00103                         if($config['type'] == 'mysql' and isset($config['charset'])){
00104                                 $this->pdo->exec('set character set '.$config['charset']);
00105                         }
00106                         $this->initialize();
00107                         return true;
00108                 } catch(PDOException $e) {
00109                         throw new KumbiaException($this->error($e->getMessage()));
00110                 }
00111 
00112         }
00113 
00120         public function query($sql_query){
00121                 $this->debug($sql_query);
00122         if($this->logger){
00123             Logger::debug($sql_query);
00124         }
00125                 if(!$this->pdo){
00126                         throw new KumbiaException('No hay conexión para realizar esta acción');
00127                 }
00128                 $this->last_query = $sql_query;
00129                 $this->pdo_statement = null;
00130                 try {
00131                         if($pdo_statement = $this->pdo->query($sql_query)){
00132                                 $this->pdo_statement = $pdo_statement;
00133                                 return $pdo_statement;
00134                         } else {
00135                                 return false;
00136                         }
00137                 }
00138                 catch(PDOException $e) {
00139                         throw new KumbiaException($this->error($e->getMessage()." al ejecutar <em>\"$sql_query\"</em>"));
00140                 }
00141         }
00142 
00149         public function exec($sql_query){
00150                 $this->debug(">".$sql_query);
00151         if($this->logger){
00152             Logger::debug($sql_query);
00153         }
00154                 if(!$this->pdo){
00155                         throw new KumbiaException('No hay conexión para realizar esta acción');
00156                 }
00157                 $this->last_query = $sql_query;
00158                 $this->pdo_statement = null;
00159                 try {
00160                         $result = $this->pdo->exec($sql_query);
00161                         $this->affected_rows = $result;
00162                         if($result===false){
00163                                 throw new KumbiaException($this->error(" al ejecutar <em>\"$sql_query\"</em>"));
00164                         }
00165                         return $result;
00166                 }
00167                 catch(PDOException $e) {
00168                         throw new KumbiaException($this->error(" al ejecutar <em>\"$sql_query\"</em>"));
00169                 }
00170         }
00171 
00176         public function close(){
00177                 if($this->pdo) {
00178                         unset($this->pdo);
00179                         return true;
00180                 }
00181                 return false;
00182         }
00183 
00191         public function fetch_array($pdo_statement='', $opt=''){
00192                 if($opt==='') {
00193                         $opt = self::DB_BOTH;
00194                 }
00195                 if(!$this->pdo){
00196                         throw new KumbiaException('No hay conexión para realizar esta acción');
00197                 }
00198                 if(!$pdo_statement){
00199                         $pdo_statement = $this->pdo_statement;
00200                         if(!$pdo_statement){
00201                                 return false;
00202                         }
00203                 }
00204                 try {
00205                         $pdo_statement->setFetchMode($opt);
00206                         return $pdo_statement->fetch();
00207                 }
00208                 catch(PDOException $e) {
00209                         throw new KumbiaException($this->error($e->getMessage()));
00210                 }
00211         }
00212 
00218         public function __construct($config){
00219                 $this->connect($config);
00220         }
00221 
00229         public function num_rows($pdo_statement=''){
00230                 if($pdo_statement){
00231                         $pdo = clone $pdo_statement;
00232                         return count($pdo->fetchAll(PDO::FETCH_NUM));
00233                 } else {
00234                         return 0;
00235                 }
00236         }
00237 
00245         public function field_name($number, $pdo_statement=''){
00246                 if(!$this->pdo){
00247                         throw new KumbiaException('No hay conexión para realizar esta acción');
00248                 }
00249                 if(!$pdo_statement){
00250                         $pdo_statement = $this->pdo_statement;
00251                         if(!$pdo_statement){
00252                                 return false;
00253                         }
00254                 }
00255                 try {
00256                         $meta = $pdo_statement->getColumnMeta($number);
00257                         return $meta['name'];
00258                 }
00259                 catch(PDOException $e) {
00260                         throw new KumbiaException($this->error($e->getMessage()));
00261                 }
00262                 return false;
00263         }
00264 
00265 
00273         public function data_seek($number, $pdo_statement=''){
00274                 return false;
00275         }
00276 
00284         public function affected_rows($pdo_statement=''){
00285                 if(!$this->pdo){
00286                         throw new KumbiaException('No hay conexión para realizar esta acción');
00287                 }
00288                 if($pdo_statement){
00289                         try {
00290                                 $row_count = $pdo_statement->rowCount();
00291                                 if($row_count===false){
00292                                         throw new KumbiaException($this->error(" al ejecutar <em>\"$sql_query\"</em>"));
00293                                 }
00294                                 return $row_count;
00295                         }
00296                         catch(PDOException $e) {
00297                                 throw new KumbiaException($this->error($e->getMessage()));
00298                         }
00299                 } else {
00300                         return $this->affected_rows;
00301                 }
00302                 return false;
00303         }
00304 
00310         public function error($err=''){
00311                 if($this->pdo){
00312                         $error = $this->pdo->errorInfo();
00313                         $error = $error[2];
00314                 } else {
00315                         $error = "";
00316                 }
00317                 $this->last_error.= $error." [".$err."]";
00318         if($this->logger){
00319             Logger::error($this->last_error);
00320         }
00321                 return $this->last_error;
00322         }
00323 
00329         public function no_error($number=0){
00330                 if($this->pdo){
00331                         $error = $this->pdo->errorInfo();
00332                         $number = $error[1];
00333                 }
00334                 return $number;
00335         }
00336 
00342         public function last_insert_id($table='', $primary_key=''){
00343                 if(!$this->pdo){
00344                         return false;
00345                 }
00346                 return $this->pdo->lastInsertId();
00347         }
00348 
00353         public function begin(){
00354                 return $this->pdo->beginTransaction();
00355         }
00356 
00357 
00362         public function rollback(){
00363                 return $this->pdo->rollBack();
00364         }
00365 
00370         public function commit(){
00371                 return $this->pdo->commit();
00372         }
00373 
00379         static public function add_quotes($value){
00380                 return "'".addslashes($value)."'";
00381         }
00382 
00391         public function insert($table, $values, $fields=null){
00392                 $insert_sql = "";
00393                 if(is_array($values)){
00394                         if(!count($values)){
00395                                 throw new KumbiaException("Imposible realizar inserción en $table sin datos");
00396                         }
00397                         if(is_array($fields)){
00398                                 $insert_sql = "INSERT INTO $table (".join(",", $fields).") VALUES (".join(",", $values).")";
00399                         } else {
00400                                 $insert_sql = "INSERT INTO $table VALUES (".join(",", $values).")";
00401                         }
00402                         return $this->exec($insert_sql);
00403                 } else{
00404                         throw new KumbiaException('El segundo parametro para insert no es un Array');
00405                 }
00406         }
00407 
00417         public function update($table, $fields, $values, $where_condition=null){
00418                 $update_sql = "UPDATE $table SET ";
00419                 if(count($fields)!=count($values)){
00420                         throw new KumbiaException('El número de valores a actualizar no es el mismo de los campos');
00421                 }
00422                 $i = 0;
00423                 $update_values = array();
00424                 foreach($fields as $field){
00425                         $update_values[] = $field.' = '.$values[$i];
00426                         $i++;
00427                 }
00428                 $update_sql.= join(',', $update_values);
00429                 if($where_condition!=null){
00430                         $update_sql.= " WHERE $where_condition";
00431                 }
00432                 return $this->exec($update_sql);
00433         }
00434 
00441         public function delete($table, $where_condition){
00442                 if($where_condition){
00443                         return $this->exec("DELETE FROM $table WHERE $where_condition");
00444                 } else {
00445                         return $this->exec("DELETE FROM $table");
00446                 }
00447         }
00448 
00449 }
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones