3 V1.71 18 Jan 2001 (c) 2000, 2001 John Lim (jlim@natsoft.com.my). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence. See License.txt.
7 Set tabs to 4 for best viewing.
9 Less commonly used libraries here to reduce size of adodb.inc.php.
10 So we put less commonly used functions here!
13 function _adodb_totalpages(&$rs)
15 if ($rs->rowsPerPage) {
16 $rows = ($rs->RecordCount()+$rs->rowsPerPage-1) / $rs->rowsPerPage;
17 if ($rows < 0) return -1;
22 // Requires $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC
23 function _adodb_getmenu(&$zthis, $name,$defstr='',$blank1stItem=true,$multiple=false,
24 $size=0, $selectAttr='',$compareFields0=true)
28 if ($multiple or is_array($defstr)) {
29 if ($size==0) $size=5;
30 $attr = " multiple size=$size";
31 if (!strpos($name,'[]')) $name .= '[]';
32 } else if ($size) $attr = " size=$size";
36 $s = "<select name=\"$name\"$attr $selectAttr>";
37 if ($blank1stItem) $s .= "\n<option></option>";
39 if ($zthis->FieldCount() > 1) $hasvalue=true;
40 else $compareFields0 = true;
43 $zval = trim($zthis->fields[0]);
44 $selected = trim($zthis->fields[$compareFields0 ? 0 : 1]);
46 if ($blank1stItem && $zval=="") {
51 $value = 'value="'.htmlspecialchars(trim($zthis->fields[1])).'"';
54 if (is_array($defstr)) {
56 if (in_array($selected,$defstr))
57 $s .= "<option selected $value>".htmlspecialchars($zval).'</option>';
59 $s .= "\n<option ".$value.'>'.htmlspecialchars($zval).'</option>';
62 if (strcasecmp($selected,$defstr)==0)
63 $s .= "<option selected $value>".htmlspecialchars($zval).'</option>';
65 $s .= "\n<option ".$value.'>'.htmlspecialchars($zval).'</option>';
70 return $s ."\n</select>\n";
73 function &_adodb_pageexecute(&$zthis, $sql, $nrows, $page, $inputarr=false, $arg3=false, $secs2cache=0)
79 if (!isset($page) || $page <= 1) { // If page number <= 1, then we are at the first page
83 if ($nrows <= 0) $nrows = 10; // If an invalid nrows is supplied, we assume a default value of 10 rows per page
85 // ***** Here we check whether $page is the last page or whether we are trying to retrieve a page number greater than
86 // the last page number.
87 $pagecounter = $page + 1;
88 $pagecounteroffset = ($pagecounter * $nrows) - $nrows;
89 if ($secs2cache>0) $rstest = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $pagecounteroffset, $inputarr, $arg3);
90 else $rstest = &$zthis->SelectLimit($sql, $nrows, $pagecounteroffset, $inputarr, $arg3, $secs2cache);
92 while ($rstest && $rstest->EOF && $pagecounter>0) {
95 $pagecounteroffset = $nrows * ($pagecounter - 1);
97 if ($secs2cache>0) $rstest = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $pagecounteroffset, $inputarr, $arg3);
98 else $rstest = &$zthis->SelectLimit($sql, $nrows, $pagecounteroffset, $inputarr, $arg3, $secs2cache);
100 if ($rstest) $rstest->Close();
102 if ($atlastpage) { // If we are at the last page or beyond it, we are going to retrieve it
103 $page = $pagecounter;
104 if ($page == 1) $atfirstpage = true; // We have to do this again in case the last page is the same as the first
105 //... page, that is, the recordset has only 1 page.
108 // We get the data we want
109 $offset = $nrows * ($page-1);
110 if ($secs2cache > 0) $rsreturn = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $offset, $inputarr, $arg3);
111 else $rsreturn = &$zthis->SelectLimit($sql, $nrows, $offset, $inputarr, $arg3, $secs2cache);
113 // Before returning the RecordSet, we set the pagination properties we need
115 $rsreturn->rowsPerPage = $nrows;
116 $rsreturn->AbsolutePage($page);
117 $rsreturn->AtFirstPage($atfirstpage);
118 $rsreturn->AtLastPage($atlastpage);
123 function _adodb_getupdatesql(&$zthis,&$rs, $arrFields,$forceUpdate=false,$magicq=false)
126 printf(ADODB_BAD_RS,'GetUpdateSQL');
130 $fieldUpdatedCount = 0;
132 // Get the table name from the existing query.
133 eregi("FROM ".ADODB_TABLE_REGEX, $rs->sql, $tableName);
135 // Get the full where clause excluding the word "WHERE" from
136 // the existing query.
137 eregi("WHERE ([]0-9a-z=' \(\)\[\t\r\n_-]*)", $rs->sql, $whereClause);
139 // updateSQL will contain the full update query when all
140 // processing has completed.
141 $updateSQL = "UPDATE " . $tableName[1] . " SET ";
143 // Loop through all of the fields in the recordset
144 for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) {
146 // Get the field from the recordset
147 $field = $rs->FetchField($i);
149 // If the recordset field is one
150 // of the fields passed in then process.
151 if (isset($arrFields[$field->name])) {
153 // If the existing field value in the recordset
154 // is different from the value passed in then
155 // go ahead and append the field name and new value to
158 if ($forceUpdate || strcmp($rs->fields[$i], $arrFields[$field->name])) {
159 // Set the counter for the number of fields that will be updated.
160 $fieldUpdatedCount++;
162 // Based on the datatype of the field
163 // Format the value properly for the database
164 $mt = $rs->MetaType($field->type);
166 // "mike" <mike@partner2partner.com> patch and "Ryan Bailey" <rebel@windriders.com>
167 //PostgreSQL uses a 't' or 'f' and therefore needs to be processed as a string ('C') type field.
168 if ((substr($zthis->databaseType,0,8) == "postgres") && ($mt == "L")) $mt = "C";
173 $updateSQL .= $field->name . " = " . $zthis->qstr($arrFields[$field->name],$magicq) . ", ";
176 $updateSQL .= $field->name . " = " . $zthis->DBDate($arrFields[$field->name]) . ", ";
179 $updateSQL .= $field->name . " = " . $zthis->DBTimeStamp($arrFields[$field->name]) . ", ";
182 $updateSQL .= $field->name . " = " . $arrFields[$field->name] . ", ";
189 // If there were any modified fields then build the rest of the update query.
190 if ($fieldUpdatedCount > 0 || $forceUpdate) {
191 // Strip off the comma and space on the end of the update query.
192 $updateSQL = substr($updateSQL, 0, -2);
194 // If the recordset has a where clause then use that same where clause
196 if ($whereClause[1]) $updateSQL .= " WHERE " . $whereClause[1];
204 function _adodb_getinsertsql(&$zthis,&$rs,$arrFields,$magicq=false)
207 printf(ADODB_BAD_RS,'GetInsertSQL');
210 // Get the table name from the existing query.
211 eregi("FROM ".ADODB_TABLE_REGEX, $rs->sql, $tableName);
213 // Loop through all of the fields in the recordset
214 for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) {
216 // Get the field from the recordset
217 $field = $rs->FetchField($i);
218 // If the recordset field is one
219 // of the fields passed in then process.
220 if (isset($arrFields[$field->name])) {
222 // Set the counter for the number of fields that will be inserted.
223 $fieldInsertedCount++;
225 // Get the name of the fields to insert
226 $fields .= $field->name . ", ";
228 // "mike" <mike@partner2partner.com> patch and "Ryan Bailey" <rebel@windriders.com>
229 //PostgreSQL uses a 't' or 'f' and therefore needs to be processed as a string ('C') type field.
230 if ((substr($zthis->databaseType,0,8) == "postgres") && ($mt == "L")) $mt = "C";
232 // Based on the datatype of the field
233 // Format the value properly for the database
234 switch($rs->MetaType($field->type)) {
237 $values .= $zthis->qstr($arrFields[$field->name],$magicq) . ", ";
240 $values .= $zthis->DBDate($arrFields[$field->name]) . ", ";
243 $values .= $zthis->DBTimeStamp($arrFields[$field->name]) . ", ";
246 $values .= $arrFields[$field->name] . ", ";
252 // If there were any inserted fields then build the rest of the insert query.
253 if ($fieldInsertedCount > 0) {
255 // Strip off the comma and space on the end of both the fields
257 $fields = substr($fields, 0, -2);
258 $values = substr($values, 0, -2);
260 // Append the fields and their values to the insert query.
261 $insertSQL = "INSERT INTO " . $tableName[1] . " ( $fields ) VALUES ( $values )";