You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.2 KiB

3 years ago
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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\Driver;
  12. use Think\Db\Driver;
  13. /**
  14. * Pgsql数据库驱动
  15. */
  16. class Pgsql extends Driver
  17. {
  18. /**
  19. * 解析pdo连接的dsn信息
  20. * @access public
  21. * @param array $config 连接信息
  22. * @return string
  23. */
  24. protected function parseDsn($config)
  25. {
  26. $dsn = 'pgsql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
  27. if (!empty($config['hostport'])) {
  28. $dsn .= ';port=' . $config['hostport'];
  29. }
  30. return $dsn;
  31. }
  32. /**
  33. * 取得数据表的字段信息
  34. * @access public
  35. * @return array
  36. */
  37. public function getFields($tableName)
  38. {
  39. list($tableName) = explode(' ', $tableName);
  40. $result = $this->query('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 . '\');');
  41. $info = array();
  42. if ($result) {
  43. foreach ($result as $key => $val) {
  44. $info[$val['field']] = array(
  45. 'name' => $val['field'],
  46. 'type' => $val['type'],
  47. 'notnull' => (bool) ('' === $val['null']), // not null is empty, null is yes
  48. 'default' => $val['default'],
  49. 'primary' => (strtolower($val['key']) == 'pri'),
  50. 'autoinc' => (strtolower($val['extra']) == 'auto_increment'),
  51. );
  52. }
  53. }
  54. return $info;
  55. }
  56. /**
  57. * 取得数据库的表信息
  58. * @access public
  59. * @return array
  60. */
  61. public function getTables($dbName = '')
  62. {
  63. $result = $this->query("select tablename as Tables_in_test from pg_tables where schemaname ='public'");
  64. $info = array();
  65. foreach ($result as $key => $val) {
  66. $info[$key] = current($val);
  67. }
  68. return $info;
  69. }
  70. /**
  71. * limit分析
  72. * @access protected
  73. * @param mixed $lmit
  74. * @return string
  75. */
  76. public function parseLimit($limit)
  77. {
  78. $limitStr = '';
  79. if (!empty($limit)) {
  80. $limit = explode(',', $limit);
  81. if (count($limit) > 1) {
  82. $limitStr .= ' LIMIT ' . $limit[1] . ' OFFSET ' . $limit[0] . ' ';
  83. } else {
  84. $limitStr .= ' LIMIT ' . $limit[0] . ' ';
  85. }
  86. }
  87. return $limitStr;
  88. }
  89. /**
  90. * 随机排序
  91. * @access protected
  92. * @return string
  93. */
  94. protected function parseRand()
  95. {
  96. return 'RANDOM()';
  97. }
  98. }