/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/widgets/data_list/PEARSQLDataListSource.inc
ViewVC logotype

Contents of /nfo/php/libs/com.newsblob.phphtmllib/widgets/data_list/PEARSQLDataListSource.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Thu Jan 30 03:29:46 2003 UTC (21 years, 7 months ago) by jonen
Branch: MAIN
Branch point for: no_vendor_tag
Initial revision

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 */
38 class PEARSQLDataListSource extends SQLDataListSource {
39
40 /**
41 * This var holds the Database object
42 * that is used to do the sql queries
43 * It is assumed that the db is already
44 * connected to, and the object provides
45 * 2 methods:
46 * query() - execute a sql query
47 */
48 var $_db = NULL;
49
50 /**
51 * this holds the query result from the
52 * PEAR::DB->query() call
53 *
54 */
55 var $_result = NULL;
56
57
58 /**
59 * The constructor is used to pass in the
60 * PEAR DB object that has already been
61 * created and connected to the db.
62 *
63 * @param PEAR::DB object - MUST BE CONNECTED
64 */
65 function PEARSQLDataListSource( &$db ) {
66 $this->set_db_object( $db );
67 }
68
69 /**
70 * Set the DB object we will use
71 * to talk to the DB.
72 *
73 * @param object - $db the babw_db object.
74 *
75 */
76 function set_db_object( &$db ) {
77 $this->_db = &$db;
78 }
79
80 function do_query() {
81 $this->_result = $this->_db->query($this->_query);
82 if (DB::isError($this->_result)) {
83 $msg = $this->_result->getMessage();
84 user_error("PEARSQLDataListSource::do_query() - query failed : ".$msg);
85 }
86 }
87
88
89
90 /**
91 * This function gets the next data row
92 * from the query()
93 *
94 * @return array()
95 */
96 function get_next_data_row() {
97 return $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
98 }
99
100 /**
101 * This function builds the limit
102 * clause portion of a DB query.
103 *
104 * @return string - the limit portion of
105 * the query.
106 */
107 function build_limit_clause($offset, $limit) {
108 if ($this->get_limit() != -1 ) {
109 if ($offset == '' || $offset == "none") {
110 $offset = 0;
111 }
112 switch(get_class($this->_db)) {
113 case "db_mysql":
114 $clause = " LIMIT $offset, $limit ";
115 break;
116 case "db_pgsql":
117 $clause = " LIMIT $limit, $offset ";
118 break;
119 default:
120 $clause = " LIMIT $offset, $limit ";
121 break;
122 }
123 return $clause;
124 } else {
125 return NULL;
126 }
127 }
128
129 /**
130 * find the number of rows to be returned
131 * from a query from a table and where clause
132 *
133 * @param string $table - the table to count from
134 * @param string $where_clause - a where clause
135 *
136 * @return int the # of rows
137 */
138 function count($tables, $where_clause='', $count_clause='*') {
139 $query = "select count(".$count_clause.") as COUNT from ".$tables." ".$where_clause;
140 $result = $this->_db->query($query);
141 if (DB::isError($this->_result)) {
142 $msg = $result->getMessage();
143 user_error("PEARSQLDataListSource::count() - query failed : ".$msg);
144 }
145 $value = $result->fetchRow(DB_FETCHMODE_ASSOC);
146 return ($value ? (int)$value["COUNT"] : NULL);
147 }
148
149 }
150
151 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed