Fixed bug #12800: Add functionality to work with caching framework and DBAL
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_sqlparser.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2004-2009 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * TYPO3 SQL parser
29 *
30 * $Id$
31 *
32 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 107: class t3lib_sqlparser
40 *
41 * SECTION: SQL Parsing, full queries
42 * 129: function parseSQL($parseString)
43 * 192: function parseSELECT($parseString)
44 * 261: function parseUPDATE($parseString)
45 * 315: function parseINSERT($parseString)
46 * 375: function parseDELETE($parseString)
47 * 413: function parseEXPLAIN($parseString)
48 * 435: function parseCREATETABLE($parseString)
49 * 514: function parseALTERTABLE($parseString)
50 * 583: function parseDROPTABLE($parseString)
51 * 616: function parseCREATEDATABASE($parseString)
52 *
53 * SECTION: SQL Parsing, helper functions for parts of queries
54 * 670: function parseFieldList(&$parseString, $stopRegex='')
55 * 791: function parseFromTables(&$parseString, $stopRegex='')
56 * 882: function parseWhereClause(&$parseString, $stopRegex='')
57 * 990: function parseFieldDef(&$parseString, $stopRegex='')
58 *
59 * SECTION: Parsing: Helper functions
60 * 1053: function nextPart(&$parseString,$regex,$trimAll=FALSE)
61 * 1068: function getValue(&$parseString,$comparator='')
62 * 1127: function getValueInQuotes(&$parseString,$quote)
63 * 1153: function parseStripslashes($str)
64 * 1167: function compileAddslashes($str)
65 * 1182: function parseError($msg,$restQuery)
66 * 1196: function trimSQL($str)
67 *
68 * SECTION: Compiling queries
69 * 1225: function compileSQL($components)
70 * 1263: function compileSELECT($components)
71 * 1294: function compileUPDATE($components)
72 * 1322: function compileINSERT($components)
73 * 1362: function compileDELETE($components)
74 * 1382: function compileCREATETABLE($components)
75 * 1415: function compileALTERTABLE($components)
76 *
77 * SECTION: Compiling queries, helper functions for parts of queries
78 * 1468: function compileFieldList($selectFields)
79 * 1510: function compileFromTables($tablesArray)
80 * 1551: function compileWhereClause($clauseArray)
81 * 1605: function compileFieldCfg($fieldCfg)
82 *
83 * SECTION: Debugging
84 * 1654: function debug_parseSQLpart($part,$str)
85 * 1679: function debug_parseSQLpartCompare($str,$newStr,$caseInsensitive=FALSE)
86 * 1712: function debug_testSQL($SQLquery)
87 *
88 * TOTAL FUNCTIONS: 35
89 * (This index is automatically created/updated by the extension "extdeveval")
90 *
91 */
92
93
94
95
96
97
98
99
100 /**
101 * TYPO3 SQL parser class.
102 *
103 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
104 * @package TYPO3
105 * @subpackage t3lib
106 */
107 class t3lib_sqlparser {
108
109 // Parser:
110 var $parse_error = ''; // Parsing error string
111 var $lastStopKeyWord = ''; // Last stop keyword used.
112
113
114
115
116 /*************************************
117 *
118 * SQL Parsing, full queries
119 *
120 **************************************/
121
122 /**
123 * Parses any single SQL query
124 *
125 * @param string SQL query
126 * @return array Result array with all the parts in - or error message string
127 * @see compileSQL(), debug_testSQL()
128 */
129 public function parseSQL($parseString) {
130 // Prepare variables:
131 $parseString = $this->trimSQL($parseString);
132 $this->parse_error = '';
133 $result = array();
134
135 // Finding starting keyword of string:
136 $_parseString = $parseString; // Protecting original string...
137 $keyword = $this->nextPart($_parseString, '^(SELECT|UPDATE|INSERT[[:space:]]+INTO|DELETE[[:space:]]+FROM|EXPLAIN|DROP[[:space:]]+TABLE|CREATE[[:space:]]+TABLE|CREATE[[:space:]]+DATABASE|ALTER[[:space:]]+TABLE)[[:space:]]+');
138 $keyword = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$keyword));
139
140 switch($keyword) {
141 case 'SELECT':
142 // Parsing SELECT query:
143 $result = $this->parseSELECT($parseString);
144 break;
145 case 'UPDATE':
146 // Parsing UPDATE query:
147 $result = $this->parseUPDATE($parseString);
148 break;
149 case 'INSERTINTO':
150 // Parsing INSERT query:
151 $result = $this->parseINSERT($parseString);
152 break;
153 case 'DELETEFROM':
154 // Parsing DELETE query:
155 $result = $this->parseDELETE($parseString);
156 break;
157 case 'EXPLAIN':
158 // Parsing EXPLAIN SELECT query:
159 $result = $this->parseEXPLAIN($parseString);
160 break;
161 case 'DROPTABLE':
162 // Parsing DROP TABLE query:
163 $result = $this->parseDROPTABLE($parseString);
164 break;
165 case 'ALTERTABLE':
166 // Parsing ALTER TABLE query:
167 $result = $this->parseALTERTABLE($parseString);
168 break;
169 case 'CREATETABLE':
170 // Parsing CREATE TABLE query:
171 $result = $this->parseCREATETABLE($parseString);
172 break;
173 case 'CREATEDATABASE':
174 // Parsing CREATE DATABASE query:
175 $result = $this->parseCREATEDATABASE($parseString);
176 break;
177 default:
178 $result = $this->parseError('"'.$keyword.'" is not a keyword',$parseString);
179 break;
180 }
181
182 return $result;
183 }
184
185 /**
186 * Parsing SELECT query
187 *
188 * @param string SQL string with SELECT query to parse
189 * @return mixed Returns array with components of SELECT query on success, otherwise an error message string.
190 * @see compileSELECT()
191 */
192 protected function parseSELECT($parseString) {
193
194 // Removing SELECT:
195 $parseString = $this->trimSQL($parseString);
196 $parseString = ltrim(substr($parseString,6));
197
198 // Init output variable:
199 $result = array();
200 $result['type'] = 'SELECT';
201
202 // Looking for STRAIGHT_JOIN keyword:
203 $result['STRAIGHT_JOIN'] = $this->nextPart($parseString, '^(STRAIGHT_JOIN)[[:space:]]+');
204
205 // Select fields:
206 $result['SELECT'] = $this->parseFieldList($parseString, '^(FROM)[[:space:]]+');
207 if ($this->parse_error) { return $this->parse_error; }
208
209 // Continue if string is not ended:
210 if ($parseString) {
211
212 // Get table list:
213 $result['FROM'] = $this->parseFromTables($parseString, '^(WHERE)[[:space:]]+');
214 if ($this->parse_error) { return $this->parse_error; }
215
216 // If there are more than just the tables (a WHERE clause that would be...)
217 if ($parseString) {
218
219 // Get WHERE clause:
220 $result['WHERE'] = $this->parseWhereClause($parseString, '^(GROUP[[:space:]]+BY|ORDER[[:space:]]+BY|LIMIT)[[:space:]]+');
221 if ($this->parse_error) { return $this->parse_error; }
222
223 // If the WHERE clause parsing was stopped by GROUP BY, ORDER BY or LIMIT, then proceed with parsing:
224 if ($this->lastStopKeyWord) {
225
226 // GROUP BY parsing:
227 if ($this->lastStopKeyWord == 'GROUPBY') {
228 $result['GROUPBY'] = $this->parseFieldList($parseString, '^(ORDER[[:space:]]+BY|LIMIT)[[:space:]]+');
229 if ($this->parse_error) { return $this->parse_error; }
230 }
231
232 // ORDER BY parsing:
233 if ($this->lastStopKeyWord == 'ORDERBY') {
234 $result['ORDERBY'] = $this->parseFieldList($parseString, '^(LIMIT)[[:space:]]+');
235 if ($this->parse_error) { return $this->parse_error; }
236 }
237
238 // LIMIT parsing:
239 if ($this->lastStopKeyWord == 'LIMIT') {
240 if (preg_match('/^([0-9]+|[0-9]+[[:space:]]*,[[:space:]]*[0-9]+)$/',trim($parseString))) {
241 $result['LIMIT'] = $parseString;
242 } else {
243 return $this->parseError('No value for limit!',$parseString);
244 }
245 }
246 }
247 }
248 } else return $this->parseError('No table to select from!',$parseString);
249
250 // Store current parseString in the result array for possible further processing (e.g., subquery support by DBAL)
251 $result['parseString'] = $parseString;
252
253 // Return result:
254 return $result;
255 }
256
257 /**
258 * Parsing UPDATE query
259 *
260 * @param string SQL string with UPDATE query to parse
261 * @return mixed Returns array with components of UPDATE query on success, otherwise an error message string.
262 * @see compileUPDATE()
263 */
264 protected function parseUPDATE($parseString) {
265
266 // Removing UPDATE
267 $parseString = $this->trimSQL($parseString);
268 $parseString = ltrim(substr($parseString,6));
269
270 // Init output variable:
271 $result = array();
272 $result['type'] = 'UPDATE';
273
274 // Get table:
275 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
276
277 // Continue if string is not ended:
278 if ($result['TABLE']) {
279 if ($parseString && $this->nextPart($parseString, '^(SET)[[:space:]]+')) {
280
281 $comma = TRUE;
282
283 // Get field/value pairs:
284 while($comma) {
285 if ($fieldName = $this->nextPart($parseString,'^([[:alnum:]_]+)[[:space:]]*=')) {
286 $this->nextPart($parseString,'^(=)'); // Strip of "=" sign.
287 $value = $this->getValue($parseString);
288 $result['FIELDS'][$fieldName] = $value;
289 } else return $this->parseError('No fieldname found',$parseString);
290
291 $comma = $this->nextPart($parseString,'^(,)');
292 }
293
294 // WHERE
295 if ($this->nextPart($parseString,'^(WHERE)')) {
296 $result['WHERE'] = $this->parseWhereClause($parseString);
297 if ($this->parse_error) { return $this->parse_error; }
298 }
299 } else return $this->parseError('Query missing SET...',$parseString);
300 } else return $this->parseError('No table found!',$parseString);
301
302 // Should be no more content now:
303 if ($parseString) {
304 return $this->parseError('Still content in clause after parsing!',$parseString);
305 }
306
307 // Return result:
308 return $result;
309 }
310
311 /**
312 * Parsing INSERT query
313 *
314 * @param string SQL string with INSERT query to parse
315 * @return mixed Returns array with components of INSERT query on success, otherwise an error message string.
316 * @see compileINSERT()
317 */
318 protected function parseINSERT($parseString) {
319
320 // Removing INSERT
321 $parseString = $this->trimSQL($parseString);
322 $parseString = ltrim(substr(ltrim(substr($parseString,6)),4));
323
324 // Init output variable:
325 $result = array();
326 $result['type'] = 'INSERT';
327
328 // Get table:
329 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)([[:space:]]+|\()');
330
331 if ($result['TABLE']) {
332
333 if ($this->nextPart($parseString,'^(VALUES)[[:space:]]+')) { // In this case there are no field names mentioned in the SQL!
334 // Get values/fieldnames (depending...)
335 $result['VALUES_ONLY'] = $this->getValue($parseString,'IN');
336 if ($this->parse_error) { return $this->parse_error; }
337 } else { // There are apparently fieldnames listed:
338 $fieldNames = $this->getValue($parseString,'_LIST');
339 if ($this->parse_error) { return $this->parse_error; }
340
341 if ($this->nextPart($parseString,'^(VALUES)[[:space:]]+')) { // "VALUES" keyword binds the fieldnames to values:
342
343 $values = $this->getValue($parseString,'IN'); // Using the "getValue" function to get the field list...
344 if ($this->parse_error) { return $this->parse_error; }
345
346 foreach($fieldNames as $k => $fN) {
347 if (preg_match('/^[[:alnum:]_]+$/',$fN)) {
348 if (isset($values[$k])) {
349 if (!isset($result['FIELDS'][$fN])) {
350 $result['FIELDS'][$fN] = $values[$k];
351 } else return $this->parseError('Fieldname ("'.$fN.'") already found in list!',$parseString);
352 } else return $this->parseError('No value set!',$parseString);
353 } else return $this->parseError('Invalid fieldname ("'.$fN.'")',$parseString);
354 }
355 if (isset($values[$k+1])) {
356 return $this->parseError('Too many values in list!',$parseString);
357 }
358 } else return $this->parseError('VALUES keyword expected',$parseString);
359 }
360 } else return $this->parseError('No table found!',$parseString);
361
362 // Should be no more content now:
363 if ($parseString) {
364 return $this->parseError('Still content after parsing!',$parseString);
365 }
366
367 // Return result
368 return $result;
369 }
370
371 /**
372 * Parsing DELETE query
373 *
374 * @param string SQL string with DELETE query to parse
375 * @return mixed Returns array with components of DELETE query on success, otherwise an error message string.
376 * @see compileDELETE()
377 */
378 protected function parseDELETE($parseString) {
379
380 // Removing DELETE
381 $parseString = $this->trimSQL($parseString);
382 $parseString = ltrim(substr(ltrim(substr($parseString,6)),4));
383
384 // Init output variable:
385 $result = array();
386 $result['type'] = 'DELETE';
387
388 // Get table:
389 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
390
391 if ($result['TABLE']) {
392
393 // WHERE
394 if ($this->nextPart($parseString,'^(WHERE)')) {
395 $result['WHERE'] = $this->parseWhereClause($parseString);
396 if ($this->parse_error) { return $this->parse_error; }
397 }
398 } else return $this->parseError('No table found!',$parseString);
399
400 // Should be no more content now:
401 if ($parseString) {
402 return $this->parseError('Still content in clause after parsing!',$parseString);
403 }
404
405 // Return result:
406 return $result;
407 }
408
409 /**
410 * Parsing EXPLAIN query
411 *
412 * @param string SQL string with EXPLAIN query to parse
413 * @return mixed Returns array with components of EXPLAIN query on success, otherwise an error message string.
414 * @see parseSELECT()
415 */
416 protected function parseEXPLAIN($parseString) {
417
418 // Removing EXPLAIN
419 $parseString = $this->trimSQL($parseString);
420 $parseString = ltrim(substr($parseString,6));
421
422 // Init output variable:
423 $result = $this->parseSELECT($parseString);
424 if (is_array($result)) {
425 $result['type'] = 'EXPLAIN';
426 }
427
428 return $result;
429 }
430
431 /**
432 * Parsing CREATE TABLE query
433 *
434 * @param string SQL string starting with CREATE TABLE
435 * @return mixed Returns array with components of CREATE TABLE query on success, otherwise an error message string.
436 * @see compileCREATETABLE()
437 */
438 protected function parseCREATETABLE($parseString) {
439
440 // Removing CREATE TABLE
441 $parseString = $this->trimSQL($parseString);
442 $parseString = ltrim(substr(ltrim(substr($parseString,6)),5));
443
444 // Init output variable:
445 $result = array();
446 $result['type'] = 'CREATETABLE';
447
448 // Get table:
449 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]*\(',TRUE);
450
451 if ($result['TABLE']) {
452
453 // While the parseString is not yet empty:
454 while(strlen($parseString)>0) {
455 if ($key = $this->nextPart($parseString, '^(KEY|PRIMARY KEY|UNIQUE KEY|UNIQUE)([[:space:]]+|\()')) { // Getting key
456 $key = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$key));
457
458 switch($key) {
459 case 'PRIMARYKEY':
460 $result['KEYS']['PRIMARYKEY'] = $this->getValue($parseString,'_LIST');
461 if ($this->parse_error) { return $this->parse_error; }
462 break;
463 case 'UNIQUE':
464 case 'UNIQUEKEY':
465 if ($keyName = $this->nextPart($parseString, '^([[:alnum:]_]+)([[:space:]]+|\()')) {
466 $result['KEYS']['UNIQUE'] = array($keyName => $this->getValue($parseString,'_LIST'));
467 if ($this->parse_error) { return $this->parse_error; }
468 } else return $this->parseError('No keyname found',$parseString);
469 break;
470 case 'KEY':
471 if ($keyName = $this->nextPart($parseString, '^([[:alnum:]_]+)([[:space:]]+|\()')) {
472 $result['KEYS'][$keyName] = $this->getValue($parseString, '_LIST', 'INDEX');
473 if ($this->parse_error) { return $this->parse_error; }
474 } else return $this->parseError('No keyname found',$parseString);
475 break;
476 }
477 } elseif ($fieldName = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+')) { // Getting field:
478 $result['FIELDS'][$fieldName]['definition'] = $this->parseFieldDef($parseString);
479 if ($this->parse_error) { return $this->parse_error; }
480 }
481
482 // Finding delimiter:
483 $delim = $this->nextPart($parseString, '^(,|\))');
484 if (!$delim) {
485 return $this->parseError('No delimiter found',$parseString);
486 } elseif ($delim==')') {
487 break;
488 }
489 }
490
491 // Finding what is after the table definition - table type in MySQL
492 if ($delim==')') {
493 if ($this->nextPart($parseString, '^((ENGINE|TYPE)[[:space:]]*=)')) {
494 $result['tableType'] = $parseString;
495 $parseString = '';
496 }
497 } else return $this->parseError('No fieldname found!',$parseString);
498
499 // Getting table type
500 } else return $this->parseError('No table found!',$parseString);
501
502 // Should be no more content now:
503 if ($parseString) {
504 return $this->parseError('Still content in clause after parsing!',$parseString);
505 }
506
507 return $result;
508 }
509
510 /**
511 * Parsing ALTER TABLE query
512 *
513 * @param string SQL string starting with ALTER TABLE
514 * @return mixed Returns array with components of ALTER TABLE query on success, otherwise an error message string.
515 * @see compileALTERTABLE()
516 */
517 protected function parseALTERTABLE($parseString) {
518
519 // Removing ALTER TABLE
520 $parseString = $this->trimSQL($parseString);
521 $parseString = ltrim(substr(ltrim(substr($parseString,5)),5));
522
523 // Init output variable:
524 $result = array();
525 $result['type'] = 'ALTERTABLE';
526
527 // Get table:
528 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
529
530 if ($result['TABLE']) {
531 if ($result['action'] = $this->nextPart($parseString, '^(CHANGE|DROP[[:space:]]+KEY|DROP[[:space:]]+PRIMARY[[:space:]]+KEY|ADD[[:space:]]+KEY|ADD[[:space:]]+PRIMARY[[:space:]]+KEY|DROP|ADD|RENAME)([[:space:]]+|\()')) {
532 $actionKey = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$result['action']));
533
534 // Getting field:
535 if (t3lib_div::inList('ADDPRIMARYKEY,DROPPRIMARYKEY',$actionKey) || $fieldKey = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+')) {
536
537 switch($actionKey) {
538 case 'ADD':
539 $result['FIELD'] = $fieldKey;
540 $result['definition'] = $this->parseFieldDef($parseString);
541 if ($this->parse_error) { return $this->parse_error; }
542 break;
543 case 'DROP':
544 case 'RENAME':
545 $result['FIELD'] = $fieldKey;
546 break;
547 case 'CHANGE':
548 $result['FIELD'] = $fieldKey;
549 if ($result['newField'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+')) {
550 $result['definition'] = $this->parseFieldDef($parseString);
551 if ($this->parse_error) { return $this->parse_error; }
552 } else return $this->parseError('No NEW field name found',$parseString);
553 break;
554
555 case 'ADDKEY':
556 case 'ADDPRIMARYKEY':
557 $result['KEY'] = $fieldKey;
558 $result['fields'] = $this->getValue($parseString,'_LIST');
559 if ($this->parse_error) { return $this->parse_error; }
560 break;
561 case 'DROPKEY':
562 $result['KEY'] = $fieldKey;
563 break;
564 case 'DROPPRIMARYKEY':
565 // ??? todo!
566 break;
567 }
568 } else return $this->parseError('No field name found',$parseString);
569 } else return $this->parseError('No action CHANGE, DROP or ADD found!',$parseString);
570 } else return $this->parseError('No table found!',$parseString);
571
572 // Should be no more content now:
573 if ($parseString) {
574 return $this->parseError('Still content in clause after parsing!',$parseString);
575 }
576
577 return $result;
578 }
579
580 /**
581 * Parsing DROP TABLE query
582 *
583 * @param string SQL string starting with DROP TABLE
584 * @return mixed Returns array with components of DROP TABLE query on success, otherwise an error message string.
585 */
586 protected function parseDROPTABLE($parseString) {
587
588 // Removing DROP TABLE
589 $parseString = $this->trimSQL($parseString);
590 $parseString = ltrim(substr(ltrim(substr($parseString,4)),5));
591
592 // Init output variable:
593 $result = array();
594 $result['type'] = 'DROPTABLE';
595
596 // IF EXISTS
597 $result['ifExists'] = $this->nextPart($parseString, '^(IF[[:space:]]+EXISTS[[:space:]]+)');
598
599 // Get table:
600 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
601
602 if ($result['TABLE']) {
603
604 // Should be no more content now:
605 if ($parseString) {
606 return $this->parseError('Still content in clause after parsing!',$parseString);
607 }
608
609 return $result;
610 } else return $this->parseError('No table found!',$parseString);
611 }
612
613 /**
614 * Parsing CREATE DATABASE query
615 *
616 * @param string SQL string starting with CREATE DATABASE
617 * @return mixed Returns array with components of CREATE DATABASE query on success, otherwise an error message string.
618 */
619 protected function parseCREATEDATABASE($parseString) {
620
621 // Removing CREATE DATABASE
622 $parseString = $this->trimSQL($parseString);
623 $parseString = ltrim(substr(ltrim(substr($parseString,6)),8));
624
625 // Init output variable:
626 $result = array();
627 $result['type'] = 'CREATEDATABASE';
628
629 // Get table:
630 $result['DATABASE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
631
632 if ($result['DATABASE']) {
633
634 // Should be no more content now:
635 if ($parseString) {
636 return $this->parseError('Still content in clause after parsing!',$parseString);
637 }
638
639 return $result;
640 } else return $this->parseError('No database found!',$parseString);
641 }
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657 /**************************************
658 *
659 * SQL Parsing, helper functions for parts of queries
660 *
661 **************************************/
662
663 /**
664 * Parsing the fields in the "SELECT [$selectFields] FROM" part of a query into an array.
665 * The output from this function can be compiled back into a field list with ->compileFieldList()
666 * Will detect the keywords "DESC" and "ASC" after the table name; thus is can be used for parsing the more simply ORDER BY and GROUP BY field lists as well!
667 *
668 * @param string The string with fieldnames, eg. "title, uid AS myUid, max(tstamp), count(*)" etc. NOTICE: passed by reference!
669 * @param string Regular expressing to STOP parsing, eg. '^(FROM)([[:space:]]*)'
670 * @return array If successful parsing, returns an array, otherwise an error string.
671 * @see compileFieldList()
672 */
673 public function parseFieldList(&$parseString, $stopRegex = '') {
674
675 $stack = array(); // Contains the parsed content
676
677 if(strlen($parseString)==0) return $stack; // FIXME - should never happen, why does it?
678
679 $pnt = 0; // Pointer to positions in $stack
680 $level = 0; // Indicates the parenthesis level we are at.
681 $loopExit = 0; // Recursivity brake.
682
683 // Prepare variables:
684 $parseString = $this->trimSQL($parseString);
685 $this->lastStopKeyWord = '';
686 $this->parse_error = '';
687
688 // $parseString is continously shortend by the process and we keep parsing it till it is zero:
689 while (strlen($parseString)) {
690
691 // Checking if we are inside / outside parenthesis (in case of a function like count(), max(), min() etc...):
692 if ($level>0) { // Inside parenthesis here (does NOT detect if values in quotes are used, the only token is ")" or "("):
693
694 // Accumulate function content until next () parenthesis:
695 $funcContent = $this->nextPart($parseString,'^([^()]*.)');
696 $stack[$pnt]['func_content.'][] = array(
697 'level' => $level,
698 'func_content' => substr($funcContent,0,-1)
699 );
700 $stack[$pnt]['func_content'].= $funcContent;
701
702 // Detecting ( or )
703 switch(substr($stack[$pnt]['func_content'],-1)) {
704 case '(':
705 $level++;
706 break;
707 case ')':
708 $level--;
709 if (!$level) { // If this was the last parenthesis:
710 $stack[$pnt]['func_content'] = substr($stack[$pnt]['func_content'],0,-1);
711 $parseString = ltrim($parseString); // Remove any whitespace after the parenthesis.
712 }
713 break;
714 }
715 } else { // Outside parenthesis, looking for next field:
716
717 // Looking for a known function (only known functions supported)
718 $func = $this->nextPart($parseString,'^(count|max|min|floor|sum|avg)[[:space:]]*\(');
719 if ($func) {
720 $parseString = trim(substr($parseString,1)); // Strip of "("
721 $stack[$pnt]['type'] = 'function';
722 $stack[$pnt]['function'] = $func;
723 $level++; // increse parenthesis level counter.
724 } else {
725 $stack[$pnt]['distinct'] = $this->nextPart($parseString,'^(distinct[[:space:]]+)');
726 // Otherwise, look for regular fieldname:
727 if ($fieldName = $this->nextPart($parseString,'^([[:alnum:]\*._]+)(,|[[:space:]]+)')) {
728 $stack[$pnt]['type'] = 'field';
729
730 // Explode fieldname into field and table:
731 $tableField = explode('.',$fieldName,2);
732 if (count($tableField)==2) {
733 $stack[$pnt]['table'] = $tableField[0];
734 $stack[$pnt]['field'] = $tableField[1];
735 } else {
736 $stack[$pnt]['table'] = '';
737 $stack[$pnt]['field'] = $tableField[0];
738 }
739 } else {
740 return $this->parseError('No field name found as expected in parseFieldList()',$parseString);
741 }
742 }
743 }
744
745 // After a function or field we look for "AS" alias and a comma to separate to the next field in the list:
746 if (!$level) {
747
748 // Looking for "AS" alias:
749 if ($as = $this->nextPart($parseString,'^(AS)[[:space:]]+')) {
750 $stack[$pnt]['as'] = $this->nextPart($parseString,'^([[:alnum:]_]+)(,|[[:space:]]+)');
751 $stack[$pnt]['as_keyword'] = $as;
752 }
753
754 // Looking for "ASC" or "DESC" keywords (for ORDER BY)
755 if ($sDir = $this->nextPart($parseString,'^(ASC|DESC)([[:space:]]+|,)')) {
756 $stack[$pnt]['sortDir'] = $sDir;
757 }
758
759 // Looking for stop-keywords:
760 if ($stopRegex && $this->lastStopKeyWord = $this->nextPart($parseString, $stopRegex)) {
761 $this->lastStopKeyWord = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$this->lastStopKeyWord));
762 return $stack;
763 }
764
765 // Looking for comma (since the stop-keyword did not trigger a return...)
766 if (strlen($parseString) && !$this->nextPart($parseString,'^(,)')) {
767 return $this->parseError('No comma found as expected in parseFieldList()',$parseString);
768 }
769
770 // Increasing pointer:
771 $pnt++;
772 }
773
774 // Check recursivity brake:
775 $loopExit++;
776 if ($loopExit>500) {
777 return $this->parseError('More than 500 loops, exiting prematurely in parseFieldList()...',$parseString);
778 }
779 }
780
781 // Return result array:
782 return $stack;
783 }
784
785 /**
786 * Parsing the tablenames in the "FROM [$parseString] WHERE" part of a query into an array.
787 * The success of this parsing determines if that part of the query is supported by TYPO3.
788 *
789 * @param string list of tables, eg. "pages, tt_content" or "pages A, pages B". NOTICE: passed by reference!
790 * @param string Regular expressing to STOP parsing, eg. '^(WHERE)([[:space:]]*)'
791 * @return array If successful parsing, returns an array, otherwise an error string.
792 * @see compileFromTables()
793 * @deprecated since TYPO3 4.3, this function will be removed in TYPO3 4.5, this is a DBAL-only method that was moved to ux_t3lib_sqlparser.
794 */
795 public function parseFromTables(&$parseString, $stopRegex = '') {
796 t3lib_div::logDeprecatedFunction();
797
798 // Prepare variables:
799 $parseString = $this->trimSQL($parseString);
800 $this->lastStopKeyWord = '';
801 $this->parse_error = '';
802
803 $stack = array(); // Contains the parsed content
804 $pnt = 0; // Pointer to positions in $stack
805 $loopExit = 0; // Recursivity brake.
806
807 // $parseString is continously shortend by the process and we keep parsing it till it is zero:
808 while (strlen($parseString)) {
809 // Looking for the table:
810 if ($stack[$pnt]['table'] = $this->nextPart($parseString,'^([[:alnum:]_]+)(,|[[:space:]]+)')) {
811 // Looking for stop-keywords before fetching potential table alias:
812 if ($stopRegex && ($this->lastStopKeyWord = $this->nextPart($parseString, $stopRegex))) {
813 $this->lastStopKeyWord = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$this->lastStopKeyWord));
814 return $stack;
815 }
816 if(!preg_match('/^(LEFT|JOIN)[[:space:]]+/i',$parseString)) {
817 $stack[$pnt]['as_keyword'] = $this->nextPart($parseString,'^(AS[[:space:]]+)');
818 $stack[$pnt]['as'] = $this->nextPart($parseString,'^([[:alnum:]_]+)[[:space:]]*');
819 }
820 } else return $this->parseError('No table name found as expected in parseFromTables()!',$parseString);
821
822 // Looking for JOIN
823 if ($join = $this->nextPart($parseString,'^(LEFT[[:space:]]+JOIN|LEFT[[:space:]]+OUTER[[:space:]]+JOIN|JOIN)[[:space:]]+')) {
824 $stack[$pnt]['JOIN']['type'] = $join;
825 if ($stack[$pnt]['JOIN']['withTable'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+', 1)) {
826 if (!preg_match('/^ON[[:space:]]+/i', $parseString)) {
827 $stack[$pnt]['JOIN']['as_keyword'] = $this->nextPart($parseString, '^(AS[[:space:]]+)');
828 $stack[$pnt]['JOIN']['as'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
829 }
830 if (!$this->nextPart($parseString, '^(ON[[:space:]]+)')) {
831 return $this->parseError('No join condition found in parseFromTables()!', $parseString);
832 }
833 $field1 = $this->nextPart($parseString,'^([[:alnum:]_.]+)[[:space:]]*=[[:space:]]*',1);
834 $field2 = $this->nextPart($parseString,'^([[:alnum:]_.]+)[[:space:]]+');
835 if ($field1 && $field2) {
836
837 // Explode fields into field and table:
838 $tableField = explode('.',$field1,2);
839 $field1 = array();
840 if (count($tableField)!=2) {
841 $field1['table'] = '';
842 $field1['field'] = $tableField[0];
843 } else {
844 $field1['table'] = $tableField[0];
845 $field1['field'] = $tableField[1];
846 }
847 $tableField = explode('.',$field2,2);
848 $field2 = array();
849 if (count($tableField)!=2) {
850 $field2['table'] = '';
851 $field2['field'] = $tableField[0];
852 } else {
853 $field2['table'] = $tableField[0];
854 $field2['field'] = $tableField[1];
855 }
856 $stack[$pnt]['JOIN']['ON'] = array($field1,$field2);
857 } else return $this->parseError('No join fields found in parseFromTables()!',$parseString);
858 } else return $this->parseError('No join table found in parseFromTables()!',$parseString);
859 }
860
861 // Looking for stop-keywords:
862 if ($stopRegex && $this->lastStopKeyWord = $this->nextPart($parseString, $stopRegex)) {
863 $this->lastStopKeyWord = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$this->lastStopKeyWord));
864 return $stack;
865 }
866
867 // Looking for comma:
868 if (strlen($parseString) && !$this->nextPart($parseString,'^(,)')) {
869 return $this->parseError('No comma found as expected in parseFromTables()',$parseString);
870 }
871
872 // Increasing pointer:
873 $pnt++;
874
875 // Check recursivity brake:
876 $loopExit++;
877 if ($loopExit>500) {
878 return $this->parseError('More than 500 loops, exiting prematurely in parseFromTables()...',$parseString);
879 }
880 }
881
882 // Return result array:
883 return $stack;
884 }
885
886 /**
887 * Parsing the WHERE clause fields in the "WHERE [$parseString] ..." part of a query into a multidimensional array.
888 * The success of this parsing determines if that part of the query is supported by TYPO3.
889 *
890 * @param string WHERE clause to parse. NOTICE: passed by reference!
891 * @param string Regular expressing to STOP parsing, eg. '^(GROUP BY|ORDER BY|LIMIT)([[:space:]]*)'
892 * @return mixed If successful parsing, returns an array, otherwise an error string.
893 * @deprecated since TYPO3 4.3, this function will be removed in TYPO3 4.5, this is a DBAL-only method that was moved to ux_t3lib_sqlparser.
894 */
895 public function parseWhereClause(&$parseString, $stopRegex = '') {
896 t3lib_div::logDeprecatedFunction();
897
898 // Prepare variables:
899 $parseString = $this->trimSQL($parseString);
900 $this->lastStopKeyWord = '';
901 $this->parse_error = '';
902
903 $stack = array(0 => array()); // Contains the parsed content
904 $pnt = array(0 => 0); // Pointer to positions in $stack
905 $level = 0; // Determines parenthesis level
906 $loopExit = 0; // Recursivity brake.
907
908 // $parseString is continously shortend by the process and we keep parsing it till it is zero:
909 while (strlen($parseString)) {
910
911 // Look for next parenthesis level:
912 $newLevel = $this->nextPart($parseString,'^([(])');
913 if ($newLevel=='(') { // If new level is started, manage stack/pointers:
914 $level++; // Increase level
915 $pnt[$level] = 0; // Reset pointer for this level
916 $stack[$level] = array(); // Reset stack for this level
917 } else { // If no new level is started, just parse the current level:
918
919 // Find "modifyer", eg. "NOT or !"
920 $stack[$level][$pnt[$level]]['modifier'] = trim($this->nextPart($parseString,'^(!|NOT[[:space:]]+)'));
921
922 // Support calculated value only for:
923 // - "&" (boolean AND)
924 // - "+" (addition)
925 // - "-" (substraction)
926 // - "*" (multiplication)
927 // - "/" (division)
928 // - "%" (modulo)
929 $calcOperators = '&|\+|-|\*|\/|%';
930
931 // Fieldname:
932 if ($fieldName = $this->nextPart($parseString, '^([[:alnum:]._]+)([[:space:]]+|' . $calcOperators . '|<=|>=|<|>|=|!=|IS)')) {
933
934 // Parse field name into field and table:
935 $tableField = explode('.',$fieldName,2);
936 if (count($tableField)==2) {
937 $stack[$level][$pnt[$level]]['table'] = $tableField[0];
938 $stack[$level][$pnt[$level]]['field'] = $tableField[1];
939 } else {
940 $stack[$level][$pnt[$level]]['table'] = '';
941 $stack[$level][$pnt[$level]]['field'] = $tableField[0];
942 }
943 } else {
944 return $this->parseError('No field name found as expected in parseWhereClause()',$parseString);
945 }
946
947 // See if the value is calculated:
948 $stack[$level][$pnt[$level]]['calc'] = $this->nextPart($parseString, '^(' . $calcOperators . ')');
949 if (strlen($stack[$level][$pnt[$level]]['calc'])) {
950 // Finding value for calculation:
951 $stack[$level][$pnt[$level]]['calc_value'] = $this->getValue($parseString);
952 }
953
954 // Find "comparator":
955 $stack[$level][$pnt[$level]]['comparator'] = $this->nextPart($parseString,'^(<=|>=|<|>|=|!=|NOT[[:space:]]+IN|IN|NOT[[:space:]]+LIKE|LIKE|IS[[:space:]]+NOT|IS)');
956 if (strlen($stack[$level][$pnt[$level]]['comparator'])) {
957 // Finding value for comparator:
958 $stack[$level][$pnt[$level]]['value'] = $this->getValue($parseString,$stack[$level][$pnt[$level]]['comparator']);
959 if ($this->parse_error) { return $this->parse_error; }
960 }
961
962 // Finished, increase pointer:
963 $pnt[$level]++;
964
965 // Checking if the current level is ended, in that case do stack management:
966 while ($this->nextPart($parseString,'^([)])')) {
967 $level--; // Decrease level:
968 $stack[$level][$pnt[$level]]['sub'] = $stack[$level+1]; // Copy stack
969 $pnt[$level]++; // Increase pointer of the new level
970
971 // Make recursivity check:
972 $loopExit++;
973 if ($loopExit>500) {
974 return $this->parseError('More than 500 loops (in search for exit parenthesis), exiting prematurely in parseWhereClause()...',$parseString);
975 }
976 }
977
978 // Detecting the operator for the next level:
979 $op = $this->nextPart($parseString, '^(AND[[:space:]]+NOT|&&[[:space:]]+NOT|OR[[:space:]]+NOT|OR[[:space:]]+NOT|\|\|[[:space:]]+NOT|AND|&&|OR|\|\|)(\(|[[:space:]]+)');
980 if ($op) {
981 // Normalize boolean operator
982 $op = str_replace(array('&&', '||'), array('AND', 'OR'), $op);
983 $stack[$level][$pnt[$level]]['operator'] = $op;
984 } elseif (strlen($parseString)) {
985
986 // Looking for stop-keywords:
987 if ($stopRegex && $this->lastStopKeyWord = $this->nextPart($parseString, $stopRegex)) {
988 $this->lastStopKeyWord = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$this->lastStopKeyWord));
989 return $stack[0];
990 } else {
991 return $this->parseError('No operator, but parsing not finished in parseWhereClause().',$parseString);
992 }
993 }
994 }
995
996 // Make recursivity check:
997 $loopExit++;
998 if ($loopExit>500) {
999 return $this->parseError('More than 500 loops, exiting prematurely in parseWhereClause()...',$parseString);
1000 }
1001 }
1002
1003 // Return the stacks lowest level:
1004 return $stack[0];
1005 }
1006
1007 /**
1008 * Parsing the WHERE clause fields in the "WHERE [$parseString] ..." part of a query into a multidimensional array.
1009 * The success of this parsing determines if that part of the query is supported by TYPO3.
1010 *
1011 * @param string WHERE clause to parse. NOTICE: passed by reference!
1012 * @param string Regular expressing to STOP parsing, eg. '^(GROUP BY|ORDER BY|LIMIT)([[:space:]]*)'
1013 * @return mixed If successful parsing, returns an array, otherwise an error string.
1014 */
1015 public function parseFieldDef(&$parseString, $stopRegex = '') {
1016 // Prepare variables:
1017 $parseString = $this->trimSQL($parseString);
1018 $this->lastStopKeyWord = '';
1019 $this->parse_error = '';
1020
1021 $result = array();
1022
1023 // Field type:
1024 if ($result['fieldType'] = $this->nextPart($parseString,'^(int|smallint|tinyint|mediumint|bigint|double|numeric|decimal|float|varchar|char|text|tinytext|mediumtext|longtext|blob|tinyblob|mediumblob|longblob)([[:space:],]+|\()')) {
1025
1026 // Looking for value:
1027 if (substr($parseString,0,1)=='(') {
1028 $parseString = substr($parseString,1);
1029 if ($result['value'] = $this->nextPart($parseString,'^([^)]*)')) {
1030 $parseString = ltrim(substr($parseString,1));
1031 } else return $this->parseError('No end-parenthesis for value found in parseFieldDef()!',$parseString);
1032 }
1033
1034 // Looking for keywords
1035 while($keyword = $this->nextPart($parseString,'^(DEFAULT|NOT[[:space:]]+NULL|AUTO_INCREMENT|UNSIGNED)([[:space:]]+|,|\))')) {
1036 $keywordCmp = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$keyword));
1037
1038 $result['featureIndex'][$keywordCmp]['keyword'] = $keyword;
1039
1040 switch($keywordCmp) {
1041 case 'DEFAULT':
1042 $result['featureIndex'][$keywordCmp]['value'] = $this->getValue($parseString);
1043 break;
1044 }
1045 }
1046 } else {
1047 return $this->parseError('Field type unknown in parseFieldDef()!',$parseString);
1048 }
1049
1050 return $result;
1051 }
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063 /************************************
1064 *
1065 * Parsing: Helper functions
1066 *
1067 ************************************/
1068
1069 /**
1070 * Strips off a part of the parseString and returns the matching part.
1071 * Helper function for the parsing methods.
1072 *
1073 * @param string Parse string; if $regex finds anything the value of the first () level will be stripped of the string in the beginning. Further $parseString is left-trimmed (on success). Notice; parsestring is passed by reference.
1074 * @param string Regex to find a matching part in the beginning of the string. Rules: You MUST start the regex with "^" (finding stuff in the beginning of string) and the result of the first parenthesis is what will be returned to you (and stripped of the string). Eg. '^(AND|OR|&&)[[:space:]]+' will return AND, OR or && if found and having one of more whitespaces after it, plus shorten $parseString with that match and any space after (by ltrim())
1075 * @param boolean If set the full match of the regex is stripped of the beginning of the string!
1076 * @return string The value of the first parenthesis level of the REGEX.
1077 */
1078 protected function nextPart(&$parseString, $regex, $trimAll = FALSE) {
1079 $reg = array();
1080 if (preg_match('/'.$regex.'/i',$parseString.' ', $reg)) { // Adding space char because [[:space:]]+ is often a requirement in regex's
1081 $parseString = ltrim(substr($parseString,strlen($reg[$trimAll?0:1])));
1082 return $reg[1];
1083 }
1084 }
1085
1086 /**
1087 * Finds value in beginning of $parseString, returns result and strips it of parseString
1088 *
1089 * @param string The parseString, eg. "(0,1,2,3) ..." or "('asdf','qwer') ..." or "1234 ..." or "'My string value here' ..."
1090 * @param string The comparator used before. If "NOT IN" or "IN" then the value is expected to be a list of values. Otherwise just an integer (un-quoted) or string (quoted)
1091 * @param string The mode, eg. "INDEX"
1092 * @return mixed The value (string/integer). Otherwise an array with error message in first key (0)
1093 */
1094 protected function getValue(&$parseString, $comparator = '', $mode = '') {
1095 $value = '';
1096
1097 if (t3lib_div::inList('NOTIN,IN,_LIST',strtoupper(str_replace(array(' ',"\n","\r","\t"),'',$comparator)))) { // List of values:
1098 if ($this->nextPart($parseString,'^([(])')) {
1099 $listValues = array();
1100 $comma=',';
1101
1102 while($comma==',') {
1103 $listValues[] = $this->getValue($parseString);
1104 if ($mode === 'INDEX') {
1105 // Remove any length restriction on INDEX definition
1106 $this->nextPart($parseString, '^([(]\d+[)])');
1107 }
1108 $comma = $this->nextPart($parseString,'^([,])');
1109 }
1110
1111 $out = $this->nextPart($parseString,'^([)])');
1112 if ($out) {
1113 if ($comparator=='_LIST') {
1114 $kVals = array();
1115 foreach ($listValues as $vArr) {
1116 $kVals[] = $vArr[0];
1117 }
1118 return $kVals;
1119 } else {
1120 return $listValues;
1121 }
1122 } else return array($this->parseError('No ) parenthesis in list',$parseString));
1123 } else return array($this->parseError('No ( parenthesis starting the list',$parseString));
1124
1125 } else { // Just plain string value, in quotes or not:
1126
1127 // Quote?
1128 $firstChar = substr($parseString,0,1);
1129 switch($firstChar) {
1130 case '"':
1131 $value = array($this->getValueInQuotes($parseString,'"'),'"');
1132 break;
1133 case "'":
1134 $value = array($this->getValueInQuotes($parseString,"'"),"'");
1135 break;
1136 default:
1137 $reg = array();
1138 if (preg_match('/^([[:alnum:]._-]+)/i',$parseString, $reg)) {
1139 $parseString = ltrim(substr($parseString,strlen($reg[0])));
1140 $value = array($reg[1]);
1141 }
1142 break;
1143 }
1144 }
1145 return $value;
1146 }
1147
1148 /**
1149 * Get value in quotes from $parseString.
1150 * NOTICE: If a query being parsed was prepared for another database than MySQL this function should probably be changed
1151 *
1152 * @param string String from which to find value in quotes. Notice that $parseString is passed by reference and is shortend by the output of this function.
1153 * @param string The quote used; input either " or '
1154 * @return string The value, passed through stripslashes() !
1155 */
1156 protected function getValueInQuotes(&$parseString, $quote) {
1157
1158 $parts = explode($quote,substr($parseString,1));
1159 $buffer = '';
1160 foreach($parts as $k => $v) {
1161 $buffer.=$v;
1162
1163 $reg = array();
1164 preg_match('/\\*$/',$v,$reg);
1165 if ($reg AND strlen($reg[0])%2) {
1166 $buffer.=$quote;
1167 } else {
1168 $parseString = ltrim(substr($parseString,strlen($buffer)+2));
1169 return $this->parseStripslashes($buffer);
1170 }
1171 }
1172 }
1173
1174 /**
1175 * Strip slashes function used for parsing
1176 * NOTICE: If a query being parsed was prepared for another database than MySQL this function should probably be changed
1177 *
1178 * @param string Input string
1179 * @return string Output string
1180 */
1181 protected function parseStripslashes($str) {
1182 $search = array('\\\\', '\\\'', '\\"', '\0', '\n', '\r', '\Z');
1183 $replace = array('\\', '\'', '"', "\x00", "\x0a", "\x0d", "\x1a");
1184
1185 return str_replace($search, $replace, $str);
1186 }
1187
1188 /**
1189 * Add slashes function used for compiling queries
1190 * NOTICE: If a query being parsed was prepared for another database than MySQL this function should probably be changed
1191 *
1192 * @param string Input string
1193 * @return string Output string
1194 */
1195 protected function compileAddslashes($str) {
1196 return $str;
1197 $search = array('\\', '\'', '"', "\x00", "\x0a", "\x0d", "\x1a");
1198 $replace = array('\\\\', '\\\'', '\\"', '\0', '\n', '\r', '\Z');
1199
1200 return str_replace($search, $replace, $str);
1201 }
1202
1203 /**
1204 * Setting the internal error message value, $this->parse_error and returns that value.
1205 *
1206 * @param string Input error message
1207 * @param string Remaining query to parse.
1208 * @return string Error message.
1209 */
1210 protected function parseError($msg, $restQuery) {
1211 $this->parse_error = 'SQL engine parse ERROR: '.$msg.': near "'.substr($restQuery,0,50).'"';
1212 return $this->parse_error;
1213 }
1214
1215 /**
1216 * Trimming SQL as preparation for parsing.
1217 * ";" in the end is stripped of.
1218 * White space is trimmed away around the value
1219 * A single space-char is added in the end
1220 *
1221 * @param string Input string
1222 * @return string Output string
1223 */
1224 protected function trimSQL($str) {
1225 return trim(rtrim($str, "; \r\n\t")).' ';
1226 }
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239 /*************************
1240 *
1241 * Compiling queries
1242 *
1243 *************************/
1244
1245 /**
1246 * Compiles an SQL query from components
1247 *
1248 * @param array Array of SQL query components
1249 * @return string SQL query
1250 * @see parseSQL()
1251 */
1252 public function compileSQL($components) {
1253 switch($components['type']) {
1254 case 'SELECT':
1255 $query = $this->compileSELECT($components);
1256 break;
1257 case 'UPDATE':
1258 $query = $this->compileUPDATE($components);
1259 break;
1260 case 'INSERT':
1261 $query = $this->compileINSERT($components);
1262 break;
1263 case 'DELETE':
1264 $query = $this->compileDELETE($components);
1265 break;
1266 case 'EXPLAIN':
1267 $query = 'EXPLAIN '.$this->compileSELECT($components);
1268 break;
1269 case 'DROPTABLE':
1270 $query = 'DROP TABLE'.($components['ifExists']?' IF EXISTS':'').' '.$components['TABLE'];
1271 break;
1272 case 'CREATETABLE':
1273 $query = $this->compileCREATETABLE($components);
1274 break;
1275 case 'ALTERTABLE':
1276 $query = $this->compileALTERTABLE($components);
1277 break;
1278 }
1279
1280 return $query;
1281 }
1282
1283 /**
1284 * Compiles a SELECT statement from components array
1285 *
1286 * @param array Array of SQL query components
1287 * @return string SQL SELECT query
1288 * @see parseSELECT()
1289 */
1290 protected function compileSELECT($components) {
1291
1292 // Initialize:
1293 $where = $this->compileWhereClause($components['WHERE']);
1294 $groupBy = $this->compileFieldList($components['GROUPBY']);
1295 $orderBy = $this->compileFieldList($components['ORDERBY']);
1296 $limit = $components['LIMIT'];
1297
1298 // Make query:
1299 $query = 'SELECT '.($components['STRAIGHT_JOIN'] ? $components['STRAIGHT_JOIN'].'' : '').'
1300 '.$this->compileFieldList($components['SELECT']).'
1301 FROM '.$this->compileFromTables($components['FROM']).
1302 (strlen($where)?'
1303 WHERE '.$where : '').
1304 (strlen($groupBy)?'
1305 GROUP BY '.$groupBy : '').
1306 (strlen($orderBy)?'
1307 ORDER BY '.$orderBy : '').
1308 (strlen($limit)?'
1309 LIMIT '.$limit : '');
1310
1311 return $query;
1312 }
1313
1314 /**
1315 * Compiles an UPDATE statement from components array
1316 *
1317 * @param array Array of SQL query components
1318 * @return string SQL UPDATE query
1319 * @see parseUPDATE()
1320 */
1321 protected function compileUPDATE($components) {
1322
1323 // Where clause:
1324 $where = $this->compileWhereClause($components['WHERE']);
1325
1326 // Fields
1327 $fields = array();
1328 foreach($components['FIELDS'] as $fN => $fV) {
1329 $fields[]=$fN.'='.$fV[1].$this->compileAddslashes($fV[0]).$fV[1];
1330 }
1331
1332 // Make query:
1333 $query = 'UPDATE '.$components['TABLE'].' SET
1334 '.implode(',
1335 ',$fields).'
1336 '.(strlen($where)?'
1337 WHERE '.$where : '');
1338
1339 return $query;
1340 }
1341
1342 /**
1343 * Compiles an INSERT statement from components array
1344 *
1345 * @param array Array of SQL query components
1346 * @return string SQL INSERT query
1347 * @see parseINSERT()
1348 */
1349 protected function compileINSERT($components) {
1350
1351 if ($components['VALUES_ONLY']) {
1352 // Initialize:
1353 $fields = array();
1354 foreach($components['VALUES_ONLY'] as $fV) {
1355 $fields[]=$fV[1].$this->compileAddslashes($fV[0]).$fV[1];
1356 }
1357
1358 // Make query:
1359 $query = 'INSERT INTO '.$components['TABLE'].'
1360 VALUES
1361 ('.implode(',
1362 ',$fields).')';
1363 } else {
1364 // Initialize:
1365 $fields = array();
1366 foreach($components['FIELDS'] as $fN => $fV) {
1367 $fields[$fN]=$fV[1].$this->compileAddslashes($fV[0]).$fV[1];
1368 }
1369
1370 // Make query:
1371 $query = 'INSERT INTO '.$components['TABLE'].'
1372 ('.implode(',
1373 ',array_keys($fields)).')
1374 VALUES
1375 ('.implode(',
1376 ',$fields).')';
1377 }
1378
1379 return $query;
1380 }
1381
1382 /**
1383 * Compiles an DELETE statement from components array
1384 *
1385 * @param array Array of SQL query components
1386 * @return string SQL DELETE query
1387 * @see parseDELETE()
1388 */
1389 protected function compileDELETE($components) {
1390
1391 // Where clause:
1392 $where = $this->compileWhereClause($components['WHERE']);
1393
1394 // Make query:
1395 $query = 'DELETE FROM '.$components['TABLE'].
1396 (strlen($where)?'
1397 WHERE '.$where : '');
1398
1399 return $query;
1400 }
1401
1402 /**
1403 * Compiles a CREATE TABLE statement from components array
1404 *
1405 * @param array Array of SQL query components
1406 * @return string SQL CREATE TABLE query
1407 * @see parseCREATETABLE()
1408 */
1409 protected function compileCREATETABLE($components) {
1410
1411 // Create fields and keys:
1412 $fieldsKeys = array();
1413 foreach($components['FIELDS'] as $fN => $fCfg) {
1414 $fieldsKeys[]=$fN.' '.$this->compileFieldCfg($fCfg['definition']);
1415 }
1416 foreach($components['KEYS'] as $kN => $kCfg) {
1417 if ($kN == 'PRIMARYKEY') {
1418 $fieldsKeys[]='PRIMARY KEY ('.implode(',', $kCfg).')';
1419 } elseif ($kN == 'UNIQUE') {
1420 $fieldsKeys[]='UNIQUE '.$kN.' ('.implode(',', $kCfg).')';
1421 } else {
1422 $fieldsKeys[]='KEY '.$kN.' ('.implode(',', $kCfg).')';
1423 }
1424 }
1425
1426 // Make query:
1427 $query = 'CREATE TABLE '.$components['TABLE'].' (
1428 '.implode(',
1429 ', $fieldsKeys).'
1430 )'.($components['tableType'] ? ' TYPE='.$components['tableType'] : '');
1431
1432 return $query;
1433 }
1434
1435 /**
1436 * Compiles an ALTER TABLE statement from components array
1437 *
1438 * @param array Array of SQL query components
1439 * @return string SQL ALTER TABLE query
1440 * @see parseALTERTABLE()
1441 */
1442 protected function compileALTERTABLE($components) {
1443
1444 // Make query:
1445 $query = 'ALTER TABLE '.$components['TABLE'].' '.$components['action'].' '.($components['FIELD']?$components['FIELD']:$components['KEY']);
1446
1447 // Based on action, add the final part:
1448 switch(strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$components['action']))) {
1449 case 'ADD':
1450 $query.=' '.$this->compileFieldCfg($components['definition']);
1451 break;
1452 case 'CHANGE':
1453 $query.=' '.$components['newField'].' '.$this->compileFieldCfg($components['definition']);
1454 break;
1455 case 'DROP':
1456 case 'DROPKEY':
1457 break;
1458 case 'ADDKEY':
1459 case 'ADDPRIMARYKEY':
1460 $query.=' ('.implode(',',$components['fields']).')';
1461 break;
1462 }
1463
1464 // Return query
1465 return $query;
1466 }
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481 /**************************************
1482 *
1483 * Compiling queries, helper functions for parts of queries
1484 *
1485 **************************************/
1486
1487 /**
1488 * Compiles a "SELECT [output] FROM..:" field list based on input array (made with ->parseFieldList())
1489 * Can also compile field lists for ORDER BY and GROUP BY.
1490 *
1491 * @param array Array of select fields, (made with ->parseFieldList())
1492 * @return string Select field string
1493 * @see parseFieldList()
1494 */
1495 public function compileFieldList($selectFields) {
1496
1497 // Prepare buffer variable:
1498 $outputParts = array();
1499
1500 // Traverse the selectFields if any:
1501 if (is_array($selectFields)) {
1502 foreach($selectFields as $k => $v) {
1503
1504 // Detecting type:
1505 switch($v['type']) {
1506 case 'function':
1507 $outputParts[$k] = $v['function'].'('.$v['func_content'].')';
1508 break;
1509 case 'field':
1510 $outputParts[$k] = ($v['distinct']?$v['distinct']:'').($v['table']?$v['table'].'.':'').$v['field'];
1511 break;
1512 }
1513
1514 // Alias:
1515 if ($v['as']) {
1516 $outputParts[$k].= ' '.$v['as_keyword'].' '.$v['as'];
1517 }
1518
1519 // Specifically for ORDER BY and GROUP BY field lists:
1520 if ($v['sortDir']) {
1521 $outputParts[$k].= ' '.$v['sortDir'];
1522 }
1523 }
1524 }
1525
1526 // Return imploded buffer:
1527 return implode(', ',$outputParts);
1528 }
1529
1530 /**
1531 * Compiles a "FROM [output] WHERE..:" table list based on input array (made with ->parseFromTables())
1532 *
1533 * @param array Array of table names, (made with ->parseFromTables())
1534 * @return string Table name string
1535 * @see parseFromTables()
1536 * @deprecated since TYPO3 4.3, this function will be removed in TYPO3 4.5, this is a DBAL-only method that was moved to ux_t3lib_sqlparser.
1537 */
1538 public function compileFromTables($tablesArray) {
1539 t3lib_div::logDeprecatedFunction();
1540
1541 // Prepare buffer variable:
1542 $outputParts = array();
1543
1544 // Traverse the table names:
1545 if (is_array($tablesArray)) {
1546 foreach($tablesArray as $k => $v) {
1547
1548 // Set table name:
1549 $outputParts[$k] = $v['table'];
1550
1551 // Add alias AS if there:
1552 if ($v['as']) {
1553 $outputParts[$k].= ' '.$v['as_keyword'].' '.$v['as'];
1554 }
1555
1556 if (is_array($v['JOIN'])) {
1557 $outputParts[$k] .= ' ' . $v['JOIN']['type'] . ' ' . $v['JOIN']['withTable'];
1558 // Add alias AS if there:
1559 if (isset($v['JOIN']['as']) && $v['JOIN']['as']) {
1560 $outputParts[$k] .= ' ' . $v['JOIN']['as_keyword'] . ' ' . $v['JOIN']['as'];
1561 }
1562 $outputParts[$k] .= ' ON ';
1563 $outputParts[$k] .= ($v['JOIN']['ON'][0]['table']) ? $v['JOIN']['ON'][0]['table'].'.' : '';
1564 $outputParts[$k] .= $v['JOIN']['ON'][0]['field'];
1565 $outputParts[$k] .= '=';
1566 $outputParts[$k] .= ($v['JOIN']['ON'][1]['table']) ? $v['JOIN']['ON'][1]['table'].'.' : '';
1567 $outputParts[$k] .= $v['JOIN']['ON'][1]['field'];
1568 }
1569 }
1570 }
1571
1572 // Return imploded buffer:
1573 return implode(', ',$outputParts);
1574 }
1575
1576 /**
1577 * Implodes an array of WHERE clause configuration into a WHERE clause.
1578 * NOTICE: MIGHT BY A TEMPORARY FUNCTION. Use for debugging only!
1579 * BUT IT IS NEEDED FOR DBAL - MAKE IT PERMANENT?!?!
1580 *
1581 * @param array WHERE clause configuration
1582 * @return string WHERE clause as string.
1583 * @see explodeWhereClause()
1584 * @deprecated since TYPO3 4.3, this function will be removed in TYPO3 4.5, this is a DBAL-only method that was moved to ux_t3lib_sqlparser.
1585 */
1586 public function compileWhereClause($clauseArray) {
1587 t3lib_div::logDeprecatedFunction();
1588
1589 // Prepare buffer variable:
1590 $output='';
1591
1592 // Traverse clause array:
1593 if (is_array($clauseArray)) {
1594 foreach($clauseArray as $k => $v) {
1595
1596 // Set operator:
1597 $output.=$v['operator'] ? ' '.$v['operator'] : '';
1598
1599 // Look for sublevel:
1600 if (is_array($v['sub'])) {
1601 $output.=' ('.trim($this->compileWhereClause($v['sub'])).')';
1602 } else {
1603
1604 // Set field/table with modifying prefix if any:
1605 $output.=' '.trim($v['modifier'].' '.($v['table']?$v['table'].'.':'').$v['field']);
1606
1607 // Set calculation, if any:
1608 if ($v['calc']) {
1609 $output.=$v['calc'].$v['calc_value'][1].$this->compileAddslashes($v['calc_value'][0]).$v['calc_value'][1];
1610 }
1611
1612 // Set comparator:
1613 if ($v['comparator']) {
1614 $output.=' '.$v['comparator'];
1615
1616 // Detecting value type; list or plain:
1617 if (t3lib_div::inList('NOTIN,IN',strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$v['comparator'])))) {
1618 $valueBuffer = array();
1619 foreach($v['value'] as $realValue) {
1620 $valueBuffer[]=$realValue[1].$this->compileAddslashes($realValue[0]).$realValue[1];
1621 }
1622 $output.=' ('.trim(implode(',',$valueBuffer)).')';
1623 } else {
1624 $output.=' '.$v['value'][1].$this->compileAddslashes($v['value'][0]).$v['value'][1];
1625 }
1626 }
1627 }
1628 }
1629 }
1630
1631 // Return output buffer:
1632 return $output;
1633 }
1634
1635 /**
1636 * Compile field definition
1637 *
1638 * @param array Field definition parts
1639 * @return string Field definition string
1640 */
1641 public function compileFieldCfg($fieldCfg) {
1642
1643 // Set type:
1644 $cfg = $fieldCfg['fieldType'];
1645
1646 // Add value, if any:
1647 if (strlen($fieldCfg['value'])) {
1648 $cfg.='('.$fieldCfg['value'].')';
1649 }
1650
1651 // Add additional features:
1652 if (is_array($fieldCfg['featureIndex'])) {
1653 foreach($fieldCfg['featureIndex'] as $featureDef) {
1654 $cfg.=' '.$featureDef['keyword'];
1655
1656 // Add value if found:
1657 if (is_array($featureDef['value'])) {
1658 $cfg.=' '.$featureDef['value'][1].$this->compileAddslashes($featureDef['value'][0]).$featureDef['value'][1];
1659 }
1660 }
1661 }
1662
1663 // Return field definition string:
1664 return $cfg;
1665 }
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677 /*************************
1678 *
1679 * Debugging
1680 *
1681 *************************/
1682
1683 /**
1684 * Check parsability of input SQL part string; Will parse and re-compile after which it is compared
1685 *
1686 * @param string Part definition of string; "SELECT" = fieldlist (also ORDER BY and GROUP BY), "FROM" = table list, "WHERE" = Where clause.
1687 * @param string SQL string to verify parsability of
1688 * @return mixed Returns array with string 1 and 2 if error, otherwise false
1689 */
1690 public function debug_parseSQLpart($part, $str) {
1691 $retVal = false;
1692
1693 switch($part) {
1694 case 'SELECT':
1695 $retVal = $this->debug_parseSQLpartCompare($str,$this->compileFieldList($this->parseFieldList($str)));
1696 break;
1697 case 'FROM':
1698 $retVal = $this->debug_parseSQLpartCompare($str,$this->compileFromTables($this->parseFromTables($str)));
1699 break;
1700 case 'WHERE':
1701 $retVal = $this->debug_parseSQLpartCompare($str,$this->compileWhereClause($this->parseWhereClause($str)));
1702 break;
1703 }
1704 return $retVal;
1705 }
1706
1707 /**
1708 * Compare two query strins by stripping away whitespace.
1709 *
1710 * @param string SQL String 1
1711 * @param string SQL string 2
1712 * @param boolean If true, the strings are compared insensitive to case
1713 * @return mixed Returns array with string 1 and 2 if error, otherwise false
1714 */
1715 public function debug_parseSQLpartCompare($str, $newStr, $caseInsensitive = FALSE) {
1716 if ($caseInsensitive) {
1717 $str1 = strtoupper($str);
1718 $str2 = strtoupper($newStr);
1719 } else {
1720 $str1 = $str;
1721 $str2 = $newStr;
1722 }
1723
1724 // Fixing escaped chars:
1725 $search = array('\0', '\n', '\r', '\Z');
1726 $replace = array("\x00", "\x0a", "\x0d", "\x1a");
1727 $str1 = str_replace($search, $replace, $str1);
1728 $str2 = str_replace($search, $replace, $str2);
1729
1730 # Normally, commented out since they are needed only in tricky cases...
1731 # $str1 = stripslashes($str1);
1732 # $str2 = stripslashes($str2);
1733
1734 if (strcmp(str_replace(array(' ',"\t","\r","\n"),'',$this->trimSQL($str1)),str_replace(array(' ',"\t","\r","\n"),'',$this->trimSQL($str2)))) {
1735 return array(
1736 str_replace(array(' ',"\t","\r","\n"),' ',$str),
1737 str_replace(array(' ',"\t","\r","\n"),' ',$newStr),
1738 );
1739 }
1740 }
1741
1742 /**
1743 * Performs the ultimate test of the parser: Direct a SQL query in; You will get it back (through the parsed and re-compiled) if no problems, otherwise the script will print the error and exit
1744 *
1745 * @param string SQL query
1746 * @return string Query if all is well, otherwise exit.
1747 */
1748 public function debug_testSQL($SQLquery) {
1749
1750 // Getting result array:
1751 $parseResult = $this->parseSQL($SQLquery);
1752
1753 // If result array was returned, proceed. Otherwise show error and exit.
1754 if (is_array($parseResult)) {
1755
1756 // Re-compile query:
1757 $newQuery = $this->compileSQL($parseResult);
1758
1759 // TEST the new query:
1760 $testResult = $this->debug_parseSQLpartCompare($SQLquery, $newQuery);
1761
1762 // Return new query if OK, otherwise show error and exit:
1763 if (!is_array($testResult)) {
1764 return $newQuery;
1765 } else {
1766 debug(array('ERROR MESSAGE'=>'Input query did not match the parsed and recompiled query exactly (not observing whitespace)', 'TEST result' => $testResult),'SQL parsing failed:');
1767 exit;
1768 }
1769 } else {
1770 debug(array('query' => $SQLquery, 'ERROR MESSAGE'=>$parseResult),'SQL parsing failed:');
1771 exit;
1772 }
1773 }
1774 }
1775
1776
1777 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_sqlparser.php']) {
1778 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_sqlparser.php']);
1779 }
1780
1781 ?>