pgsql.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. class Db_Pgsql implements IDb {
  3. private $link;
  4. function connect($host, $user, $pass, $db, $port) {
  5. $string = "dbname=$db user=$user";
  6. if ($pass) {
  7. $string .= " password=$pass";
  8. }
  9. if ($host) {
  10. $string .= " host=$host";
  11. }
  12. if (is_numeric($port) && $port > 0) {
  13. $string = "$string port=" . $port;
  14. }
  15. $this->link = pg_connect($string);
  16. if (!$this->link) {
  17. die("Unable to connect to database (as $user to $host, database $db):" . pg_last_error());
  18. }
  19. $this->init();
  20. return $this->link;
  21. }
  22. function escape_string($s, $strip_tags = true) {
  23. if ($strip_tags) $s = strip_tags($s);
  24. return pg_escape_string($s);
  25. }
  26. function query($query, $die_on_error = true) {
  27. global $last_query;
  28. if (strpos($query, "ttrss_error_log") === FALSE) $last_query = $query;
  29. $result = @pg_query($this->link, $query);
  30. if (!$result) {
  31. $error = @pg_last_error($this->link);
  32. @pg_query($this->link, "ROLLBACK");
  33. $query = htmlspecialchars($query); // just in case
  34. user_error("Query $query failed: " . ($this->link ? $error : "No connection"),
  35. $die_on_error ? E_USER_ERROR : E_USER_WARNING);
  36. }
  37. return $result;
  38. }
  39. function fetch_assoc($result) {
  40. return pg_fetch_assoc($result);
  41. }
  42. function num_rows($result) {
  43. return pg_num_rows($result);
  44. }
  45. function fetch_result($result, $row, $param) {
  46. return pg_fetch_result($result, $row, $param);
  47. }
  48. function close() {
  49. return pg_close($this->link);
  50. }
  51. function affected_rows($result) {
  52. return pg_affected_rows($result);
  53. }
  54. function last_error() {
  55. return pg_last_error($this->link);
  56. }
  57. function init() {
  58. $this->query("set client_encoding = 'UTF-8'");
  59. pg_set_client_encoding("UNICODE");
  60. $this->query("set datestyle = 'ISO, european'");
  61. $this->query("set TIME ZONE 0");
  62. $this->query("set cpu_tuple_cost = 0.5");
  63. return true;
  64. }
  65. }
  66. ?>