pgsql.php 1.6 KB

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