3 @version v5.20.9 21-Dec-2016
4 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
5 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
6 Released under both BSD license and Lesser GPL library license.
7 Whenever there is any discrepancy between the two licenses,
8 the BSD license will take precedence.
13 class ADODB_pdo_mysql
extends ADODB_pdo
{
15 var $metaTablesSQL = "SELECT
17 CASE WHEN TABLE_TYPE = 'VIEW' THEN 'V' ELSE 'T' END
18 FROM INFORMATION_SCHEMA.TABLES
20 var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`";
21 var $sysDate = 'CURDATE()';
22 var $sysTimeStamp = 'NOW()';
24 var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
25 var $_dropSeqSQL = "drop table %s";
26 var $fmtTimeStamp = "'Y-m-d, H:i:s'";
29 function _init($parentDriver)
31 $parentDriver->hasTransactions
= false;
32 #$parentDriver->_bindInputArray = false;
33 $parentDriver->hasInsertID
= true;
34 $parentDriver->_connectionID
->setAttribute(PDO
::MYSQL_ATTR_USE_BUFFERED_QUERY
, true);
37 // dayFraction is a day in floating point
38 function OffsetDate($dayFraction, $date=false)
41 $date = $this->sysDate
;
44 $fraction = $dayFraction * 24 * 3600;
45 return $date . ' + INTERVAL ' . $fraction . ' SECOND';
46 // return "from_unixtime(unix_timestamp($date)+$fraction)";
52 $arr = func_get_args();
54 // suggestion by andrew005#mnogo.ru
55 $s = implode(',', $arr);
64 $arr['description'] = ADOConnection
::GetOne('select version()');
65 $arr['version'] = ADOConnection
::_findvers($arr['description']);
69 function MetaTables($ttype=false, $showSchema=false, $mask=false)
71 $save = $this->metaTablesSQL
;
72 if ($showSchema && is_string($showSchema)) {
73 $this->metaTablesSQL
.= $this->qstr($showSchema);
75 $this->metaTablesSQL
.= 'schema()';
79 $mask = $this->qstr($mask);
80 $this->metaTablesSQL
.= " like $mask";
82 $ret = ADOConnection
::MetaTables($ttype, $showSchema);
84 $this->metaTablesSQL
= $save;
88 function SetTransactionMode($transaction_mode)
90 $this->_transmode
= $transaction_mode;
91 if (empty($transaction_mode)) {
92 $this->Execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
95 if (!stristr($transaction_mode, 'isolation')) {
96 $transaction_mode = 'ISOLATION LEVEL ' . $transaction_mode;
98 $this->Execute('SET SESSION TRANSACTION ' . $transaction_mode);
101 function MetaColumns($table, $normalize=true)
103 $this->_findschema($table, $schema);
105 $dbName = $this->database
;
106 $this->SelectDB($schema);
108 global $ADODB_FETCH_MODE;
109 $save = $ADODB_FETCH_MODE;
110 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
112 if ($this->fetchMode
!== false) {
113 $savem = $this->SetFetchMode(false);
115 $rs = $this->Execute(sprintf($this->metaColumnsSQL
, $table));
118 $this->SelectDB($dbName);
122 $this->SetFetchMode($savem);
124 $ADODB_FETCH_MODE = $save;
125 if (!is_object($rs)) {
132 $fld = new ADOFieldObject();
133 $fld->name
= $rs->fields
[0];
134 $type = $rs->fields
[1];
136 // split type into type(length):
138 if (preg_match('/^(.+)\((\d+),(\d+)/', $type, $query_array)) {
139 $fld->type
= $query_array[1];
140 $fld->max_length
= is_numeric($query_array[2]) ?
$query_array[2] : -1;
141 $fld->scale
= is_numeric($query_array[3]) ?
$query_array[3] : -1;
142 } elseif (preg_match('/^(.+)\((\d+)/', $type, $query_array)) {
143 $fld->type
= $query_array[1];
144 $fld->max_length
= is_numeric($query_array[2]) ?
$query_array[2] : -1;
145 } elseif (preg_match('/^(enum)\((.*)\)$/i', $type, $query_array)) {
146 $fld->type
= $query_array[1];
147 $arr = explode(',', $query_array[2]);
149 $zlen = max(array_map('strlen', $arr)) - 2; // PHP >= 4.0.6
150 $fld->max_length
= ($zlen > 0) ?
$zlen : 1;
153 $fld->max_length
= -1;
155 $fld->not_null
= ($rs->fields
[2] != 'YES');
156 $fld->primary_key
= ($rs->fields
[3] == 'PRI');
157 $fld->auto_increment
= (strpos($rs->fields
[5], 'auto_increment') !== false);
158 $fld->binary
= (strpos($type, 'blob') !== false);
159 $fld->unsigned
= (strpos($type, 'unsigned') !== false);
163 if ($d != '' && $d != 'NULL') {
164 $fld->has_default
= true;
165 $fld->default_value
= $d;
167 $fld->has_default
= false;
171 if ($save == ADODB_FETCH_NUM
) {
174 $retarr[strtoupper($fld->name
)] = $fld;
183 // returns true or false
184 function SelectDB($dbName)
186 $this->database
= $dbName;
187 $this->databaseName
= $dbName; # obsolete, retained for compat with older adodb versions
188 $try = $this->Execute('use ' . $dbName);
189 return ($try !== false);
192 // parameters use PostgreSQL convention, not MySQL
193 function SelectLimit($sql, $nrows=-1, $offset=-1, $inputarr=false, $secs=0)
195 $offsetStr =($offset>=0) ?
"$offset," : '';
196 // jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
198 $nrows = '18446744073709551615';
202 $rs = $this->CacheExecute($secs, $sql . " LIMIT $offsetStr$nrows", $inputarr);
204 $rs = $this->Execute($sql . " LIMIT $offsetStr$nrows", $inputarr);
209 function SQLDate($fmt, $col=false)
212 $col = $this->sysTimeStamp
;
214 $s = 'DATE_FORMAT(' . $col . ",'";
217 for ($i=0; $i < $len; $i++
) {
224 $ch = substr($fmt, $i, 1);
252 $s .= "'),Quarter($col)";
255 $s .= ",DATE_FORMAT($col,'";