Pgsql.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * Pgsql数据库驱动
  16. */
  17. class Pgsql 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 = 'pgsql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
  28. if (!empty($config['hostport'])) {
  29. $dsn .= ';port=' . $config['hostport'];
  30. }
  31. return $dsn;
  32. }
  33. /**
  34. * 取得数据表的字段信息
  35. * @access public
  36. * @param string $tableName
  37. * @return array
  38. */
  39. public function getFields($tableName)
  40. {
  41. $this->initConnect(true);
  42. list($tableName) = explode(' ', $tableName);
  43. $sql = 'select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\'' . $tableName . '\');';
  44. // 调试开始
  45. $this->debug(true);
  46. $pdo = $this->linkID->query($sql);
  47. // 调试结束
  48. $this->debug(false, $sql);
  49. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  50. $info = [];
  51. if ($result) {
  52. foreach ($result as $key => $val) {
  53. $val = array_change_key_case($val);
  54. $info[$val['field']] = [
  55. 'name' => $val['field'],
  56. 'type' => $val['type'],
  57. 'notnull' => (bool) ('' !== $val['null']),
  58. 'default' => $val['default'],
  59. 'primary' => !empty($val['key']),
  60. 'autoinc' => (0 === strpos($val['extra'], 'nextval(')),
  61. ];
  62. }
  63. }
  64. return $this->fieldCase($info);
  65. }
  66. /**
  67. * 取得数据库的表信息
  68. * @access public
  69. * @param string $dbName
  70. * @return array
  71. */
  72. public function getTables($dbName = '')
  73. {
  74. $this->initConnect(true);
  75. $sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'";
  76. // 调试开始
  77. $this->debug(true);
  78. $pdo = $this->linkID->query($sql);
  79. // 调试结束
  80. $this->debug(false, $sql);
  81. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  82. $info = [];
  83. foreach ($result as $key => $val) {
  84. $info[$key] = current($val);
  85. }
  86. return $info;
  87. }
  88. /**
  89. * SQL性能分析
  90. * @access protected
  91. * @param string $sql
  92. * @return array
  93. */
  94. protected function getExplain($sql)
  95. {
  96. return [];
  97. }
  98. protected function supportSavepoint()
  99. {
  100. return true;
  101. }
  102. }