00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class CI_DB_sqlite_driver extends CI_DB {
00034
00035
00036
00037
00038
00039
00040 var $_count_string = "SELECT COUNT(*) AS ";
00041 var $_random_keyword = ' Random()';
00042
00043
00044
00045
00046
00047
00048
00049 function db_connect()
00050 {
00051 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
00052 {
00053 log_message('error', $error);
00054
00055 if ($this->db_debug)
00056 {
00057 $this->display_error($error, '', TRUE);
00058 }
00059
00060 return FALSE;
00061 }
00062
00063 return $conn_id;
00064 }
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 function db_pconnect()
00075 {
00076 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
00077 {
00078 log_message('error', $error);
00079
00080 if ($this->db_debug)
00081 {
00082 $this->display_error($error, '', TRUE);
00083 }
00084
00085 return FALSE;
00086 }
00087
00088 return $conn_id;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 function db_select()
00100 {
00101 return TRUE;
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 function db_set_charset($charset, $collation)
00115 {
00116
00117 return TRUE;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 function _version()
00129 {
00130 return sqlite_libversion();
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 function _execute($sql)
00143 {
00144 $sql = $this->_prep_query($sql);
00145 return @sqlite_query($this->conn_id, $sql);
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 function _prep_query($sql)
00160 {
00161 return $sql;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 function trans_begin($test_mode = FALSE)
00173 {
00174 if ( ! $this->trans_enabled)
00175 {
00176 return TRUE;
00177 }
00178
00179
00180 if ($this->_trans_depth > 0)
00181 {
00182 return TRUE;
00183 }
00184
00185
00186
00187
00188 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
00189
00190 $this->simple_query('BEGIN TRANSACTION');
00191 return TRUE;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 function trans_commit()
00203 {
00204 if ( ! $this->trans_enabled)
00205 {
00206 return TRUE;
00207 }
00208
00209
00210 if ($this->_trans_depth > 0)
00211 {
00212 return TRUE;
00213 }
00214
00215 $this->simple_query('COMMIT');
00216 return TRUE;
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 function trans_rollback()
00228 {
00229 if ( ! $this->trans_enabled)
00230 {
00231 return TRUE;
00232 }
00233
00234
00235 if ($this->_trans_depth > 0)
00236 {
00237 return TRUE;
00238 }
00239
00240 $this->simple_query('ROLLBACK');
00241 return TRUE;
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 function escape_str($str)
00254 {
00255 return sqlite_escape_string($str);
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 function affected_rows()
00267 {
00268 return sqlite_changes($this->conn_id);
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 function insert_id()
00280 {
00281 return @sqlite_last_insert_rowid($this->conn_id);
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 function count_all($table = '')
00297 {
00298 if ($table == '')
00299 return '0';
00300
00301 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
00302
00303 if ($query->num_rows() == 0)
00304 return '0';
00305
00306 $row = $query->row();
00307 return $row->numrows;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 function _list_tables($prefix_limit = FALSE)
00322 {
00323 $sql = "SELECT name from sqlite_master WHERE type='table'";
00324
00325 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
00326 {
00327 $sql .= " AND 'name' LIKE '".$this->dbprefix."%'";
00328 }
00329 return $sql;
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 function _list_columns($table = '')
00344 {
00345
00346 return FALSE;
00347 }
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 function _field_data($table)
00361 {
00362 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 function _error_message()
00374 {
00375 return sqlite_error_string(sqlite_last_error($this->conn_id));
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 function _error_number()
00387 {
00388 return sqlite_last_error($this->conn_id);
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 function _escape_table($table)
00404 {
00405
00406
00407
00408 return $table;
00409 }
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 function _protect_identifiers($item, $first_word_only = FALSE)
00424 {
00425 if (is_array($item))
00426 {
00427 $escaped_array = array();
00428
00429 foreach($item as $k=>$v)
00430 {
00431 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
00432 }
00433
00434 return $escaped_array;
00435 }
00436
00437
00438
00439 if (ctype_alnum($item) === FALSE)
00440 {
00441 if (strpos($item, '.') !== FALSE)
00442 {
00443 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
00444 $table_name = substr($item, 0, strpos($item, '.')+1);
00445 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
00446 }
00447
00448
00449 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
00450
00451 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1$2$3', $item);
00452 }
00453 else
00454 {
00455 return "{$item}";
00456 }
00457
00458 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
00459
00460 foreach ($exceptions as $exception)
00461 {
00462
00463 if (stristr($item, " {$exception} ") !== FALSE)
00464 {
00465 $item = preg_replace('/ ('.preg_quote($exception).') /i', ' $1 ', $item);
00466 }
00467 }
00468 return $item;
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 function _from_tables($tables)
00484 {
00485 if ( ! is_array($tables))
00486 {
00487 $tables = array($tables);
00488 }
00489
00490 return '('.implode(', ', $tables).')';
00491 }
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506 function _insert($table, $keys, $values)
00507 {
00508 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
00509 }
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
00527 {
00528 foreach($values as $key => $val)
00529 {
00530 $valstr[] = $key." = ".$val;
00531 }
00532
00533 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
00534
00535 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
00536
00537 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
00538 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
00539 $sql .= $orderby.$limit;
00540
00541 return $sql;
00542 }
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558 function _truncate($table)
00559 {
00560 return $this->_delete($table);
00561 }
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
00577 {
00578 $conditions = '';
00579
00580 if (count($where) > 0 OR count($like) > 0)
00581 {
00582 $conditions = "\nWHERE ";
00583 $conditions .= implode("\n", $this->ar_where);
00584
00585 if (count($where) > 0 && count($like) > 0)
00586 {
00587 $conditions .= " AND ";
00588 }
00589 $conditions .= implode("\n", $like);
00590 }
00591
00592 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
00593
00594 return "DELETE FROM ".$table.$conditions.$limit;
00595 }
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610 function _limit($sql, $limit, $offset)
00611 {
00612 if ($offset == 0)
00613 {
00614 $offset = '';
00615 }
00616 else
00617 {
00618 $offset .= ", ";
00619 }
00620
00621 return $sql."LIMIT ".$offset.$limit;
00622 }
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633 function _close($conn_id)
00634 {
00635 @sqlite_close($conn_id);
00636 }
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650 function _rename_table($table_name, $new_table_name)
00651 {
00652 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
00653 return $sql;
00654 }
00655
00656
00657 }
00658
00659
00660
00661