Quick Search:

View

Revision:

Diff

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