Postgre连接类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Postgre连接类相关的知识,希望对你有一定的参考价值。

  1. <?php
  2.  
  3. define('VM_FETCH_ARRAY' , 'array');
  4. define('VM_FETCH_ASSOC' , 'assoc');
  5. define('VM_FETCH_OBJECT', 'object');
  6. define('VM_FETCH_ROW' , 'row');
  7.  
  8. class Conexao {
  9.  
  10. private $host = DB_SERVER;
  11. private $dbname = DB_NAME;
  12. private $user = DB_USER;
  13. private $pswd = DB_PASS;
  14. private $port = DB_PORT;
  15.  
  16. var $_query_id;
  17.  
  18. private $con;
  19.  
  20. public function __construct() {
  21.  
  22. $this->con = pg_connect("host=$this->host dbname=$this->dbname user=$this->user password=$this->pswd port=$this->port");
  23. if (!$this->con) {
  24. die('Não foi possível conectar-se com o servidor: ' . pg_last_error());
  25. }
  26.  
  27. } //método construtor
  28.  
  29. public function fecha(){
  30. # fecha cpnexão
  31. pg_close($this->con);
  32. }
  33.  
  34. public function execute($sql) {
  35. $sql = trim($sql);
  36. $retorno = false;
  37.  
  38. # executa a query
  39. $result = pg_query($sql);
  40.  
  41. //print_r($sql);
  42. # no caso de select, os dados são colocados em um array
  43. if (is_bool($result))
  44. $retorno = $result;
  45. else {
  46.  
  47. while ($row = $this->fetch_assoc($result)) {
  48. # codifica resultado para utf8
  49. array_walk($row, 'toUtf8');
  50. $retorno[] = $row;
  51. }
  52. }
  53.  
  54. return (is_bool($retorno) && !$retorno)?true:$retorno;
  55. }
  56.  
  57. /**
  58. * Metodo que irá criar um Array com os dados da consulta.
  59. * @param {string} $result: Resultado da Consulta.
  60. * @return {array} $rows: Array com os dados da Consulta.
  61. **/
  62. function criarArray($result = ''){
  63. $result = !$result ? $this->_queryId : $result;
  64. $rows = array();
  65.  
  66. // LOOP que irá criar o Array com os dados.
  67. while ($row = $this->fetch_object()) {
  68. # codifica resultado para utf8
  69. array_walk($row, 'toUtf8');
  70. $rows[] = $row;
  71. }
  72. return $rows;
  73. }
  74.  
  75. public function sqlQuery($sql) {
  76. $this->_query_id = pg_query($sql) or die(pg_last_error());
  77. return $this->_query_id ? $this->_query_id : false;
  78. }
  79.  
  80. public function insert_id() {
  81. return pg_insert($this->con);
  82. }
  83.  
  84. public function fetch_array($query_id = '') {
  85. $query_id = (!$query_id) ? $this->_query_id :
  86. $query_id;
  87. return pg_fetch_array($query_id, PGSQL_ASSOC);
  88. }
  89.  
  90. public function fetch_row($query_id = '') {
  91. $query_id = (!$query_id) ? $this->_query_id :
  92. $query_id;
  93. return pg_fetch_row($query_id);
  94. }
  95.  
  96. public function fetch_object($query_id = '') {
  97. $query_id = (!$query_id) ? $this->_query_id :
  98. $query_id;
  99. return pg_fetch_object($query_id);
  100. }
  101.  
  102. public function fetch_assoc($query_id = '') {
  103. $query_id = (!$query_id) ? $this->_query_id :
  104. $query_id;
  105. return pg_fetch_assoc($query_id);
  106. }
  107.  
  108. public function num_rows($query_id = '') {
  109. $query_id = (!$query_id) ? $this->_query_id :
  110. $query_id;
  111. return pg_num_rows($query_id);
  112. }
  113.  
  114. public function result($result = '', $row = 0, $cols = 0) {
  115. $result = !$result ? $this->_query_id :
  116. $result;
  117. return pg_fetch_result($result, $row, $cols);
  118. }
  119.  
  120. public function num_fields($result = '') {
  121. $result = !$result ? $this->_query_id :
  122. $result;
  123. return pg_num_fields($result);
  124. }
  125.  
  126. public function affected_rows() {
  127. return pg_affected_rows($this->con);
  128. }
  129.  
  130. public function field_name($result = '', $i = 0) {
  131. $result = !$result ? $this->_query_id :
  132. $result;
  133. return pg_field_name($result, $i);
  134. }
  135.  
  136. public function field_type($result = '', $i = 0) {
  137. $result = !$result ? $this->_query_id :
  138. $result;
  139. return pg_field_type($result, $i);
  140. }
  141.  
  142. function start_transaction() {
  143. pg_query("START TRANSACTION");
  144. }
  145.  
  146. function commit() {
  147. pg_query("COMMIT");
  148. }
  149.  
  150. function rollback() {
  151. pg_query("ROLLBACK");
  152. }
  153.  
  154. }
  155. ?>

以上是关于Postgre连接类的主要内容,如果未能解决你的问题,请参考以下文章

使用 AWS 驱动程序与 Redshift 的 R 连接不起作用,但可以与 Postgre 驱动程序一起使用

python连接postgres方法

libpq VS qpsql(postgre 的 QT 驱动程序)

postgre 批量删除表

使用 postgre 数据库配置水晶报表 XI 的问题

postgre 的一个小坑(GoFrame)