Quick Search:

View

Revision:

Diff

Diff from 5 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 /*
3   $Id: mysql.php 5 2005-01-31 01:40:15Z hpdl $
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   if (!function_exists('mysql_fetch_assoc')) {
14     function mysql_fetch_assoc($resource) {
15       return mysql_fetch_array($resource, MYSQL_ASSOC);
16     }
17   }
18
19   class osC_Database_mysql extends osC_Database {
20     var $sql_parse_string = 'addslashes',
21         $sql_parse_string_with_connection_handler = false,
22         $use_transactions = false;
23
24     function osC_Database_mysql($server, $username, $password) {
25       $this->server = $server;
26       $this->username = $username;
27       $this->password = $password;
28
29       if (function_exists('mysql_real_escape_string')) {
30         $this->sql_parse_string = 'mysql_real_escape_string';
31         $this->sql_parse_string_with_connection_handler = true;
32       } elseif (function_exists('mysql_escape_string')) {
33         $this->sql_parse_string = 'mysql_escape_string';
34       }
35
36       if ($this->is_connected === false) {
37         $this->connect();
38       }
39     }
40
41     function connect() {
42       if (defined('USE_PCONNECT') && (USE_PCONNECT == 'true')) {
43         $connect_function = 'mysql_pconnect';
44       } else {
45         $connect_function = 'mysql_connect';
46       }
47
48       if ($this->link = @$connect_function($this->server, $this->username, $this->password)) {
49         $this->setConnected(true);
50
51         return true;
52       } else {
53         $this->setError(mysql_error(), mysql_errno());
54
55         return false;
56       }
57     }
58
59     function disconnect() {
60       if ($this->isConnected()) {
61         if (@mysql_close($this->link)) {
62           return true;
63         } else {
64           return false;
65         }
66       } else {
67         return true;
68       }
69     }
70
71     function selectDatabase($database) {
72       if ($this->isConnected()) {
73         if (@mysql_select_db($database, $this->link)) {
74           return true;
75         } else {
76           $this->setError(mysql_error($this->link), mysql_errno($this->link));
77
78           return false;
79         }
80       } else {
81         return false;
82       }
83     }
84
85     function simpleQuery($query, $debug = false) {
86       global $messageStack, $osC_Services;
87
88       if ($this->isConnected()) {
89         $this->number_of_queries++;
90
91         if ( ($debug === false) && ($this->debug === true) ) {
92           $debug = true;
93         }
94
95         if (isset($osC_Services) && $osC_Services->isStarted('debug')) {
96           if ( ($debug === false) && (SERVICE_DEBUG_OUTPUT_DB_QUERIES == 'True') ) {
97             $debug = true;
98           }
99
100           if (tep_not_null(SERVICE_DEBUG_EXECUTION_TIME_LOG) && (SERVICE_DEBUG_LOG_DB_QUERIES == 'True')) {
101             @error_log('QUERY ' . $query . "\n", 3, SERVICE_DEBUG_EXECUTION_TIME_LOG);
102           }
103         } elseif ($debug === true) {
104           $debug = false;
105         }
106
107         if ($debug === true) {
108           $time_start = $this->getMicroTime();
109         }
110
111         $resource = @mysql_query($query, $this->link);
112
113         if ($debug === true) {
114           $time_end = $this->getMicroTime();
115
116           $query_time = number_format($time_end - $time_start, 5);
117
118           if ($this->debug === true) {
119             $this->time_of_queries += $query_time;
120           }
121
122           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>';
123
124           $messageStack->add('debug', '<a name=\'query' . $this->number_of_queries . '\'></a>[#' . $this->number_of_queries . ' - ' . $query_time . 's] ' . $query, 'warning');
125         }
126
127         if ($resource !== false) {
128           return $resource;
129         } else {
130           $this->setError(mysql_error($this->link), mysql_errno($this->link));
131
132           return false;
133         }
134       } else {
135         return false;
136       }
137     }
138
139     function dataSeek($row_number, $resource) {
140       return @mysql_data_seek($resource, $row_number);
141     }
142
143     function randomQuery($query) {
144       $query .= ' order by rand() limit 1';
145
146       return $this->simpleQuery($query);
147     }
148
149     function randomQueryMulti($query) {
150       $resource = $this->simpleQuery($query);
151
152       $num_rows = $this->numberOfRows($resource);
153
154       if ($num_rows > 0) {
155         $random_row = tep_rand(0, ($num_rows - 1));
156
157         $this->dataSeek($random_row, $resource);
158
159         return $resource;
160       } else {
161         return false;
162       }
163     }
164
165     function next($resource) {
166       return @mysql_fetch_assoc($resource);
167     }
168
169     function freeResult($resource) {
170       if (@mysql_free_result($resource)) {
171         return true;
172       } else {
173         $this->setError('Resource \'osC_Database->' . $resource . '\' could not be freed.');
174
175         return false;
176       }
177     }
178
179     function nextID() {
180       if ($id = @mysql_insert_id($this->link)) {
181         return $id;
182       } else {
183         $this->setError(mysql_error($this->link), mysql_errno($this->link));
184
185         return false;
186       }
187     }
188
189     function numberOfRows($resource) {
190       return @mysql_num_rows($resource);
191     }
192
193     function affectedRows() {
194       return mysql_affected_rows($this->link);
195     }
196
197     function startTransaction() {
198       if ($this->use_transactions === true) {
199         return $this->simpleQuery('start transaction');
200       }
201
202       return false;
203     }
204
205     function commitTransaction() {
206       if ($this->use_transactions === true) {
207         return $this->simpleQuery('commit');
208       }
209
210       return false;
211     }
212
213     function rollbackTransaction() {
214       if ($this->use_transactions === true) {
215         return $this->simpleQuery('rollback');
216       }
217
218       return false;
219     }
220
221     function setBatchLimit($sql_query, $from, $maximum_rows) {
222       return $sql_query . ' limit ' . $from . ', ' . $maximum_rows;
223     }
224
225     function batchSize($sql_query, $select_field = '*') {
226       if (strpos($sql_query, 'SQL_CALC_FOUND_ROWS') !== false) {
227         $bb = $this->query('select found_rows() as total');
228       } else {
229         $total_query = substr($sql_query, 0, strpos($sql_query, ' limit'));
230         $bb = $this->query('select count(' . $select_field . ') as total ' . substr($total_query, strpos($sql_query, 'from ')));
231       }
232
233       return $bb->value('total');
234     }
235   }
236 ?>