DbSqlite.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. defined('THINK_PATH') or exit();
  12. /**
  13. * Sqlite数据库驱动
  14. * @category Extend
  15. * @package Extend
  16. * @subpackage Driver.Db
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class DbSqlite extends Db {
  20. /**
  21. * 架构函数 读取数据库配置信息
  22. * @access public
  23. * @param array $config 数据库配置数组
  24. */
  25. public function __construct($config='') {
  26. if ( !extension_loaded('sqlite') ) {
  27. throw_exception(L('_NOT_SUPPERT_').':sqlite');
  28. }
  29. if(!empty($config)) {
  30. if(!isset($config['mode'])) {
  31. $config['mode'] = 0666;
  32. }
  33. $this->config = $config;
  34. if(empty($this->config['params'])) {
  35. $this->config['params'] = array();
  36. }
  37. }
  38. }
  39. /**
  40. * 连接数据库方法
  41. * @access public
  42. */
  43. public function connect($config='',$linkNum=0) {
  44. if ( !isset($this->linkID[$linkNum]) ) {
  45. if(empty($config)) $config = $this->config;
  46. $pconnect = !empty($config['params']['persist'])? $config['params']['persist']:$this->pconnect;
  47. $conn = $pconnect ? 'sqlite_popen':'sqlite_open';
  48. $this->linkID[$linkNum] = $conn($config['database'],$config['mode']);
  49. if ( !$this->linkID[$linkNum]) {
  50. throw_exception(sqlite_error_string());
  51. }
  52. // 标记连接成功
  53. $this->connected = true;
  54. //注销数据库安全信息
  55. if(1 != C('DB_DEPLOY_TYPE')) unset($this->config);
  56. }
  57. return $this->linkID[$linkNum];
  58. }
  59. /**
  60. * 释放查询结果
  61. * @access public
  62. */
  63. public function free() {
  64. $this->queryID = null;
  65. }
  66. /**
  67. * 执行查询 返回数据集
  68. * @access public
  69. * @param string $str sql指令
  70. * @return mixed
  71. */
  72. public function query($str) {
  73. $this->initConnect(false);
  74. if ( !$this->_linkID ) return false;
  75. $this->queryStr = $str;
  76. //释放前次的查询结果
  77. if ( $this->queryID ) $this->free();
  78. N('db_query',1);
  79. // 记录开始执行时间
  80. G('queryStartTime');
  81. $this->queryID = sqlite_query($this->_linkID,$str);
  82. $this->debug();
  83. if ( false === $this->queryID ) {
  84. $this->error();
  85. return false;
  86. } else {
  87. $this->numRows = sqlite_num_rows($this->queryID);
  88. return $this->getAll();
  89. }
  90. }
  91. /**
  92. * 执行语句
  93. * @access public
  94. * @param string $str sql指令
  95. * @return integer
  96. */
  97. public function execute($str) {
  98. $this->initConnect(true);
  99. if ( !$this->_linkID ) return false;
  100. $this->queryStr = $str;
  101. //释放前次的查询结果
  102. if ( $this->queryID ) $this->free();
  103. N('db_write',1);
  104. // 记录开始执行时间
  105. G('queryStartTime');
  106. $result = sqlite_exec($this->_linkID,$str);
  107. $this->debug();
  108. if ( false === $result ) {
  109. $this->error();
  110. return false;
  111. } else {
  112. $this->numRows = sqlite_changes($this->_linkID);
  113. $this->lastInsID = sqlite_last_insert_rowid($this->_linkID);
  114. return $this->numRows;
  115. }
  116. }
  117. /**
  118. * 启动事务
  119. * @access public
  120. * @return void
  121. */
  122. public function startTrans() {
  123. $this->initConnect(true);
  124. if ( !$this->_linkID ) return false;
  125. //数据rollback 支持
  126. if ($this->transTimes == 0) {
  127. sqlite_query($this->_linkID,'BEGIN TRANSACTION');
  128. }
  129. $this->transTimes++;
  130. return ;
  131. }
  132. /**
  133. * 用于非自动提交状态下面的查询提交
  134. * @access public
  135. * @return boolen
  136. */
  137. public function commit() {
  138. if ($this->transTimes > 0) {
  139. $result = sqlite_query($this->_linkID,'COMMIT TRANSACTION');
  140. if(!$result){
  141. $this->error();
  142. return false;
  143. }
  144. $this->transTimes = 0;
  145. }
  146. return true;
  147. }
  148. /**
  149. * 事务回滚
  150. * @access public
  151. * @return boolen
  152. */
  153. public function rollback() {
  154. if ($this->transTimes > 0) {
  155. $result = sqlite_query($this->_linkID,'ROLLBACK TRANSACTION');
  156. if(!$result){
  157. $this->error();
  158. return false;
  159. }
  160. $this->transTimes = 0;
  161. }
  162. return true;
  163. }
  164. /**
  165. * 获得所有的查询数据
  166. * @access private
  167. * @return array
  168. */
  169. private function getAll() {
  170. //返回数据集
  171. $result = array();
  172. if($this->numRows >0) {
  173. for($i=0;$i<$this->numRows ;$i++ ){
  174. // 返回数组集
  175. $result[$i] = sqlite_fetch_array($this->queryID,SQLITE_ASSOC);
  176. }
  177. sqlite_seek($this->queryID,0);
  178. }
  179. return $result;
  180. }
  181. /**
  182. * 取得数据表的字段信息
  183. * @access public
  184. * @return array
  185. */
  186. public function getFields($tableName) {
  187. $result = $this->query('PRAGMA table_info( '.$tableName.' )');
  188. $info = array();
  189. if($result){
  190. foreach ($result as $key => $val) {
  191. $info[$val['Field']] = array(
  192. 'name' => $val['Field'],
  193. 'type' => $val['Type'],
  194. 'notnull' => (bool) ($val['Null'] === ''), // not null is empty, null is yes
  195. 'default' => $val['Default'],
  196. 'primary' => (strtolower($val['Key']) == 'pri'),
  197. 'autoinc' => (strtolower($val['Extra']) == 'auto_increment'),
  198. );
  199. }
  200. }
  201. return $info;
  202. }
  203. /**
  204. * 取得数据库的表信息
  205. * @access public
  206. * @return array
  207. */
  208. public function getTables($dbName='') {
  209. $result = $this->query("SELECT name FROM sqlite_master WHERE type='table' "
  210. . "UNION ALL SELECT name FROM sqlite_temp_master "
  211. . "WHERE type='table' ORDER BY name");
  212. $info = array();
  213. foreach ($result as $key => $val) {
  214. $info[$key] = current($val);
  215. }
  216. return $info;
  217. }
  218. /**
  219. * 关闭数据库
  220. * @access public
  221. */
  222. public function close() {
  223. if ($this->_linkID){
  224. sqlite_close($this->_linkID);
  225. }
  226. $this->_linkID = null;
  227. }
  228. /**
  229. * 数据库错误信息
  230. * 并显示当前的SQL语句
  231. * @access public
  232. * @return string
  233. */
  234. public function error() {
  235. $code = sqlite_last_error($this->_linkID);
  236. $this->error = $code.':'.sqlite_error_string($code);
  237. if('' != $this->queryStr){
  238. $this->error .= "\n [ SQL语句 ] : ".$this->queryStr;
  239. }
  240. trace($this->error,'','ERR');
  241. return $this->error;
  242. }
  243. /**
  244. * SQL指令安全过滤
  245. * @access public
  246. * @param string $str SQL指令
  247. * @return string
  248. */
  249. public function escapeString($str) {
  250. return sqlite_escape_string($str);
  251. }
  252. /**
  253. * limit
  254. * @access public
  255. * @return string
  256. */
  257. public function parseLimit($limit) {
  258. $limitStr = '';
  259. if(!empty($limit)) {
  260. $limit = explode(',',$limit);
  261. if(count($limit)>1) {
  262. $limitStr .= ' LIMIT '.$limit[1].' OFFSET '.$limit[0].' ';
  263. }else{
  264. $limitStr .= ' LIMIT '.$limit[0].' ';
  265. }
  266. }
  267. return $limitStr;
  268. }
  269. }