Quick Search:

View

Revision:

Diff

Diff from 757 to:

Annotations

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

Annotated File View

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