36 |
kaklik |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
V4.80 8 Mar 2006 (c) 2000-2006 John Lim (jlim@natsoft.com.my). All rights reserved.
|
|
|
4 |
Released under both BSD license and Lesser GPL library license.
|
|
|
5 |
Whenever there is any discrepancy between the two licenses,
|
|
|
6 |
the BSD license will take precedence.
|
|
|
7 |
Set tabs to 8.
|
|
|
8 |
|
|
|
9 |
MySQL code that does not support transactions. Use mysqlt if you need transactions.
|
|
|
10 |
Requires mysql client. Works on Windows and Unix.
|
|
|
11 |
|
|
|
12 |
28 Feb 2001: MetaColumns bug fix - suggested by Freek Dijkstra (phpeverywhere@macfreek.com)
|
|
|
13 |
*/
|
|
|
14 |
|
|
|
15 |
// security - hide paths
|
|
|
16 |
if (!defined('ADODB_DIR')) die();
|
|
|
17 |
|
|
|
18 |
if (! defined("_ADODB_MYSQL_LAYER")) {
|
|
|
19 |
define("_ADODB_MYSQL_LAYER", 1 );
|
|
|
20 |
|
|
|
21 |
class ADODB_mysql extends ADOConnection {
|
|
|
22 |
var $databaseType = 'mysql';
|
|
|
23 |
var $dataProvider = 'mysql';
|
|
|
24 |
var $hasInsertID = true;
|
|
|
25 |
var $hasAffectedRows = true;
|
|
|
26 |
var $metaTablesSQL = "SHOW TABLES";
|
|
|
27 |
var $metaColumnsSQL = "SHOW COLUMNS FROM %s";
|
|
|
28 |
var $fmtTimeStamp = "'Y-m-d H:i:s'";
|
|
|
29 |
var $hasLimit = true;
|
|
|
30 |
var $hasMoveFirst = true;
|
|
|
31 |
var $hasGenID = true;
|
|
|
32 |
var $isoDates = true; // accepts dates in ISO format
|
|
|
33 |
var $sysDate = 'CURDATE()';
|
|
|
34 |
var $sysTimeStamp = 'NOW()';
|
|
|
35 |
var $hasTransactions = false;
|
|
|
36 |
var $forceNewConnect = false;
|
|
|
37 |
var $poorAffectedRows = true;
|
|
|
38 |
var $clientFlags = 0;
|
|
|
39 |
var $substr = "substring";
|
|
|
40 |
var $nameQuote = '`'; /// string to use to quote identifiers and names
|
|
|
41 |
var $compat323 = false; // true if compat with mysql 3.23
|
|
|
42 |
|
|
|
43 |
function ADODB_mysql()
|
|
|
44 |
{
|
|
|
45 |
if (defined('ADODB_EXTENSION')) $this->rsPrefix .= 'ext_';
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
function ServerInfo()
|
|
|
49 |
{
|
|
|
50 |
$arr['description'] = ADOConnection::GetOne("select version()");
|
|
|
51 |
$arr['version'] = ADOConnection::_findvers($arr['description']);
|
|
|
52 |
return $arr;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
function IfNull( $field, $ifNull )
|
|
|
56 |
{
|
|
|
57 |
return " IFNULL($field, $ifNull) "; // if MySQL
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
function &MetaTables($ttype=false,$showSchema=false,$mask=false)
|
|
|
62 |
{
|
|
|
63 |
$save = $this->metaTablesSQL;
|
|
|
64 |
if ($showSchema && is_string($showSchema)) {
|
|
|
65 |
$this->metaTablesSQL .= " from $showSchema";
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
if ($mask) {
|
|
|
69 |
$mask = $this->qstr($mask);
|
|
|
70 |
$this->metaTablesSQL .= " like $mask";
|
|
|
71 |
}
|
|
|
72 |
$ret =& ADOConnection::MetaTables($ttype,$showSchema);
|
|
|
73 |
|
|
|
74 |
$this->metaTablesSQL = $save;
|
|
|
75 |
return $ret;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
function &MetaIndexes ($table, $primary = FALSE, $owner=false)
|
|
|
80 |
{
|
|
|
81 |
// save old fetch mode
|
|
|
82 |
global $ADODB_FETCH_MODE;
|
|
|
83 |
|
|
|
84 |
$false = false;
|
|
|
85 |
$save = $ADODB_FETCH_MODE;
|
|
|
86 |
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
|
|
|
87 |
if ($this->fetchMode !== FALSE) {
|
|
|
88 |
$savem = $this->SetFetchMode(FALSE);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
// get index details
|
|
|
92 |
$rs = $this->Execute(sprintf('SHOW INDEX FROM %s',$table));
|
|
|
93 |
|
|
|
94 |
// restore fetchmode
|
|
|
95 |
if (isset($savem)) {
|
|
|
96 |
$this->SetFetchMode($savem);
|
|
|
97 |
}
|
|
|
98 |
$ADODB_FETCH_MODE = $save;
|
|
|
99 |
|
|
|
100 |
if (!is_object($rs)) {
|
|
|
101 |
return $false;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
$indexes = array ();
|
|
|
105 |
|
|
|
106 |
// parse index data into array
|
|
|
107 |
while ($row = $rs->FetchRow()) {
|
|
|
108 |
if ($primary == FALSE AND $row[2] == 'PRIMARY') {
|
|
|
109 |
continue;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
if (!isset($indexes[$row[2]])) {
|
|
|
113 |
$indexes[$row[2]] = array(
|
|
|
114 |
'unique' => ($row[1] == 0),
|
|
|
115 |
'columns' => array()
|
|
|
116 |
);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
$indexes[$row[2]]['columns'][$row[3] - 1] = $row[4];
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
// sort columns by order in the index
|
|
|
123 |
foreach ( array_keys ($indexes) as $index )
|
|
|
124 |
{
|
|
|
125 |
ksort ($indexes[$index]['columns']);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
return $indexes;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
// if magic quotes disabled, use mysql_real_escape_string()
|
|
|
133 |
function qstr($s,$magic_quotes=false)
|
|
|
134 |
{
|
|
|
135 |
if (!$magic_quotes) {
|
|
|
136 |
|
|
|
137 |
if (ADODB_PHPVER >= 0x4300) {
|
|
|
138 |
if (is_resource($this->_connectionID))
|
|
|
139 |
return "'".mysql_real_escape_string($s,$this->_connectionID)."'";
|
|
|
140 |
}
|
|
|
141 |
if ($this->replaceQuote[0] == '\\'){
|
|
|
142 |
$s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s);
|
|
|
143 |
}
|
|
|
144 |
return "'".str_replace("'",$this->replaceQuote,$s)."'";
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
// undo magic quotes for "
|
|
|
148 |
$s = str_replace('\\"','"',$s);
|
|
|
149 |
return "'$s'";
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
function _insertid()
|
|
|
153 |
{
|
|
|
154 |
return ADOConnection::GetOne('SELECT LAST_INSERT_ID()');
|
|
|
155 |
//return mysql_insert_id($this->_connectionID);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
function GetOne($sql,$inputarr=false)
|
|
|
159 |
{
|
|
|
160 |
if ($this->compat323 == false && strncasecmp($sql,'sele',4) == 0) {
|
|
|
161 |
$rs =& $this->SelectLimit($sql,1,-1,$inputarr);
|
|
|
162 |
if ($rs) {
|
|
|
163 |
$rs->Close();
|
|
|
164 |
if ($rs->EOF) return false;
|
|
|
165 |
return reset($rs->fields);
|
|
|
166 |
}
|
|
|
167 |
} else {
|
|
|
168 |
return ADOConnection::GetOne($sql,$inputarr);
|
|
|
169 |
}
|
|
|
170 |
return false;
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
function BeginTrans()
|
|
|
174 |
{
|
|
|
175 |
if ($this->debug) ADOConnection::outp("Transactions not supported in 'mysql' driver. Use 'mysqlt' or 'mysqli' driver");
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
function _affectedrows()
|
|
|
179 |
{
|
|
|
180 |
return mysql_affected_rows($this->_connectionID);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
// See http://www.mysql.com/doc/M/i/Miscellaneous_functions.html
|
|
|
184 |
// Reference on Last_Insert_ID on the recommended way to simulate sequences
|
|
|
185 |
var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
|
|
|
186 |
var $_genSeqSQL = "create table %s (id int not null)";
|
|
|
187 |
var $_genSeq2SQL = "insert into %s values (%s)";
|
|
|
188 |
var $_dropSeqSQL = "drop table %s";
|
|
|
189 |
|
|
|
190 |
function CreateSequence($seqname='adodbseq',$startID=1)
|
|
|
191 |
{
|
|
|
192 |
if (empty($this->_genSeqSQL)) return false;
|
|
|
193 |
$u = strtoupper($seqname);
|
|
|
194 |
|
|
|
195 |
$ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
|
|
|
196 |
if (!$ok) return false;
|
|
|
197 |
return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
function GenID($seqname='adodbseq',$startID=1)
|
|
|
202 |
{
|
|
|
203 |
// post-nuke sets hasGenID to false
|
|
|
204 |
if (!$this->hasGenID) return false;
|
|
|
205 |
|
|
|
206 |
$savelog = $this->_logsql;
|
|
|
207 |
$this->_logsql = false;
|
|
|
208 |
$getnext = sprintf($this->_genIDSQL,$seqname);
|
|
|
209 |
$holdtransOK = $this->_transOK; // save the current status
|
|
|
210 |
$rs = @$this->Execute($getnext);
|
|
|
211 |
if (!$rs) {
|
|
|
212 |
if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset
|
|
|
213 |
$u = strtoupper($seqname);
|
|
|
214 |
$this->Execute(sprintf($this->_genSeqSQL,$seqname));
|
|
|
215 |
$this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
|
|
|
216 |
$rs = $this->Execute($getnext);
|
|
|
217 |
}
|
|
|
218 |
$this->genID = mysql_insert_id($this->_connectionID);
|
|
|
219 |
|
|
|
220 |
if ($rs) $rs->Close();
|
|
|
221 |
|
|
|
222 |
$this->_logsql = $savelog;
|
|
|
223 |
return $this->genID;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
function &MetaDatabases()
|
|
|
227 |
{
|
|
|
228 |
$qid = mysql_list_dbs($this->_connectionID);
|
|
|
229 |
$arr = array();
|
|
|
230 |
$i = 0;
|
|
|
231 |
$max = mysql_num_rows($qid);
|
|
|
232 |
while ($i < $max) {
|
|
|
233 |
$db = mysql_tablename($qid,$i);
|
|
|
234 |
if ($db != 'mysql') $arr[] = $db;
|
|
|
235 |
$i += 1;
|
|
|
236 |
}
|
|
|
237 |
return $arr;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
// Format date column in sql string given an input format that understands Y M D
|
|
|
242 |
function SQLDate($fmt, $col=false)
|
|
|
243 |
{
|
|
|
244 |
if (!$col) $col = $this->sysTimeStamp;
|
|
|
245 |
$s = 'DATE_FORMAT('.$col.",'";
|
|
|
246 |
$concat = false;
|
|
|
247 |
$len = strlen($fmt);
|
|
|
248 |
for ($i=0; $i < $len; $i++) {
|
|
|
249 |
$ch = $fmt[$i];
|
|
|
250 |
switch($ch) {
|
|
|
251 |
|
|
|
252 |
default:
|
|
|
253 |
if ($ch == '\\') {
|
|
|
254 |
$i++;
|
|
|
255 |
$ch = substr($fmt,$i,1);
|
|
|
256 |
}
|
|
|
257 |
/** FALL THROUGH */
|
|
|
258 |
case '-':
|
|
|
259 |
case '/':
|
|
|
260 |
$s .= $ch;
|
|
|
261 |
break;
|
|
|
262 |
|
|
|
263 |
case 'Y':
|
|
|
264 |
case 'y':
|
|
|
265 |
$s .= '%Y';
|
|
|
266 |
break;
|
|
|
267 |
case 'M':
|
|
|
268 |
$s .= '%b';
|
|
|
269 |
break;
|
|
|
270 |
|
|
|
271 |
case 'm':
|
|
|
272 |
$s .= '%m';
|
|
|
273 |
break;
|
|
|
274 |
case 'D':
|
|
|
275 |
case 'd':
|
|
|
276 |
$s .= '%d';
|
|
|
277 |
break;
|
|
|
278 |
|
|
|
279 |
case 'Q':
|
|
|
280 |
case 'q':
|
|
|
281 |
$s .= "'),Quarter($col)";
|
|
|
282 |
|
|
|
283 |
if ($len > $i+1) $s .= ",DATE_FORMAT($col,'";
|
|
|
284 |
else $s .= ",('";
|
|
|
285 |
$concat = true;
|
|
|
286 |
break;
|
|
|
287 |
|
|
|
288 |
case 'H':
|
|
|
289 |
$s .= '%H';
|
|
|
290 |
break;
|
|
|
291 |
|
|
|
292 |
case 'h':
|
|
|
293 |
$s .= '%I';
|
|
|
294 |
break;
|
|
|
295 |
|
|
|
296 |
case 'i':
|
|
|
297 |
$s .= '%i';
|
|
|
298 |
break;
|
|
|
299 |
|
|
|
300 |
case 's':
|
|
|
301 |
$s .= '%s';
|
|
|
302 |
break;
|
|
|
303 |
|
|
|
304 |
case 'a':
|
|
|
305 |
case 'A':
|
|
|
306 |
$s .= '%p';
|
|
|
307 |
break;
|
|
|
308 |
|
|
|
309 |
case 'w':
|
|
|
310 |
$s .= '%w';
|
|
|
311 |
break;
|
|
|
312 |
|
|
|
313 |
case 'W':
|
|
|
314 |
$s .= '%U';
|
|
|
315 |
break;
|
|
|
316 |
|
|
|
317 |
case 'l':
|
|
|
318 |
$s .= '%W';
|
|
|
319 |
break;
|
|
|
320 |
}
|
|
|
321 |
}
|
|
|
322 |
$s.="')";
|
|
|
323 |
if ($concat) $s = "CONCAT($s)";
|
|
|
324 |
return $s;
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
|
|
|
328 |
// returns concatenated string
|
|
|
329 |
// much easier to run "mysqld --ansi" or "mysqld --sql-mode=PIPES_AS_CONCAT" and use || operator
|
|
|
330 |
function Concat()
|
|
|
331 |
{
|
|
|
332 |
$s = "";
|
|
|
333 |
$arr = func_get_args();
|
|
|
334 |
|
|
|
335 |
// suggestion by andrew005@mnogo.ru
|
|
|
336 |
$s = implode(',',$arr);
|
|
|
337 |
if (strlen($s) > 0) return "CONCAT($s)";
|
|
|
338 |
else return '';
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
function OffsetDate($dayFraction,$date=false)
|
|
|
342 |
{
|
|
|
343 |
if (!$date) $date = $this->sysDate;
|
|
|
344 |
$fraction = $dayFraction * 24 * 3600;
|
|
|
345 |
return "from_unixtime(unix_timestamp($date)+$fraction)";
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
// returns true or false
|
|
|
349 |
function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
|
|
|
350 |
{
|
|
|
351 |
if (!empty($this->port)) $argHostname .= ":".$this->port;
|
|
|
352 |
|
|
|
353 |
if (ADODB_PHPVER >= 0x4300)
|
|
|
354 |
$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
|
|
|
355 |
$this->forceNewConnect,$this->clientFlags);
|
|
|
356 |
else if (ADODB_PHPVER >= 0x4200)
|
|
|
357 |
$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
|
|
|
358 |
$this->forceNewConnect);
|
|
|
359 |
else
|
|
|
360 |
$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword);
|
|
|
361 |
|
|
|
362 |
if ($this->_connectionID === false) return false;
|
|
|
363 |
if ($argDatabasename) return $this->SelectDB($argDatabasename);
|
|
|
364 |
return true;
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
// returns true or false
|
|
|
368 |
function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
|
|
|
369 |
{
|
|
|
370 |
if (!empty($this->port)) $argHostname .= ":".$this->port;
|
|
|
371 |
|
|
|
372 |
if (ADODB_PHPVER >= 0x4300)
|
|
|
373 |
$this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword,$this->clientFlags);
|
|
|
374 |
else
|
|
|
375 |
$this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword);
|
|
|
376 |
if ($this->_connectionID === false) return false;
|
|
|
377 |
if ($this->autoRollback) $this->RollbackTrans();
|
|
|
378 |
if ($argDatabasename) return $this->SelectDB($argDatabasename);
|
|
|
379 |
return true;
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
function _nconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
|
|
|
383 |
{
|
|
|
384 |
$this->forceNewConnect = true;
|
|
|
385 |
return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename);
|
|
|
386 |
}
|
|
|
387 |
|
|
|
388 |
function &MetaColumns($table)
|
|
|
389 |
{
|
|
|
390 |
$this->_findschema($table,$schema);
|
|
|
391 |
if ($schema) {
|
|
|
392 |
$dbName = $this->database;
|
|
|
393 |
$this->SelectDB($schema);
|
|
|
394 |
}
|
|
|
395 |
global $ADODB_FETCH_MODE;
|
|
|
396 |
$save = $ADODB_FETCH_MODE;
|
|
|
397 |
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
|
|
|
398 |
|
|
|
399 |
if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
|
|
|
400 |
$rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
|
|
|
401 |
|
|
|
402 |
if ($schema) {
|
|
|
403 |
$this->SelectDB($dbName);
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
if (isset($savem)) $this->SetFetchMode($savem);
|
|
|
407 |
$ADODB_FETCH_MODE = $save;
|
|
|
408 |
if (!is_object($rs)) {
|
|
|
409 |
$false = false;
|
|
|
410 |
return $false;
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
$retarr = array();
|
|
|
414 |
while (!$rs->EOF){
|
|
|
415 |
$fld = new ADOFieldObject();
|
|
|
416 |
$fld->name = $rs->fields[0];
|
|
|
417 |
$type = $rs->fields[1];
|
|
|
418 |
|
|
|
419 |
// split type into type(length):
|
|
|
420 |
$fld->scale = null;
|
|
|
421 |
if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
|
|
|
422 |
$fld->type = $query_array[1];
|
|
|
423 |
$fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
|
|
|
424 |
$fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
|
|
|
425 |
} elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
|
|
|
426 |
$fld->type = $query_array[1];
|
|
|
427 |
$fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
|
|
|
428 |
} elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) {
|
|
|
429 |
$fld->type = $query_array[1];
|
|
|
430 |
$arr = explode(",",$query_array[2]);
|
|
|
431 |
$fld->enums = $arr;
|
|
|
432 |
$zlen = max(array_map("strlen",$arr)) - 2; // PHP >= 4.0.6
|
|
|
433 |
$fld->max_length = ($zlen > 0) ? $zlen : 1;
|
|
|
434 |
} else {
|
|
|
435 |
$fld->type = $type;
|
|
|
436 |
$fld->max_length = -1;
|
|
|
437 |
}
|
|
|
438 |
$fld->not_null = ($rs->fields[2] != 'YES');
|
|
|
439 |
$fld->primary_key = ($rs->fields[3] == 'PRI');
|
|
|
440 |
$fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
|
|
|
441 |
$fld->binary = (strpos($type,'blob') !== false);
|
|
|
442 |
$fld->unsigned = (strpos($type,'unsigned') !== false);
|
|
|
443 |
|
|
|
444 |
if (!$fld->binary) {
|
|
|
445 |
$d = $rs->fields[4];
|
|
|
446 |
if ($d != '' && $d != 'NULL') {
|
|
|
447 |
$fld->has_default = true;
|
|
|
448 |
$fld->default_value = $d;
|
|
|
449 |
} else {
|
|
|
450 |
$fld->has_default = false;
|
|
|
451 |
}
|
|
|
452 |
}
|
|
|
453 |
|
|
|
454 |
if ($save == ADODB_FETCH_NUM) {
|
|
|
455 |
$retarr[] = $fld;
|
|
|
456 |
} else {
|
|
|
457 |
$retarr[strtoupper($fld->name)] = $fld;
|
|
|
458 |
}
|
|
|
459 |
$rs->MoveNext();
|
|
|
460 |
}
|
|
|
461 |
|
|
|
462 |
$rs->Close();
|
|
|
463 |
return $retarr;
|
|
|
464 |
}
|
|
|
465 |
|
|
|
466 |
// returns true or false
|
|
|
467 |
function SelectDB($dbName)
|
|
|
468 |
{
|
|
|
469 |
$this->database = $dbName;
|
|
|
470 |
$this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
|
|
|
471 |
if ($this->_connectionID) {
|
|
|
472 |
return @mysql_select_db($dbName,$this->_connectionID);
|
|
|
473 |
}
|
|
|
474 |
else return false;
|
|
|
475 |
}
|
|
|
476 |
|
|
|
477 |
// parameters use PostgreSQL convention, not MySQL
|
|
|
478 |
function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs=0)
|
|
|
479 |
{
|
|
|
480 |
$offsetStr =($offset>=0) ? ((integer)$offset)."," : '';
|
|
|
481 |
// jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
|
|
|
482 |
if ($nrows < 0) $nrows = '18446744073709551615';
|
|
|
483 |
|
|
|
484 |
if ($secs)
|
|
|
485 |
$rs =& $this->CacheExecute($secs,$sql." LIMIT $offsetStr".((integer)$nrows),$inputarr);
|
|
|
486 |
else
|
|
|
487 |
$rs =& $this->Execute($sql." LIMIT $offsetStr".((integer)$nrows),$inputarr);
|
|
|
488 |
return $rs;
|
|
|
489 |
}
|
|
|
490 |
|
|
|
491 |
// returns queryID or false
|
|
|
492 |
function _query($sql,$inputarr)
|
|
|
493 |
{
|
|
|
494 |
//global $ADODB_COUNTRECS;
|
|
|
495 |
//if($ADODB_COUNTRECS)
|
|
|
496 |
return mysql_query($sql,$this->_connectionID);
|
|
|
497 |
//else return @mysql_unbuffered_query($sql,$this->_connectionID); // requires PHP >= 4.0.6
|
|
|
498 |
}
|
|
|
499 |
|
|
|
500 |
/* Returns: the last error message from previous database operation */
|
|
|
501 |
function ErrorMsg()
|
|
|
502 |
{
|
|
|
503 |
|
|
|
504 |
if ($this->_logsql) return $this->_errorMsg;
|
|
|
505 |
if (empty($this->_connectionID)) $this->_errorMsg = @mysql_error();
|
|
|
506 |
else $this->_errorMsg = @mysql_error($this->_connectionID);
|
|
|
507 |
return $this->_errorMsg;
|
|
|
508 |
}
|
|
|
509 |
|
|
|
510 |
/* Returns: the last error number from previous database operation */
|
|
|
511 |
function ErrorNo()
|
|
|
512 |
{
|
|
|
513 |
if ($this->_logsql) return $this->_errorCode;
|
|
|
514 |
if (empty($this->_connectionID)) return @mysql_errno();
|
|
|
515 |
else return @mysql_errno($this->_connectionID);
|
|
|
516 |
}
|
|
|
517 |
|
|
|
518 |
// returns true or false
|
|
|
519 |
function _close()
|
|
|
520 |
{
|
|
|
521 |
@mysql_close($this->_connectionID);
|
|
|
522 |
$this->_connectionID = false;
|
|
|
523 |
}
|
|
|
524 |
|
|
|
525 |
|
|
|
526 |
/*
|
|
|
527 |
* Maximum size of C field
|
|
|
528 |
*/
|
|
|
529 |
function CharMax()
|
|
|
530 |
{
|
|
|
531 |
return 255;
|
|
|
532 |
}
|
|
|
533 |
|
|
|
534 |
/*
|
|
|
535 |
* Maximum size of X field
|
|
|
536 |
*/
|
|
|
537 |
function TextMax()
|
|
|
538 |
{
|
|
|
539 |
return 4294967295;
|
|
|
540 |
}
|
|
|
541 |
|
|
|
542 |
// "Innox - Juan Carlos Gonzalez" <jgonzalez#innox.com.mx>
|
|
|
543 |
function MetaForeignKeys( $table, $owner = FALSE, $upper = FALSE, $associative = FALSE )
|
|
|
544 |
{
|
|
|
545 |
if ( !empty($owner) ) {
|
|
|
546 |
$table = "$owner.$table";
|
|
|
547 |
}
|
|
|
548 |
$a_create_table = $this->getRow(sprintf('SHOW CREATE TABLE %s', $table));
|
|
|
549 |
if ($associative) $create_sql = $a_create_table["Create Table"];
|
|
|
550 |
else $create_sql = $a_create_table[1];
|
|
|
551 |
|
|
|
552 |
$matches = array();
|
|
|
553 |
|
|
|
554 |
if (!preg_match_all("/FOREIGN KEY \(`(.*?)`\) REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql, $matches)) return false;
|
|
|
555 |
$foreign_keys = array();
|
|
|
556 |
$num_keys = count($matches[0]);
|
|
|
557 |
for ( $i = 0; $i < $num_keys; $i ++ ) {
|
|
|
558 |
$my_field = explode('`, `', $matches[1][$i]);
|
|
|
559 |
$ref_table = $matches[2][$i];
|
|
|
560 |
$ref_field = explode('`, `', $matches[3][$i]);
|
|
|
561 |
|
|
|
562 |
if ( $upper ) {
|
|
|
563 |
$ref_table = strtoupper($ref_table);
|
|
|
564 |
}
|
|
|
565 |
|
|
|
566 |
$foreign_keys[$ref_table] = array();
|
|
|
567 |
$num_fields = count($my_field);
|
|
|
568 |
for ( $j = 0; $j < $num_fields; $j ++ ) {
|
|
|
569 |
if ( $associative ) {
|
|
|
570 |
$foreign_keys[$ref_table][$ref_field[$j]] = $my_field[$j];
|
|
|
571 |
} else {
|
|
|
572 |
$foreign_keys[$ref_table][] = "{$my_field[$j]}={$ref_field[$j]}";
|
|
|
573 |
}
|
|
|
574 |
}
|
|
|
575 |
}
|
|
|
576 |
|
|
|
577 |
return $foreign_keys;
|
|
|
578 |
}
|
|
|
579 |
|
|
|
580 |
|
|
|
581 |
}
|
|
|
582 |
|
|
|
583 |
/*--------------------------------------------------------------------------------------
|
|
|
584 |
Class Name: Recordset
|
|
|
585 |
--------------------------------------------------------------------------------------*/
|
|
|
586 |
|
|
|
587 |
|
|
|
588 |
class ADORecordSet_mysql extends ADORecordSet{
|
|
|
589 |
|
|
|
590 |
var $databaseType = "mysql";
|
|
|
591 |
var $canSeek = true;
|
|
|
592 |
|
|
|
593 |
function ADORecordSet_mysql($queryID,$mode=false)
|
|
|
594 |
{
|
|
|
595 |
if ($mode === false) {
|
|
|
596 |
global $ADODB_FETCH_MODE;
|
|
|
597 |
$mode = $ADODB_FETCH_MODE;
|
|
|
598 |
}
|
|
|
599 |
switch ($mode)
|
|
|
600 |
{
|
|
|
601 |
case ADODB_FETCH_NUM: $this->fetchMode = MYSQL_NUM; break;
|
|
|
602 |
case ADODB_FETCH_ASSOC:$this->fetchMode = MYSQL_ASSOC; break;
|
|
|
603 |
case ADODB_FETCH_DEFAULT:
|
|
|
604 |
case ADODB_FETCH_BOTH:
|
|
|
605 |
default:
|
|
|
606 |
$this->fetchMode = MYSQL_BOTH; break;
|
|
|
607 |
}
|
|
|
608 |
$this->adodbFetchMode = $mode;
|
|
|
609 |
$this->ADORecordSet($queryID);
|
|
|
610 |
}
|
|
|
611 |
|
|
|
612 |
function _initrs()
|
|
|
613 |
{
|
|
|
614 |
//GLOBAL $ADODB_COUNTRECS;
|
|
|
615 |
// $this->_numOfRows = ($ADODB_COUNTRECS) ? @mysql_num_rows($this->_queryID):-1;
|
|
|
616 |
$this->_numOfRows = @mysql_num_rows($this->_queryID);
|
|
|
617 |
$this->_numOfFields = @mysql_num_fields($this->_queryID);
|
|
|
618 |
}
|
|
|
619 |
|
|
|
620 |
function &FetchField($fieldOffset = -1)
|
|
|
621 |
{
|
|
|
622 |
if ($fieldOffset != -1) {
|
|
|
623 |
$o = @mysql_fetch_field($this->_queryID, $fieldOffset);
|
|
|
624 |
$f = @mysql_field_flags($this->_queryID,$fieldOffset);
|
|
|
625 |
$o->max_length = @mysql_field_len($this->_queryID,$fieldOffset); // suggested by: Jim Nicholson (jnich@att.com)
|
|
|
626 |
//$o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable
|
|
|
627 |
$o->binary = (strpos($f,'binary')!== false);
|
|
|
628 |
}
|
|
|
629 |
else if ($fieldOffset == -1) { /* The $fieldOffset argument is not provided thus its -1 */
|
|
|
630 |
$o = @mysql_fetch_field($this->_queryID);
|
|
|
631 |
$o->max_length = @mysql_field_len($this->_queryID); // suggested by: Jim Nicholson (jnich@att.com)
|
|
|
632 |
//$o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable
|
|
|
633 |
}
|
|
|
634 |
|
|
|
635 |
return $o;
|
|
|
636 |
}
|
|
|
637 |
|
|
|
638 |
function &GetRowAssoc($upper=true)
|
|
|
639 |
{
|
|
|
640 |
if ($this->fetchMode == MYSQL_ASSOC && !$upper) return $this->fields;
|
|
|
641 |
$row =& ADORecordSet::GetRowAssoc($upper);
|
|
|
642 |
return $row;
|
|
|
643 |
}
|
|
|
644 |
|
|
|
645 |
/* Use associative array to get fields array */
|
|
|
646 |
function Fields($colname)
|
|
|
647 |
{
|
|
|
648 |
// added @ by "Michael William Miller" <mille562@pilot.msu.edu>
|
|
|
649 |
if ($this->fetchMode != MYSQL_NUM) return @$this->fields[$colname];
|
|
|
650 |
|
|
|
651 |
if (!$this->bind) {
|
|
|
652 |
$this->bind = array();
|
|
|
653 |
for ($i=0; $i < $this->_numOfFields; $i++) {
|
|
|
654 |
$o = $this->FetchField($i);
|
|
|
655 |
$this->bind[strtoupper($o->name)] = $i;
|
|
|
656 |
}
|
|
|
657 |
}
|
|
|
658 |
return $this->fields[$this->bind[strtoupper($colname)]];
|
|
|
659 |
}
|
|
|
660 |
|
|
|
661 |
function _seek($row)
|
|
|
662 |
{
|
|
|
663 |
if ($this->_numOfRows == 0) return false;
|
|
|
664 |
return @mysql_data_seek($this->_queryID,$row);
|
|
|
665 |
}
|
|
|
666 |
|
|
|
667 |
function MoveNext()
|
|
|
668 |
{
|
|
|
669 |
//return adodb_movenext($this);
|
|
|
670 |
//if (defined('ADODB_EXTENSION')) return adodb_movenext($this);
|
|
|
671 |
if (@$this->fields = mysql_fetch_array($this->_queryID,$this->fetchMode)) {
|
|
|
672 |
$this->_currentRow += 1;
|
|
|
673 |
return true;
|
|
|
674 |
}
|
|
|
675 |
if (!$this->EOF) {
|
|
|
676 |
$this->_currentRow += 1;
|
|
|
677 |
$this->EOF = true;
|
|
|
678 |
}
|
|
|
679 |
return false;
|
|
|
680 |
}
|
|
|
681 |
|
|
|
682 |
function _fetch()
|
|
|
683 |
{
|
|
|
684 |
$this->fields = @mysql_fetch_array($this->_queryID,$this->fetchMode);
|
|
|
685 |
return is_array($this->fields);
|
|
|
686 |
}
|
|
|
687 |
|
|
|
688 |
function _close() {
|
|
|
689 |
@mysql_free_result($this->_queryID);
|
|
|
690 |
$this->_queryID = false;
|
|
|
691 |
}
|
|
|
692 |
|
|
|
693 |
function MetaType($t,$len=-1,$fieldobj=false)
|
|
|
694 |
{
|
|
|
695 |
if (is_object($t)) {
|
|
|
696 |
$fieldobj = $t;
|
|
|
697 |
$t = $fieldobj->type;
|
|
|
698 |
$len = $fieldobj->max_length;
|
|
|
699 |
}
|
|
|
700 |
|
|
|
701 |
$len = -1; // mysql max_length is not accurate
|
|
|
702 |
switch (strtoupper($t)) {
|
|
|
703 |
case 'STRING':
|
|
|
704 |
case 'CHAR':
|
|
|
705 |
case 'VARCHAR':
|
|
|
706 |
case 'TINYBLOB':
|
|
|
707 |
case 'TINYTEXT':
|
|
|
708 |
case 'ENUM':
|
|
|
709 |
case 'SET':
|
|
|
710 |
if ($len <= $this->blobSize) return 'C';
|
|
|
711 |
|
|
|
712 |
case 'TEXT':
|
|
|
713 |
case 'LONGTEXT':
|
|
|
714 |
case 'MEDIUMTEXT':
|
|
|
715 |
return 'X';
|
|
|
716 |
|
|
|
717 |
// php_mysql extension always returns 'blob' even if 'text'
|
|
|
718 |
// so we have to check whether binary...
|
|
|
719 |
case 'IMAGE':
|
|
|
720 |
case 'LONGBLOB':
|
|
|
721 |
case 'BLOB':
|
|
|
722 |
case 'MEDIUMBLOB':
|
|
|
723 |
return !empty($fieldobj->binary) ? 'B' : 'X';
|
|
|
724 |
|
|
|
725 |
case 'YEAR':
|
|
|
726 |
case 'DATE': return 'D';
|
|
|
727 |
|
|
|
728 |
case 'TIME':
|
|
|
729 |
case 'DATETIME':
|
|
|
730 |
case 'TIMESTAMP': return 'T';
|
|
|
731 |
|
|
|
732 |
case 'INT':
|
|
|
733 |
case 'INTEGER':
|
|
|
734 |
case 'BIGINT':
|
|
|
735 |
case 'TINYINT':
|
|
|
736 |
case 'MEDIUMINT':
|
|
|
737 |
case 'SMALLINT':
|
|
|
738 |
|
|
|
739 |
if (!empty($fieldobj->primary_key)) return 'R';
|
|
|
740 |
else return 'I';
|
|
|
741 |
|
|
|
742 |
default: return 'N';
|
|
|
743 |
}
|
|
|
744 |
}
|
|
|
745 |
|
|
|
746 |
}
|
|
|
747 |
|
|
|
748 |
class ADORecordSet_ext_mysql extends ADORecordSet_mysql {
|
|
|
749 |
function ADORecordSet_ext_mysql($queryID,$mode=false)
|
|
|
750 |
{
|
|
|
751 |
if ($mode === false) {
|
|
|
752 |
global $ADODB_FETCH_MODE;
|
|
|
753 |
$mode = $ADODB_FETCH_MODE;
|
|
|
754 |
}
|
|
|
755 |
switch ($mode)
|
|
|
756 |
{
|
|
|
757 |
case ADODB_FETCH_NUM: $this->fetchMode = MYSQL_NUM; break;
|
|
|
758 |
case ADODB_FETCH_ASSOC:$this->fetchMode = MYSQL_ASSOC; break;
|
|
|
759 |
case ADODB_FETCH_DEFAULT:
|
|
|
760 |
case ADODB_FETCH_BOTH:
|
|
|
761 |
default:
|
|
|
762 |
$this->fetchMode = MYSQL_BOTH; break;
|
|
|
763 |
}
|
|
|
764 |
$this->adodbFetchMode = $mode;
|
|
|
765 |
$this->ADORecordSet($queryID);
|
|
|
766 |
}
|
|
|
767 |
|
|
|
768 |
function MoveNext()
|
|
|
769 |
{
|
|
|
770 |
return @adodb_movenext($this);
|
|
|
771 |
}
|
|
|
772 |
}
|
|
|
773 |
|
|
|
774 |
|
|
|
775 |
}
|
|
|
776 |
?>
|