1 |
jonen |
1.1 |
<?php |
2 |
|
|
|
3 |
|
|
/** |
4 |
|
|
* This file contains the SQLDataListSource child class |
5 |
|
|
* that uses the ADODB DB object to talk to the DB. |
6 |
|
|
* |
7 |
|
|
* you can check out ADODB from |
8 |
|
|
* http://php.weblogs.com/ADODB |
9 |
|
|
* |
10 |
|
|
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
11 |
|
|
* @package phpHtmlLib |
12 |
|
|
*/ |
13 |
|
|
|
14 |
|
|
/** |
15 |
|
|
* This requires the SQLDataListSource |
16 |
|
|
* parent class |
17 |
|
|
* |
18 |
|
|
*/ |
19 |
|
|
include_once($phphtmllib."/widgets/data_list/SQLDataListSource.inc"); |
20 |
|
|
|
21 |
|
|
/** |
22 |
|
|
* NOTE: |
23 |
|
|
* You'll need to include the base |
24 |
|
|
* ADODB include file prior to using this |
25 |
|
|
* file. |
26 |
|
|
* include_once( 'adodb.inc.php' ); |
27 |
|
|
* |
28 |
|
|
*/ |
29 |
|
|
|
30 |
|
|
|
31 |
|
|
/** |
32 |
|
|
* This SQLDataListSource child class interacts with |
33 |
|
|
* with the specific DB via the php ADODB DB abstraction |
34 |
|
|
* objects. |
35 |
|
|
* |
36 |
|
|
* How to use? |
37 |
|
|
* in the DataList child's get_data_source() method |
38 |
|
|
* you pass in the already connected PEAR DB object |
39 |
|
|
* in to the constructor. PEARSQLDataListSource |
40 |
|
|
* takes care of the rest. |
41 |
|
|
* |
42 |
|
|
*/ |
43 |
|
|
class ADODBSQLDataListSource extends SQLDataListSource { |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* This var holds the Database object |
47 |
|
|
* that is used to do the sql queries |
48 |
|
|
* It is assumed that the db is already |
49 |
|
|
* connected to, and the object provides |
50 |
|
|
* 2 methods: |
51 |
|
|
* query() - execute a sql query |
52 |
|
|
*/ |
53 |
|
|
var $_db = NULL; |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* this holds the query result from the |
57 |
|
|
* PEAR::DB->query() call |
58 |
|
|
* |
59 |
|
|
*/ |
60 |
|
|
var $_result = NULL; |
61 |
|
|
|
62 |
|
|
|
63 |
|
|
/** |
64 |
|
|
* The constructor is used to pass in the |
65 |
|
|
* PEAR DB object that has already been |
66 |
|
|
* created and connected to the db. |
67 |
|
|
* |
68 |
|
|
* @param ADONewConnection::Execute object - MUST BE CONNECTED |
69 |
|
|
*/ |
70 |
|
|
function ADODBSQLDataListSource( &$db ) { |
71 |
|
|
$this->set_db_object( $db ); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
/** |
75 |
|
|
* Set the DB object we will use |
76 |
|
|
* to talk to the DB. |
77 |
|
|
* |
78 |
|
|
* @param object - $db the babw_db object. |
79 |
|
|
* |
80 |
|
|
*/ |
81 |
|
|
function set_db_object( &$db ) { |
82 |
|
|
$this->_db = &$db; |
83 |
|
|
} |
84 |
|
|
|
85 |
jonen |
1.2 |
/** |
86 |
|
|
* This is the function that does the data fetching, |
87 |
|
|
* and sorting if needed. |
88 |
|
|
* If the source is a sql database, this is where the |
89 |
|
|
* query gets called. This function doesn't actually read the |
90 |
|
|
* data from the DB yet. That is what get_next_data_row() |
91 |
|
|
* does. |
92 |
|
|
* |
93 |
|
|
* @return boolean - the query passed/failed. |
94 |
|
|
*/ |
95 |
jonen |
1.1 |
function do_query() { |
96 |
|
|
$this->_result = $this->_db->SelectLimit($this->_query, |
97 |
|
|
$this->get_limit(), |
98 |
|
|
$this->get_offset()); |
99 |
|
|
if (!$this->_result) { |
100 |
jonen |
1.2 |
$msg = $this->_db->ErrorMsg(); |
101 |
jonen |
1.1 |
user_error("ADODBSQLDataListSource::do_query() - query failed : ".$msg); |
102 |
jonen |
1.2 |
return false; |
103 |
|
|
} else { |
104 |
|
|
return true; |
105 |
|
|
} |
106 |
jonen |
1.1 |
} |
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
/** |
111 |
|
|
* This function gets the next data row |
112 |
|
|
* from the query() |
113 |
|
|
* |
114 |
|
|
* @return array() |
115 |
|
|
*/ |
116 |
|
|
function get_next_data_row() { |
117 |
|
|
return $this->_result->FetchRow(); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* This function builds the limit |
122 |
|
|
* clause portion of a DB query. |
123 |
|
|
* |
124 |
|
|
* @return string - the limit portion of |
125 |
|
|
* the query. |
126 |
|
|
*/ |
127 |
|
|
function build_limit_clause($offset, $limit) { |
128 |
|
|
return NULL; |
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->Execute($query); |
143 |
|
|
if (!$result) { |
144 |
jonen |
1.2 |
$msg = $this->_db->ErrorMsg(); |
145 |
jonen |
1.1 |
user_error("ADODBSQLDataListSource::count() - query failed : ".$msg); |
146 |
|
|
} |
147 |
|
|
$value = $result->FetchRow(); |
148 |
|
|
return ($value ? (int)$value["COUNT"] : NULL); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
?> |