f9613a8a77cb10eba67a474acd355f0c47ef121e
[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 // Parse any SQL hint / comments
689 $stack[$pnt]['comments'] = $this->nextPart($parseString, '^(\/\*.*\*\/)');
690
691 // $parseString is continously shortend by the process and we keep parsing it till it is zero:
692 while (strlen($parseString)) {
693
694 // Checking if we are inside / outside parenthesis (in case of a function like count(), max(), min() etc...):
695 if ($level>0) { // Inside parenthesis here (does NOT detect if values in quotes are used, the only token is ")" or "("):
696
697 // Accumulate function content until next () parenthesis:
698 $funcContent = $this->nextPart($parseString,'^([^()]*.)');
699 $stack[$pnt]['func_content.'][] = array(
700 'level' => $level,
701 'func_content' => substr($funcContent,0,-1)
702 );
703 $stack[$pnt]['func_content'].= $funcContent;
704
705 // Detecting ( or )
706 switch(substr($stack[$pnt]['func_content'],-1)) {
707 case '(':
708 $level++;
709 break;
710 case ')':
711 $level--;
712 if (!$level) { // If this was the last parenthesis:
713 $stack[$pnt]['func_content'] = substr($stack[$pnt]['func_content'],0,-1);
714 $parseString = ltrim($parseString); // Remove any whitespace after the parenthesis.
715 }
716 break;
717 }
718 } else { // Outside parenthesis, looking for next field:
719
720 // Looking for a flow-control construct (only known constructs supported)
721 if (preg_match('/^case([[:space:]][[:alnum:]\*._]+)?[[:space:]]when/i', $parseString)) {
722 $stack[$pnt]['type'] = 'flow-control';
723 $stack[$pnt]['flow-control'] = $this->parseCaseStatement($parseString);
724 // Looking for "AS" alias:
725 if ($as = $this->nextPart($parseString, '^(AS)[[:space:]]+')) {
726 $stack[$pnt]['as'] = $this->nextPart($parseString, '^([[:alnum:]_]+)(,|[[:space:]]+)');
727 $stack[$pnt]['as_keyword'] = $as;
728 }
729 } else {
730 // Looking for a known function (only known functions supported)
731 $func = $this->nextPart($parseString,'^(count|max|min|floor|sum|avg)[[:space:]]*\(');
732 if ($func) {
733 $parseString = trim(substr($parseString,1)); // Strip of "("
734 $stack[$pnt]['type'] = 'function';
735 $stack[$pnt]['function'] = $func;
736 $level++; // increse parenthesis level counter.
737 } else {
738 $stack[$pnt]['distinct'] = $this->nextPart($parseString,'^(distinct[[:space:]]+)');
739 // Otherwise, look for regular fieldname:
740 if ($fieldName = $this->nextPart($parseString,'^([[:alnum:]\*._]+)(,|[[:space:]]+)')) {
741 $stack[$pnt]['type'] = 'field';
742
743 // Explode fieldname into field and table:
744 $tableField = explode('.',$fieldName,2);
745 if (count($tableField)==2) {
746 $stack[$pnt]['table'] = $tableField[0];
747 $stack[$pnt]['field'] = $tableField[1];
748 } else {
749 $stack[$pnt]['table'] = '';
750 $stack[$pnt]['field'] = $tableField[0];
751 }
752 } else {
753 return $this->parseError('No field name found as expected in parseFieldList()',$parseString);
754 }
755 }
756 }
757 }
758
759 // After a function or field we look for "AS" alias and a comma to separate to the next field in the list:
760 if (!$level) {
761
762 // Looking for "AS" alias:
763 if ($as = $this->nextPart($parseString,'^(AS)[[:space:]]+')) {
764 $stack[$pnt]['as'] = $this->nextPart($parseString,'^([[:alnum:]_]+)(,|[[:space:]]+)');
765 $stack[$pnt]['as_keyword'] = $as;
766 }
767
768 // Looking for "ASC" or "DESC" keywords (for ORDER BY)
769 if ($sDir = $this->nextPart($parseString,'^(ASC|DESC)([[:space:]]+|,)')) {
770 $stack[$pnt]['sortDir'] = $sDir;
771 }
772
773 // Looking for stop-keywords:
774 if ($stopRegex && $this->lastStopKeyWord = $this->nextPart($parseString, $stopRegex)) {
775 $this->lastStopKeyWord = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$this->lastStopKeyWord));
776 return $stack;
777 }
778
779 // Looking for comma (since the stop-keyword did not trigger a return...)
780 if (strlen($parseString) && !$this->nextPart($parseString,'^(,)')) {
781 return $this->parseError('No comma found as expected in parseFieldList()',$parseString);
782 }
783
784 // Increasing pointer:
785 $pnt++;
786 }
787
788 // Check recursivity brake:
789 $loopExit++;
790 if ($loopExit>500) {
791 return $this->parseError('More than 500 loops, exiting prematurely in parseFieldList()...',$parseString);
792 }
793 }
794
795 // Return result array:
796 return $stack;
797 }
798
799 /**
800 * Parsing a CASE ... WHEN flow-control construct.
801 * The output from this function can be compiled back with ->compileCaseStatement()
802 *
803 * @param string The string with the CASE ... WHEN construct, eg. "CASE field WHEN 1 THEN 0 ELSE ..." etc. NOTICE: passed by reference!
804 * @return array If successful parsing, returns an array, otherwise an error string.
805 * @see compileCaseConstruct()
806 */
807 protected function parseCaseStatement(&$parseString) {
808 $result = array();
809 $result['type'] = $this->nextPart($parseString, '^(case)[[:space:]]+');
810 if (!preg_match('/^when[[:space:]]+/i', $parseString)) {
811 $value = $this->getValue($parseString);
812 if (!(isset($value[1]) || is_numeric($value[0]))) {
813 $result['case_field'] = $value[0];
814 } else {
815 $result['case_value'] = $value;
816 }
817 }
818 $result['when'] = array();
819 while ($this->nextPart($parseString, '^(when)[[:space:]]')) {
820 $when = array();
821 $when['when_value'] = $this->parseWhereClause($parseString, '^(then)[[:space:]]+');
822 $when['then_value'] = $this->getValue($parseString);
823 $result['when'][] = $when;
824 }
825 if ($this->nextPart($parseString, '^(else)[[:space:]]+')) {
826 $result['else'] = $this->getValue($parseString);
827 }
828 if (!$this->nextPart($parseString, '^(end)[[:space:]]+')) {
829 return $this->parseError('No "end" keyword found as expected in parseCaseStatement()', $parseString);
830 }
831 return $result;
832 }
833
834 /**
835 * Parsing the tablenames in the "FROM [$parseString] WHERE" part of a query into an array.
836 * The success of this parsing determines if that part of the query is supported by TYPO3.
837 *
838 * @param string list of tables, eg. "pages, tt_content" or "pages A, pages B". NOTICE: passed by reference!
839 * @param string Regular expressing to STOP parsing, eg. '^(WHERE)([[:space:]]*)'
840 * @return array If successful parsing, returns an array, otherwise an error string.
841 * @see compileFromTables()
842 */
843 public function parseFromTables(&$parseString, $stopRegex = '') {
844
845 // Prepare variables:
846 $parseString = $this->trimSQL($parseString);
847 $this->lastStopKeyWord = '';
848 $this->parse_error = '';
849
850 $stack = array(); // Contains the parsed content
851 $pnt = 0; // Pointer to positions in $stack
852 $loopExit = 0; // Recursivity brake.
853
854 // $parseString is continously shortend by the process and we keep parsing it till it is zero:
855 while (strlen($parseString)) {
856 // Looking for the table:
857 if ($stack[$pnt]['table'] = $this->nextPart($parseString,'^([[:alnum:]_]+)(,|[[:space:]]+)')) {
858 // Looking for stop-keywords before fetching potential table alias:
859 if ($stopRegex && ($this->lastStopKeyWord = $this->nextPart($parseString, $stopRegex))) {
860 $this->lastStopKeyWord = strtoupper(str_replace(array(' ',"\t","\r","\n"), '', $this->lastStopKeyWord));
861 return $stack;
862 }
863 if (!preg_match('/^(LEFT|RIGHT|JOIN|INNER)[[:space:]]+/i', $parseString)) {
864 $stack[$pnt]['as_keyword'] = $this->nextPart($parseString,'^(AS[[:space:]]+)');
865 $stack[$pnt]['as'] = $this->nextPart($parseString,'^([[:alnum:]_]+)[[:space:]]*');
866 }
867 } else return $this->parseError('No table name found as expected in parseFromTables()!', $parseString);
868
869 // Looking for JOIN
870 $joinCnt = 0;
871 while ($join = $this->nextPart($parseString,'^(LEFT[[:space:]]+JOIN|LEFT[[:space:]]+OUTER[[:space:]]+JOIN|RIGHT[[:space:]]+JOIN|RIGHT[[:space:]]+OUTER[[:space:]]+JOIN|INNER[[:space:]]+JOIN|JOIN)[[:space:]]+')) {
872 $stack[$pnt]['JOIN'][$joinCnt]['type'] = $join;
873 if ($stack[$pnt]['JOIN'][$joinCnt]['withTable'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+', 1)) {
874 if (!preg_match('/^ON[[:space:]]+/i', $parseString)) {
875 $stack[$pnt]['JOIN'][$joinCnt]['as_keyword'] = $this->nextPart($parseString, '^(AS[[:space:]]+)');
876 $stack[$pnt]['JOIN'][$joinCnt]['as'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
877 }
878 if (!$this->nextPart($parseString, '^(ON[[:space:]]+)')) {
879 return $this->parseError('No join condition found in parseFromTables()!', $parseString);
880 }
881 $field1 = $this->nextPart($parseString,'^([[:alnum:]_.]+)[[:space:]]*=[[:space:]]*', 1);
882 $field2 = $this->nextPart($parseString,'^([[:alnum:]_.]+)[[:space:]]+');
883 if ($field1 && $field2) {
884
885 // Explode fields into field and table:
886 $tableField = explode('.', $field1, 2);
887 $field1 = array();
888 if (count($tableField) != 2) {
889 $field1['table'] = '';
890 $field1['field'] = $tableField[0];
891 } else {
892 $field1['table'] = $tableField[0];
893 $field1['field'] = $tableField[1];
894 }
895 $tableField = explode('.', $field2, 2);
896 $field2 = array();
897 if (count($tableField) != 2) {
898 $field2['table'] = '';
899 $field2['field'] = $tableField[0];
900 } else {
901 $field2['table'] = $tableField[0];
902 $field2['field'] = $tableField[1];
903 }
904 $stack[$pnt]['JOIN'][$joinCnt]['ON'] = array($field1, $field2);
905 $joinCnt++;
906 } else return $this->parseError('No join fields found in parseFromTables()!', $parseString);
907 } else return $this->parseError('No join table found in parseFromTables()!', $parseString);
908 }
909
910 // Looking for stop-keywords:
911 if ($stopRegex && $this->lastStopKeyWord = $this->nextPart($parseString, $stopRegex)) {
912 $this->lastStopKeyWord = strtoupper(str_replace(array(' ',"\t","\r","\n"), '', $this->lastStopKeyWord));
913 return $stack;
914 }
915
916 // Looking for comma:
917 if (strlen($parseString) && !$this->nextPart($parseString, '^(,)')) {
918 return $this->parseError('No comma found as expected in parseFromTables()', $parseString);
919 }
920
921 // Increasing pointer:
922 $pnt++;
923
924 // Check recursivity brake:
925 $loopExit++;
926 if ($loopExit > 500) {
927 return $this->parseError('More than 500 loops, exiting prematurely in parseFromTables()...', $parseString);
928 }
929 }
930
931 // Return result array:
932 return $stack;
933 }
934
935 /**
936 * Parsing the WHERE clause fields in the "WHERE [$parseString] ..." part of a query into a multidimensional array.
937 * The success of this parsing determines if that part of the query is supported by TYPO3.
938 *
939 * @param string WHERE clause to parse. NOTICE: passed by reference!
940 * @param string Regular expressing to STOP parsing, eg. '^(GROUP BY|ORDER BY|LIMIT)([[:space:]]*)'
941 * @return mixed If successful parsing, returns an array, otherwise an error string.
942 */
943 public function parseWhereClause(&$parseString, $stopRegex = '') {
944
945 // Prepare variables:
946 $parseString = $this->trimSQL($parseString);
947 $this->lastStopKeyWord = '';
948 $this->parse_error = '';
949
950 $stack = array(0 => array()); // Contains the parsed content
951 $pnt = array(0 => 0); // Pointer to positions in $stack
952 $level = 0; // Determines parenthesis level
953 $loopExit = 0; // Recursivity brake.
954
955 // $parseString is continously shortend by the process and we keep parsing it till it is zero:
956 while (strlen($parseString)) {
957
958 // Look for next parenthesis level:
959 $newLevel = $this->nextPart($parseString,'^([(])');
960 if ($newLevel == '(') { // If new level is started, manage stack/pointers:
961 $level++; // Increase level
962 $pnt[$level] = 0; // Reset pointer for this level
963 $stack[$level] = array(); // Reset stack for this level
964 } else { // If no new level is started, just parse the current level:
965
966 // Find "modifier", eg. "NOT or !"
967 $stack[$level][$pnt[$level]]['modifier'] = trim($this->nextPart($parseString, '^(!|NOT[[:space:]]+)'));
968
969 // See if condition is EXISTS with a subquery
970 if (preg_match('/^EXISTS[[:space:]]*[(]/', $parseString)) {
971 $stack[$level][$pnt[$level]]['func']['type'] = $this->nextPart($parseString, '^(EXISTS)');
972 $this->nextPart($parseString, '^([(])');
973 $stack[$level][$pnt[$level]]['func']['subquery'] = $this->parseSELECT($parseString);
974 // Seek to new position in parseString after parsing of the subquery
975 $parseString = $stack[$level][$pnt[$level]]['func']['subquery']['parseString'];
976 unset($stack[$level][$pnt[$level]]['func']['subquery']['parseString']);
977 if (!$this->nextPart($parseString, '^([)])')) {
978 return 'No ) parenthesis at end of subquery';
979 }
980 } else {
981
982 // Support calculated value only for:
983 // - "&" (boolean AND)
984 // - "+" (addition)
985 // - "-" (substraction)
986 // - "*" (multiplication)
987 // - "/" (division)
988 // - "%" (modulo)
989 $calcOperators = '&|\+|-|\*|\/|%';
990
991 // Fieldname:
992 if ($fieldName = $this->nextPart($parseString, '^([[:alnum:]._]+)([[:space:]]+|' . $calcOperators . '|<=|>=|<|>|=|!=|IS)')) {
993
994 // Parse field name into field and table:
995 $tableField = explode('.', $fieldName, 2);
996 if (count($tableField) == 2) {
997 $stack[$level][$pnt[$level]]['table'] = $tableField[0];
998 $stack[$level][$pnt[$level]]['field'] = $tableField[1];
999 } else {
1000 $stack[$level][$pnt[$level]]['table'] = '';
1001 $stack[$level][$pnt[$level]]['field'] = $tableField[0];
1002 }
1003 } else {
1004 return $this->parseError('No field name found as expected in parseWhereClause()', $parseString);
1005 }
1006
1007 // See if the value is calculated:
1008 $stack[$level][$pnt[$level]]['calc'] = $this->nextPart($parseString, '^(' . $calcOperators . ')');
1009 if (strlen($stack[$level][$pnt[$level]]['calc'])) {
1010 // Finding value for calculation:
1011 $calc_value = $this->getValue($parseString);
1012 $stack[$level][$pnt[$level]]['calc_value'] = $calc_value;
1013 if (count($calc_value) == 1 && is_string($calc_value[0])) {
1014 // Value is a field, store it to allow DBAL to post-process it (quoting, remapping)
1015 $tableField = explode('.', $calc_value[0], 2);
1016 if (count($tableField) == 2) {
1017 $stack[$level][$pnt[$level]]['calc_table'] = $tableField[0];
1018 $stack[$level][$pnt[$level]]['calc_field'] = $tableField[1];
1019 } else {
1020 $stack[$level][$pnt[$level]]['calc_table'] = '';
1021 $stack[$level][$pnt[$level]]['calc_field'] = $tableField[0];
1022 }
1023 }
1024 }
1025
1026 // Find "comparator":
1027 $stack[$level][$pnt[$level]]['comparator'] = $this->nextPart($parseString, '^(<=|>=|<|>|=|!=|NOT[[:space:]]+IN|IN|NOT[[:space:]]+LIKE|LIKE|IS[[:space:]]+NOT|IS)');
1028 if (strlen($stack[$level][$pnt[$level]]['comparator'])) {
1029 if (preg_match('/^CONCAT[[:space:]]*\(/', $parseString)) {
1030 $this->nextPart($parseString, '^(CONCAT[[:space:]]?[(])');
1031 $values = array(
1032 'operator' => 'CONCAT',
1033 'args' => array(),
1034 );
1035 $cnt = 0;
1036 while ($fieldName = $this->nextPart($parseString, '^([[:alnum:]._]+)')) {
1037 // Parse field name into field and table:
1038 $tableField = explode('.', $fieldName, 2);
1039 if (count($tableField) == 2) {
1040 $values['args'][$cnt]['table'] = $tableField[0];
1041 $values['args'][$cnt]['field'] = $tableField[1];
1042 } else {
1043 $values['args'][$cnt]['table'] = '';
1044 $values['args'][$cnt]['field'] = $tableField[0];
1045 }
1046 // Looking for comma:
1047 $this->nextPart($parseString, '^(,)');
1048 $cnt++;
1049 }
1050 // Look for ending parenthesis:
1051 $this->nextPart($parseString, '([)])');
1052 $stack[$level][$pnt[$level]]['value'] = $values;
1053 } else if (t3lib_div::inList('IN,NOT IN', $stack[$level][$pnt[$level]]['comparator']) && preg_match('/^[(][[:space:]]*SELECT[[:space:]]+/', $parseString)) {
1054 $this->nextPart($parseString, '^([(])');
1055 $stack[$level][$pnt[$level]]['subquery'] = $this->parseSELECT($parseString);
1056 // Seek to new position in parseString after parsing of the subquery
1057 $parseString = $stack[$level][$pnt[$level]]['subquery']['parseString'];
1058 unset($stack[$level][$pnt[$level]]['subquery']['parseString']);
1059 if (!$this->nextPart($parseString, '^([)])')) {
1060 return 'No ) parenthesis at end of subquery';
1061 }
1062 } else {
1063 // Finding value for comparator:
1064 $stack[$level][$pnt[$level]]['value'] = $this->getValue($parseString, $stack[$level][$pnt[$level]]['comparator']);
1065 if ($this->parse_error) {
1066 return $this->parse_error;
1067 }
1068 }
1069 }
1070 }
1071
1072 // Finished, increase pointer:
1073 $pnt[$level]++;
1074
1075 // Checking if we are back to level 0 and we should still decrease level,
1076 // meaning we were probably parsing as subquery and should return here:
1077 if ($level === 0 && preg_match('/^[)]/', $parseString)) {
1078 // Return the stacks lowest level:
1079 return $stack[0];
1080 }
1081
1082 // Checking if we are back to level 0 and we should still decrease level,
1083 // meaning we were probably parsing a subquery and should return here:
1084 if ($level === 0 && preg_match('/^[)]/', $parseString)) {
1085 // Return the stacks lowest level:
1086 return $stack[0];
1087 }
1088
1089 // Checking if the current level is ended, in that case do stack management:
1090 while ($this->nextPart($parseString,'^([)])')) {
1091 $level--; // Decrease level:
1092 $stack[$level][$pnt[$level]]['sub'] = $stack[$level+1]; // Copy stack
1093 $pnt[$level]++; // Increase pointer of the new level
1094
1095 // Make recursivity check:
1096 $loopExit++;
1097 if ($loopExit > 500) {
1098 return $this->parseError('More than 500 loops (in search for exit parenthesis), exiting prematurely in parseWhereClause()...', $parseString);
1099 }
1100 }
1101
1102 // Detecting the operator for the next level:
1103 $op = $this->nextPart($parseString, '^(AND[[:space:]]+NOT|&&[[:space:]]+NOT|OR[[:space:]]+NOT|OR[[:space:]]+NOT|\|\|[[:space:]]+NOT|AND|&&|OR|\|\|)(\(|[[:space:]]+)');
1104 if ($op) {
1105 // Normalize boolean operator
1106 $op = str_replace(array('&&', '||'), array('AND', 'OR'), $op);
1107 $stack[$level][$pnt[$level]]['operator'] = $op;
1108 } elseif (strlen($parseString)) {
1109
1110 // Looking for stop-keywords:
1111 if ($stopRegex && $this->lastStopKeyWord = $this->nextPart($parseString, $stopRegex)) {
1112 $this->lastStopKeyWord = strtoupper(str_replace(array(' ',"\t","\r","\n"), '', $this->lastStopKeyWord));
1113 return $stack[0];
1114 } else {
1115 return $this->parseError('No operator, but parsing not finished in parseWhereClause().', $parseString);
1116 }
1117 }
1118 }
1119
1120 // Make recursivity check:
1121 $loopExit++;
1122 if ($loopExit > 500) {
1123 return $this->parseError('More than 500 loops, exiting prematurely in parseWhereClause()...', $parseString);
1124 }
1125 }
1126
1127 // Return the stacks lowest level:
1128 return $stack[0];
1129 }
1130
1131 /**
1132 * Parsing the WHERE clause fields in the "WHERE [$parseString] ..." part of a query into a multidimensional array.
1133 * The success of this parsing determines if that part of the query is supported by TYPO3.
1134 *
1135 * @param string WHERE clause to parse. NOTICE: passed by reference!
1136 * @param string Regular expressing to STOP parsing, eg. '^(GROUP BY|ORDER BY|LIMIT)([[:space:]]*)'
1137 * @return mixed If successful parsing, returns an array, otherwise an error string.
1138 */
1139 public function parseFieldDef(&$parseString, $stopRegex = '') {
1140 // Prepare variables:
1141 $parseString = $this->trimSQL($parseString);
1142 $this->lastStopKeyWord = '';
1143 $this->parse_error = '';
1144
1145 $result = array();
1146
1147 // Field type:
1148 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:],]+|\()')) {
1149
1150 // Looking for value:
1151 if (substr($parseString,0,1)=='(') {
1152 $parseString = substr($parseString,1);
1153 if ($result['value'] = $this->nextPart($parseString,'^([^)]*)')) {
1154 $parseString = ltrim(substr($parseString,1));
1155 } else return $this->parseError('No end-parenthesis for value found in parseFieldDef()!',$parseString);
1156 }
1157
1158 // Looking for keywords
1159 while($keyword = $this->nextPart($parseString,'^(DEFAULT|NOT[[:space:]]+NULL|AUTO_INCREMENT|UNSIGNED)([[:space:]]+|,|\))')) {
1160 $keywordCmp = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$keyword));
1161
1162 $result['featureIndex'][$keywordCmp]['keyword'] = $keyword;
1163
1164 switch($keywordCmp) {
1165 case 'DEFAULT':
1166 $result['featureIndex'][$keywordCmp]['value'] = $this->getValue($parseString);
1167 break;
1168 }
1169 }
1170 } else {
1171 return $this->parseError('Field type unknown in parseFieldDef()!',$parseString);
1172 }
1173
1174 return $result;
1175 }
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187 /************************************
1188 *
1189 * Parsing: Helper functions
1190 *
1191 ************************************/
1192
1193 /**
1194 * Strips off a part of the parseString and returns the matching part.
1195 * Helper function for the parsing methods.
1196 *
1197 * @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.
1198 * @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())
1199 * @param boolean If set the full match of the regex is stripped of the beginning of the string!
1200 * @return string The value of the first parenthesis level of the REGEX.
1201 */
1202 protected function nextPart(&$parseString, $regex, $trimAll = FALSE) {
1203 $reg = array();
1204 if (preg_match('/'.$regex.'/i',$parseString.' ', $reg)) { // Adding space char because [[:space:]]+ is often a requirement in regex's
1205 $parseString = ltrim(substr($parseString,strlen($reg[$trimAll?0:1])));
1206 return $reg[1];
1207 }
1208 }
1209
1210 /**
1211 * Finds value in beginning of $parseString, returns result and strips it of parseString
1212 *
1213 * @param string The parseString, eg. "(0,1,2,3) ..." or "('asdf','qwer') ..." or "1234 ..." or "'My string value here' ..."
1214 * @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)
1215 * @param string The mode, eg. "INDEX"
1216 * @return mixed The value (string/integer). Otherwise an array with error message in first key (0)
1217 */
1218 protected function getValue(&$parseString, $comparator = '', $mode = '') {
1219 $value = '';
1220
1221 if (t3lib_div::inList('NOTIN,IN,_LIST',strtoupper(str_replace(array(' ',"\n","\r","\t"),'',$comparator)))) { // List of values:
1222 if ($this->nextPart($parseString,'^([(])')) {
1223 $listValues = array();
1224 $comma=',';
1225
1226 while($comma==',') {
1227 $listValues[] = $this->getValue($parseString);
1228 if ($mode === 'INDEX') {
1229 // Remove any length restriction on INDEX definition
1230 $this->nextPart($parseString, '^([(]\d+[)])');
1231 }
1232 $comma = $this->nextPart($parseString,'^([,])');
1233 }
1234
1235 $out = $this->nextPart($parseString,'^([)])');
1236 if ($out) {
1237 if ($comparator=='_LIST') {
1238 $kVals = array();
1239 foreach ($listValues as $vArr) {
1240 $kVals[] = $vArr[0];
1241 }
1242 return $kVals;
1243 } else {
1244 return $listValues;
1245 }
1246 } else return array($this->parseError('No ) parenthesis in list',$parseString));
1247 } else return array($this->parseError('No ( parenthesis starting the list',$parseString));
1248
1249 } else { // Just plain string value, in quotes or not:
1250
1251 // Quote?
1252 $firstChar = substr($parseString,0,1);
1253 switch($firstChar) {
1254 case '"':
1255 $value = array($this->getValueInQuotes($parseString,'"'),'"');
1256 break;
1257 case "'":
1258 $value = array($this->getValueInQuotes($parseString,"'"),"'");
1259 break;
1260 default:
1261 $reg = array();
1262 if (preg_match('/^([[:alnum:]._-]+)/i',$parseString, $reg)) {
1263 $parseString = ltrim(substr($parseString,strlen($reg[0])));
1264 $value = array($reg[1]);
1265 }
1266 break;
1267 }
1268 }
1269 return $value;
1270 }
1271
1272 /**
1273 * Get value in quotes from $parseString.
1274 * NOTICE: If a query being parsed was prepared for another database than MySQL this function should probably be changed
1275 *
1276 * @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.
1277 * @param string The quote used; input either " or '
1278 * @return string The value, passed through stripslashes() !
1279 */
1280 protected function getValueInQuotes(&$parseString, $quote) {
1281
1282 $parts = explode($quote,substr($parseString,1));
1283 $buffer = '';
1284 foreach($parts as $k => $v) {
1285 $buffer.=$v;
1286
1287 $reg = array();
1288 preg_match('/\\\\$/', $v, $reg);
1289 if ($reg AND strlen($reg[0])%2) {
1290 $buffer.=$quote;
1291 } else {
1292 $parseString = ltrim(substr($parseString,strlen($buffer)+2));
1293 return $this->parseStripslashes($buffer);
1294 }
1295 }
1296 }
1297
1298 /**
1299 * Strip slashes function used for parsing
1300 * NOTICE: If a query being parsed was prepared for another database than MySQL this function should probably be changed
1301 *
1302 * @param string Input string
1303 * @return string Output string
1304 */
1305 protected function parseStripslashes($str) {
1306 $search = array('\\\\', '\\\'', '\\"', '\0', '\n', '\r', '\Z');
1307 $replace = array('\\', '\'', '"', "\x00", "\x0a", "\x0d", "\x1a");
1308
1309 return str_replace($search, $replace, $str);
1310 }
1311
1312 /**
1313 * Add slashes function used for compiling queries
1314 * NOTICE: If a query being parsed was prepared for another database than MySQL this function should probably be changed
1315 *
1316 * @param string Input string
1317 * @return string Output string
1318 */
1319 protected function compileAddslashes($str) {
1320 $search = array('\\', '\'', '"', "\x00", "\x0a", "\x0d", "\x1a");
1321 $replace = array('\\\\', '\\\'', '\\"', '\0', '\n', '\r', '\Z');
1322
1323 return str_replace($search, $replace, $str);
1324 }
1325
1326 /**
1327 * Setting the internal error message value, $this->parse_error and returns that value.
1328 *
1329 * @param string Input error message
1330 * @param string Remaining query to parse.
1331 * @return string Error message.
1332 */
1333 protected function parseError($msg, $restQuery) {
1334 $this->parse_error = 'SQL engine parse ERROR: '.$msg.': near "'.substr($restQuery,0,50).'"';
1335 return $this->parse_error;
1336 }
1337
1338 /**
1339 * Trimming SQL as preparation for parsing.
1340 * ";" in the end is stripped of.
1341 * White space is trimmed away around the value
1342 * A single space-char is added in the end
1343 *
1344 * @param string Input string
1345 * @return string Output string
1346 */
1347 protected function trimSQL($str) {
1348 return trim(rtrim($str, "; \r\n\t")).' ';
1349 }
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362 /*************************
1363 *
1364 * Compiling queries
1365 *
1366 *************************/
1367
1368 /**
1369 * Compiles an SQL query from components
1370 *
1371 * @param array Array of SQL query components
1372 * @return string SQL query
1373 * @see parseSQL()
1374 */
1375 public function compileSQL($components) {
1376 switch($components['type']) {
1377 case 'SELECT':
1378 $query = $this->compileSELECT($components);
1379 break;
1380 case 'UPDATE':
1381 $query = $this->compileUPDATE($components);
1382 break;
1383 case 'INSERT':
1384 $query = $this->compileINSERT($components);
1385 break;
1386 case 'DELETE':
1387 $query = $this->compileDELETE($components);
1388 break;
1389 case 'EXPLAIN':
1390 $query = 'EXPLAIN '.$this->compileSELECT($components);
1391 break;
1392 case 'DROPTABLE':
1393 $query = 'DROP TABLE'.($components['ifExists']?' IF EXISTS':'').' '.$components['TABLE'];
1394 break;
1395 case 'CREATETABLE':
1396 $query = $this->compileCREATETABLE($components);
1397 break;
1398 case 'ALTERTABLE':
1399 $query = $this->compileALTERTABLE($components);
1400 break;
1401 }
1402
1403 return $query;
1404 }
1405
1406 /**
1407 * Compiles a SELECT statement from components array
1408 *
1409 * @param array Array of SQL query components
1410 * @return string SQL SELECT query
1411 * @see parseSELECT()
1412 */
1413 protected function compileSELECT($components) {
1414
1415 // Initialize:
1416 $where = $this->compileWhereClause($components['WHERE']);
1417 $groupBy = $this->compileFieldList($components['GROUPBY']);
1418 $orderBy = $this->compileFieldList($components['ORDERBY']);
1419 $limit = $components['LIMIT'];
1420
1421 // Make query:
1422 $query = 'SELECT '.($components['STRAIGHT_JOIN'] ? $components['STRAIGHT_JOIN'].'' : '').'
1423 '.$this->compileFieldList($components['SELECT']).'
1424 FROM '.$this->compileFromTables($components['FROM']).
1425 (strlen($where)?'
1426 WHERE '.$where : '').
1427 (strlen($groupBy)?'
1428 GROUP BY '.$groupBy : '').
1429 (strlen($orderBy)?'
1430 ORDER BY '.$orderBy : '').
1431 (strlen($limit)?'
1432 LIMIT '.$limit : '');
1433
1434 return $query;
1435 }
1436
1437 /**
1438 * Compiles an UPDATE statement from components array
1439 *
1440 * @param array Array of SQL query components
1441 * @return string SQL UPDATE query
1442 * @see parseUPDATE()
1443 */
1444 protected function compileUPDATE($components) {
1445
1446 // Where clause:
1447 $where = $this->compileWhereClause($components['WHERE']);
1448
1449 // Fields
1450 $fields = array();
1451 foreach($components['FIELDS'] as $fN => $fV) {
1452 $fields[]=$fN.'='.$fV[1].$this->compileAddslashes($fV[0]).$fV[1];
1453 }
1454
1455 // Make query:
1456 $query = 'UPDATE '.$components['TABLE'].' SET
1457 '.implode(',
1458 ',$fields).'
1459 '.(strlen($where)?'
1460 WHERE '.$where : '');
1461
1462 return $query;
1463 }
1464
1465 /**
1466 * Compiles an INSERT statement from components array
1467 *
1468 * @param array Array of SQL query components
1469 * @return string SQL INSERT query
1470 * @see parseINSERT()
1471 */
1472 protected function compileINSERT($components) {
1473
1474 if ($components['VALUES_ONLY']) {
1475 // Initialize:
1476 $fields = array();
1477 foreach($components['VALUES_ONLY'] as $fV) {
1478 $fields[]=$fV[1].$this->compileAddslashes($fV[0]).$fV[1];
1479 }
1480
1481 // Make query:
1482 $query = 'INSERT INTO '.$components['TABLE'].'
1483 VALUES
1484 ('.implode(',
1485 ',$fields).')';
1486 } else {
1487 // Initialize:
1488 $fields = array();
1489 foreach($components['FIELDS'] as $fN => $fV) {
1490 $fields[$fN]=$fV[1].$this->compileAddslashes($fV[0]).$fV[1];
1491 }
1492
1493 // Make query:
1494 $query = 'INSERT INTO '.$components['TABLE'].'
1495 ('.implode(',
1496 ',array_keys($fields)).')
1497 VALUES
1498 ('.implode(',
1499 ',$fields).')';
1500 }
1501
1502 return $query;
1503 }
1504
1505 /**
1506 * Compiles an DELETE statement from components array
1507 *
1508 * @param array Array of SQL query components
1509 * @return string SQL DELETE query
1510 * @see parseDELETE()
1511 */
1512 protected function compileDELETE($components) {
1513
1514 // Where clause:
1515 $where = $this->compileWhereClause($components['WHERE']);
1516
1517 // Make query:
1518 $query = 'DELETE FROM '.$components['TABLE'].
1519 (strlen($where)?'
1520 WHERE '.$where : '');
1521
1522 return $query;
1523 }
1524
1525 /**
1526 * Compiles a CREATE TABLE statement from components array
1527 *
1528 * @param array Array of SQL query components
1529 * @return string SQL CREATE TABLE query
1530 * @see parseCREATETABLE()
1531 */
1532 protected function compileCREATETABLE($components) {
1533
1534 // Create fields and keys:
1535 $fieldsKeys = array();
1536 foreach($components['FIELDS'] as $fN => $fCfg) {
1537 $fieldsKeys[]=$fN.' '.$this->compileFieldCfg($fCfg['definition']);
1538 }
1539 foreach($components['KEYS'] as $kN => $kCfg) {
1540 if ($kN == 'PRIMARYKEY') {
1541 $fieldsKeys[]='PRIMARY KEY ('.implode(',', $kCfg).')';
1542 } elseif ($kN == 'UNIQUE') {
1543 $fieldsKeys[]='UNIQUE '.$kN.' ('.implode(',', $kCfg).')';
1544 } else {
1545 $fieldsKeys[]='KEY '.$kN.' ('.implode(',', $kCfg).')';
1546 }
1547 }
1548
1549 // Make query:
1550 $query = 'CREATE TABLE '.$components['TABLE'].' (
1551 '.implode(',
1552 ', $fieldsKeys).'
1553 )'.($components['tableType'] ? ' TYPE='.$components['tableType'] : '');
1554
1555 return $query;
1556 }
1557
1558 /**
1559 * Compiles an ALTER TABLE statement from components array
1560 *
1561 * @param array Array of SQL query components
1562 * @return string SQL ALTER TABLE query
1563 * @see parseALTERTABLE()
1564 */
1565 protected function compileALTERTABLE($components) {
1566
1567 // Make query:
1568 $query = 'ALTER TABLE '.$components['TABLE'].' '.$components['action'].' '.($components['FIELD']?$components['FIELD']:$components['KEY']);
1569
1570 // Based on action, add the final part:
1571 switch(strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$components['action']))) {
1572 case 'ADD':
1573 $query.=' '.$this->compileFieldCfg($components['definition']);
1574 break;
1575 case 'CHANGE':
1576 $query.=' '.$components['newField'].' '.$this->compileFieldCfg($components['definition']);
1577 break;
1578 case 'DROP':
1579 case 'DROPKEY':
1580 break;
1581 case 'ADDKEY':
1582 case 'ADDPRIMARYKEY':
1583 $query.=' ('.implode(',',$components['fields']).')';
1584 break;
1585 }
1586
1587 // Return query
1588 return $query;
1589 }
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604 /**************************************
1605 *
1606 * Compiling queries, helper functions for parts of queries
1607 *
1608 **************************************/
1609
1610 /**
1611 * Compiles a "SELECT [output] FROM..:" field list based on input array (made with ->parseFieldList())
1612 * Can also compile field lists for ORDER BY and GROUP BY.
1613 *
1614 * @param array Array of select fields, (made with ->parseFieldList())
1615 * @param boolean Whether comments should be compiled
1616 * @return string Select field string
1617 * @see parseFieldList()
1618 */
1619 public function compileFieldList($selectFields, $compileComments = TRUE) {
1620
1621 // Prepare buffer variable:
1622 $fields = '';
1623
1624 // Traverse the selectFields if any:
1625 if (is_array($selectFields)) {
1626 $outputParts = array();
1627 foreach($selectFields as $k => $v) {
1628
1629 // Detecting type:
1630 switch($v['type']) {
1631 case 'function':
1632 $outputParts[$k] = $v['function'].'('.$v['func_content'].')';
1633 break;
1634 case 'flow-control':
1635 if ($v['flow-control']['type'] === 'CASE') {
1636 $outputParts[$k] = $this->compileCaseStatement($v['flow-control']);
1637 }
1638 break;
1639 case 'field':
1640 $outputParts[$k] = ($v['distinct']?$v['distinct']:'').($v['table']?$v['table'].'.':'').$v['field'];
1641 break;
1642 }
1643
1644 // Alias:
1645 if ($v['as']) {
1646 $outputParts[$k].= ' '.$v['as_keyword'].' '.$v['as'];
1647 }
1648
1649 // Specifically for ORDER BY and GROUP BY field lists:
1650 if ($v['sortDir']) {
1651 $outputParts[$k].= ' '.$v['sortDir'];
1652 }
1653 }
1654 if ($compileComments && $selectFields[0]['comments']) {
1655 $fields = $selectFields[0]['comments'] . ' ';
1656 }
1657 $fields .= implode(', ', $outputParts);
1658 }
1659
1660 return $fields;
1661 }
1662
1663 /**
1664 * Compiles a CASE ... WHEN flow-control construct based on input array (made with ->parseCaseStatement())
1665 *
1666 * @param array Array of case components, (made with ->parseCaseStatement())
1667 * @return string case when string
1668 * @see parseCaseStatement()
1669 */
1670 protected function compileCaseStatement(array $components) {
1671 $statement = 'CASE';
1672 if (isset($components['case_field'])) {
1673 $statement .= ' ' . $components['case_field'];
1674 } elseif (isset($components['case_value'])) {
1675 $statement .= ' ' . $components['case_value'][1] . $components['case_value'][0] . $components['case_value'][1];
1676 }
1677 foreach ($components['when'] as $when) {
1678 $statement .= ' WHEN ';
1679 $statement .= $this->compileWhereClause($when['when_value']);
1680 $statement .= ' THEN ';
1681 $statement .= $when['then_value'][1] . $when['then_value'][0] . $when['then_value'][1];
1682 }
1683 if (isset($components['else'])) {
1684 $statement .= ' ELSE ';
1685 $statement .= $components['else'][1] . $components['else'][0] . $components['else'][1];
1686 }
1687 $statement .= ' END';
1688 return $statement;
1689 }
1690
1691 /**
1692 * Compiles a "FROM [output] WHERE..:" table list based on input array (made with ->parseFromTables())
1693 *
1694 * @param array Array of table names, (made with ->parseFromTables())
1695 * @return string Table name string
1696 * @see parseFromTables()
1697 */
1698 public function compileFromTables($tablesArray) {
1699
1700 // Prepare buffer variable:
1701 $outputParts = array();
1702
1703 // Traverse the table names:
1704 if (is_array($tablesArray)) {
1705 foreach ($tablesArray as $k => $v) {
1706
1707 // Set table name:
1708 $outputParts[$k] = $v['table'];
1709
1710 // Add alias AS if there:
1711 if ($v['as']) {
1712 $outputParts[$k] .= ' ' . $v['as_keyword'] . ' ' . $v['as'];
1713 }
1714
1715 if (is_array($v['JOIN'])) {
1716 foreach ($v['JOIN'] as $join) {
1717 $outputParts[$k] .= ' ' . $join['type'] . ' ' . $join['withTable'];
1718 // Add alias AS if there:
1719 if (isset($join['as']) && $join['as']) {
1720 $outputParts[$k] .= ' ' . $join['as_keyword'] . ' ' . $join['as'];
1721 }
1722 $outputParts[$k] .= ' ON ';
1723 $outputParts[$k] .= ($join['ON'][0]['table']) ? $join['ON'][0]['table'] . '.' : '';
1724 $outputParts[$k] .= $join['ON'][0]['field'];
1725 $outputParts[$k] .= '=';
1726 $outputParts[$k] .= ($join['ON'][1]['table']) ? $join['ON'][1]['table'] . '.' : '';
1727 $outputParts[$k] .= $join['ON'][1]['field'];
1728 }
1729 }
1730 }
1731 }
1732
1733 // Return imploded buffer:
1734 return implode(', ', $outputParts);
1735 }
1736
1737 /**
1738 * Implodes an array of WHERE clause configuration into a WHERE clause.
1739 *
1740 * @param array WHERE clause configuration
1741 * @return string WHERE clause as string.
1742 * @see explodeWhereClause()
1743 */
1744 public function compileWhereClause($clauseArray) {
1745
1746 // Prepare buffer variable:
1747 $output = '';
1748
1749 // Traverse clause array:
1750 if (is_array($clauseArray)) {
1751 foreach ($clauseArray as $k => $v) {
1752
1753 // Set operator:
1754 $output .= $v['operator'] ? ' ' . $v['operator'] : '';
1755
1756 // Look for sublevel:
1757 if (is_array($v['sub'])) {
1758 $output .= ' (' . trim($this->compileWhereClause($v['sub'])) . ')';
1759 } elseif (isset($v['func'])) {
1760 $output .= ' ' . trim($v['modifier']) . ' ' . $v['func']['type'] . ' (' . $this->compileSELECT($v['func']['subquery']) . ')';
1761 } else {
1762
1763 // Set field/table with modifying prefix if any:
1764 $output .= ' ' . trim($v['modifier'] . ' ' . ($v['table'] ? $v['table'] . '.' : '') . $v['field']);
1765
1766 // Set calculation, if any:
1767 if ($v['calc']) {
1768 $output .= $v['calc'] . $v['calc_value'][1] . $this->compileAddslashes($v['calc_value'][0]) . $v['calc_value'][1];
1769 }
1770
1771 // Set comparator:
1772 if ($v['comparator']) {
1773 $output .= ' ' . $v['comparator'];
1774
1775 // Detecting value type; list or plain:
1776 if (t3lib_div::inList('NOTIN,IN', strtoupper(str_replace(array(' ', "\t", "\r", "\n"), '', $v['comparator'])))) {
1777 if (isset($v['subquery'])) {
1778 $output .= ' (' . $this->compileSELECT($v['subquery']) . ')';
1779 } else {
1780 $valueBuffer = array();
1781 foreach ($v['value'] as $realValue) {
1782 $valueBuffer[] = $realValue[1] . $this->compileAddslashes($realValue[0]) . $realValue[1];
1783 }
1784 $output .= ' (' . trim(implode(',', $valueBuffer)) . ')';
1785 }
1786 } else if (isset($v['value']['operator'])) {
1787 $values = array();
1788 foreach ($v['value']['args'] as $fieldDef) {
1789 $values[] = ($fieldDef['table'] ? $fieldDef['table'] . '.' : '') . $fieldDef['field'];
1790 }
1791 $output .= ' ' . $v['value']['operator'] . '(' . implode(',', $values) . ')';
1792 } else {
1793 $output .= ' ' . $v['value'][1] . $this->compileAddslashes($v['value'][0]) . $v['value'][1];
1794 }
1795 }
1796 }
1797 }
1798 }
1799
1800 // Return output buffer:
1801 return $output;
1802 }
1803
1804 /**
1805 * Compile field definition
1806 *
1807 * @param array Field definition parts
1808 * @return string Field definition string
1809 */
1810 public function compileFieldCfg($fieldCfg) {
1811
1812 // Set type:
1813 $cfg = $fieldCfg['fieldType'];
1814
1815 // Add value, if any:
1816 if (strlen($fieldCfg['value'])) {
1817 $cfg.='('.$fieldCfg['value'].')';
1818 }
1819
1820 // Add additional features:
1821 if (is_array($fieldCfg['featureIndex'])) {
1822 foreach($fieldCfg['featureIndex'] as $featureDef) {
1823 $cfg.=' '.$featureDef['keyword'];
1824
1825 // Add value if found:
1826 if (is_array($featureDef['value'])) {
1827 $cfg.=' '.$featureDef['value'][1].$this->compileAddslashes($featureDef['value'][0]).$featureDef['value'][1];
1828 }
1829 }
1830 }
1831
1832 // Return field definition string:
1833 return $cfg;
1834 }
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846 /*************************
1847 *
1848 * Debugging
1849 *
1850 *************************/
1851
1852 /**
1853 * Check parsability of input SQL part string; Will parse and re-compile after which it is compared
1854 *
1855 * @param string Part definition of string; "SELECT" = fieldlist (also ORDER BY and GROUP BY), "FROM" = table list, "WHERE" = Where clause.
1856 * @param string SQL string to verify parsability of
1857 * @return mixed Returns array with string 1 and 2 if error, otherwise false
1858 */
1859 public function debug_parseSQLpart($part, $str) {
1860 $retVal = false;
1861
1862 switch($part) {
1863 case 'SELECT':
1864 $retVal = $this->debug_parseSQLpartCompare($str,$this->compileFieldList($this->parseFieldList($str)));
1865 break;
1866 case 'FROM':
1867 $retVal = $this->debug_parseSQLpartCompare($str,$this->compileFromTables($this->parseFromTables($str)));
1868 break;
1869 case 'WHERE':
1870 $retVal = $this->debug_parseSQLpartCompare($str,$this->compileWhereClause($this->parseWhereClause($str)));
1871 break;
1872 }
1873 return $retVal;
1874 }
1875
1876 /**
1877 * Compare two query strins by stripping away whitespace.
1878 *
1879 * @param string SQL String 1
1880 * @param string SQL string 2
1881 * @param boolean If true, the strings are compared insensitive to case
1882 * @return mixed Returns array with string 1 and 2 if error, otherwise false
1883 */
1884 public function debug_parseSQLpartCompare($str, $newStr, $caseInsensitive = FALSE) {
1885 if ($caseInsensitive) {
1886 $str1 = strtoupper($str);
1887 $str2 = strtoupper($newStr);
1888 } else {
1889 $str1 = $str;
1890 $str2 = $newStr;
1891 }
1892
1893 // Fixing escaped chars:
1894 $search = array('\0', '\n', '\r', '\Z');
1895 $replace = array("\x00", "\x0a", "\x0d", "\x1a");
1896 $str1 = str_replace($search, $replace, $str1);
1897 $str2 = str_replace($search, $replace, $str2);
1898
1899 # Normally, commented out since they are needed only in tricky cases...
1900 # $str1 = stripslashes($str1);
1901 # $str2 = stripslashes($str2);
1902
1903 if (strcmp(str_replace(array(' ',"\t","\r","\n"),'',$this->trimSQL($str1)),str_replace(array(' ',"\t","\r","\n"),'',$this->trimSQL($str2)))) {
1904 return array(
1905 str_replace(array(' ',"\t","\r","\n"),' ',$str),
1906 str_replace(array(' ',"\t","\r","\n"),' ',$newStr),
1907 );
1908 }
1909 }
1910
1911 /**
1912 * 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
1913 *
1914 * @param string SQL query
1915 * @return string Query if all is well, otherwise exit.
1916 */
1917 public function debug_testSQL($SQLquery) {
1918
1919 // Getting result array:
1920 $parseResult = $this->parseSQL($SQLquery);
1921
1922 // If result array was returned, proceed. Otherwise show error and exit.
1923 if (is_array($parseResult)) {
1924
1925 // Re-compile query:
1926 $newQuery = $this->compileSQL($parseResult);
1927
1928 // TEST the new query:
1929 $testResult = $this->debug_parseSQLpartCompare($SQLquery, $newQuery);
1930
1931 // Return new query if OK, otherwise show error and exit:
1932 if (!is_array($testResult)) {
1933 return $newQuery;
1934 } else {
1935 debug(array('ERROR MESSAGE'=>'Input query did not match the parsed and recompiled query exactly (not observing whitespace)', 'TEST result' => $testResult),'SQL parsing failed:');
1936 exit;
1937 }
1938 } else {
1939 debug(array('query' => $SQLquery, 'ERROR MESSAGE'=>$parseResult),'SQL parsing failed:');
1940 exit;
1941 }
1942 }
1943 }
1944
1945
1946 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_sqlparser.php']) {
1947 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_sqlparser.php']);
1948 }
1949
1950 ?>