Quick Search:

View

Revision:

Diff

Diff from 1076 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 1076 2006-10-25 15:37:50Z 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       static $sql_parse_string;
358
359       switch ($type) {
360         case 'int':
361           $value = intval($value);
362           break;
hpdl
368
363         case 'float':
364           $value = floatval($value);
365           break;
hpdl
1
366         case 'raw':
367           break;
368         case 'string':
369         default:
370           $sql_parse_string = $this->db_class->sql_parse_string;
371
372           $value = trim($value);
373
374           if ($this->db_class->sql_parse_string_with_connection_handler === true) {
375             $value = "'" . $sql_parse_string($value, $this->db_class->link) . "'";
376           } else {
377             $value = "'" . $sql_parse_string($value) . "'";
378           }
379       }
380
381       $this->bindReplace($place_holder, $value);
382     }
383
384     function bindReplace($place_holder, $value) {
385       $pos = strpos($this->sql_query, $place_holder);
386
387       if ($pos !== false) {
388         $length = strlen($place_holder);
389         $character_after_place_holder = substr($this->sql_query, $pos+$length, 1);
390
391         if (($character_after_place_holder === false) || ereg('[ ,)"]', $character_after_place_holder)) {
392           $this->sql_query = substr_replace($this->sql_query, $value, $pos, $length);
393         }
394       }
395     }
396
397     function bindValue($place_holder, $value) {
398       $this->bindValueMixed($place_holder, $value, 'string');
399     }
400
401     function bindInt($place_holder, $value) {
402       $this->bindValueMixed($place_holder, $value, 'int');
403     }
404
hpdl
368
405     function bindFloat($place_holder, $value) {
406       $this->bindValueMixed($place_holder, $value, 'float');
407     }
408
hpdl
1
409     function bindRaw($place_holder, $value) {
410       $this->bindValueMixed($place_holder, $value, 'raw');
411     }
412
413     function bindTable($place_holder, $value) {
414       $this->bindValueMixed($place_holder, $value, 'raw');
415     }
416
417     function next() {
418       if ($this->cache_read === true) {
419         list(, $this->result) = each($this->cache_data);
420       } else {
421         if (!isset($this->query_handler)) {
422           $this->execute();
423         }
424
425         $this->result = $this->db_class->next($this->query_handler);
426
427         if (isset($this->cache_key)) {
428           $this->cache_data[] = $this->result;
429         }
430       }
431
432       return $this->result;
433     }
434
435     function freeResult() {
436       global $osC_Cache;
437
438       if ($this->cache_read === false) {
439         if (eregi('^SELECT', $this->sql_query)) {
440           $this->db_class->freeResult($this->query_handler);
441         }
442
443         if (isset($this->cache_key)) {
444           $osC_Cache->write($this->cache_key, $this->cache_data);
445         }
446       }
447
448       unset($this);
449     }
450
451     function numberOfRows() {
452       if (!isset($this->rows)) {
453         if (!isset($this->query_handler)) {
454           $this->execute();
455         }
456
457         if (isset($this->cache_key) && ($this->cache_read === true)) {
458           $this->rows = sizeof($this->cache_data);
459         } else {
460           $this->rows = $this->db_class->numberOfRows($this->query_handler);
461         }
462       }
463
464       return $this->rows;
465     }
466
467     function affectedRows() {
468       if (!isset($this->affected_rows)) {
469         if (!isset($this->query_handler)) {
470           $this->execute();
471         }
472
473         $this->affected_rows = $this->db_class->affectedRows();
474       }
475
476       return $this->affected_rows;
477     }
478
479     function execute() {
480       global $osC_Cache;
481
482       if (isset($this->cache_key)) {
483         if ($osC_Cache->read($this->cache_key, $this->cache_expire)) {
484           $this->cache_data = $osC_Cache->cached_data;
485
486           $this->cache_read = true;
487         }
488       }
489
490       if ($this->cache_read === false) {
491         $this->query_handler = $this->db_class->simpleQuery($this->sql_query, $this->debug);
492
493         if ($this->batch_query === true) {
hpdl
368
494           $this->getBatchSize();
hpdl
1
495
496           $this->batch_to = ($this->batch_rows * $this->batch_number);
497           if ($this->batch_to > $this->batch_size) {
498             $this->batch_to = $this->batch_size;
499           }
500
501           $this->batch_from = ($this->batch_rows * ($this->batch_number - 1));
502           if ($this->batch_to == 0) {
503             $this->batch_from = 0;
504           } else {
505             $this->batch_from++;
506           }
507         }
508
509         return $this->query_handler;
510       }
511     }
512
513     function executeRandom() {
514       return $this->query_handler = $this->db_class->randomQuery($this->sql_query);
515     }
516
517     function executeRandomMulti() {
518       return $this->query_handler = $this->db_class->randomQueryMulti($this->sql_query);
519     }
520
521     function setCache($key, $expire = 0) {
522       $this->cache_key = $key;
523       $this->cache_expire = $expire;
524     }
525
526     function toArray() {
527       if (!isset($this->result)) {
528         $this->next();
529       }
530
531       return $this->result;
532     }
533
hpdl
368
534     function prepareSearch($keywords, $columns, $embedded = false) {
535       if ($embedded === true) {
536         $this->sql_query .= ' and ';
537       }
538
539       $keywords_array = explode(' ', $keywords);
540
541       if ($this->db_class->use_fulltext === true) {
542         if ($this->db_class->use_fulltext_boolean === true) {
543           $keywords = '';
544
545           foreach ($keywords_array as $keyword) {
546             if ((substr($keyword, 0, 1) != '-') && (substr($keyword, 0, 1) != '+')) {
547               $keywords .= '+';
548             }
549
550             $keywords .= $keyword . ' ';
551           }
552
553           $keywords = substr($keywords, 0, -1);
554         }
555
556         $this->sql_query .= $this->db_class->prepareSearch($columns);
557         $this->bindValue(':keywords', $keywords);
558       } else {
559         foreach ($keywords_array as $keyword) {
560           $this->sql_query .= $this->db_class->prepareSearch($columns);
561
562           foreach ($columns as $column) {
563             $this->bindValue(':keyword', '%' . $keyword . '%');
564           }
565
566           $this->sql_query .= ' and ';
567         }
568
569         $this->sql_query = substr($this->sql_query, 0, -5);
570       }
571     }
572
hpdl
108
573     function setBatchLimit($batch_number = 1, $maximum_rows = 20, $select_field = '') {
hpdl
1
574       $this->batch_query = true;
hpdl
108
575       $this->batch_number = (is_numeric($batch_number) ? $batch_number : 1);
hpdl
1
576       $this->batch_rows = $maximum_rows;
577       $this->batch_select_field = (empty($select_field) ? '*' : $select_field);
578
hpdl
448
579       $from = max(($this->batch_number * $maximum_rows) - $maximum_rows, 0);
hpdl
1
580
581       $this->sql_query = $this->db_class->setBatchLimit($this->sql_query, $from, $maximum_rows);
582
583     }
584
hpdl
368
585     function getBatchSize() {
hpdl
1
586       global $osC_Database;
587
588       if (!isset($this->batch_size)) {
hpdl
368
589         $this->batch_size = $this->db_class->getBatchSize($this->sql_query, $this->batch_select_field);
hpdl
1
590       }
591
592       return $this->batch_size;
593     }
594
595     function displayBatchLinksTotal($text) {
596       return sprintf($text, $this->batch_from, $this->batch_to, $this->batch_size);
597     }
598
599     function displayBatchLinksPullDown($batch_keyword = 'page', $parameters = '') {
hpdl
410
600       global $osC_Language;
601
hpdl
1
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 = '';
hpdl
1076
612
hpdl
1
613         if (!empty($parameters)) {
hpdl
754
614           $parameters = explode('&', $parameters);
hpdl
1076
615
hpdl
1
616           foreach ($parameters as $parameter) {
hpdl
1076
617             $keys = explode('=', $parameter, 2);
hpdl
1
618
hpdl
1076
619             if ($keys[0] != $batch_keyword) {
620               $get_parameter .= $keys[0] . (isset($keys[1]) ? '=' . $keys[1] : '') . '&';
621               $hidden_parameter .= osc_draw_hidden_field($keys[0], (isset($keys[1]) ? $keys[1] : ''));
hpdl
1
622             }
623           }
624         }
625
hpdl
1076
626         $display_links = '<form action="' . osc_href_link(basename($_SERVER['SCRIPT_FILENAME'])) . '" action="get">' . $hidden_parameter;
hpdl
1
627
628         if ($this->batch_number > 1) {
hpdl
784
629           $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
630         } else {
hpdl
410
631           $display_links .= $osC_Language->get('result_set_previous_page');
hpdl
1
632         }
633
hpdl
410
634         $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
635
636         if (($this->batch_number < $number_of_pages) && ($number_of_pages != 1)) {
hpdl
784
637           $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
638         } else {
hpdl
410
639           $display_links .= $osC_Language->get('result_set_previous_page');
hpdl
1
640         }
641
hpdl
1076
642         $display_links .= osc_draw_hidden_session_id_field() . '</form>';
hpdl
1
643       } else {
hpdl
410
644         $display_links = sprintf($osC_Language->get('result_set_current_page'), 1, 1);
hpdl
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 ?>