1 |
jonen |
1.1 |
<?php |
2 |
|
|
|
3 |
|
|
/** |
4 |
|
|
* This file contains the SQLDataListSource child class |
5 |
|
|
* that uses the PEAR DB object to talk to the DB. |
6 |
|
|
* |
7 |
|
|
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
8 |
|
|
* @package phpHtmlLib |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
/** |
12 |
|
|
* This requires the SQLDataListSource |
13 |
|
|
* parent class |
14 |
|
|
* |
15 |
|
|
*/ |
16 |
|
|
include_once($phphtmllib."/widgets/data_list/SQLDataListSource.inc"); |
17 |
|
|
|
18 |
|
|
/** |
19 |
|
|
* Have to have PEAR and DB included |
20 |
|
|
* the pear dir must be in the |
21 |
|
|
* include path. |
22 |
|
|
*/ |
23 |
|
|
require_once("PEAR.php"); |
24 |
|
|
require_once("DB.php"); |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* This SQLDataListSource child class interacts with |
28 |
|
|
* with the specific DB via the php PEAR DB abstraction |
29 |
|
|
* objects. |
30 |
|
|
* |
31 |
|
|
* How to use? |
32 |
|
|
* in the DataList child's get_data_source() method |
33 |
|
|
* you pass in the already connected PEAR DB object |
34 |
|
|
* in to the constructor. PEARSQLDataListSource |
35 |
|
|
* takes care of the rest. |
36 |
|
|
* |
37 |
jonen |
1.2 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
38 |
|
|
* @package phpHtmlLib |
39 |
jonen |
1.1 |
*/ |
40 |
|
|
class PEARSQLDataListSource extends SQLDataListSource { |
41 |
|
|
|
42 |
|
|
/** |
43 |
|
|
* This var holds the Database object |
44 |
|
|
* that is used to do the sql queries |
45 |
|
|
* It is assumed that the db is already |
46 |
|
|
* connected to, and the object provides |
47 |
|
|
* 2 methods: |
48 |
|
|
* query() - execute a sql query |
49 |
|
|
*/ |
50 |
|
|
var $_db = NULL; |
51 |
|
|
|
52 |
|
|
/** |
53 |
|
|
* this holds the query result from the |
54 |
|
|
* PEAR::DB->query() call |
55 |
|
|
* |
56 |
|
|
*/ |
57 |
|
|
var $_result = NULL; |
58 |
|
|
|
59 |
|
|
|
60 |
|
|
/** |
61 |
|
|
* The constructor is used to pass in the |
62 |
|
|
* PEAR DB object that has already been |
63 |
|
|
* created and connected to the db. |
64 |
|
|
* |
65 |
|
|
* @param PEAR::DB object - MUST BE CONNECTED |
66 |
|
|
*/ |
67 |
|
|
function PEARSQLDataListSource( &$db ) { |
68 |
|
|
$this->set_db_object( $db ); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* Set the DB object we will use |
73 |
|
|
* to talk to the DB. |
74 |
|
|
* |
75 |
jonen |
1.3 |
* @param object - $db the babw_db object. |
76 |
jonen |
1.1 |
*/ |
77 |
|
|
function set_db_object( &$db ) { |
78 |
|
|
$this->_db = &$db; |
79 |
|
|
} |
80 |
|
|
|
81 |
jonen |
1.3 |
/** |
82 |
|
|
* This is the function that does the data fetching, |
83 |
|
|
* and sorting if needed. |
84 |
|
|
* If the source is a sql database, this is where the |
85 |
|
|
* query gets called. This function doesn't actually read the |
86 |
|
|
* data from the DB yet. That is what get_next_data_row() |
87 |
|
|
* does. |
88 |
|
|
* |
89 |
|
|
* @return boolean - the query passed/failed. |
90 |
|
|
*/ |
91 |
jonen |
1.1 |
function do_query() { |
92 |
|
|
$this->_result = $this->_db->query($this->_query); |
93 |
|
|
if (DB::isError($this->_result)) { |
94 |
|
|
$msg = $this->_result->getMessage(); |
95 |
|
|
user_error("PEARSQLDataListSource::do_query() - query failed : ".$msg); |
96 |
jonen |
1.3 |
return false; |
97 |
|
|
} else { |
98 |
|
|
return true; |
99 |
|
|
} |
100 |
jonen |
1.1 |
} |
101 |
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* This function gets the next data row |
106 |
|
|
* from the query() |
107 |
|
|
* |
108 |
|
|
* @return array() |
109 |
|
|
*/ |
110 |
|
|
function get_next_data_row() { |
111 |
|
|
return $this->_result->fetchRow(DB_FETCHMODE_ASSOC); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* This function builds the limit |
116 |
|
|
* clause portion of a DB query. |
117 |
|
|
* |
118 |
|
|
* @return string - the limit portion of |
119 |
|
|
* the query. |
120 |
|
|
*/ |
121 |
|
|
function build_limit_clause($offset, $limit) { |
122 |
|
|
if ($this->get_limit() != -1 ) { |
123 |
|
|
if ($offset == '' || $offset == "none") { |
124 |
|
|
$offset = 0; |
125 |
|
|
} |
126 |
|
|
switch(get_class($this->_db)) { |
127 |
|
|
case "db_mysql": |
128 |
|
|
$clause = " LIMIT $offset, $limit "; |
129 |
|
|
break; |
130 |
|
|
case "db_pgsql": |
131 |
|
|
$clause = " LIMIT $limit, $offset "; |
132 |
|
|
break; |
133 |
|
|
default: |
134 |
|
|
$clause = " LIMIT $offset, $limit "; |
135 |
|
|
break; |
136 |
|
|
} |
137 |
|
|
return $clause; |
138 |
|
|
} else { |
139 |
|
|
return NULL; |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
/** |
144 |
|
|
* find the number of rows to be returned |
145 |
|
|
* from a query from a table and where clause |
146 |
|
|
* |
147 |
|
|
* @param string $table - the table to count from |
148 |
|
|
* @param string $where_clause - a where clause |
149 |
|
|
* |
150 |
|
|
* @return int the # of rows |
151 |
|
|
*/ |
152 |
|
|
function count($tables, $where_clause='', $count_clause='*') { |
153 |
|
|
$query = "select count(".$count_clause.") as COUNT from ".$tables." ".$where_clause; |
154 |
|
|
$result = $this->_db->query($query); |
155 |
jonen |
1.3 |
if (DB::isError($result)) { |
156 |
jonen |
1.1 |
$msg = $result->getMessage(); |
157 |
|
|
user_error("PEARSQLDataListSource::count() - query failed : ".$msg); |
158 |
jonen |
1.3 |
return 0; |
159 |
|
|
} else { |
160 |
|
|
$value = $result->fetchRow(DB_FETCHMODE_ASSOC); |
161 |
|
|
return ($value ? (int)$value["COUNT"] : NULL); |
162 |
|
|
} |
163 |
jonen |
1.1 |
} |
164 |
|
|
|
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
?> |