KumbiaPHP beta2-dev
Framework PHP en español
|
00001 <?php 00023 class DbSQLite extends DbBase implements DbBaseInterface { 00024 00030 public $id_connection; 00031 00037 public $last_result_query; 00038 00044 private $last_query; 00045 00051 public $last_error; 00052 00057 const DB_ASSOC = SQLITE_ASSOC; 00058 00063 const DB_BOTH = SQLITE_BOTH; 00064 00069 const DB_NUM = SQLITE_NUM; 00070 00071 00076 const TYPE_INTEGER = 'INTEGER'; 00077 00082 const TYPE_DATE = 'DATE'; 00083 00088 const TYPE_VARCHAR = 'VARCHAR'; 00089 00094 const TYPE_DECIMAL = 'DECIMAL'; 00095 00100 const TYPE_DATETIME = 'DATETIME'; 00101 00106 const TYPE_CHAR = 'CHAR'; 00113 public function connect($config){ 00114 00115 if(!extension_loaded('sqlite')){ 00116 throw new KumbiaException('Debe cargar la extensión de PHP llamada sqlite'); 00117 } 00118 if($this->id_connection = sqlite_open(APP_PATH.'config/sql/'.$config['name'])){ 00119 return true; 00120 } else { 00121 throw new KumbiaException($this->error('No se puede conectar a la base de datos')); 00122 } 00123 } 00124 00131 function query($sqlQuery){ 00132 $this->debug($sqlQuery); 00133 if($this->logger){ 00134 Logger::debug($sqlQuery); 00135 } 00136 if(!$this->id_connection){ 00137 $this->connect(); 00138 if(!$this->id_connection){ 00139 return false; 00140 } 00141 } 00142 $this->last_query = $sqlQuery; 00143 if($resultQuery = @sqlite_query($this->id_connection, $sqlQuery)){ 00144 $this->last_result_query = $resultQuery; 00145 return $resultQuery; 00146 } else { 00147 throw new KumbiaException($this->error(" al ejecutar <em>'$sqlQuery'</em>")); 00148 } 00149 } 00150 00154 function close(){ 00155 if($this->id_connection) { 00156 return sqlite_close($this->id_connection); 00157 } else { 00158 return false; 00159 } 00160 } 00161 00169 function fetch_array($resultQuery='', $opt=SQLITE_BOTH){ 00170 if(!$this->id_connection){ 00171 return false; 00172 } 00173 if(!$resultQuery){ 00174 $resultQuery = $this->last_result_query; 00175 if(!$resultQuery){ 00176 return false; 00177 } 00178 } 00179 00180 return sqlite_fetch_array($resultQuery, $opt); 00181 } 00182 00188 function __construct($config){ 00189 $this->connect($config); 00190 } 00191 00195 function num_rows($resultQuery=''){ 00196 if(!$this->id_connection){ 00197 return false; 00198 } 00199 if(!$resultQuery){ 00200 $resultQuery = $this->last_result_query; 00201 if(!$resultQuery){ 00202 return false; 00203 } 00204 } 00205 if(($numberRows = sqlite_num_rows($resultQuery))!==false){ 00206 return $numberRows; 00207 } else { 00208 throw new KumbiaException($this->error()); 00209 } 00210 return false; 00211 } 00212 00220 function field_name($number, $resultQuery=''){ 00221 if(!$this->id_connection){ 00222 return false; 00223 } 00224 if(!$resultQuery){ 00225 $resultQuery = $this->last_result_query; 00226 if(!$resultQuery){ 00227 return false; 00228 } 00229 } 00230 if(($fieldName = sqlite_field_name($resultQuery, $number))!==false){ 00231 return $fieldName; 00232 } else { 00233 throw new KumbiaException($this->error()); 00234 } 00235 return false; 00236 } 00237 00238 00246 function data_seek($number, $resultQuery=''){ 00247 if(!$resultQuery){ 00248 $resultQuery = $this->last_result_query; 00249 if(!$resultQuery){ 00250 return false; 00251 } 00252 } 00253 if(($success = sqlite_rewind($resultQuery, $number))!==false){ 00254 return $success; 00255 } else { 00256 throw new KumbiaException($this->error()); 00257 } 00258 return false; 00259 } 00260 00267 function affected_rows($resultQuery=''){ 00268 if(!$this->id_connection){ 00269 return false; 00270 } 00271 if(!$resultQuery){ 00272 $resultQuery = $this->last_result_query; 00273 if(!$resultQuery){ 00274 return false; 00275 } 00276 } 00277 if(($numberRows = pg_affected_rows($resultQuery))!==false){ 00278 return $numberRows; 00279 } else { 00280 throw new KumbiaException($this->error()); 00281 } 00282 return false; 00283 } 00284 00290 function error($err=''){ 00291 if(!$this->id_connection){ 00292 $this->last_error = sqlite_last_error() ? sqlite_last_error().$err : "[Error Desconocido en SQLite \"$err\"]"; 00293 if($this->logger){ 00294 Logger::error($this->last_error); 00295 } 00296 return $this->last_error; 00297 } 00298 $this->last_error = 'SQLite error: '.sqlite_error_string(sqlite_last_error($this->id_connection)); 00299 $this->last_error.= $err; 00300 if($this->logger){ 00301 Logger::error($this->last_error); 00302 } 00303 return $this->last_error; 00304 } 00305 00311 function no_error(){ 00312 if(!$this->id_connection){ 00313 return false; 00314 } 00315 return "0"; //Codigo de Error? 00316 } 00317 00323 public function last_insert_id($table='', $primary_key=''){ 00324 if(!$this->id_connection){ 00325 return false; 00326 } 00327 $last_id = $this->fetch_one("SELECT COUNT(*) FROM $table"); 00328 return $last_id[0]; 00329 } 00330 00337 function table_exists($table, $schema=''){ 00338 $table = addslashes(strtolower($table)); 00339 if(strpos($table, ".")){ 00340 list($schema, $table) = explode(".", $table); 00341 } 00342 $num = $this->fetch_one("SELECT COUNT(*) FROM sqlite_master WHERE name = '$table'"); 00343 return $num[0]; 00344 } 00345 00352 public function limit($sql){ 00353 $params = Util::getParams(func_get_args()); 00354 $sql_new = $sql; 00355 00356 if(isset($params['limit']) && is_numeric($params['limit'])){ 00357 $sql_new.=" LIMIT $params[limit]"; 00358 } 00359 00360 if(isset($params['offset']) && is_numeric($params['offset'])){ 00361 $sql_new.=" OFFSET $params[offset]"; 00362 } 00363 00364 return $sql_new; 00365 } 00366 00373 public function drop_table($table, $if_exists=true){ 00374 if($if_exists){ 00375 if($this->table_exists($table)){ 00376 return $this->query("DROP TABLE $table"); 00377 } else { 00378 return true; 00379 } 00380 } else { 00381 return $this->query("DROP TABLE $table"); 00382 } 00383 } 00384 00392 public function create_table($table, $definition, $index=array()){ 00393 00394 } 00395 00401 public function list_tables(){ 00402 return $this->fetch_all("SELECT name FROM sqlite_master WHERE type='table' ". 00403 "UNION ALL SELECT name FROM sqlite_temp_master ". 00404 "WHERE type='table' ORDER BY name"); 00405 } 00406 00413 public function describe_table($table, $schema=''){ 00414 $fields = array(); 00415 $results = $this->fetch_all("PRAGMA table_info($table)"); 00416 //var_dump($results); die(); 00417 foreach($results as $field){ 00418 $fields[] = array( 00419 "Field" => $field["name"], 00420 "Type" => $field["type"], 00421 "Null" => $field["notnull"] == '0' ? "YES" : "NO", 00422 "Key" => $field['pk'] == 1 ? "PRI" : "" 00423 ); 00424 } 00425 return $fields; 00426 } 00427 }