KumbiaPHP beta2-dev
Framework PHP en español
db_base.php
Ir a la documentación de este archivo.
00001 <?php
00036 class DbBase 
00037 {
00043         public $debug = false;
00044 
00050         public $logger = false;
00051 
00061         public function find($table, $where="1=1", $fields="*", $orderBy="1"){
00062                 ActiveRecord::sql_item_sanizite($table);
00063                 ActiveRecord::sql_sanizite($fields);
00064                 ActiveRecord::sql_sanizite($orderBy);
00065                 $q = $this->query("SELECT $fields FROM $table WHERE $where ORDER BY $orderBy");
00066                 $results = array();
00067                 while($row=$this->fetch_array($q)){
00068                         $results[] = $row;
00069                 }
00070                 return $results;
00071         }
00072 
00081         public function in_query($sql){
00082                 $q = $this->query($sql);
00083                 $results = array();
00084                 if($q){
00085                         while($row=$this->fetch_array($q)){
00086                                 $results[] = $row;
00087                         }
00088                 }
00089                 return $results;
00090         }
00091 
00100         public function fetch_all($sql){
00101                 return $this->in_query($sql);
00102         }
00103 
00112         public function in_query_assoc($sql){
00113                 $q = $this->query($sql);
00114                 $results = array();
00115                 if($q){
00116                         while($row=$this->fetch_array($q, db::DB_ASSOC)){
00117                                 $results[] = $row;
00118                         }
00119                 }
00120                 return $results;
00121         }
00122 
00131         public function in_query_num($sql){
00132                 $q = $this->query($sql);
00133                 $results = array();
00134                 if($q){
00135                         while($row=$this->fetch_array($q, db::DB_NUM)){
00136                                 $results[] = $row;
00137                         }
00138                 }
00139                 return $results;
00140         }
00141 
00148         public function fetch_one($sql){
00149                 $q = $this->query($sql);
00150                 if($q){
00151                         if($this->num_rows($q)>1){
00152                                 Flash::warning("Una sentencia SQL: \"$sql\" retorno mas de una fila cuando se esperaba una sola");
00153                         }
00154                         return $this->fetch_array($q);
00155                 } else {
00156                         return array();
00157                 }
00158         }
00159 
00168         public function insert($table, $values, $fields=null){
00169                 $insert_sql = "";
00170                 if(is_array($values)){
00171                         if(!count($values)){
00172                                 new KumbiaException("Imposible realizar inserci&oacute;n en $table sin datos");
00173                         }
00174                         if(is_array($fields)){
00175                                 $insert_sql = "INSERT INTO $table (".join(",", $fields).") VALUES (".join(",", $values).")";
00176                         } else {
00177                                 $insert_sql = "INSERT INTO $table VALUES (".join(",", $values).")";
00178                         }
00179                         return $this->query($insert_sql);
00180                 } else{
00181                         throw new KumbiaException('El segundo parametro para insert no es un Array');
00182                 }
00183         }
00184 
00194         public function update($table, $fields, $values, $where_condition=null){
00195                 $update_sql = "UPDATE $table SET ";
00196                 if(count($fields)!=count($values)){
00197                         throw new KumbiaException('Los n&uacute;mero de valores a actualizar no es el mismo de los campos');
00198                 }
00199                 $i = 0;
00200                 $update_values = array();
00201                 foreach($fields as $field){
00202                         $update_values[] = $field.' = '.$values[$i];
00203                         $i++;
00204                 }
00205                 $update_sql.= join(',', $update_values);
00206                 if($where_condition!=null){
00207                         $update_sql.= " WHERE $where_condition";
00208                 }
00209                 return $this->query($update_sql);
00210         }
00211 
00218         public function delete($table, $where_condition){
00219                 if(trim($where_condition)){
00220                         return $this->query("DELETE FROM $table WHERE $where_condition");
00221                 } else {
00222                         return $this->query("DELETE FROM $table");
00223                 }
00224         }
00225 
00230         public function begin(){
00231                 return $this->query('BEGIN');
00232         }
00233 
00234 
00239         public function rollback(){
00240                 return $this->query('ROLLBACK');
00241         }
00242 
00247         public function commit(){
00248                 return $this->query('COMMIT');
00249         }
00250 
00256         static public function add_quotes($value){
00257                 return "'".addslashes($value)."'";
00258         }
00259 
00266         protected function log($msg, $type){
00267                 if($this->logger) {
00268             Logger::log($this->logger, $msg, $type);
00269                 }
00270         }
00271 
00277         protected function debug($sql){
00278                 if($this->debug){
00279                         Flash::notice($sql);
00280                 }
00281         }
00282 }
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones