Commit 48f1cf998fb43a77ce140f91cb4a8047d6e7e6aa

  • avatar
  • p4bl0 <pablo.rauzy @gm…l.com>
  • Thu Apr 30 15:04:39 CEST 2009
Adding a new Belokan_Db_PostgreSQL class
.gitignore
(0 / 1)
  
11*~
22doc
3Db/PostgreSQL.php
Db.php
(9 / 0)
  
1414 $_connection,
1515 $_persistent,
1616 $_host,
17 $_port,
1718 $_user,
1819 $_pass,
1920 $_base,
2828 $this->_connection = null;
2929 if (isset($db_params['persistent']))
3030 $this->_persistent = $db_params['persistent'];
31 else
32 $this->_persistent = null;
3133 if (isset($db_params['host'])) $this->_host = $db_params['host'];
34 else $this->_host = null;
35 if (isset($db_params['port'])) $this->_port = $db_params['port'];
36 else $this->_port = null;
3237 if (isset($db_params['user'])) $this->_user = $db_params['user'];
38 else $this->_user = null;
3339 if (isset($db_params['pass'])) $this->_pass = $db_params['pass'];
40 else $this->_pass = null;
3441 if (isset($db_params['base'])) $this->_base = $db_params['base'];
42 else $this->_base = null;
3543 $this->_query = null;
3644 $this->_numberOfQueries = 0;
3745 $this->_results = null;
  
1313 public function __construct ($db_params)
1414 {
1515 parent::__construct($db_params);
16 if ($this->_port != null) {
17 $this->_host .= ':'.$this->_port;
18 $this->_port = null;
19 }
1620 $this->connect();
1721 }
1822
  
1<?php
2/*
3 Belokan Micro Framework
4 Licensed under GNU GPL
5 http://gitorious.org/projects/belokan
6 ---
7 Belokan/Db/PostgreSQL.php:
8 PostgreSQL database class
9*/
10
11class Belokan_Db_PostgreSQL extends Belokan_Db {
12
13 public function __construct ($db_params)
14 {
15 parent::__construct($db_params);
16 $this->connect();
17 }
18
19 public function connect ()
20 {
21 $con = '';
22 if ($this->_host != null) $con .= ' host='.$this->_host;
23 if ($this->_port != null) $con .= ' port='.$this->_port;
24 if ($this->_base != null) $con .= ' dbname='.$this->_base;
25 if ($this->_user != null) $con .= ' user='.$this->_user;
26 if ($this->_pass != null) $con .= ' password='.$this->_pass;
27
28 if (empty($con)) return;
29 $con = substr($con, 1);
30
31 if ($this->_persistent)
32 $this->_connection = pg_pconnect($con);
33 else
34 $this->_connection = pg_connect($con);
35 }
36
37 public function execute ()
38 {
39 $this->_results = pg_query($this->_connection, $this->_query);
40 $this->_numberOfQueries++;
41 return $this->_results;
42 }
43
44 public function execQuery ($query, $update_query=false)
45 {
46 if ($update_query == true) $this->_query = $query;
47 $this->_numberOfQueries++;
48 return pg_query($this->_connection, $query);
49 }
50
51 public function seek ($row)
52 {
53 pg_result_seek($this->_results, $row);
54 return pg_fetch_row($this->_results);
55 }
56
57 public function lastAutoIncrementValue ($table, $ai_col)
58 {
59 $laiv = pg_query($this->_connection,
60 "SELECT currval(pg_get_serial_sequence('".$table."', '".$ai_col."'));");
61 $this->_numberOfQueries++;
62 $laiv = pg_fetch_array($laiv, null, PGSQL_NUM);
63 return $laiv[0];
64 }
65
66 public function fetchRows ($fetch_object=false)
67 {
68 if ($this->_results == null) return null;
69 $this->_rows = array();
70 if ($fetch_object)
71 while ($row = pg_fetch_object($this->_results))
72 $this->_rows[] = $row;
73 else
74 while ($row = pg_fetch_array($this->_results))
75 $this->_rows[] = $row;
76 return $this->_rows;
77 }
78
79 public function fetchRow ($fetch_object=false)
80 {
81 if ($this->_results == null) return null;
82 if ($fetch_object) return pg_fetch_object($this->_results);
83 return pg_fetch_array($this->_results);
84 }
85
86 public function numberOfRows ()
87 {
88 if (strtolower(substr($this->_query, 0, 6)) == 'select' && $this->_results != null)
89 return mysql_num_rows($this->_results);
90 else if ($this->_results === true)
91 return $this->affectedRows();
92 return null;
93 }
94
95 public function affectedRows ()
96 {
97 if (in_array(strtolower(substr($this->_query, 0, 7)), array('insert ', 'update ', 'delete ')))
98 return pg_affected_rows($this->_results);
99 return null;
100 }
101
102 public function freeResults ()
103 {
104 if (strtolower(substr($this->_query, 0, 6)) == 'select' && $this->_results != null)
105 pg_free_result($this->_results);
106 }
107
108}