Quick Search:

View

Revision:

Diff

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