updated adodb package to work with php 7.1
[openemr.git] / vendor / adodb / adodb-php / drivers / adodb-pdo_mysql.inc.php
blob36d97b6ece6a570b4e1628063fcdf067e0faad0b
1 <?php
2 /*
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.
9 Set tabs to 8.
13 class ADODB_pdo_mysql extends ADODB_pdo {
15 var $metaTablesSQL = "SELECT
16 TABLE_NAME,
17 CASE WHEN TABLE_TYPE = 'VIEW' THEN 'V' ELSE 'T' END
18 FROM INFORMATION_SCHEMA.TABLES
19 WHERE TABLE_SCHEMA=";
20 var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`";
21 var $sysDate = 'CURDATE()';
22 var $sysTimeStamp = 'NOW()';
23 var $hasGenID = true;
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'";
27 var $nameQuote = '`';
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)
40 if (!$date) {
41 $date = $this->sysDate;
44 $fraction = $dayFraction * 24 * 3600;
45 return $date . ' + INTERVAL ' . $fraction . ' SECOND';
46 // return "from_unixtime(unix_timestamp($date)+$fraction)";
49 function Concat()
51 $s = '';
52 $arr = func_get_args();
54 // suggestion by andrew005#mnogo.ru
55 $s = implode(',', $arr);
56 if (strlen($s) > 0) {
57 return "CONCAT($s)";
59 return '';
62 function ServerInfo()
64 $arr['description'] = ADOConnection::GetOne('select version()');
65 $arr['version'] = ADOConnection::_findvers($arr['description']);
66 return $arr;
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);
74 } else {
75 $this->metaTablesSQL .= 'schema()';
78 if ($mask) {
79 $mask = $this->qstr($mask);
80 $this->metaTablesSQL .= " like $mask";
82 $ret = ADOConnection::MetaTables($ttype, $showSchema);
84 $this->metaTablesSQL = $save;
85 return $ret;
88 function SetTransactionMode($transaction_mode)
90 $this->_transmode = $transaction_mode;
91 if (empty($transaction_mode)) {
92 $this->Execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
93 return;
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);
104 if ($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));
117 if ($schema) {
118 $this->SelectDB($dbName);
121 if (isset($savem)) {
122 $this->SetFetchMode($savem);
124 $ADODB_FETCH_MODE = $save;
125 if (!is_object($rs)) {
126 $false = false;
127 return $false;
130 $retarr = array();
131 while (!$rs->EOF){
132 $fld = new ADOFieldObject();
133 $fld->name = $rs->fields[0];
134 $type = $rs->fields[1];
136 // split type into type(length):
137 $fld->scale = null;
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]);
148 $fld->enums = $arr;
149 $zlen = max(array_map('strlen', $arr)) - 2; // PHP >= 4.0.6
150 $fld->max_length = ($zlen > 0) ? $zlen : 1;
151 } else {
152 $fld->type = $type;
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);
161 if (!$fld->binary) {
162 $d = $rs->fields[4];
163 if ($d != '' && $d != 'NULL') {
164 $fld->has_default = true;
165 $fld->default_value = $d;
166 } else {
167 $fld->has_default = false;
171 if ($save == ADODB_FETCH_NUM) {
172 $retarr[] = $fld;
173 } else {
174 $retarr[strtoupper($fld->name)] = $fld;
176 $rs->MoveNext();
179 $rs->Close();
180 return $retarr;
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
197 if ($nrows < 0) {
198 $nrows = '18446744073709551615';
201 if ($secs) {
202 $rs = $this->CacheExecute($secs, $sql . " LIMIT $offsetStr$nrows", $inputarr);
203 } else {
204 $rs = $this->Execute($sql . " LIMIT $offsetStr$nrows", $inputarr);
206 return $rs;
209 function SQLDate($fmt, $col=false)
211 if (!$col) {
212 $col = $this->sysTimeStamp;
214 $s = 'DATE_FORMAT(' . $col . ",'";
215 $concat = false;
216 $len = strlen($fmt);
217 for ($i=0; $i < $len; $i++) {
218 $ch = $fmt[$i];
219 switch($ch) {
221 default:
222 if ($ch == '\\') {
223 $i++;
224 $ch = substr($fmt, $i, 1);
226 // FALL THROUGH
227 case '-':
228 case '/':
229 $s .= $ch;
230 break;
232 case 'Y':
233 case 'y':
234 $s .= '%Y';
235 break;
237 case 'M':
238 $s .= '%b';
239 break;
241 case 'm':
242 $s .= '%m';
243 break;
245 case 'D':
246 case 'd':
247 $s .= '%d';
248 break;
250 case 'Q':
251 case 'q':
252 $s .= "'),Quarter($col)";
254 if ($len > $i+1) {
255 $s .= ",DATE_FORMAT($col,'";
256 } else {
257 $s .= ",('";
259 $concat = true;
260 break;
262 case 'H':
263 $s .= '%H';
264 break;
266 case 'h':
267 $s .= '%I';
268 break;
270 case 'i':
271 $s .= '%i';
272 break;
274 case 's':
275 $s .= '%s';
276 break;
278 case 'a':
279 case 'A':
280 $s .= '%p';
281 break;
283 case 'w':
284 $s .= '%w';
285 break;
287 case 'W':
288 $s .= '%U';
289 break;
291 case 'l':
292 $s .= '%W';
293 break;
296 $s .= "')";
297 if ($concat) {
298 $s = "CONCAT($s)";
300 return $s;