Sqlite.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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\connector;
  12. use PDO;
  13. use think\db\Connection;
  14. /**
  15. * Sqlite数据库驱动
  16. */
  17. class Sqlite extends Connection
  18. {
  19. /**
  20. * 解析pdo连接的dsn信息
  21. * @access protected
  22. * @param array $config 连接信息
  23. * @return string
  24. */
  25. protected function parseDsn($config)
  26. {
  27. $dsn = 'sqlite:' . $config['database'];
  28. return $dsn;
  29. }
  30. /**
  31. * 取得数据表的字段信息
  32. * @access public
  33. * @param string $tableName
  34. * @return array
  35. */
  36. public function getFields($tableName)
  37. {
  38. $this->initConnect(true);
  39. list($tableName) = explode(' ', $tableName);
  40. $sql = 'PRAGMA table_info( ' . $tableName . ' )';
  41. // 调试开始
  42. $this->debug(true);
  43. $pdo = $this->linkID->query($sql);
  44. // 调试结束
  45. $this->debug(false, $sql);
  46. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  47. $info = [];
  48. if ($result) {
  49. foreach ($result as $key => $val) {
  50. $val = array_change_key_case($val);
  51. $info[$val['name']] = [
  52. 'name' => $val['name'],
  53. 'type' => $val['type'],
  54. 'notnull' => 1 === $val['notnull'],
  55. 'default' => $val['dflt_value'],
  56. 'primary' => '1' == $val['pk'],
  57. 'autoinc' => '1' == $val['pk'],
  58. ];
  59. }
  60. }
  61. return $this->fieldCase($info);
  62. }
  63. /**
  64. * 取得数据库的表信息
  65. * @access public
  66. * @param string $dbName
  67. * @return array
  68. */
  69. public function getTables($dbName = '')
  70. {
  71. $this->initConnect(true);
  72. $sql = "SELECT name FROM sqlite_master WHERE type='table' "
  73. . "UNION ALL SELECT name FROM sqlite_temp_master "
  74. . "WHERE type='table' ORDER BY name";
  75. // 调试开始
  76. $this->debug(true);
  77. $pdo = $this->linkID->query($sql);
  78. // 调试结束
  79. $this->debug(false, $sql);
  80. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  81. $info = [];
  82. foreach ($result as $key => $val) {
  83. $info[$key] = current($val);
  84. }
  85. return $info;
  86. }
  87. /**
  88. * SQL性能分析
  89. * @access protected
  90. * @param string $sql
  91. * @return array
  92. */
  93. protected function getExplain($sql)
  94. {
  95. return [];
  96. }
  97. protected function supportSavepoint()
  98. {
  99. return true;
  100. }
  101. }