Quick Search:

View

Revision:

Diff

Diff from 1372 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 1372 2007-03-05 22:58:51Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
1372
8   Copyright (c) 2007 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() {
hpdl
1372
186       if ( is_numeric($this->nextID) ) {
187         $id = $this->nextID;
188         $this->nextID = null;
189
hpdl
1
190         return $id;
hpdl
1372
191       } elseif ($id = @mysql_insert_id($this->link)) {
192         return $id;
hpdl
1
193       } else {
194         $this->setError(mysql_error($this->link), mysql_errno($this->link));
195
196         return false;
197       }
198     }
199
200     function numberOfRows($resource) {
201       return @mysql_num_rows($resource);
202     }
203
204     function affectedRows() {
205       return mysql_affected_rows($this->link);
206     }
207
208     function startTransaction() {
hpdl
1372
209       $this->logging_transaction = true;
210
hpdl
1
211       if ($this->use_transactions === true) {
212         return $this->simpleQuery('start transaction');
213       }
214
215       return false;
216     }
217
218     function commitTransaction() {
hpdl
1372
219       if ($this->logging_transaction === true) {
220         $this->logging_transaction = false;
221         $this->logging_transaction_action = false;
222       }
223
hpdl
1
224       if ($this->use_transactions === true) {
225         return $this->simpleQuery('commit');
226       }
227
228       return false;
229     }
230
231     function rollbackTransaction() {
hpdl
1372
232       if ($this->logging_transaction === true) {
233         $this->logging_transaction = false;
234         $this->logging_transaction_action = false;
235       }
236
hpdl
1
237       if ($this->use_transactions === true) {
238         return $this->simpleQuery('rollback');
239       }
240
241       return false;
242     }
243
244     function setBatchLimit($sql_query, $from, $maximum_rows) {
245       return $sql_query . ' limit ' . $from . ', ' . $maximum_rows;
246     }
247
hpdl
368
248     function getBatchSize($sql_query, $select_field = '*') {
hpdl
1
249       if (strpos($sql_query, 'SQL_CALC_FOUND_ROWS') !== false) {
250         $bb = $this->query('select found_rows() as total');
251       } else {
hpdl
48
252         $total_query = substr($sql_query, 0, strpos($sql_query, ' limit '));
253
254         $pos_to = strlen($total_query);
255         $pos_from = strpos($total_query, ' from ');
256
257         if (($pos_group_by = strpos($total_query, ' group by ', $pos_from)) !== false) {
258           if ($pos_group_by < $pos_to) {
259             $pos_to = $pos_group_by;
260           }
261         }
262
263         if (($pos_having = strpos($total_query, ' having ', $pos_from)) !== false) {
264           if ($pos_having < $pos_to) {
265             $pos_to = $pos_having;
266           }
267         }
268
269         if (($pos_order_by = strpos($total_query, ' order by ', $pos_from)) !== false) {
270           if ($pos_order_by < $pos_to) {
271             $pos_to = $pos_order_by;
272           }
273         }
274
275         $bb = $this->query('select count(' . $select_field . ') as total ' . substr($total_query, $pos_from, ($pos_to - $pos_from)));
hpdl
1
276       }
277
278       return $bb->value('total');
279     }
hpdl
368
280
281     function prepareSearch($columns) {
282       if ($this->use_fulltext === true) {
283         return 'match (' . implode(', ', $columns) . ') against (:keywords' . (($this->use_fulltext_boolean === true) ? ' in boolean mode' : '') . ')';
284       } else {
285         $search_sql = '(';
286
287         foreach ($columns as $column) {
288           $search_sql .= $column . ' like :keyword or ';
289         }
290
291         $search_sql = substr($search_sql, 0, -4) . ')';
292
293         return $search_sql;
294       }
295     }
hpdl
1
296   }
297 ?>