<?php<liu21st@gmail.com>namespace Think\Db\Driver;
use Think\Db;
defined('THINK_PATH') or exit();
class Sqlite extends Db {
public function __construct($config='') {
if ( !extension_loaded('sqlite') ) {
E(L('_NOT_SUPPERT_').':sqlite');
}
if(!empty($config)) {
if(!isset($config['mode'])) {
$config['mode'] = 0666;
}
$this->config = $config;
if(empty($this->config['params'])) {
$this->config['params'] = array();
}
}
}
public function connect($config='',$linkNum=0) {
if ( !isset($this->linkID[$linkNum]) ) {
if(empty($config)) $config = $this->config;
$pconnect = !empty($config['params']['persist'])? $config['params']['persist']:$this->pconnect;
$conn = $pconnect ? 'sqlite_popen':'sqlite_open';
$this->linkID[$linkNum] = $conn($config['database'],$config['mode']);
if ( !$this->linkID[$linkNum]) {
E(sqlite_error_string());
}
$this->connected = true;
if(1 != C('DB_DEPLOY_TYPE')) unset($this->config);
}
return $this->linkID[$linkNum];
}
public function free() {
$this->queryID = null;
}
public function query($str) {
$this->initConnect(false);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
if ( $this->queryID ) $this->free();
N('db_query',1);
G('queryStartTime');
$this->queryID = sqlite_query($this->_linkID,$str);
$this->debug();
if ( false === $this->queryID ) {
$this->error();
return false;
} else {
$this->numRows = sqlite_num_rows($this->queryID);
return $this->getAll();
}
}
public function execute($str) {
$this->initConnect(true);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
if ( $this->queryID ) $this->free();
N('db_write',1);
G('queryStartTime');
$result = sqlite_exec($this->_linkID,$str);
$this->debug();
if ( false === $result ) {
$this->error();
return false;
} else {
$this->numRows = sqlite_changes($this->_linkID);
$this->lastInsID = sqlite_last_insert_rowid($this->_linkID);
return $this->numRows;
}
}
public function startTrans() {
$this->initConnect(true);
if ( !$this->_linkID ) return false;
if ($this->transTimes == 0) {
sqlite_query($this->_linkID,'BEGIN TRANSACTION');
}
$this->transTimes++;
return ;
}
public function commit() {
if ($this->transTimes > 0) {
$result = sqlite_query($this->_linkID,'COMMIT TRANSACTION');
if(!$result){
$this->error();
return false;
}
$this->transTimes = 0;
}
return true;
}
public function rollback() {
if ($this->transTimes > 0) {
$result = sqlite_query($this->_linkID,'ROLLBACK TRANSACTION');
if(!$result){
$this->error();
return false;
}
$this->transTimes = 0;
}
return true;
}
private function getAll() {
$result = array();
if($this->numRows >0) {
for($i=0;$i<$this->numRows ;$i++ ){
$result[$i] = sqlite_fetch_array($this->queryID,SQLITE_ASSOC);
}
sqlite_seek($this->queryID,0);
}
return $result;
}
public function getFields($tableName) {
$result = $this->query('PRAGMA table_info( '.$tableName.' )');
$info = array();
if($result){
foreach ($result as $key => $val) {
$info[$val['Field']] = array(
'name' => $val['Field'],
'type' => $val['Type'],
'notnull' => (bool) ($val['Null'] === ''), 'default' => $val['Default'],
'primary' => (strtolower($val['Key']) == 'pri'),
'autoinc' => (strtolower($val['Extra']) == 'auto_increment'),
);
}
}
return $info;
}
public function getTables($dbName='') {
$result = $this->query("SELECT name FROM sqlite_master WHERE type='table' "
. "UNION ALL SELECT name FROM sqlite_temp_master "
. "WHERE type='table' ORDER BY name");
$info = array();
foreach ($result as $key => $val) {
$info[$key] = current($val);
}
return $info;
}
public function close() {
if ($this->_linkID){
sqlite_close($this->_linkID);
}
$this->_linkID = null;
}
public function error() {
$code = sqlite_last_error($this->_linkID);
$this->error = $code.':'.sqlite_error_string($code);
if('' != $this->queryStr){
$this->error .= "\n [ SQL语句 ] : ".$this->queryStr;
}
trace($this->error,'','ERR');
return $this->error;
}
public function escapeString($str) {
return sqlite_escape_string($str);
}
public function parseLimit($limit) {
$limitStr = '';
if(!empty($limit)) {
$limit = explode(',',$limit);
if(count($limit)>1) {
$limitStr .= ' LIMIT '.$limit[1].' OFFSET '.$limit[0].' ';
}else{
$limitStr .= ' LIMIT '.$limit[0].' ';
}
}
return $limitStr;
}
}