pgsql.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. $result = @pg_query($this->link, $query);
  28. if (!$result) {
  29. $error = @pg_last_error($this->link);
  30. @pg_query($this->link, "ROLLBACK");
  31. $query = htmlspecialchars($query); // just in case
  32. user_error("Query $query failed: " . ($this->link ? $error : "No connection"),
  33. $die_on_error ? E_USER_ERROR : E_USER_WARNING);
  34. }
  35. return $result;
  36. }
  37. function fetch_assoc($result) {
  38. return pg_fetch_assoc($result);
  39. }
  40. function num_rows($result) {
  41. return pg_num_rows($result);
  42. }
  43. function fetch_result($result, $row, $param) {
  44. return pg_fetch_result($result, $row, $param);
  45. }
  46. function close() {
  47. return pg_close($this->link);
  48. }
  49. function affected_rows($result) {
  50. return pg_affected_rows($result);
  51. }
  52. function last_error() {
  53. return pg_last_error($this->link);
  54. }
  55. function init() {
  56. $this->query("set client_encoding = 'UTF-8'");
  57. pg_set_client_encoding("UNICODE");
  58. $this->query("set datestyle = 'ISO, european'");
  59. $this->query("set TIME ZONE 0");
  60. return true;
  61. }
  62. }
  63. ?>