PageRenderTime 33ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/core/db/DbPgsql.php

http://zoop.googlecode.com/
PHP | 84 lines | 70 code | 12 blank | 2 comment | 5 complexity | 969968d9e573f67f3e99e56977b64976 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. class DbPgsql extends DbConnection
  3. {
  4. private $connectionString;
  5. private $connection;
  6. public function init()
  7. {
  8. // create the connection string
  9. $connString = 'dbname=' . $this->params['database'];
  10. $connString .= ' user=' . $this->params['username'];
  11. if(isset($this->params['host']))
  12. $connString .= ' host=' . $this->params['host'];
  13. if(isset($this->params['port']))
  14. $connString .= ' port=' . $this->params['port'];
  15. if(isset($this->params['password']))
  16. $connString .= ' password=' . $this->params['password'];
  17. $this->connectionString = $connString;
  18. }
  19. public function getRequireds()
  20. {
  21. return array('database', 'username');
  22. }
  23. function escapeString($string)
  24. {
  25. self::connect();
  26. if(version_compare(PHP_VERSION, "5.2", "<"))
  27. return "'" . pg_escape_string($string) . "'";
  28. else
  29. return "'" . pg_escape_string($this->connection, $string) . "'";
  30. }
  31. function _query($sql)
  32. {
  33. self::connect();
  34. $result = pg_query($this->connection, $sql);
  35. return new DbPgResult($this->connection, $result);
  36. }
  37. function getLastInsertId()
  38. {
  39. return $this->fetchCell("select lastval()", array());
  40. }
  41. private function connect()
  42. {
  43. // lazy connection to the database
  44. if(!$this->connection)
  45. $this->connection = pg_connect($this->connectionString, PGSQL_CONNECT_FORCE_NEW);
  46. }
  47. public function tableExists($name)
  48. {
  49. $sql = "SELECT table_name
  50. FROM information_schema.tables
  51. WHERE table_type = 'BASE TABLE'
  52. AND table_schema NOT IN ('pg_catalog', 'information_schema')
  53. AND table_name = :name";
  54. return $this->fetchCell($sql, array('name' => $name)) ? 1 : 0;
  55. }
  56. public function getTableNames()
  57. {
  58. $sql = "SELECT table_name
  59. FROM information_schema.tables
  60. WHERE table_type = 'BASE TABLE' and table_schema <> 'pg_catalog' and table_schema <> 'information_schema'";
  61. return $this->fetchColumn($sql, array());
  62. }
  63. public function getTableFieldInfo($tableName)
  64. {
  65. $sql = "SELECT
  66. column_name as name,
  67. data_type as type
  68. --, *
  69. from information_schema.columns where table_schema = 'public' and table_name = :name";
  70. return $this->fetchRows($sql, array('name' => $tableName));
  71. }
  72. }