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