Postgres.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. * use this file except in compliance with the License. You may obtain a copy of
  7. * the License at
  8. *
  9. * Http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. namespace PhpLife\Library;
  18. require_once 'Medoo/Medoo.php';
  19. use \Medoo\Medoo;
  20. use PhpLife\Config\Postgres\Core;
  21. /**
  22. * Medoo数据库操作
  23. */
  24. class Postgres
  25. {
  26. public static $instance;
  27. /**
  28. * @var Medoo
  29. */
  30. public $postgres;
  31. public function __construct()
  32. {
  33. $this->connect();
  34. }
  35. public static function getInstance()
  36. {
  37. if (!isset(self::$instance)) {
  38. $instance = new self();
  39. self::$instance = $instance;
  40. } else {
  41. $instance = self::$instance;
  42. }
  43. return $instance;
  44. }
  45. public function select($table,$where=[],$column='*',$limit=10,$offset=0,$order=null){
  46. if ($limit){
  47. $where['LIMIT']=[$offset,$limit];
  48. }
  49. if ($order){
  50. $where['ORDER']=$order;
  51. }
  52. //'t_article', "*",['LIMIT'=>[4,2],'ORDER'=>'articleno']
  53. return $this->postgres->select($table,$column,$where);
  54. }
  55. public function connect()
  56. {
  57. $confg = Core::getData();
  58. $this->postgres = new medoo([
  59. // 必须配置项
  60. 'database_type' => 'pgsql',
  61. 'database_name' => $confg['dbname'],
  62. 'server' => $confg['host'],
  63. 'username' => $confg['username'],
  64. 'password' => $confg['password'],
  65. 'charset' => 'utf8',
  66. // 可选参数
  67. 'port' => $confg['port'],
  68. // 连接参数扩展, 更多参考 http://www.php.net/manual/en/pdo.setattribute.php
  69. 'option' => [
  70. \PDO::ATTR_CASE => \PDO::CASE_NATURAL
  71. ]
  72. ]);
  73. }
  74. }