]> projects.mako.cc - scuttle/blob - includes/db/db2.php
* Updated isValidEmail regex to match plussed addresses
[scuttle] / includes / db / db2.php
1 <?php
2 /** 
3 *
4 * @package dbal_db2
5 * @version $Id: db2.php,v 1.2 2005/06/10 08:52:03 devalley Exp $
6 * @copyright (c) 2005 phpBB Group 
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License 
8 *
9 */
10
11 /**
12 * @ignore
13 */
14 if(!defined("SQL_LAYER"))
15 {
16
17 define("SQL_LAYER","db2");
18
19 /**
20 * @package dbal_db2
21 * DB2 Database Abstraction Layer
22 */
23 class sql_db
24 {
25
26         var $db_connect_id;
27         var $query_result;
28         var $query_resultset;
29         var $query_numrows;
30         var $next_id;
31         var $row = array();
32         var $rowset = array();
33         var $row_index;
34         var $num_queries = 0;
35
36         //
37         // Constructor
38         //
39         function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
40         {
41                 $this->persistency = $persistency;
42                 $this->user = $sqluser;
43                 $this->password = $sqlpassword;
44                 $this->dbname = $database;
45
46                 $this->server = $sqlserver;
47
48                 if($this->persistency)
49                 {
50                         $this->db_connect_id = odbc_pconnect($this->server, "", "");
51                 }
52                 else
53                 {
54                         $this->db_connect_id = odbc_connect($this->server, "", "");
55                 }
56
57                 if($this->db_connect_id)
58                 {
59                         @odbc_autocommit($this->db_connect_id, off);
60
61                         return $this->db_connect_id;
62                 }
63                 else
64                 {
65                         return false;
66                 }
67         }
68         //
69         // Other base methods
70         //
71         function sql_close()
72         {
73                 if($this->db_connect_id)
74                 {
75                         if($this->query_result)
76                         {
77                                 @odbc_free_result($this->query_result);
78                         }
79                         $result = @odbc_close($this->db_connect_id);
80                         return $result;
81                 }
82                 else
83                 {
84                         return false;
85                 }
86         }
87
88
89         //
90         // Query method
91         //
92         function sql_query($query = "", $transaction = FALSE)
93         {
94                 //
95                 // Remove any pre-existing queries
96                 //
97                 unset($this->query_result);
98                 unset($this->row);
99                 if($query != "")
100                 {
101                         $this->num_queries++;
102
103                         if(!eregi("^INSERT ",$query))
104                         {
105                                 if(eregi("LIMIT", $query))
106                                 {
107                                         preg_match("/^(.*)LIMIT ([0-9]+)[, ]*([0-9]+)*/s", $query, $limits);
108
109                                         $query = $limits[1];
110                                         if($limits[3])
111                                         {
112                                                 $row_offset = $limits[2];
113                                                 $num_rows = $limits[3];
114                                         }
115                                         else
116                                         {
117                                                 $row_offset = 0;
118                                                 $num_rows = $limits[2];
119                                         }
120
121                                         $query .= " FETCH FIRST ".($row_offset+$num_rows)." ROWS ONLY OPTIMIZE FOR ".($row_offset+$num_rows)." ROWS";
122
123                                         $this->query_result = odbc_exec($this->db_connect_id, $query);
124
125                                         $query_limit_offset = $row_offset;
126                                         $this->result_numrows[$this->query_result] = $num_rows;
127                                 }
128                                 else
129                                 {
130                                         $this->query_result = odbc_exec($this->db_connect_id, $query);
131
132                                         $row_offset = 0;
133                                         $this->result_numrows[$this->query_result] = 5E6;
134                                 }
135
136                                 $result_id = $this->query_result;
137                                 if($this->query_result && eregi("^SELECT", $query))
138                                 {
139
140                                         for($i = 1; $i < odbc_num_fields($result_id)+1; $i++)
141                                         {
142                                                 $this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);
143                                         }
144
145                                         $i =  $row_offset + 1;
146                                         $k = 0;
147                                         while(odbc_fetch_row($result_id, $i) && $k < $this->result_numrows[$result_id])
148                                         {
149
150                                                 for($j = 1; $j < count($this->result_field_names[$result_id])+1; $j++)
151                                                 {
152                                                         $this->result_rowset[$result_id][$k][$this->result_field_names[$result_id][$j-1]] = odbc_result($result_id, $j);
153                                                 }
154                                                 $i++;
155                                                 $k++;
156                                         }
157
158                                         $this->result_numrows[$result_id] = $k;
159                                         $this->row_index[$result_id] = 0;
160                                 }
161                                 else
162                                 {
163                                         $this->result_numrows[$result_id] = @odbc_num_rows($result_id);
164                                         $this->row_index[$result_id] = 0;
165                                 }
166                         }
167                         else
168                         {
169                                 if(eregi("^(INSERT|UPDATE) ", $query))
170                                 {
171                                         $query = preg_replace("/\\\'/s", "''", $query);
172                                 }
173
174                                 $this->query_result = odbc_exec($this->db_connect_id, $query);
175
176                                 if($this->query_result)
177                                 {
178                                         $sql_id = "VALUES(IDENTITY_VAL_LOCAL())";
179
180                                         $id_result = odbc_exec($this->db_connect_id, $sql_id);
181                                         if($id_result)
182                                         {
183                                                 $row_result = odbc_fetch_row($id_result);
184                                                 if($row_result)
185                                                 {
186                                                         $this->next_id[$this->query_result] = odbc_result($id_result, 1);
187                                                 }
188                                         }
189                                 }
190
191                                 odbc_commit($this->db_connect_id);
192
193                                 $this->query_limit_offset[$this->query_result] = 0;
194                                 $this->result_numrows[$this->query_result] = 0;
195                         }
196
197                         return $this->query_result;
198                 }
199                 else
200                 {
201                         return false;
202                 }
203         }
204
205         //
206         // Other query methods
207         //
208         function sql_numrows($query_id = 0)
209         {
210                 if(!$query_id)
211                 {
212                         $query_id = $this->query_result;
213                 }
214                 if($query_id)
215                 {
216                         return $this->result_numrows[$query_id];
217                 }
218                 else
219                 {
220                         return false;
221                 }
222         }
223         function sql_affectedrows($query_id = 0)
224         {
225                 if(!$query_id)
226                 {
227                         $query_id = $this->query_result;
228                 }
229                 if($query_id)
230                 {
231                         return $this->result_numrows[$query_id];
232                 }
233                 else
234                 {
235                         return false;
236                 }
237         }
238         function sql_numfields($query_id = 0)
239         {
240                 if(!$query_id)
241                 {
242                         $query_id = $this->query_result;
243                 }
244                 if($query_id)
245                 {
246                         $result = count($this->result_field_names[$query_id]);
247                         return $result;
248                 }
249                 else
250                 {
251                         return false;
252                 }
253         }
254         function sql_fieldname($offset, $query_id = 0)
255         {
256                 if(!$query_id)
257                 {
258                         $query_id = $this->query_result;
259                 }
260                 if($query_id)
261                 {
262                         $result = $this->result_field_names[$query_id][$offset];
263                         return $result;
264                 }
265                 else
266                 {
267                         return false;
268                 }
269         }
270         function sql_fieldtype($offset, $query_id = 0)
271         {
272                 if(!$query_id)
273                 {
274                         $query_id = $this->query_result;
275                 }
276                 if($query_id)
277                 {
278                         $result = @odbc_field_type($query_id, $offset);
279                         return $result;
280                 }
281                 else
282                 {
283                         return false;
284                 }
285         }
286         function sql_fetchrow($query_id = 0)
287         {
288                 if(!$query_id)
289                 {
290                         $query_id = $this->query_result;
291                 }
292                 if($query_id)
293                 {
294                         if($this->row_index[$query_id] < $this->result_numrows[$query_id])
295                         {
296                                 $result = $this->result_rowset[$query_id][$this->row_index[$query_id]];
297                                 $this->row_index[$query_id]++;
298                                 return $result;
299                         }
300                         else
301                         {
302                                 return false;
303                         }
304                 }
305                 else
306                 {
307                         return false;
308                 }
309         }
310         function sql_fetchrowset($query_id = 0)
311         {
312                 if(!$query_id)
313                 {
314                         $query_id = $this->query_result;
315                 }
316                 if($query_id)
317                 {
318                         $this->row_index[$query_id] = $this->result_numrows[$query_id];
319                         return $this->result_rowset[$query_id];
320                 }
321                 else
322                 {
323                         return false;
324                 }
325         }
326         function sql_fetchfield($field, $row = -1, $query_id = 0)
327         {
328                 if(!$query_id)
329                 {
330                         $query_id = $this->query_result;
331                 }
332                 if($query_id)
333                 {
334                         if($row < $this->result_numrows[$query_id])
335                         {
336                                 if($row == -1)
337                                 {
338                                         $getrow = $this->row_index[$query_id]-1;
339                                 }
340                                 else
341                                 {
342                                         $getrow = $row;
343                                 }
344
345                                 return $this->result_rowset[$query_id][$getrow][$this->result_field_names[$query_id][$field]];
346
347                         }
348                         else
349                         {
350                                 return false;
351                         }
352                 }
353                 else
354                 {
355                         return false;
356                 }
357         }
358         function sql_rowseek($offset, $query_id = 0)
359         {
360                 if(!$query_id)
361                 {
362                         $query_id = $this->query_result;
363                 }
364                 if($query_id)
365                 {
366                         $this->row_index[$query_id] = 0;
367                         return true;
368                 }
369                 else
370                 {
371                         return false;
372                 }
373         }
374         function sql_nextid($query_id = 0)
375         {
376                 if(!$query_id)
377                 {
378                         $query_id = $this->query_result;
379                 }
380                 if($query_id)
381                 {
382                         return $this->next_id[$query_id];
383                 }
384                 else
385                 {
386                         return false;
387                 }
388         }
389         function sql_freeresult($query_id = 0)
390         {
391                 if(!$query_id)
392                 {
393                         $query_id = $this->query_result;
394                 }
395                 if($query_id)
396                 {
397                         $result = @odbc_free_result($query_id);
398                         return $result;
399                 }
400                 else
401                 {
402                         return false;
403                 }
404         }
405         function sql_error($query_id = 0)
406         {
407 //              $result['code'] = @odbc_error($this->db_connect_id);
408 //              $result['message'] = @odbc_errormsg($this->db_connect_id);
409
410                 return "";
411         }
412
413 } // class sql_db
414
415 } // if ... define
416
417 ?>

Benjamin Mako Hill || Want to submit a patch?