1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
<?php
/**
* PDO MySQL driver
*
* This file is part of ADOdb, a Database Abstraction Layer library for PHP.
*
* @package ADOdb
* @link https://adodb.org Project's web site and documentation
* @link https://github.com/ADOdb/ADOdb Source code and issue tracker
*
* The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
* and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
* any later version. This means you can use it in proprietary products.
* See the LICENSE.md file distributed with this source code for details.
* @license BSD-3-Clause
* @license LGPL-2.1-or-later
*
* @copyright 2000-2013 John Lim
* @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
*/
class ADODB_pdo_mysql extends ADODB_pdo {
var $metaTablesSQL = "SELECT
TABLE_NAME,
CASE WHEN TABLE_TYPE = 'VIEW' THEN 'V' ELSE 'T' END
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA=";
var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`";
var $sysDate = 'CURDATE()';
var $sysTimeStamp = 'NOW()';
var $hasGenID = true;
var $_genIDSQL = "UPDATE %s SET id=LAST_INSERT_ID(id+1);";
var $_genSeqSQL = "CREATE TABLE if NOT EXISTS %s (id int not null)";
var $_genSeqCountSQL = "SELECT count(*) FROM %s";
var $_genSeq2SQL = "INSERT INTO %s VALUES (%s)";
var $_dropSeqSQL = "drop table %s";
var $fmtTimeStamp = "'Y-m-d H:i:s'";
var $nameQuote = '`';
function _init($parentDriver)
{
$parentDriver->hasTransactions = false;
#$parentDriver->_bindInputArray = false;
$parentDriver->hasInsertID = true;
$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
}
// dayFraction is a day in floating point
function OffsetDate($dayFraction, $date=false)
{
if (!$date) {
$date = $this->sysDate;
}
$fraction = $dayFraction * 24 * 3600;
return $date . ' + INTERVAL ' . $fraction . ' SECOND';
// return "from_unixtime(unix_timestamp($date)+$fraction)";
}
/**
* Get a list of indexes on the specified table.
*
* @param string $table The name of the table to get indexes for.
* @param bool $primary (Optional) Whether or not to include the primary key.
* @param bool $owner (Optional) Unused.
*
* @return array|bool An array of the indexes, or false if the query to get the indexes failed.
*/
function metaIndexes($table, $primary = false, $owner = false)
{
// save old fetch mode
global $ADODB_FETCH_MODE;
$false = false;
$save = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
if ($this->fetchMode !== FALSE) {
$savem = $this->setFetchMode(FALSE);
}
// get index details
$rs = $this->execute(sprintf('SHOW INDEXES FROM %s',$table));
// restore fetchmode
if (isset($savem)) {
$this->setFetchMode($savem);
}
$ADODB_FETCH_MODE = $save;
if (!is_object($rs)) {
return $false;
}
$indexes = array ();
// parse index data into array
while ($row = $rs->fetchRow()) {
if ($primary == FALSE AND $row[2] == 'PRIMARY') {
continue;
}
if (!isset($indexes[$row[2]])) {
$indexes[$row[2]] = array(
'unique' => ($row[1] == 0),
'columns' => array()
);
}
$indexes[$row[2]]['columns'][$row[3] - 1] = $row[4];
}
// sort columns by order in the index
foreach ( array_keys ($indexes) as $index )
{
ksort ($indexes[$index]['columns']);
}
return $indexes;
}
function Concat()
{
$s = '';
$arr = func_get_args();
// suggestion by andrew005#mnogo.ru
$s = implode(',', $arr);
if (strlen($s) > 0) {
return "CONCAT($s)";
}
return '';
}
function ServerInfo()
{
$arr['description'] = ADOConnection::GetOne('select version()');
$arr['version'] = ADOConnection::_findvers($arr['description']);
return $arr;
}
function MetaTables($ttype=false, $showSchema=false, $mask=false)
{
$save = $this->metaTablesSQL;
if ($showSchema && is_string($showSchema)) {
$this->metaTablesSQL .= $this->qstr($showSchema);
} else {
$this->metaTablesSQL .= 'schema()';
}
if ($mask) {
$mask = $this->qstr($mask);
$this->metaTablesSQL .= " like $mask";
}
$ret = ADOConnection::MetaTables($ttype, $showSchema);
$this->metaTablesSQL = $save;
return $ret;
}
/**
* @param bool $auto_commit
* @return void
*/
function SetAutoCommit($auto_commit)
{
$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT, $auto_commit);
}
function SetTransactionMode($transaction_mode)
{
$this->_transmode = $transaction_mode;
if (empty($transaction_mode)) {
$this->Execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
return;
}
if (!stristr($transaction_mode, 'isolation')) {
$transaction_mode = 'ISOLATION LEVEL ' . $transaction_mode;
}
$this->Execute('SET SESSION TRANSACTION ' . $transaction_mode);
}
function MetaColumns($table, $normalize=true)
{
$this->_findschema($table, $schema);
if ($schema) {
$dbName = $this->database;
$this->SelectDB($schema);
}
global $ADODB_FETCH_MODE;
$save = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
if ($this->fetchMode !== false) {
$savem = $this->SetFetchMode(false);
}
$rs = $this->Execute(sprintf($this->metaColumnsSQL, $table));
if ($schema) {
$this->SelectDB($dbName);
}
if (isset($savem)) {
$this->SetFetchMode($savem);
}
$ADODB_FETCH_MODE = $save;
if (!is_object($rs)) {
$false = false;
return $false;
}
$retarr = array();
while (!$rs->EOF){
$fld = new ADOFieldObject();
$fld->name = $rs->fields[0];
$type = $rs->fields[1];
// split type into type(length):
$fld->scale = null;
if (preg_match('/^(.+)\((\d+),(\d+)/', $type, $query_array)) {
$fld->type = $query_array[1];
$fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
$fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
} elseif (preg_match('/^(.+)\((\d+)/', $type, $query_array)) {
$fld->type = $query_array[1];
$fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
} elseif (preg_match('/^(enum)\((.*)\)$/i', $type, $query_array)) {
$fld->type = $query_array[1];
$arr = explode(',', $query_array[2]);
$fld->enums = $arr;
$zlen = max(array_map('strlen', $arr)) - 2; // PHP >= 4.0.6
$fld->max_length = ($zlen > 0) ? $zlen : 1;
} else {
$fld->type = $type;
$fld->max_length = -1;
}
$fld->not_null = ($rs->fields[2] != 'YES');
$fld->primary_key = ($rs->fields[3] == 'PRI');
$fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
$fld->binary = (strpos($type, 'blob') !== false);
$fld->unsigned = (strpos($type, 'unsigned') !== false);
if (!$fld->binary) {
$d = $rs->fields[4];
if ($d != '' && $d != 'NULL') {
$fld->has_default = true;
$fld->default_value = $d;
} else {
$fld->has_default = false;
}
}
if ($save == ADODB_FETCH_NUM) {
$retarr[] = $fld;
} else {
$retarr[strtoupper($fld->name)] = $fld;
}
$rs->MoveNext();
}
$rs->Close();
return $retarr;
}
// returns true or false
function SelectDB($dbName)
{
$this->database = $dbName;
$this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
$try = $this->Execute('use ' . $dbName);
return ($try !== false);
}
// parameters use PostgreSQL convention, not MySQL
function SelectLimit($sql, $nrows=-1, $offset=-1, $inputarr=false, $secs=0)
{
$nrows = (int) $nrows;
$offset = (int) $offset;
$offsetStr =($offset>=0) ? "$offset," : '';
// jason judge, see PHPLens Issue No: 9220
if ($nrows < 0) {
$nrows = '18446744073709551615';
}
if ($secs) {
$rs = $this->CacheExecute($secs, $sql . " LIMIT $offsetStr$nrows", $inputarr);
} else {
$rs = $this->Execute($sql . " LIMIT $offsetStr$nrows", $inputarr);
}
return $rs;
}
function SQLDate($fmt, $col=false)
{
if (!$col) {
$col = $this->sysTimeStamp;
}
$s = 'DATE_FORMAT(' . $col . ",'";
$concat = false;
$len = strlen($fmt);
for ($i=0; $i < $len; $i++) {
$ch = $fmt[$i];
switch($ch) {
default:
if ($ch == '\\') {
$i++;
$ch = substr($fmt, $i, 1);
}
// FALL THROUGH
case '-':
case '/':
$s .= $ch;
break;
case 'Y':
case 'y':
$s .= '%Y';
break;
case 'M':
$s .= '%b';
break;
case 'm':
$s .= '%m';
break;
case 'D':
case 'd':
$s .= '%d';
break;
case 'Q':
case 'q':
$s .= "'),Quarter($col)";
if ($len > $i+1) {
$s .= ",DATE_FORMAT($col,'";
} else {
$s .= ",('";
}
$concat = true;
break;
case 'H':
$s .= '%H';
break;
case 'h':
$s .= '%I';
break;
case 'i':
$s .= '%i';
break;
case 's':
$s .= '%s';
break;
case 'a':
case 'A':
$s .= '%p';
break;
case 'w':
$s .= '%w';
break;
case 'W':
$s .= '%U';
break;
case 'l':
$s .= '%W';
break;
}
}
$s .= "')";
if ($concat) {
$s = "CONCAT($s)";
}
return $s;
}
function GenID($seqname='adodbseq',$startID=1)
{
$getnext = sprintf($this->_genIDSQL,$seqname);
$holdtransOK = $this->_transOK; // save the current status
$rs = @$this->Execute($getnext);
if (!$rs) {
if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset
$this->Execute(sprintf($this->_genSeqSQL,$seqname));
$cnt = $this->GetOne(sprintf($this->_genSeqCountSQL,$seqname));
if (!$cnt) $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
$rs = $this->Execute($getnext);
}
if ($rs) {
$this->genID = $this->_connectionID->lastInsertId($seqname);
$rs->Close();
} else {
$this->genID = 0;
}
return $this->genID;
}
function createSequence($seqname='adodbseq',$startID=1)
{
if (empty($this->_genSeqSQL)) {
return false;
}
$ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname,$startID));
if (!$ok) {
return false;
}
return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
}
}
|