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 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
38 |
* @package phpHtmlLib |
39 |
*/ |
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 |
* @param object - $db the babw_db object. |
76 |
*/ |
77 |
function set_db_object( &$db ) { |
78 |
$this->_db = &$db; |
79 |
} |
80 |
|
81 |
/** |
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 |
function do_query() { |
92 |
$this->_result = $this->_db->query($this->_query); |
93 |
if (DB::isError($this->_result)) { |
94 |
$msg = $this->_result->getMessage(); |
95 |
$query = $this->_query; |
96 |
user_error("PEARSQLDataListSource::do_query($query) - query failed : ".$msg); |
97 |
return false; |
98 |
} else { |
99 |
return true; |
100 |
} |
101 |
} |
102 |
|
103 |
|
104 |
|
105 |
/** |
106 |
* This function gets the next data row |
107 |
* from the query() |
108 |
* |
109 |
* @return array() |
110 |
*/ |
111 |
function get_next_data_row() { |
112 |
return $this->_result->fetchRow(DB_FETCHMODE_ASSOC); |
113 |
} |
114 |
|
115 |
/** |
116 |
* This function builds the limit |
117 |
* clause portion of a DB query. |
118 |
* |
119 |
* @return string - the limit portion of |
120 |
* the query. |
121 |
*/ |
122 |
function build_limit_clause($offset, $limit) { |
123 |
if ($this->get_limit() != -1 ) { |
124 |
if ($offset == '' || $offset == "none") { |
125 |
$offset = 0; |
126 |
} |
127 |
switch(get_class($this->_db)) { |
128 |
case "db_mysql": |
129 |
$clause = " LIMIT $offset, $limit "; |
130 |
break; |
131 |
case "db_pgsql": |
132 |
$clause = " LIMIT $limit OFFSET $offset "; |
133 |
break; |
134 |
default: |
135 |
$clause = " LIMIT $offset, $limit "; |
136 |
break; |
137 |
} |
138 |
return $clause; |
139 |
} else { |
140 |
return NULL; |
141 |
} |
142 |
} |
143 |
|
144 |
/** |
145 |
* find the number of rows to be returned |
146 |
* from a query from a table and where clause |
147 |
* |
148 |
* @param string $table - the table to count from |
149 |
* @param string $where_clause - a where clause |
150 |
* |
151 |
* @return int the # of rows |
152 |
*/ |
153 |
function count($tables, $where_clause='', $count_clause='*') { |
154 |
$query = "select count(".$count_clause.") as COUNT from ".$tables." ".$where_clause; |
155 |
$result = $this->_db->getOne($query); |
156 |
if (DB::isError($result)) { |
157 |
$msg = $result->getMessage(); |
158 |
user_error("PEARSQLDataListSource::count() - query failed : ".$msg); |
159 |
return 0; |
160 |
} else { |
161 |
return $result; |
162 |
} |
163 |
} |
164 |
|
165 |
} |
166 |
|
167 |
?> |