Sqlite.php 2.9 KB

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