]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/adodb-lib.inc.php
First cut of ADODB backend. I've only tested this on MySql. I still have some work...
[SourceForge/phpwiki.git] / lib / WikiDB / adodb / adodb-lib.inc.php
1 <?php
2 /* 
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.
8   
9   Less commonly used libraries here to reduce size of adodb.inc.php. 
10   So we put less commonly used functions here!
11 */ 
12
13 function _adodb_totalpages(&$rs)
14 {
15         if  ($rs->rowsPerPage) {
16                 $rows = ($rs->RecordCount()+$rs->rowsPerPage-1) / $rs->rowsPerPage;
17                 if ($rows < 0) return -1;
18                 else return $rows;
19         } else return -1;
20 }
21
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)
25 {
26         $hasvalue = false;
27
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";
33         else $attr ='';
34         
35                         
36         $s = "<select name=\"$name\"$attr $selectAttr>";
37         if ($blank1stItem) $s .= "\n<option></option>";
38
39         if ($zthis->FieldCount() > 1) $hasvalue=true;
40         else $compareFields0 = true;
41         
42         while(!$zthis->EOF) {
43                 $zval = trim($zthis->fields[0]);
44                 $selected = trim($zthis->fields[$compareFields0 ? 0 : 1]);
45                 
46                 if ($blank1stItem && $zval=="") {
47                         $zthis->MoveNext();
48                         continue;
49                 }
50                 if ($hasvalue) 
51                         $value = 'value="'.htmlspecialchars(trim($zthis->fields[1])).'"';
52                 
53                 
54                 if (is_array($defstr))  {
55                         
56                         if (in_array($selected,$defstr)) 
57                                 $s .= "<option selected $value>".htmlspecialchars($zval).'</option>';
58                         else 
59                                 $s .= "\n<option ".$value.'>'.htmlspecialchars($zval).'</option>';
60                 }
61                 else {
62                         if (strcasecmp($selected,$defstr)==0) 
63                                 $s .= "<option selected $value>".htmlspecialchars($zval).'</option>';
64                         else 
65                                 $s .= "\n<option ".$value.'>'.htmlspecialchars($zval).'</option>';
66                 }
67                 $zthis->MoveNext();
68         } // while
69         
70         return $s ."\n</select>\n";
71 }
72
73 function &_adodb_pageexecute(&$zthis, $sql, $nrows, $page, $inputarr=false, $arg3=false, $secs2cache=0) 
74 {
75
76         $atfirstpage = false;
77         $atlastpage = false;
78         
79         if (!isset($page) || $page <= 1) {      // If page number <= 1, then we are at the first page
80                 $page = 1;
81                 $atfirstpage = true;
82         }
83         if ($nrows <= 0) $nrows = 10;   // If an invalid nrows is supplied, we assume a default value of 10 rows per page
84         
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);
91         if ($rstest) {
92                 while ($rstest && $rstest->EOF && $pagecounter>0) {
93                         $atlastpage = true;
94                         $pagecounter--;
95                         $pagecounteroffset = $nrows * ($pagecounter - 1);
96                         $rstest->Close();
97                         if ($secs2cache>0) $rstest = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $pagecounteroffset, $inputarr, $arg3);
98                         else $rstest = &$zthis->SelectLimit($sql, $nrows, $pagecounteroffset, $inputarr, $arg3, $secs2cache);
99                 }
100                 if ($rstest) $rstest->Close();
101         }
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.
106         }
107         
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);
112         
113         // Before returning the RecordSet, we set the pagination properties we need
114         if ($rsreturn) {
115                 $rsreturn->rowsPerPage = $nrows;
116                 $rsreturn->AbsolutePage($page);
117                 $rsreturn->AtFirstPage($atfirstpage);
118                 $rsreturn->AtLastPage($atlastpage);
119         }
120         return $rsreturn;
121 }
122
123 function _adodb_getupdatesql(&$zthis,&$rs, $arrFields,$forceUpdate=false,$magicq=false)
124 {
125                 if (!$rs) {
126                         printf(ADODB_BAD_RS,'GetUpdateSQL');
127                         return false;
128                 }
129         
130                 $fieldUpdatedCount = 0;
131                 
132                 // Get the table name from the existing query.
133                 eregi("FROM ".ADODB_TABLE_REGEX, $rs->sql, $tableName);
134
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);
138
139                 // updateSQL will contain the full update query when all
140                 // processing has completed.
141                 $updateSQL = "UPDATE " . $tableName[1] . " SET ";
142                 
143                 // Loop through all of the fields in the recordset
144                 for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) {
145                 
146                         // Get the field from the recordset
147                         $field = $rs->FetchField($i);
148
149                         // If the recordset field is one
150                         // of the fields passed in then process.
151                         if (isset($arrFields[$field->name])) {
152
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
156                                 // the update query.
157
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++;
161
162                                         // Based on the datatype of the field
163                                         // Format the value properly for the database
164                                         $mt = $rs->MetaType($field->type);
165                                         
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";
169
170                                         switch($mt) {
171                                                 case "C":
172                                                 case "X":
173                                                         $updateSQL .= $field->name . " = " . $zthis->qstr($arrFields[$field->name],$magicq) . ", ";
174                                                         break;
175                                                 case "D":
176                                                         $updateSQL .= $field->name . " = " . $zthis->DBDate($arrFields[$field->name]) . ", ";
177                                                 break;
178                                                 case "T":
179                                                         $updateSQL .= $field->name . " = " . $zthis->DBTimeStamp($arrFields[$field->name]) . ", ";
180                                                         break;
181                                                 default:
182                                                         $updateSQL .= $field->name . " = " . $arrFields[$field->name] . ", ";
183                                                         break;
184                                         };
185                                 };
186                 };
187                 };
188
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);
193
194                         // If the recordset has a where clause then use that same where clause
195                         // for the update.
196                         if ($whereClause[1]) $updateSQL .= " WHERE " . $whereClause[1];
197
198                         return $updateSQL;
199                 } else {
200                         return false;
201                 };
202 }
203
204 function _adodb_getinsertsql(&$zthis,&$rs,$arrFields,$magicq=false)
205 {
206         if (!$rs) {
207                         printf(ADODB_BAD_RS,'GetInsertSQL');
208                         return false;
209                 }
210                 // Get the table name from the existing query.
211                 eregi("FROM ".ADODB_TABLE_REGEX, $rs->sql, $tableName);
212
213                 // Loop through all of the fields in the recordset
214                 for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) {
215
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])) {
221         
222                                 // Set the counter for the number of fields that will be inserted.
223                                 $fieldInsertedCount++;
224
225                                 // Get the name of the fields to insert
226                                 $fields .= $field->name . ", ";
227                                 
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";
231
232                                 // Based on the datatype of the field
233                                 // Format the value properly for the database
234                                 switch($rs->MetaType($field->type)) {
235                                         case "C":
236                                         case "X":
237                                                 $values .= $zthis->qstr($arrFields[$field->name],$magicq) . ", ";
238                                                 break;
239                                         case "D":
240                                                 $values .= $zthis->DBDate($arrFields[$field->name]) . ", ";
241                                                 break;
242                                         case "T":
243                                                 $values .= $zthis->DBTimeStamp($arrFields[$field->name]) . ", ";
244                                                 break;
245                                         default:
246                                                 $values .= $arrFields[$field->name] . ", ";
247                                                 break;
248                                 };
249                 };
250         };
251
252                 // If there were any inserted fields then build the rest of the insert query.
253                 if ($fieldInsertedCount > 0) {
254
255                         // Strip off the comma and space on the end of both the fields
256                         // and their values.
257                         $fields = substr($fields, 0, -2);
258                         $values = substr($values, 0, -2);
259
260                         // Append the fields and their values to the insert query.
261                         $insertSQL = "INSERT INTO " . $tableName[1] . " ( $fields ) VALUES ( $values )";
262
263                         return $insertSQL;
264
265                 } else {
266                         return false;
267                 };
268 }
269 ?>