Pgsql.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * Pgsql数据库驱动
  16. */
  17. class Pgsql extends Connection
  18. {
  19. protected $builder = '\\think\\db\\builder\\Pgsql';
  20. /**
  21. * 解析pdo连接的dsn信息
  22. * @access protected
  23. * @param array $config 连接信息
  24. * @return string
  25. */
  26. protected function parseDsn($config)
  27. {
  28. $dsn = 'pgsql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
  29. if (!empty($config['hostport'])) {
  30. $dsn .= ';port=' . $config['hostport'];
  31. }
  32. return $dsn;
  33. }
  34. /**
  35. * 取得数据表的字段信息
  36. * @access public
  37. * @param string $tableName
  38. * @return array
  39. */
  40. public function getFields($tableName)
  41. {
  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. $pdo = $this->query($sql, [], false, true);
  45. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  46. $info = [];
  47. if ($result) {
  48. foreach ($result as $key => $val) {
  49. $val = array_change_key_case($val);
  50. $info[$val['field']] = [
  51. 'name' => $val['field'],
  52. 'type' => $val['type'],
  53. 'notnull' => (bool) ('' !== $val['null']),
  54. 'default' => $val['default'],
  55. 'primary' => !empty($val['key']),
  56. 'autoinc' => (0 === strpos($val['extra'], 'nextval(')),
  57. ];
  58. }
  59. }
  60. return $this->fieldCase($info);
  61. }
  62. /**
  63. * 取得数据库的表信息
  64. * @access public
  65. * @param string $dbName
  66. * @return array
  67. */
  68. public function getTables($dbName = '')
  69. {
  70. $sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'";
  71. $pdo = $this->query($sql, [], false, true);
  72. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  73. $info = [];
  74. foreach ($result as $key => $val) {
  75. $info[$key] = current($val);
  76. }
  77. return $info;
  78. }
  79. /**
  80. * SQL性能分析
  81. * @access protected
  82. * @param string $sql
  83. * @return array
  84. */
  85. protected function getExplain($sql)
  86. {
  87. return [];
  88. }
  89. protected function supportSavepoint()
  90. {
  91. return true;
  92. }
  93. }