Quick Search:

View

Revision:

Diff

Diff from 1 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/database.php

Annotated File View

hpdl
1
1 <?php
2 /*
3   $Id: database.php,v 1.8 2004/10/25 22:19:28 hpdl Exp $
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
8   Copyright (c) 2004 osCommerce
9
10   Released under the GNU General Public License
11 */
12
13   class osC_Database {
14     var $is_connected = false,
15         $link,
16         $error_number,
17         $error = false,
18         $error_reporting = true,
19         $server,
20         $username,
21         $password,
22         $debug = false,
23         $number_of_queries = 0,
24         $time_of_queries = 0;
25
26     function &connect($server, $username, $password, $type = '') {
27       if (empty($type)) {
28         $type = DB_DATABASE_CLASS;
29       }
30
31       require('database/' . $type . '.php');
32
33       $class = 'osC_Database_' . $type;
34
35       $database_object =& new $class($server, $username, $password);
36
37       return $database_object;
38     }
39
40     function setConnected($boolean) {
41       if ($boolean === true) {
42         $this->is_connected = true;
43       } else {
44         $this->is_connected = false;
45       }
46     }
47
48     function isConnected() {
49       if ($this->is_connected === true) {
50         return true;
51       } else {
52         return false;
53       }
54     }
55
56     function &query($query) {
57       $osC_Database_Result =& new osC_Database_Result($this);
58       $osC_Database_Result->setQuery($query);
59
60       return $osC_Database_Result;
61     }
62
63     function setError($error, $error_number = '') {
64       global $messageStack;
65
66       if ($this->error_reporting === true) {
67         $this->error = $error;
68         $this->error_number = $error_number;
69
70         if (isset($messageStack)) {
71           $messageStack->add('debug', $this->getError());
72         }
73       }
74     }
75
76     function isError() {
77       if ($this->error === false) {
78         return false;
79       } else {
80         return true;
81       }
82     }
83
84     function getError() {
85       if ($this->isError()) {
86         $error = '';
87
88         if (!empty($this->error_number)) {
89           $error .= $this->error_number . ': ';
90         }
91
92         $error .= $this->error;
93
94         return $error;
95       } else {
96         return false;
97       }
98     }
99
100     function setErrorReporting($boolean) {
101       if ($boolean === true) {
102         $this->error_reporting = true;
103       } else {
104         $this->error_reporting = false;
105       }
106     }
107
108     function setDebug($boolean) {
109       if ($boolean === true) {
110         $this->debug = true;
111       } else {
112         $this->debug = false;
113       }
114     }
115
116     function importSQL($sql_file, $database, $table_prefix = -1) {
117       if ($this->selectDatabase($database)) {
118         if (file_exists($sql_file)) {
119           $fd = fopen($sql_file, 'rb');
120           $import_queries = fread($fd, filesize($sql_file));
121           fclose($fd);
122         } else {
123           $this->setError(sprintf(ERROR_SQL_FILE_NONEXISTENT, $sql_file));
124
125           return false;
126         }
127
128         if (!get_cfg_var('safe_mode')) {
129           @set_time_limit(0);
130         }
131
132         $sql_queries = array();
133         $sql_length = strlen($import_queries);
134         $pos = strpos($import_queries, ';');
135
136         for ($i=$pos; $i<$sql_length; $i++) {
137 // remove comments
138           if ($import_queries[0] == '#') {
139             $import_queries = ltrim(substr($import_queries, strpos($import_queries, "\n")));
140             $sql_length = strlen($import_queries);
141             $i = strpos($import_queries, ';')-1;
142             continue;
143           }
144
145           if ($import_queries[($i+1)] == "\n") {
146             for ($j=($i+2); $j<$sql_length; $j++) {
147               if (!empty($import_queries[$j])) {
148                 $next = substr($import_queries, $j, 6);
149
150                 if ($next[0] == '#') {
151 // find out where the break position is so we can remove this line (#comment line)
152                   for ($k=$j; $k<$sql_length; $k++) {
153                     if ($import_queries[$k] == "\n") {
154                       break;
155                     }
156                   }
157
158                   $query = substr($import_queries, 0, $i+1);
159
160                   $import_queries = substr($import_queries, $k);
161
162 // join the query before the comment appeared, with the rest of the dump
163                   $import_queries = $query . $import_queries;
164                   $sql_length = strlen($import_queries);
165                   $i = strpos($import_queries, ';')-1;
166                   continue 2;
167                 }
168
169                 break;
170               }
171             }
172
173             if (empty($next)) { // get the last insert query
174               $next = 'insert';
175             }
176
177             if ((strtoupper($next) == 'DROP T') || (strtoupper($next) == 'CREATE') || (strtoupper($next) == 'INSERT')) {
178               $next = '';
179
180               $sql_query = substr($import_queries, 0, $i);
181
182               if ($table_prefix !== -1) {
183                 if (strtoupper(substr($sql_query, 0, 25)) == 'DROP TABLE IF EXISTS OSC_') {
184                   $sql_query = 'DROP TABLE IF EXISTS ' . $table_prefix . substr($sql_query, 25);
185                 } elseif (strtoupper(substr($sql_query, 0, 17)) == 'CREATE TABLE OSC_') {
186                   $sql_query = 'CREATE TABLE ' . $table_prefix . substr($sql_query, 17);
187                 } elseif (strtoupper(substr($sql_query, 0, 16)) == 'INSERT INTO OSC_') {
188                   $sql_query = 'INSERT INTO ' . $table_prefix . substr($sql_query, 16);
189                 }
190               }
191
192               $sql_queries[] = $sql_query;
193
194               $import_queries = ltrim(substr($import_queries, $i+1));
195               $sql_length = strlen($import_queries);
196               $i = strpos($import_queries, ';')-1;
197             }
198           }
199         }
200
201         for ($i=0, $n=sizeof($sql_queries); $i<$n; $i++) {
202           $this->simpleQuery($sql_queries[$i]);
203         }
204       }
205
206       if ($this->isError()) {
207         return false;
208       } else {
209         return true;
210       }
211     }
212
213     function hasCreatePermission($database) {
214       $db_created = false;
215
216       if (empty($database)) {
217         $this->setError(ERROR_DB_NO_DATABASE_SELECTED);
218
219         return false;
220       }
221
222       $this->setErrorReporting(false);
223
224       if ($this->selectDatabase($database) === false) {
225         $this->setErrorReporting(true);
226
227         if ($this->simpleQuery('create database ' . $database)) {
228           $db_created = true;
229         }
230       }
231
232       $this->setErrorReporting(true);
233
234       if ($this->isError() === false) {
235         if ($this->selectDatabase($database)) {
236           if ($this->simpleQuery('create table osCommerceTestTable1536f ( temp_id int )')) {
237             if ($db_created === true) {
238               $this->simpleQuery('drop database ' . $database);
239             } else {
240               $this->simpleQuery('drop table osCommerceTestTable1536f');
241             }
242           }
243         }
244       }
245
246       if ($this->isError()) {
247         return false;
248       } else {
249         return true;
250       }
251     }
252
253     function numberOfQueries() {
254       return $this->number_of_queries;
255     }
256
257     function timeOfQueries() {
258       return $this->time_of_queries;
259     }
260
261     function getMicroTime() {
262       list($usec, $sec) = explode(' ', microtime());
263
264       return ((float)$usec + (float)$sec);
265     }
266   }
267
268   class osC_Database_Result {
269     var $db_class,
270         $sql_query,
271         $query_handler,
272         $result,
273         $rows,
274         $affected_rows,
275         $cache_key,
276         $cache_expire,
277         $cache_data,
278         $cache_read = false,
279         $debug = false,
280         $batch_query = false,
281         $batch_number,
282         $batch_rows,
283         $batch_size,
284         $batch_to,
285         $batch_from,
286         $batch_select_field;
287
288     function osC_Database_Result(&$db_class) {
289       $this->db_class =& $db_class;
290     }
291
292     function setQuery($query) {
293       $this->sql_query = $query;
294     }
295
296     function appendQuery($query) {
297       $this->sql_query .= ' ' . $query;
298     }
299
300     function getQuery() {
301       return $this->sql_query;
302     }
303
304     function setDebug($boolean) {
305       if ($boolean === true) {
306         $this->debug = true;
307       } else {
308         $this->debug = false;
309       }
310     }
311
312     function valueMixed($column, $type = 'string') {
313       if (!isset($this->result)) {
314         $this->next();
315       }
316
317       switch ($type) {
318         case 'protected':
319           return tep_output_string_protected($this->result[$column]);
320           break;
321         case 'int':
322           return (int)$this->result[$column];
323           break;
324         case 'decimal':
325           return (float)$this->result[$column];
326           break;
327         case 'string':
328         default:
329           return $this->result[$column];
330       }
331     }
332
333     function value($column) {
334       return $this->valueMixed($column, 'string');
335     }
336
337     function valueProtected($column) {
338       return $this->valueMixed($column, 'protected');
339     }
340
341     function valueInt($column) {
342       return $this->valueMixed($column, 'int');
343     }
344
345     function valueDecimal($column) {
346       return $this->valueMixed($column, 'decimal');
347     }
348
349     function bindValueMixed($place_holder, $value, $type = 'string') {
350       static $sql_parse_string;
351
352       switch ($type) {
353         case 'int':
354           $value = intval($value);
355           break;
356         case 'raw':
357           break;
358         case 'string':
359         default:
360           $sql_parse_string = $this->db_class->sql_parse_string;
361
362           $value = trim($value);
363
364           if ($this->db_class->sql_parse_string_with_connection_handler === true) {
365             $value = "'" . $sql_parse_string($value, $this->db_class->link) . "'";
366           } else {
367             $value = "'" . $sql_parse_string($value) . "'";
368           }
369       }
370
371       $this->bindReplace($place_holder, $value);
372     }
373
374     function bindReplace($place_holder, $value) {
375       $pos = strpos($this->sql_query, $place_holder);
376
377       if ($pos !== false) {
378         $length = strlen($place_holder);
379         $character_after_place_holder = substr($this->sql_query, $pos+$length, 1);
380
381         if (($character_after_place_holder === false) || ereg('[ ,)"]', $character_after_place_holder)) {
382           $this->sql_query = substr_replace($this->sql_query, $value, $pos, $length);
383         }
384       }
385     }
386
387     function bindValue($place_holder, $value) {
388       $this->bindValueMixed($place_holder, $value, 'string');
389     }
390
391     function bindInt($place_holder, $value) {
392       $this->bindValueMixed($place_holder, $value, 'int');
393     }
394
395     function bindRaw($place_holder, $value) {
396       $this->bindValueMixed($place_holder, $value, 'raw');
397     }
398
399     function bindTable($place_holder, $value) {
400       $this->bindValueMixed($place_holder, $value, 'raw');
401     }
402
403     function next() {
404       if ($this->cache_read === true) {
405         list(, $this->result) = each($this->cache_data);
406       } else {
407         if (!isset($this->query_handler)) {
408           $this->execute();
409         }
410
411         $this->result = $this->db_class->next($this->query_handler);
412
413         if (isset($this->cache_key)) {
414           $this->cache_data[] = $this->result;
415         }
416       }
417
418       return $this->result;
419     }
420
421     function freeResult() {
422       global $osC_Cache;
423
424       if ($this->cache_read === false) {
425         if (eregi('^SELECT', $this->sql_query)) {
426           $this->db_class->freeResult($this->query_handler);
427         }
428
429         if (isset($this->cache_key)) {
430           $osC_Cache->write($this->cache_key, $this->cache_data);
431         }
432       }
433
434       unset($this);
435     }
436
437     function numberOfRows() {
438       if (!isset($this->rows)) {
439         if (!isset($this->query_handler)) {
440           $this->execute();
441         }
442
443         if (isset($this->cache_key) && ($this->cache_read === true)) {
444           $this->rows = sizeof($this->cache_data);
445         } else {
446           $this->rows = $this->db_class->numberOfRows($this->query_handler);
447         }
448       }
449
450       return $this->rows;
451     }
452
453     function affectedRows() {
454       if (!isset($this->affected_rows)) {
455         if (!isset($this->query_handler)) {
456           $this->execute();
457         }
458
459         $this->affected_rows = $this->db_class->affectedRows();
460       }
461
462       return $this->affected_rows;
463     }
464
465     function execute() {
466       global $osC_Cache;
467
468       if (isset($this->cache_key)) {
469         if ($osC_Cache->read($this->cache_key, $this->cache_expire)) {
470           $this->cache_data = $osC_Cache->cached_data;
471
472           $this->cache_read = true;
473         }
474       }
475
476       if ($this->cache_read === false) {
477         $this->query_handler = $this->db_class->simpleQuery($this->sql_query, $this->debug);
478
479         if ($this->batch_query === true) {
480           $this->batchSize();
481
482           $this->batch_to = ($this->batch_rows * $this->batch_number);
483           if ($this->batch_to > $this->batch_size) {
484             $this->batch_to = $this->batch_size;
485           }
486
487           $this->batch_from = ($this->batch_rows * ($this->batch_number - 1));
488           if ($this->batch_to == 0) {
489             $this->batch_from = 0;
490           } else {
491             $this->batch_from++;
492           }
493         }
494
495         return $this->query_handler;
496       }
497     }
498
499     function executeRandom() {
500       return $this->query_handler = $this->db_class->randomQuery($this->sql_query);
501     }
502
503     function executeRandomMulti() {
504       return $this->query_handler = $this->db_class->randomQueryMulti($this->sql_query);
505     }
506
507     function setCache($key, $expire = 0) {
508       $this->cache_key = $key;
509       $this->cache_expire = $expire;
510     }
511
512     function toArray() {
513       if (!isset($this->result)) {
514         $this->next();
515       }
516
517       return $this->result;
518     }
519
520     function setBatchLimit($batch_number, $maximum_rows, $select_field = '') {
521       $this->batch_query = true;
522       $this->batch_number = $batch_number;
523       $this->batch_rows = $maximum_rows;
524       $this->batch_select_field = (empty($select_field) ? '*' : $select_field);
525
526       $from = ($batch_number * $maximum_rows) - $maximum_rows;
527
528       $this->sql_query = $this->db_class->setBatchLimit($this->sql_query, $from, $maximum_rows);
529
530     }
531
532     function batchSize() {
533       global $osC_Database;
534
535       if (!isset($this->batch_size)) {
536
537
538         $this->batch_size = $this->db_class->batchSize($this->sql_query, $this->batch_select_field);
539       }
540
541       return $this->batch_size;
542     }
543
544     function displayBatchLinksTotal($text) {
545       return sprintf($text, $this->batch_from, $this->batch_to, $this->batch_size);
546     }
547
548     function displayBatchLinksPullDown($batch_keyword = 'page', $parameters = '') {
549       if (PHP_VERSION < 4.1) {
550         global $_GET;
551       }
552
553       global $PHP_SELF;
554
555       $number_of_pages = ceil($this->batch_size / $this->batch_rows);
556
557       if ($number_of_pages > 1) {
558         $pages_array = array();
559         for ($i=1; $i<=$number_of_pages; $i++) {
560           $pages_array[] = array('id' => $i, 'text' => $i);
561         }
562
563         $get_parameter = '';
564         $hidden_parameter = '';
565         if (!empty($parameters)) {
566           $parameters = explode('&', $parameters);
567           foreach ($parameters as $parameter) {
568             list($key, $value) = explode('=', $parameter);
569
570             if ($key != $batch_keyword) {
571               $get_parameter .= $key . '=' . $value . '&';
572               $hidden_parameter .= osc_draw_hidden_field($key, $value);
573             }
574           }
575         }
576
577         $display_links = tep_draw_form($batch_keyword, basename($PHP_SELF), '', 'get');
578
579         if ($this->batch_number > 1) {
580           $display_links .= '<a href="' . tep_href_link(basename($PHP_SELF), $get_parameter . $batch_keyword . '=' . ($this->batch_number - 1)) . '" class="splitPageLink">' . PREVNEXT_BUTTON_PREV . '</a>';
581         } else {
582           $display_links .= PREVNEXT_BUTTON_PREV;
583         }
584
585         $display_links .= '&nbsp;&nbsp;' . sprintf(TEXT_RESULT_PAGE, osc_draw_pull_down_menu($batch_keyword, $pages_array, $this->batch_number, 'onChange="this.form.submit();"'), $number_of_pages) . '&nbsp;&nbsp;';
586
587         if (($this->batch_number < $number_of_pages) && ($number_of_pages != 1)) {
588           $display_links .= '<a href="' . tep_href_link(basename($PHP_SELF), $get_parameter . $batch_keyword . '=' . ($this->batch_number + 1)) . '" class="splitPageLink">' . PREVNEXT_BUTTON_NEXT . '</a>';
589         } else {
590           $display_links .= PREVNEXT_BUTTON_NEXT;
591         }
592
593 //        if (SID) $display_links .= tep_draw_hidden_field(tep_session_name(), tep_session_id());
594
595         $display_links .= $hidden_parameter . '</form>';
596       } else {
597         $display_links = sprintf(TEXT_RESULT_PAGE, 1, 1);
598       }
599
600       return $display_links;
601     }
602
603     function isBatchQuery() {
604       if ($this->batch_query === true) {
605         return true;
606       }
607
608       return false;
609     }
610   }
611 ?>