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 |
|
|
* @param object - $db the babw_db object. |
76 |
|
|
* |
77 |
|
|
*/ |
78 |
|
|
function set_db_object( &$db ) { |
79 |
|
|
$this->_db = &$db; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
function do_query() { |
83 |
|
|
$this->_result = $this->_db->query($this->_query); |
84 |
|
|
if (DB::isError($this->_result)) { |
85 |
|
|
$msg = $this->_result->getMessage(); |
86 |
|
|
user_error("PEARSQLDataListSource::do_query() - query failed : ".$msg); |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
/** |
93 |
|
|
* This function gets the next data row |
94 |
|
|
* from the query() |
95 |
|
|
* |
96 |
|
|
* @return array() |
97 |
|
|
*/ |
98 |
|
|
function get_next_data_row() { |
99 |
|
|
return $this->_result->fetchRow(DB_FETCHMODE_ASSOC); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
/** |
103 |
|
|
* This function builds the limit |
104 |
|
|
* clause portion of a DB query. |
105 |
|
|
* |
106 |
|
|
* @return string - the limit portion of |
107 |
|
|
* the query. |
108 |
|
|
*/ |
109 |
|
|
function build_limit_clause($offset, $limit) { |
110 |
|
|
if ($this->get_limit() != -1 ) { |
111 |
|
|
if ($offset == '' || $offset == "none") { |
112 |
|
|
$offset = 0; |
113 |
|
|
} |
114 |
|
|
switch(get_class($this->_db)) { |
115 |
|
|
case "db_mysql": |
116 |
|
|
$clause = " LIMIT $offset, $limit "; |
117 |
|
|
break; |
118 |
|
|
case "db_pgsql": |
119 |
|
|
$clause = " LIMIT $limit, $offset "; |
120 |
|
|
break; |
121 |
|
|
default: |
122 |
|
|
$clause = " LIMIT $offset, $limit "; |
123 |
|
|
break; |
124 |
|
|
} |
125 |
|
|
return $clause; |
126 |
|
|
} else { |
127 |
|
|
return NULL; |
128 |
|
|
} |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
/** |
132 |
|
|
* find the number of rows to be returned |
133 |
|
|
* from a query from a table and where clause |
134 |
|
|
* |
135 |
|
|
* @param string $table - the table to count from |
136 |
|
|
* @param string $where_clause - a where clause |
137 |
|
|
* |
138 |
|
|
* @return int the # of rows |
139 |
|
|
*/ |
140 |
|
|
function count($tables, $where_clause='', $count_clause='*') { |
141 |
|
|
$query = "select count(".$count_clause.") as COUNT from ".$tables." ".$where_clause; |
142 |
|
|
$result = $this->_db->query($query); |
143 |
|
|
if (DB::isError($this->_result)) { |
144 |
|
|
$msg = $result->getMessage(); |
145 |
|
|
user_error("PEARSQLDataListSource::count() - query failed : ".$msg); |
146 |
|
|
} |
147 |
|
|
$value = $result->fetchRow(DB_FETCHMODE_ASSOC); |
148 |
|
|
return ($value ? (int)$value["COUNT"] : NULL); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
?> |