Sqlite.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. $name = isset($val['field']) ? $val['field'] : $val['name'];
  39. $info[$name] = array(
  40. 'name' => $name,
  41. 'type' => $val['type'],
  42. 'notnull' => (bool)(((isset($val['null'])) && ($val['null'] === '')) || ((isset($val['notnull'])) && ($val['notnull'] === ''))), // not null is empty, null is yes
  43. 'default' => isset($val['default']) ? $val['default'] : (isset($val['dflt_value'])?$val['dflt_value']:""),
  44. 'primary' => isset($val['key']) ? strtolower($val['key']) == 'pri' : (isset($val['pk']) ? $val['pk'] : false),
  45. 'autoinc' => isset($val['extra']) ? strtolower($val['extra']) == 'auto_increment' : (isset($val['key']) ? $val['key'] : false),
  46. );
  47. }
  48. }
  49. return $info;
  50. }
  51. /**
  52. * 取得数据库的表信息
  53. * @access public
  54. * @return array
  55. */
  56. public function getTables($dbName='') {
  57. $result = $this->query("SELECT name FROM sqlite_master WHERE type='table' "
  58. . "UNION ALL SELECT name FROM sqlite_temp_master "
  59. . "WHERE type='table' ORDER BY name");
  60. $info = array();
  61. foreach ($result as $key => $val) {
  62. $info[$key] = current($val);
  63. }
  64. return $info;
  65. }
  66. /**
  67. * SQL指令安全过滤
  68. * @access public
  69. * @param string $str SQL指令
  70. * @return string
  71. */
  72. public function escapeString($str) {
  73. return str_ireplace("'", "''", $str);
  74. }
  75. /**
  76. * limit
  77. * @access public
  78. * @return string
  79. */
  80. public function parseLimit($limit) {
  81. $limitStr = '';
  82. if(!empty($limit)) {
  83. $limit = explode(',',$limit);
  84. if(count($limit)>1) {
  85. $limitStr .= ' LIMIT '.$limit[1].' OFFSET '.$limit[0].' ';
  86. }else{
  87. $limitStr .= ' LIMIT '.$limit[0].' ';
  88. }
  89. }
  90. return $limitStr;
  91. }
  92. }