Quick Search:

View

Revision:

Diff

Diff from 1319 to:

Annotations

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

Annotated File View

hpdl
1
1 <?php
2 /*
hpdl
48
3   $Id: mysql.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
1319
8   Copyright (c) 2006 osCommerce
hpdl
1
9
10   Released under the GNU General Public License
11 */
12
13   class osC_Database_mysql extends osC_Database {
hpdl
1319
14     var $use_transactions = false,
hpdl
368
15         $use_fulltext = false,
hpdl
1319
16         $use_fulltext_boolean = false,
17         $sql_parse_string = 'mysql_escape_string',
18         $sql_parse_string_with_connection_handler = false;
hpdl
1
19
20     function osC_Database_mysql($server, $username, $password) {
21       $this->server = $server;
22       $this->username = $username;
23       $this->password = $password;
24
25       if (function_exists('mysql_real_escape_string')) {
26         $this->sql_parse_string = 'mysql_real_escape_string';
27         $this->sql_parse_string_with_connection_handler = true;
28       }
29
30       if ($this->is_connected === false) {
31         $this->connect();
32       }
33     }
34
35     function connect() {
36       if (defined('USE_PCONNECT') && (USE_PCONNECT == 'true')) {
37         $connect_function = 'mysql_pconnect';
38       } else {
39         $connect_function = 'mysql_connect';
40       }
41
42       if ($this->link = @$connect_function($this->server, $this->username, $this->password)) {
43         $this->setConnected(true);
44
45         return true;
46       } else {
47         $this->setError(mysql_error(), mysql_errno());
48
49         return false;
50       }
51     }
52
53     function disconnect() {
54       if ($this->isConnected()) {
55         if (@mysql_close($this->link)) {
56           return true;
57         } else {
58           return false;
59         }
60       } else {
61         return true;
62       }
63     }
64
65     function selectDatabase($database) {
66       if ($this->isConnected()) {
67         if (@mysql_select_db($database, $this->link)) {
68           return true;
69         } else {
70           $this->setError(mysql_error($this->link), mysql_errno($this->link));
71
72           return false;
73         }
74       } else {
75         return false;
76       }
77     }
78
hpdl
1319
79     function parseString($value) {
80       if ($this->sql_parse_string_with_connection_handler === true) {
81         return call_user_func_array($this->sql_parse_string, array($value, $this->link));
82       } else {
83         return call_user_func_array($this->sql_parse_string, array($value));
84       }
85     }
86
hpdl
1
87     function simpleQuery($query, $debug = false) {
88       global $messageStack, $osC_Services;
89
90       if ($this->isConnected()) {
91         $this->number_of_queries++;
92
93         if ( ($debug === false) && ($this->debug === true) ) {
94           $debug = true;
95         }
96
97         if (isset($osC_Services) && $osC_Services->isStarted('debug')) {
hpdl
554
98           if ( ($debug === false) && (SERVICE_DEBUG_OUTPUT_DB_QUERIES == '1') ) {
hpdl
1
99             $debug = true;
100           }
101
hpdl
757
102           if (!osc_empty(SERVICE_DEBUG_EXECUTION_TIME_LOG) && (SERVICE_DEBUG_LOG_DB_QUERIES == '1')) {
hpdl
1
103             @error_log('QUERY ' . $query . "\n", 3, SERVICE_DEBUG_EXECUTION_TIME_LOG);
104           }
105         } elseif ($debug === true) {
106           $debug = false;
107         }
108
109         if ($debug === true) {
110           $time_start = $this->getMicroTime();
111         }
112
113         $resource = @mysql_query($query, $this->link);
114
115         if ($debug === true) {
116           $time_end = $this->getMicroTime();
117
118           $query_time = number_format($time_end - $time_start, 5);
119
120           if ($this->debug === true) {
121             $this->time_of_queries += $query_time;
122           }
123
124           echo '<div style="font-family: Verdana, Arial, sans-serif; font-size: 7px; font-weight: bold;">[<a href="#query' . $this->number_of_queries . '">#' . $this->number_of_queries . '</a>]</div>';
125
126           $messageStack->add('debug', '<a name=\'query' . $this->number_of_queries . '\'></a>[#' . $this->number_of_queries . ' - ' . $query_time . 's] ' . $query, 'warning');
127         }
128
129         if ($resource !== false) {
hpdl
368
130           $this->error = false;
131           $this->error_number = null;
132           $this->error_query = null;
133
hpdl
1
134           return $resource;
135         } else {
hpdl
368
136           $this->setError(mysql_error($this->link), mysql_errno($this->link), $query);
hpdl
1
137
138           return false;
139         }
140       } else {
141         return false;
142       }
143     }
144
145     function dataSeek($row_number, $resource) {
146       return @mysql_data_seek($resource, $row_number);
147     }
148
149     function randomQuery($query) {
150       $query .= ' order by rand() limit 1';
151
152       return $this->simpleQuery($query);
153     }
154
155     function randomQueryMulti($query) {
156       $resource = $this->simpleQuery($query);
157
158       $num_rows = $this->numberOfRows($resource);
159
160       if ($num_rows > 0) {
hpdl
757
161         $random_row = osc_rand(0, ($num_rows - 1));
hpdl
1
162
163         $this->dataSeek($random_row, $resource);
164
165         return $resource;
166       } else {
167         return false;
168       }
169     }
170
171     function next($resource) {
172       return @mysql_fetch_assoc($resource);
173     }
174
175     function freeResult($resource) {
176       if (@mysql_free_result($resource)) {
177         return true;
178       } else {
179         $this->setError('Resource \'osC_Database->' . $resource . '\' could not be freed.');
180
181         return false;
182       }
183     }
184
185     function nextID() {
186       if ($id = @mysql_insert_id($this->link)) {
187         return $id;
188       } else {
189         $this->setError(mysql_error($this->link), mysql_errno($this->link));
190
191         return false;
192       }
193     }
194
195     function numberOfRows($resource) {
196       return @mysql_num_rows($resource);
197     }
198
199     function affectedRows() {
200       return mysql_affected_rows($this->link);
201     }
202
203     function startTransaction() {
204       if ($this->use_transactions === true) {
205         return $this->simpleQuery('start transaction');
206       }
207
208       return false;
209     }
210
211     function commitTransaction() {
212       if ($this->use_transactions === true) {
213         return $this->simpleQuery('commit');
214       }
215
216       return false;
217     }
218
219     function rollbackTransaction() {
220       if ($this->use_transactions === true) {
221         return $this->simpleQuery('rollback');
222       }
223
224       return false;
225     }
226
227     function setBatchLimit($sql_query, $from, $maximum_rows) {
228       return $sql_query . ' limit ' . $from . ', ' . $maximum_rows;
229     }
230
hpdl
368
231     function getBatchSize($sql_query, $select_field = '*') {
hpdl
1
232       if (strpos($sql_query, 'SQL_CALC_FOUND_ROWS') !== false) {
233         $bb = $this->query('select found_rows() as total');
234       } else {
hpdl
48
235         $total_query = substr($sql_query, 0, strpos($sql_query, ' limit '));
236
237         $pos_to = strlen($total_query);
238         $pos_from = strpos($total_query, ' from ');
239
240         if (($pos_group_by = strpos($total_query, ' group by ', $pos_from)) !== false) {
241           if ($pos_group_by < $pos_to) {
242             $pos_to = $pos_group_by;
243           }
244         }
245
246         if (($pos_having = strpos($total_query, ' having ', $pos_from)) !== false) {
247           if ($pos_having < $pos_to) {
248             $pos_to = $pos_having;
249           }
250         }
251
252         if (($pos_order_by = strpos($total_query, ' order by ', $pos_from)) !== false) {
253           if ($pos_order_by < $pos_to) {
254             $pos_to = $pos_order_by;
255           }
256         }
257
258         $bb = $this->query('select count(' . $select_field . ') as total ' . substr($total_query, $pos_from, ($pos_to - $pos_from)));
hpdl
1
259       }
260
261       return $bb->value('total');
262     }
hpdl
368
263
264     function prepareSearch($columns) {
265       if ($this->use_fulltext === true) {
266         return 'match (' . implode(', ', $columns) . ') against (:keywords' . (($this->use_fulltext_boolean === true) ? ' in boolean mode' : '') . ')';
267       } else {
268         $search_sql = '(';
269
270         foreach ($columns as $column) {
271           $search_sql .= $column . ' like :keyword or ';
272         }
273
274         $search_sql = substr($search_sql, 0, -4) . ')';
275
276         return $search_sql;
277       }
278     }
hpdl
1
279   }
280 ?>