00001 <?php 00027 require CORE_PATH . 'extensions/db/db_base_interface.php'; 00028 00032 require CORE_PATH . 'extensions/db/loader/loader.php'; 00036 require CORE_PATH . 'extensions/db/active_record_base/active_record_base.php'; 00037 00041 require APP_PATH . 'model_base.php'; 00054 class DbBase 00055 { 00061 public $debug = false; 00062 00068 public $logger; 00069 00075 static private $raw_connection = null; 00076 00082 protected static $raw_connections = array(); 00083 00093 public function find($table, $where="1=1", $fields="*", $orderBy="1"){ 00094 ActiveRecord::sql_item_sanizite($table); 00095 ActiveRecord::sql_sanizite($fields); 00096 ActiveRecord::sql_sanizite($orderBy); 00097 $q = $this->query("SELECT $fields FROM $table WHERE $where ORDER BY $orderBy"); 00098 $results = array(); 00099 while($row=$this->fetch_array($q)){ 00100 $results[] = $row; 00101 } 00102 var_dump ($results); 00103 return $results; 00104 } 00105 00114 public function in_query($sql, $type=db::DB_BOTH){ 00115 $q = $this->query($sql); 00116 $results = array(); 00117 if($q){ 00118 while($row=$this->fetch_array($q, $type)){ 00119 $results[] = $row; 00120 } 00121 } 00122 return $results; 00123 } 00124 00133 public function fetch_all($sql, $type=db::DB_BOTH){ 00134 return $this->in_query($sql, $type); 00135 } 00136 00145 public function in_query_assoc($sql){ 00146 $q = $this->query($sql); 00147 $results = array(); 00148 if($q){ 00149 while($row=$this->fetch_array($q, db::DB_ASSOC)){ 00150 $results[] = $row; 00151 } 00152 } 00153 return $results; 00154 } 00155 00164 public function in_query_num($sql){ 00165 $q = $this->query($sql); 00166 $results = array(); 00167 if($q){ 00168 while($row=$this->fetch_array($q, db::DB_NUM)){ 00169 $results[] = $row; 00170 } 00171 } 00172 return $results; 00173 } 00174 00181 public function fetch_one($sql){ 00182 $q = $this->query($sql); 00183 if($q){ 00184 if($this->num_rows($q)>1){ 00185 Flash::warning("Una sentencia SQL: \"$sql\" retorno mas de una fila cuando se esperaba una sola"); 00186 } 00187 return $this->fetch_array($q); 00188 } else { 00189 return array(); 00190 } 00191 } 00192 00201 public function insert($table, $values, $fields=null){ 00202 $insert_sql = ""; 00203 if(is_array($values)){ 00204 if(!count($values)){ 00205 new KumbiaException("Imposible realizar inserción en $table sin datos"); 00206 } 00207 if(is_array($fields)){ 00208 $insert_sql = "INSERT INTO $table (".join(",", $fields).") VALUES (".join(",", $values).")"; 00209 } else { 00210 $insert_sql = "INSERT INTO $table VALUES (".join(",", $values).")"; 00211 } 00212 return $this->query($insert_sql); 00213 } else{ 00214 throw new KumbiaException("El segundo parametro para insert no es un Array"); 00215 } 00216 } 00217 00227 public function update($table, $fields, $values, $where_condition=null){ 00228 $update_sql = "UPDATE $table SET "; 00229 if(count($fields)!=count($values)){ 00230 throw new KumbiaException('Los número de valores a actualizar no es el mismo de los campos'); 00231 } 00232 $i = 0; 00233 $update_values = array(); 00234 foreach($fields as $field){ 00235 $update_values[] = $field.' = '.$values[$i]; 00236 $i++; 00237 } 00238 $update_sql.= join(',', $update_values); 00239 if($where_condition!=null){ 00240 $update_sql.= " WHERE $where_condition"; 00241 } 00242 return $this->query($update_sql); 00243 } 00244 00251 public function delete($table, $where_condition){ 00252 if(trim($where_condition)){ 00253 return $this->query("DELETE FROM $table WHERE $where_condition"); 00254 } else { 00255 return $this->query("DELETE FROM $table"); 00256 } 00257 } 00258 00263 public function begin(){ 00264 return $this->query('BEGIN'); 00265 } 00266 00267 00272 public function rollback(){ 00273 return $this->query('ROLLBACK'); 00274 } 00275 00280 public function commit(){ 00281 return $this->query('COMMIT'); 00282 } 00283 00289 static public function add_quotes($value){ 00290 return "'".addslashes($value)."'"; 00291 } 00292 00299 protected function log($msg, $type){ 00300 if($this->logger) { 00301 Logger::log($this->logger, $msg, $type); 00302 } 00303 } 00304 00310 protected function debug($sql){ 00311 if($this->debug){ 00312 Flash::notice($sql); 00313 } 00314 } 00315 00324 public static function raw_connect($new_connection=false, $database = null){ 00328 if(!$database) { 00329 $database = Config::get('config.application.database'); 00330 } 00331 00332 $databases = Config::read('databases.ini'); 00333 $config = $databases[$database]; 00334 00339 if(!isset($config['port'])){ 00340 $config['port'] = 0; 00341 } 00342 if(!isset($config['dsn'])){ 00343 $config['dsn'] = ''; 00344 } 00345 if(!isset($config['host'])){ 00346 $config['host'] = ''; 00347 } 00348 if(!isset($config['username'])){ 00349 $config['username'] = ''; 00350 } 00351 if(!isset($config['password'])){ 00352 $config['password'] = ''; 00353 } 00354 00358 if(!$new_connection && isset(self::$raw_connections[$database])) { 00359 return self::$raw_connections[$database]; 00360 } 00361 00365 if(isset($config['pdo'])){ 00366 $dbclass = "DbPDO{$config['type']}"; 00367 if(!class_exists($dbclass)) { 00371 require_once CORE_PATH . 'extensions/db/adapters/pdo.php'; 00372 require_once CORE_PATH . 'extensions/db/adapters/pdo/' . $config['type'] . '.php'; 00373 } 00374 } else { 00375 $dbclass = "Db{$config['type']}"; 00376 if(!class_exists($dbclass)) { 00377 require_once CORE_PATH . 'extensions/db/adapters/' . $config['type'] . '.php'; 00378 } 00379 } 00380 if(!class_exists($dbclass)){ 00381 throw new KumbiaException("No existe la clase {$dbclass}, necesaria para iniciar el adaptador", 0); 00382 } 00383 00384 $connection = new $dbclass($config); 00385 00389 if(!$new_connection) { 00390 self::$raw_connections[$database] = $connection; 00391 } 00392 00393 return $connection; 00394 } 00395 } 00396 return DbLoader::load_driver();