3 class postgreSql
extends interfaceSql
{
11 public function __construct( $dbHost = null, $dbUser = null, $dbPass = null , $dbName = null ){
13 $this->host
= $dbHost;
14 elseif (defined('DB_HOST'))
15 $this->host
= DB_HOST
;
17 trigger_error('Database connection data missing!', E_USER_ERROR
);
20 $this->user
= $dbUser;
21 elseif (defined('DB_USER'))
22 $this->user
= DB_USER
;
24 trigger_error('Database connection data missing!', E_USER_ERROR
);
27 $this->pass
= $dbPass;
28 elseif (defined('DB_PASS'))
29 $this->pass
= DB_PASS
;
31 trigger_error('Database connection data missing!', E_USER_ERROR
);
33 // this is needed for postgresql
35 $this->name
= $dbName;
36 elseif (defined('DB_NAME'))
37 $this->name
= DB_NAME
;
39 trigger_error('Database connection data missing!', E_USER_ERROR
);
41 if (!empty($this->host
) && !empty($this->user
) && !empty($this->pass
)) {
46 public function __destruct() {
47 // var_dump($this->link);
48 // if (!empty ($this->link) && $this->link instanceof mysqli)
54 * wrapper for pg_connect
58 private function connect () {
59 $this->link
= pg_connect('host='.$this->host
.' user='.$this->user
.' password='.$this->pass
. (!empty ($this->name
) ?
'dbname='.$this->name
: '' ));
60 $err = pg_last_error($this->link
);
63 // trigger_error ($this->link->error, E_USER_ERROR);
70 * wrapper for pg_close
74 public function close (){
75 // if ($this->link instanceof mysqli)
76 pg_close($this->link
);
82 * @param string $incData
85 public function selectDatabase ($incData){
86 // in postgres we don't quite need it as pg_connect handles it
91 * wrapper for pg_escape_string
93 * @param mixed $incData
96 public function escape ($incData){
97 // so far no escaping on BYTEA fields
98 // also there's a problem with the fact that I use tdoAbstract->escape
99 // to enclose values in quotes for MySQL
100 // TODO - this fracks up the postgres stuff
101 if (is_string($incData))
102 return pg_escape_string ($this->link
, $incData);
103 elseif (is_numeric ($incData))
104 return (int)$incData;
108 * wrapper for mysql_query
110 * @param string $query
113 public function query ($query){
114 if (pg_connection_status($this->link
) !== PGSQL_CONNECTION_OK
) {
117 if (!empty($query)) {
118 $qst = microtime(true);
119 $this->conn
= pg_query($this->link
, $query);
120 $qend = microtime(true);
121 echo htmlentities ($query).' ['.number_format($qend-$qst, 5, ',', '.').'s]<br/>'."\n";
122 if (isset($GLOBALS['qCnt']))
128 trigger_error ($this->link
->error
.'<br/> ' . $query);
132 if (stristr('select', $query))
135 elseif ( preg_match('/insert|update|replace|delete/i', $query) )
136 return pg_affected_rows ($this->conn
);
140 * wrapper for mysql_fetch_row
144 public function getRow (){
146 return pg_fetch_row ($this->conn
);
149 // FIXME: for some reason the getAssoc and getArray work differently
150 public function getAssoc () {
152 return pg_fetch_assoc ($this->conn
);
156 * wrapper for mysql_fetch_row
160 public function getObjects () {
164 // if ($this->conn && $this->link) {
165 // while ($i < mysqli_field_count ($this->link)) {
166 // $t = $this->conn->fetch_field_direct ($i++);
175 * wrapper for mysql_fetch_assoc
179 public function getArray (){
182 while ( ($r = pg_fetch_assoc ($this->conn
)) ){
190 * getting the first result in the resultset
194 public function getScalar() {
195 $retVal = $this->getRow();
196 if (is_array($retVal))
197 $retVal = current($retVal);
203 * @param array $incObj = array (array('field1','alias1),array('field2','alias2),...)
206 public function _SELECT ($incObj){
211 return $retStr.' "'.$incObj.'" ';
214 public function _CREATE (){
218 public function _SET(){
222 public function _INSERT ($incData){
223 if (empty ($incData))
225 return ' INSERT INTO "' . $incData . '"';
228 public function _VALUES ($incData) {
229 if (empty ($incData))
232 if (is_array ($incData)) {
233 $ret = implode("', '", $incData);
237 return ' VALUES (' . "'" . $ret . "')";
240 public function _UPDATE ($incOb){
241 if (!is_array($incOb))
242 $incOb[] = array ($incOb);
243 return ' UPDATE "'.$incOb[0].(!empty($incOb[1]) ?
'" AS "'.$incOb[1] : '').'"';
247 * returns the FROM tabl...es part of the query
249 * @param string or array of strings $incData - table names
252 public function _FROM ($incData){
253 if (empty ($incData))
255 if (is_array($incData))
256 $incData = implode('", "',$incData);
258 return ' FROM "'.$incData.'" ';
264 public function _AND (){
271 public function _OR (){
274 public function _JOIN ($type) {
281 public function _AS ($str){
282 return ' AS "' . $str . '"';
285 public function _LIMIT ($start, $end = 0){
287 return ' LIMIT '.(int)$start . ', '.(int)$end;
288 elseif (!empty ($start))
289 return ' LIMIT '.(int)$start;
295 * TODO make it receive an array of tdoHabstractFields
298 * @param array of strings $colName
301 public function _GROUP ($incObj = null){
305 $retStr = ' GROUP BY ';
306 return $retStr . ' "' . $incObj . '"';
310 * method that abstracts the ORDER BY clauses
312 * @param string $orderBys
315 public function _ORDER ($orderBys = null){
317 if (empty($orderBys))
319 $retStr = ' ORDER BY ';
321 return $retStr.$orderBys;
324 public function _WHERE ($clause) {
325 return ' WHERE '.$clause;