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 return false;
00118 }
00119 if($this->id_connection = sqlite_open(APP_PATH.'config/sql/'.$config['name'])){
00120 return true;
00121 } else {
00122 throw new KumbiaException($this->error("No se puede conectar a la base de datos"), $this->no_error(), false);
00123 }
00124 }
00125
00132 function query($sqlQuery){
00133 $this->debug($sqlQuery);
00134 if($this->logger){
00135 Logger::debug($sqlQuery);
00136 }
00137 if(!$this->id_connection){
00138 $this->connect();
00139 if(!$this->id_connection){
00140 return false;
00141 }
00142 }
00143 $this->last_query = $sqlQuery;
00144 if($resultQuery = @sqlite_query($this->id_connection, $sqlQuery)){
00145 $this->last_result_query = $resultQuery;
00146 return $resultQuery;
00147 } else {
00148 throw new KumbiaException($this->error(" al ejecutar <i>'$sqlQuery'</i>"), $this->no_error());
00149 return false;
00150 }
00151 }
00152
00156 function close(){
00157 if($this->id_connection) {
00158 return sqlite_close($this->id_connection);
00159 } else {
00160 return false;
00161 }
00162 }
00163
00171 function fetch_array($resultQuery='', $opt=''){
00172 if($opt==='') $opt = db::DB_BOTH;
00173 if(!$this->id_connection){
00174 return false;
00175 }
00176 if(!$resultQuery){
00177 $resultQuery = $this->last_result_query;
00178 if(!$resultQuery){
00179 return false;
00180 }
00181 }
00182
00183 return sqlite_fetch_array($resultQuery, $opt);
00184 }
00185
00191 function __construct($config){
00192 $this->connect($config);
00193 }
00194
00198 function num_rows($resultQuery=''){
00199 if(!$this->id_connection){
00200 return false;
00201 }
00202 if(!$resultQuery){
00203 $resultQuery = $this->last_result_query;
00204 if(!$resultQuery){
00205 return false;
00206 }
00207 }
00208 if(($numberRows = sqlite_num_rows($resultQuery))!==false){
00209 return $numberRows;
00210 } else {
00211 throw new KumbiaException($this->error(), $this->no_error());
00212 return false;
00213 }
00214 return false;
00215 }
00216
00224 function field_name($number, $resultQuery=''){
00225 if(!$this->id_connection){
00226 return false;
00227 }
00228 if(!$resultQuery){
00229 $resultQuery = $this->last_result_query;
00230 if(!$resultQuery){
00231 return false;
00232 }
00233 }
00234 if(($fieldName = sqlite_field_name($resultQuery, $number))!==false){
00235 return $fieldName;
00236 } else {
00237 throw new KumbiaException($this->error(), $this->no_error());
00238 return false;
00239 }
00240 return false;
00241 }
00242
00243
00251 function data_seek($number, $resultQuery=''){
00252 if(!$resultQuery){
00253 $resultQuery = $this->last_result_query;
00254 if(!$resultQuery){
00255 return false;
00256 }
00257 }
00258 if(($success = sqlite_rewind($resultQuery, $number))!==false){
00259 return $success;
00260 } else {
00261 throw new KumbiaException($this->error(), $this->no_error());
00262 return false;
00263 }
00264 return false;
00265 }
00266
00273 function affected_rows($resultQuery=''){
00274 if(!$this->id_connection){
00275 return false;
00276 }
00277 if(!$resultQuery){
00278 $resultQuery = $this->last_result_query;
00279 if(!$resultQuery){
00280 return false;
00281 }
00282 }
00283 if(($numberRows = pg_affected_rows($resultQuery))!==false){
00284 return $numberRows;
00285 } else {
00286 throw new KumbiaException($this->error(), $this->no_error());
00287 return false;
00288 }
00289 return false;
00290 }
00291
00297 function error($err=''){
00298 if(!$this->id_connection){
00299 $this->last_error = @sqlite_last_error() ? @sqlite_last_error().$err : "[Error Desconocido en PostgreSQL \"$err\"]";
00300 if($this->logger){
00301 Logger::error($this->last_error);
00302 }
00303 return $this->last_error;
00304 }
00305 $this->last_error = @sqlite_last_error() ? @sqlite_last_error().$err : "[Error Desconocido en PostgreSQL: $err]";
00306 $this->last_error.= $err;
00307 if($this->logger){
00308 Logger::error($this->last_error);
00309 }
00310 return pg_last_error($this->id_connection).$err;
00311 }
00312
00318 function no_error(){
00319 if(!$this->id_connection){
00320 return false;
00321 }
00322 return "0";
00323 }
00324
00330 public function last_insert_id($table='', $primary_key=''){
00331 if(!$this->id_connection){
00332 return false;
00333 }
00334 $last_id = $this->fetch_one("SELECT COUNT(*) FROM $table");
00335 return $last_id[0];
00336 }
00337
00344 function table_exists($table, $schema=''){
00345 $table = addslashes(strtolower($table));
00346 if(strpos($table, ".")){
00347 list($schema, $table) = explode(".", $table);
00348 }
00349 $num = $this->fetch_one("SELECT COUNT(*) FROM sqlite_master WHERE name = '$table'");
00350 return $num[0];
00351 }
00352
00359 public function limit($sql){
00360 $params = Util::getParams(func_get_args());
00361 $sql_new = $sql;
00362
00363 if(isset($params['limit']) && is_numeric($params['limit'])){
00364 $sql_new.=" LIMIT $params[limit]";
00365 }
00366
00367 if(isset($params['offset']) && is_numeric($params['offset'])){
00368 $sql_new.=" OFFSET $params[offset]";
00369 }
00370
00371 return $sql_new;
00372 }
00373
00380 public function drop_table($table, $if_exists=true){
00381 if($if_exists){
00382 if($this->table_exists($table)){
00383 return $this->query("DROP TABLE $table");
00384 } else {
00385 return true;
00386 }
00387 } else {
00388 return $this->query("DROP TABLE $table");
00389 }
00390 }
00391
00405 public function create_table($table, $definition, $index=array()){
00406
00407 }
00408
00414 public function list_tables(){
00415 return $this->fetch_all("SELECT name FROM sqlite_master WHERE type='table' ".
00416 "UNION ALL SELECT name FROM sqlite_temp_master ".
00417 "WHERE type='table' ORDER BY name");
00418 }
00419
00426 public function describe_table($table, $schema=''){
00427 $fields = array();
00428 $results = $this->fetch_all("PRAGMA table_info($table)");
00429
00430 foreach($results as $field){
00431 $fields[] = array(
00432 "Field" => $field["name"],
00433 "Type" => $field["type"],
00434 "Null" => $field["notnull"] == '0' ? "YES" : "NO",
00435 "Key" => $field['pk'] == 1 ? "PRI" : ""
00436 );
00437 }
00438 return $fields;
00439 }
00440 }