Pgsql.php 3.4 KB

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