Sqlite.class.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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. namespace Think\Db\Driver;
  12. use Think\Db\Driver;
  13. /**
  14. * Sqlite数据库驱动
  15. */
  16. class Sqlite extends Driver {
  17. /**
  18. * 解析pdo连接的dsn信息
  19. * @access public
  20. * @param array $config 连接信息
  21. * @return string
  22. */
  23. protected function parseDsn($config){
  24. $dsn = 'sqlite:'.$config['database'];
  25. return $dsn;
  26. }
  27. /**
  28. * 取得数据表的字段信息
  29. * @access public
  30. * @return array
  31. */
  32. public function getFields($tableName) {
  33. list($tableName) = explode(' ', $tableName);
  34. $result = $this->query('PRAGMA table_info( '.$tableName.' )');
  35. $info = array();
  36. if($result){
  37. foreach ($result as $key => $val) {
  38. $info[$val['field']] = array(
  39. 'name' => $val['field'],
  40. 'type' => $val['type'],
  41. 'notnull' => (bool) ($val['null'] === ''), // not null is empty, null is yes
  42. 'default' => $val['default'],
  43. 'primary' => (strtolower($val['dey']) == 'pri'),
  44. 'autoinc' => (strtolower($val['extra']) == 'auto_increment'),
  45. );
  46. }
  47. }
  48. return $info;
  49. }
  50. /**
  51. * 取得数据库的表信息
  52. * @access public
  53. * @return array
  54. */
  55. public function getTables($dbName='') {
  56. $result = $this->query("SELECT name FROM sqlite_master WHERE type='table' "
  57. . "UNION ALL SELECT name FROM sqlite_temp_master "
  58. . "WHERE type='table' ORDER BY name");
  59. $info = array();
  60. foreach ($result as $key => $val) {
  61. $info[$key] = current($val);
  62. }
  63. return $info;
  64. }
  65. /**
  66. * SQL指令安全过滤
  67. * @access public
  68. * @param string $str SQL指令
  69. * @return string
  70. */
  71. public function escapeString($str) {
  72. return str_ireplace("'", "''", $str);
  73. }
  74. /**
  75. * limit
  76. * @access public
  77. * @return string
  78. */
  79. public function parseLimit($limit) {
  80. $limitStr = '';
  81. if(!empty($limit)) {
  82. $limit = explode(',',$limit);
  83. if(count($limit)>1) {
  84. $limitStr .= ' LIMIT '.$limit[1].' OFFSET '.$limit[0].' ';
  85. }else{
  86. $limitStr .= ' LIMIT '.$limit[0].' ';
  87. }
  88. }
  89. return $limitStr;
  90. }
  91. }