Pgsql.class.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. * 解析pdo连接的dsn信息
  19. * @access public
  20. * @param array $config 连接信息
  21. * @return string
  22. */
  23. protected function parseDsn($config){
  24. $dsn = 'pgsql:dbname='.$config['database'].';host='.$config['hostname'];
  25. if(!empty($config['hostport'])) {
  26. $dsn .= ';port='.$config['hostport'];
  27. }
  28. return $dsn;
  29. }
  30. /**
  31. * 取得数据表的字段信息
  32. * @access public
  33. * @return array
  34. */
  35. public function getFields($tableName) {
  36. list($tableName) = explode(' ', $tableName);
  37. $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.');');
  38. $info = [];
  39. if($result){
  40. foreach ($result as $key => $val) {
  41. $info[$val['field']] = array(
  42. 'name' => $val['field'],
  43. 'type' => $val['type'],
  44. 'notnull' => (bool) ($val['null'] === ''), // not null is empty, null is yes
  45. 'default' => $val['default'],
  46. 'primary' => (strtolower($val['key']) == 'pri'),
  47. 'autoinc' => (strtolower($val['extra']) == 'auto_increment'),
  48. );
  49. }
  50. }
  51. return $info;
  52. }
  53. /**
  54. * 取得数据库的表信息
  55. * @access public
  56. * @return array
  57. */
  58. public function getTables($dbName='') {
  59. $result = $this->query("select tablename as Tables_in_test from pg_tables where schemaname ='public'");
  60. $info = array();
  61. foreach ($result as $key => $val) {
  62. $info[$key] = current($val);
  63. }
  64. return $info;
  65. }
  66. /**
  67. * limit分析
  68. * @access protected
  69. * @param mixed $lmit
  70. * @return string
  71. */
  72. public function parseLimit($limit) {
  73. $limitStr = '';
  74. if(!empty($limit)) {
  75. $limit = explode(',',$limit);
  76. if(count($limit)>1) {
  77. $limitStr .= ' LIMIT '.$limit[1].' OFFSET '.$limit[0].' ';
  78. }else{
  79. $limitStr .= ' LIMIT '.$limit[0].' ';
  80. }
  81. }
  82. return $limitStr;
  83. }
  84. }