1 |
<?php |
2 |
|
3 |
/** |
4 |
* This file contains the SQLDataListSource child class |
5 |
* that uses PHP PostgreSQL functions. Quick hack of PEARDataListSource.inc |
6 |
* |
7 |
* @author Aris Basic <aris.basic@silencesoftware.com> |
8 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
9 |
* @package phpHtmlLib |
10 |
*/ |
11 |
|
12 |
/** |
13 |
* This requires the SQLDataListSource |
14 |
* parent class |
15 |
* |
16 |
*/ |
17 |
include_once($phphtmllib."/widgets/data_list/SQLDataListSource.inc"); |
18 |
|
19 |
/** |
20 |
* This SQLDataListSource child class interacts with |
21 |
* with a PostgreSQL Database using built in pg_* |
22 |
* calls. This assumes that you have PostgreSQL |
23 |
* support compiled into PHP |
24 |
* |
25 |
* How to use? |
26 |
* in the DataList child's get_data_source() method |
27 |
* you pass in the result of the pg_connect() call |
28 |
* to the constructor. |
29 |
* |
30 |
* @author Aris Basic <aris.basic@silencesoftware.com> |
31 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
32 |
* @package phpHtmlLib |
33 |
*/ |
34 |
class PGSQLDataListSource extends SQLDataListSource { |
35 |
|
36 |
/** |
37 |
* This var holds the Database connection reference |
38 |
* that is used to do the sql queries |
39 |
* It is assumed that the db is already |
40 |
* connected to and is valid postgresi connection reference |
41 |
*/ |
42 |
var $_db = NULL; |
43 |
|
44 |
/** |
45 |
* Result of pg_query() |
46 |
*/ |
47 |
var $_result = NULL; |
48 |
|
49 |
|
50 |
/** |
51 |
* The constructor is used to pass in the |
52 |
* postgres connection reference |
53 |
* |
54 |
* @param result of pg_connect() |
55 |
*/ |
56 |
function PGSQLDataListSource( $db ) { |
57 |
$this->set_db_object( $db ); |
58 |
} |
59 |
|
60 |
/** |
61 |
* Set the DB object we will use |
62 |
* to talk to the DB. |
63 |
* |
64 |
* @param object - $db the babw_db object. |
65 |
* |
66 |
*/ |
67 |
function set_db_object( $db ) { |
68 |
$this->_db = $db; |
69 |
} |
70 |
|
71 |
function do_query() { |
72 |
$this->_result = pg_query($this->_db,$this->_query); |
73 |
if ( pg_result_status($this->_result)>4 ) { |
74 |
$msg = pg_result_error($this->_result); |
75 |
user_error("PGSQLDataListSource::pg_query() - query failed : ".$msg); |
76 |
return false; |
77 |
} else { |
78 |
return true; |
79 |
} |
80 |
} |
81 |
|
82 |
|
83 |
|
84 |
/** |
85 |
* This function gets the next data row |
86 |
* from the query() |
87 |
* |
88 |
* @return array() |
89 |
*/ |
90 |
function get_next_data_row() { |
91 |
return pg_fetch_assoc($this->_result); |
92 |
} |
93 |
|
94 |
/** |
95 |
* This function builds the limit |
96 |
* clause portion of a DB query. |
97 |
* |
98 |
* @return string - the limit portion of |
99 |
* the query. |
100 |
*/ |
101 |
function build_limit_clause($offset, $limit) { |
102 |
if ( $this->get_limit() != -1 ) { |
103 |
if ( $offset == '' || $offset == "none" ) { |
104 |
$offset = 0; |
105 |
} |
106 |
$clause = " LIMIT $limit OFFSET $offset "; |
107 |
return $clause; |
108 |
} else { |
109 |
return NULL; |
110 |
} |
111 |
} |
112 |
|
113 |
/** |
114 |
* find the number of rows to be returned |
115 |
* from a query from a table and where clause |
116 |
* |
117 |
* @param string $table - the table to count from |
118 |
* @param string $where_clause - a where clause |
119 |
* |
120 |
* @return int the # of rows |
121 |
*/ |
122 |
function count($tables, $where_clause='', $count_clause='*') { |
123 |
$query = "select count(".$count_clause.") as \"COUNT\" from ".$tables." ".$where_clause; |
124 |
$result = pg_query($this->_db,$query); |
125 |
if ( pg_result_error($result)>4 ) { |
126 |
$msg = pg_result_error($result); |
127 |
user_error("PGSQLDataListSource::count() - query failed : ".$msg); |
128 |
} |
129 |
$value = pg_fetch_assoc($result); |
130 |
return($value ? (int)$value["COUNT"] : NULL); |
131 |
} |
132 |
|
133 |
} |
134 |
|
135 |
?> |