[!!!][TASK] Doctrine: Remove ext:dbal
[Packages/TYPO3.CMS.git] / typo3 / sysext / adodb / adodb / drivers / adodb-pdo_mysql.inc.php
1 <?php
2 /*
3 @version v5.20.3 01-Jan-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.
10
11 */
12
13 class ADODB_pdo_mysql extends ADODB_pdo {
14
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 = '`';
28
29 function _init($parentDriver)
30 {
31 $parentDriver->hasTransactions = false;
32 #$parentDriver->_bindInputArray = false;
33 $parentDriver->hasInsertID = true;
34 $parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
35 }
36
37 // dayFraction is a day in floating point
38 function OffsetDate($dayFraction, $date=false)
39 {
40 if (!$date) {
41 $date = $this->sysDate;
42 }
43
44 $fraction = $dayFraction * 24 * 3600;
45 return $date . ' + INTERVAL ' . $fraction . ' SECOND';
46 // return "from_unixtime(unix_timestamp($date)+$fraction)";
47 }
48
49 function Concat()
50 {
51 $s = '';
52 $arr = func_get_args();
53
54 // suggestion by andrew005#mnogo.ru
55 $s = implode(',', $arr);
56 if (strlen($s) > 0) {
57 return "CONCAT($s)";
58 }
59 return '';
60 }
61
62 function ServerInfo()
63 {
64 $arr['description'] = ADOConnection::GetOne('select version()');
65 $arr['version'] = ADOConnection::_findvers($arr['description']);
66 return $arr;
67 }
68
69 function MetaTables($ttype=false, $showSchema=false, $mask=false)
70 {
71 $save = $this->metaTablesSQL;
72 if ($showSchema && is_string($showSchema)) {
73 $this->metaTablesSQL .= " from $showSchema";
74 }
75
76 if ($mask) {
77 $mask = $this->qstr($mask);
78 $this->metaTablesSQL .= " like $mask";
79 }
80 $ret = ADOConnection::MetaTables($ttype, $showSchema);
81
82 $this->metaTablesSQL = $save;
83 return $ret;
84 }
85
86 function SetTransactionMode($transaction_mode)
87 {
88 $this->_transmode = $transaction_mode;
89 if (empty($transaction_mode)) {
90 $this->Execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
91 return;
92 }
93 if (!stristr($transaction_mode, 'isolation')) {
94 $transaction_mode = 'ISOLATION LEVEL ' . $transaction_mode;
95 }
96 $this->Execute('SET SESSION TRANSACTION ' . $transaction_mode);
97 }
98
99 function MetaColumns($table, $normalize=true)
100 {
101 $this->_findschema($table, $schema);
102 if ($schema) {
103 $dbName = $this->database;
104 $this->SelectDB($schema);
105 }
106 global $ADODB_FETCH_MODE;
107 $save = $ADODB_FETCH_MODE;
108 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
109
110 if ($this->fetchMode !== false) {
111 $savem = $this->SetFetchMode(false);
112 }
113 $rs = $this->Execute(sprintf($this->metaColumnsSQL, $table));
114
115 if ($schema) {
116 $this->SelectDB($dbName);
117 }
118
119 if (isset($savem)) {
120 $this->SetFetchMode($savem);
121 }
122 $ADODB_FETCH_MODE = $save;
123 if (!is_object($rs)) {
124 $false = false;
125 return $false;
126 }
127
128 $retarr = array();
129 while (!$rs->EOF){
130 $fld = new ADOFieldObject();
131 $fld->name = $rs->fields[0];
132 $type = $rs->fields[1];
133
134 // split type into type(length):
135 $fld->scale = null;
136 if (preg_match('/^(.+)\((\d+),(\d+)/', $type, $query_array)) {
137 $fld->type = $query_array[1];
138 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
139 $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
140 } elseif (preg_match('/^(.+)\((\d+)/', $type, $query_array)) {
141 $fld->type = $query_array[1];
142 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
143 } elseif (preg_match('/^(enum)\((.*)\)$/i', $type, $query_array)) {
144 $fld->type = $query_array[1];
145 $arr = explode(',', $query_array[2]);
146 $fld->enums = $arr;
147 $zlen = max(array_map('strlen', $arr)) - 2; // PHP >= 4.0.6
148 $fld->max_length = ($zlen > 0) ? $zlen : 1;
149 } else {
150 $fld->type = $type;
151 $fld->max_length = -1;
152 }
153 $fld->not_null = ($rs->fields[2] != 'YES');
154 $fld->primary_key = ($rs->fields[3] == 'PRI');
155 $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
156 $fld->binary = (strpos($type, 'blob') !== false);
157 $fld->unsigned = (strpos($type, 'unsigned') !== false);
158
159 if (!$fld->binary) {
160 $d = $rs->fields[4];
161 if ($d != '' && $d != 'NULL') {
162 $fld->has_default = true;
163 $fld->default_value = $d;
164 } else {
165 $fld->has_default = false;
166 }
167 }
168
169 if ($save == ADODB_FETCH_NUM) {
170 $retarr[] = $fld;
171 } else {
172 $retarr[strtoupper($fld->name)] = $fld;
173 }
174 $rs->MoveNext();
175 }
176
177 $rs->Close();
178 return $retarr;
179 }
180
181 // returns true or false
182 function SelectDB($dbName)
183 {
184 $this->database = $dbName;
185 $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
186 $try = $this->Execute('use ' . $dbName);
187 return ($try !== false);
188 }
189
190 // parameters use PostgreSQL convention, not MySQL
191 function SelectLimit($sql, $nrows=-1, $offset=-1, $inputarr=false, $secs=0)
192 {
193 $offsetStr =($offset>=0) ? "$offset," : '';
194 // jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
195 if ($nrows < 0) {
196 $nrows = '18446744073709551615';
197 }
198
199 if ($secs) {
200 $rs = $this->CacheExecute($secs, $sql . " LIMIT $offsetStr$nrows", $inputarr);
201 } else {
202 $rs = $this->Execute($sql . " LIMIT $offsetStr$nrows", $inputarr);
203 }
204 return $rs;
205 }
206
207 function SQLDate($fmt, $col=false)
208 {
209 if (!$col) {
210 $col = $this->sysTimeStamp;
211 }
212 $s = 'DATE_FORMAT(' . $col . ",'";
213 $concat = false;
214 $len = strlen($fmt);
215 for ($i=0; $i < $len; $i++) {
216 $ch = $fmt[$i];
217 switch($ch) {
218
219 default:
220 if ($ch == '\\') {
221 $i++;
222 $ch = substr($fmt, $i, 1);
223 }
224 // FALL THROUGH
225 case '-':
226 case '/':
227 $s .= $ch;
228 break;
229
230 case 'Y':
231 case 'y':
232 $s .= '%Y';
233 break;
234
235 case 'M':
236 $s .= '%b';
237 break;
238
239 case 'm':
240 $s .= '%m';
241 break;
242
243 case 'D':
244 case 'd':
245 $s .= '%d';
246 break;
247
248 case 'Q':
249 case 'q':
250 $s .= "'),Quarter($col)";
251
252 if ($len > $i+1) {
253 $s .= ",DATE_FORMAT($col,'";
254 } else {
255 $s .= ",('";
256 }
257 $concat = true;
258 break;
259
260 case 'H':
261 $s .= '%H';
262 break;
263
264 case 'h':
265 $s .= '%I';
266 break;
267
268 case 'i':
269 $s .= '%i';
270 break;
271
272 case 's':
273 $s .= '%s';
274 break;
275
276 case 'a':
277 case 'A':
278 $s .= '%p';
279 break;
280
281 case 'w':
282 $s .= '%w';
283 break;
284
285 case 'W':
286 $s .= '%U';
287 break;
288
289 case 'l':
290 $s .= '%W';
291 break;
292 }
293 }
294 $s .= "')";
295 if ($concat) {
296 $s = "CONCAT($s)";
297 }
298 return $s;
299 }
300 }