Quick Search:

View

Revision:

Diff

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