You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.1 KiB

3 years ago
  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. /**
  19. * 解析pdo连接的dsn信息
  20. * @access public
  21. * @param array $config 连接信息
  22. * @return string
  23. */
  24. protected function parseDsn($config)
  25. {
  26. $dsn = 'sqlite:' . $config['database'];
  27. return $dsn;
  28. }
  29. /**
  30. * 取得数据表的字段信息
  31. * @access public
  32. * @return array
  33. */
  34. public function getFields($tableName)
  35. {
  36. list($tableName) = explode(' ', $tableName);
  37. $result = $this->query('PRAGMA table_info( ' . $tableName . ' )');
  38. $info = array();
  39. if ($result) {
  40. foreach ($result as $key => $val) {
  41. $info[$val['name']] = array(
  42. 'name' => $val['name'],
  43. 'type' => $val['type'],
  44. 'notnull' => (bool) (1 === $val['notnull']),
  45. 'default' => $val['dflt_value'],
  46. 'primary' => '1' == $val['pk'],
  47. 'autoinc' => false,
  48. );
  49. }
  50. }
  51. return $info;
  52. }
  53. /**
  54. * 取得数据库的表信息
  55. * @access public
  56. * @return array
  57. */
  58. public function getTables($dbName = '')
  59. {
  60. $result = $this->query("SELECT name FROM sqlite_master WHERE type='table' "
  61. . "UNION ALL SELECT name FROM sqlite_temp_master "
  62. . "WHERE type='table' ORDER BY name");
  63. $info = array();
  64. foreach ($result as $key => $val) {
  65. $info[$key] = current($val);
  66. }
  67. return $info;
  68. }
  69. /**
  70. * SQL指令安全过滤
  71. * @access public
  72. * @param string $str SQL指令
  73. * @return string
  74. */
  75. public function escapeString($str)
  76. {
  77. return str_ireplace("'", "''", $str);
  78. }
  79. /**
  80. * limit
  81. * @access public
  82. * @return string
  83. */
  84. public function parseLimit($limit)
  85. {
  86. $limitStr = '';
  87. if (!empty($limit)) {
  88. $limit = explode(',', $limit);
  89. if (count($limit) > 1) {
  90. $limitStr .= ' LIMIT ' . $limit[1] . ' OFFSET ' . $limit[0] . ' ';
  91. } else {
  92. $limitStr .= ' LIMIT ' . $limit[0] . ' ';
  93. }
  94. }
  95. return $limitStr;
  96. }
  97. /**
  98. * 随机排序
  99. * @access protected
  100. * @return string
  101. */
  102. protected function parseRand()
  103. {
  104. return 'RANDOM()';
  105. }
  106. }