2 /***************************************************************
5 * (c) 2004 Kasper Skaarhoj (kasperYYYY@typo3.com)
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
32 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
35 * [CLASS/FUNCTION INDEX of SCRIPT]
39 * 106: class t3lib_sqlparser
41 * SECTION: SQL Parsing, full queries
42 * 128: function parseSQL($parseString)
43 * 188: function parseSELECT($parseString)
44 * 257: function parseUPDATE($parseString)
45 * 311: function parseINSERT($parseString)
46 * 371: function parseDELETE($parseString)
47 * 409: function parseEXPLAIN($parseString)
48 * 431: function parseCREATETABLE($parseString)
49 * 503: function parseALTERTABLE($parseString)
50 * 572: function parseDROPTABLE($parseString)
52 * SECTION: SQL Parsing, helper functions for parts of queries
53 * 631: function parseFieldList(&$parseString, $stopRegex='')
54 * 749: function parseFromTables(&$parseString, $stopRegex='')
55 * 816: function parseWhereClause(&$parseString, $stopRegex='')
56 * 924: function parseFieldDef(&$parseString, $stopRegex='')
58 * SECTION: Parsing: Helper functions
59 * 985: function nextPart(&$parseString,$regex,$trimAll=FALSE)
60 * 999: function getValue(&$parseString,$comparator='')
61 * 1054: function getValueInQuotes(&$parseString,$quote)
62 * 1079: function parseStripslashes($str)
63 * 1093: function compileAddslashes($str)
64 * 1107: function parseError($msg,$restQuery)
65 * 1121: function trimSQL($str)
67 * SECTION: Compiling queries
68 * 1149: function compileSQL($components)
69 * 1187: function compileSELECT($components)
70 * 1218: function compileUPDATE($components)
71 * 1246: function compileINSERT($components)
72 * 1286: function compileDELETE($components)
73 * 1306: function compileCREATETABLE($components)
74 * 1337: function compileALTERTABLE($components)
76 * SECTION: Compiling queries, helper functions for parts of queries
77 * 1390: function compileFieldList($selectFields)
78 * 1432: function compileFromTables($tablesArray)
79 * 1468: function compileWhereClause($clauseArray)
80 * 1522: function compileFieldCfg($fieldCfg)
83 * 1571: function debug_parseSQLpart($part,$str)
84 * 1593: function debug_parseSQLpartCompare($str,$newStr,$caseInsensitive=FALSE)
85 * 1626: function debug_testSQL($SQLquery)
88 * (This index is automatically created/updated by the extension "extdeveval")
100 * TYPO3 SQL parser class.
102 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
106 class t3lib_sqlparser
{
109 var $parse_error = ''; // Parsing error string
110 var $lastStopKeyWord = ''; // Last stop keyword used.
115 /*************************************
117 * SQL Parsing, full queries
119 **************************************/
122 * Parses any single SQL query
124 * @param string SQL query
125 * @return array Result array with all the parts in - or error message string
126 * @see compileSQL(), debug_testSQL()
128 function parseSQL($parseString) {
129 // Prepare variables:
130 $parseString = $this->trimSQL($parseString);
131 $this->parse_error
= '';
134 // Finding starting keyword of string:
135 $_parseString = $parseString; // Protecting original string...
136 $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:]]+');
137 $keyword = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$keyword));
141 // Parsing SELECT query:
142 $result = $this->parseSELECT($parseString);
145 // Parsing UPDATE query:
146 $result = $this->parseUPDATE($parseString);
149 // Parsing INSERT query:
150 $result = $this->parseINSERT($parseString);
153 // Parsing DELETE query:
154 $result = $this->parseDELETE($parseString);
157 // Parsing EXPLAIN SELECT query:
158 $result = $this->parseEXPLAIN($parseString);
161 // Parsing DROP TABLE query:
162 $result = $this->parseDROPTABLE($parseString);
165 // Parsing ALTER TABLE query:
166 $result = $this->parseALTERTABLE($parseString);
169 // Parsing CREATE TABLE query:
170 $result = $this->parseCREATETABLE($parseString);
172 case 'CREATEDATABASE':
173 // Parsing CREATE DATABASE query:
174 $result = $this->parseCREATEDATABASE($parseString);
177 return $this->parseError('"'.$keyword.'" is not a keyword',$parseString);
185 * Parsing SELECT query
187 * @param string SQL string with SELECT query to parse
188 * @return mixed Returns array with components of SELECT query on success, otherwise an error message string.
189 * @see compileSELECT()
191 function parseSELECT($parseString) {
194 $parseString = $this->trimSQL($parseString);
195 $parseString = ltrim(substr($parseString,6)); // REMOVE eregi_replace('^SELECT[[:space:]]+','',$parseString);
197 // Init output variable:
199 $result['type'] = 'SELECT';
201 // Looking for STRAIGHT_JOIN keyword:
202 $result['STRAIGHT_JOIN'] = $this->nextPart($parseString, '^(STRAIGHT_JOIN)[[:space:]]+');
205 $result['SELECT'] = $this->parseFieldList($parseString, '^(FROM)[[:space:]]+');
206 if ($this->parse_error
) { return $this->parse_error
; }
208 // Continue if string is not ended:
212 $result['FROM'] = $this->parseFromTables($parseString, '^(WHERE)[[:space:]]+');
213 if ($this->parse_error
) { return $this->parse_error
; }
215 // If there are more than just the tables (a WHERE clause that would be...)
219 $result['WHERE'] = $this->parseWhereClause($parseString, '^(GROUP[[:space:]]+BY|ORDER[[:space:]]+BY|LIMIT)[[:space:]]+');
220 if ($this->parse_error
) { return $this->parse_error
; }
222 // If the WHERE clause parsing was stopped by GROUP BY, ORDER BY or LIMIT, then proceed with parsing:
223 if ($this->lastStopKeyWord
) {
226 if ($this->lastStopKeyWord
== 'GROUPBY') {
227 $result['GROUPBY'] = $this->parseFieldList($parseString, '^(ORDER[[:space:]]+BY|LIMIT)[[:space:]]+');
228 if ($this->parse_error
) { return $this->parse_error
; }
232 if ($this->lastStopKeyWord
== 'ORDERBY') {
233 $result['ORDERBY'] = $this->parseFieldList($parseString, '^(LIMIT)[[:space:]]+');
234 if ($this->parse_error
) { return $this->parse_error
; }
238 if ($this->lastStopKeyWord
== 'LIMIT') {
239 if (preg_match('/^([0-9]+|[0-9]+[[:space:]]*,[[:space:]]*[0-9]+)$/',trim($parseString))) {
240 $result['LIMIT'] = $parseString;
242 return $this->parseError('No value for limit!',$parseString);
247 } else return $this->parseError('No table to select from!',$parseString);
254 * Parsing UPDATE query
256 * @param string SQL string with UPDATE query to parse
257 * @return mixed Returns array with components of UPDATE query on success, otherwise an error message string.
258 * @see compileUPDATE()
260 function parseUPDATE($parseString) {
263 $parseString = $this->trimSQL($parseString);
264 $parseString = ltrim(substr($parseString,6)); // REMOVE eregi_replace('^UPDATE[[:space:]]+','',$parseString);
266 // Init output variable:
268 $result['type'] = 'UPDATE';
271 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
273 // Continue if string is not ended:
274 if ($result['TABLE']) {
275 if ($parseString && $this->nextPart($parseString, '^(SET)[[:space:]]+')) {
279 // Get field/value pairs:
281 if ($fieldName = $this->nextPart($parseString,'^([[:alnum:]_]+)[[:space:]]*=')) {
282 $this->nextPart($parseString,'^(=)'); // Strip of "=" sign.
283 $value = $this->getValue($parseString);
284 $result['FIELDS'][$fieldName] = $value;
285 } else return $this->parseError('No fieldname found',$parseString);
287 $comma = $this->nextPart($parseString,'^(,)');
291 if ($this->nextPart($parseString,'^(WHERE)')) {
292 $result['WHERE'] = $this->parseWhereClause($parseString);
293 if ($this->parse_error
) { return $this->parse_error
; }
295 } else return $this->parseError('Query missing SET...',$parseString);
296 } else return $this->parseError('No table found!',$parseString);
298 // Should be no more content now:
300 return $this->parseError('Still content in clause after parsing!',$parseString);
308 * Parsing INSERT query
310 * @param string SQL string with INSERT query to parse
311 * @return mixed Returns array with components of INSERT query on success, otherwise an error message string.
312 * @see compileINSERT()
314 function parseINSERT($parseString) {
317 $parseString = $this->trimSQL($parseString);
318 $parseString = ltrim(substr(ltrim(substr($parseString,6)),4)); // REMOVE eregi_replace('^INSERT[[:space:]]+INTO[[:space:]]+','',$parseString);
320 // Init output variable:
322 $result['type'] = 'INSERT';
325 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)([[:space:]]+|\()');
327 if ($result['TABLE']) {
329 if ($this->nextPart($parseString,'^(VALUES)[[:space:]]+')) { // In this case there are no field names mentioned in the SQL!
330 // Get values/fieldnames (depending...)
331 $result['VALUES_ONLY'] = $this->getValue($parseString,'IN');
332 if ($this->parse_error
) { return $this->parse_error
; }
333 } else { // There are apparently fieldnames listed:
334 $fieldNames = $this->getValue($parseString,'_LIST');
335 if ($this->parse_error
) { return $this->parse_error
; }
337 if ($this->nextPart($parseString,'^(VALUES)[[:space:]]+')) { // "VALUES" keyword binds the fieldnames to values:
339 $values = $this->getValue($parseString,'IN'); // Using the "getValue" function to get the field list...
340 if ($this->parse_error
) { return $this->parse_error
; }
342 foreach($fieldNames as $k => $fN) {
343 if (preg_match('/^[[:alnum:]_]+$/',$fN)) {
344 if (isset($values[$k])) {
345 if (!isset($result['FIELDS'][$fN])) {
346 $result['FIELDS'][$fN] = $values[$k];
347 } else return $this->parseError('Fieldname ("'.$fN.'") already found in list!',$parseString);
348 } else return $this->parseError('No value set!',$parseString);
349 } else return $this->parseError('Invalid fieldname ("'.$fN.'")',$parseString);
351 if (isset($values[$k+
1])) {
352 return $this->parseError('Too many values in list!',$parseString);
354 } else return $this->parseError('VALUES keyword expected',$parseString);
356 } else return $this->parseError('No table found!',$parseString);
358 // Should be no more content now:
360 return $this->parseError('Still content after parsing!',$parseString);
368 * Parsing DELETE query
370 * @param string SQL string with DELETE query to parse
371 * @return mixed Returns array with components of DELETE query on success, otherwise an error message string.
372 * @see compileDELETE()
374 function parseDELETE($parseString) {
377 $parseString = $this->trimSQL($parseString);
378 $parseString = ltrim(substr(ltrim(substr($parseString,6)),4)); // REMOVE eregi_replace('^DELETE[[:space:]]+FROM[[:space:]]+','',$parseString);
380 // Init output variable:
382 $result['type'] = 'DELETE';
385 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
387 if ($result['TABLE']) {
390 if ($this->nextPart($parseString,'^(WHERE)')) {
391 $result['WHERE'] = $this->parseWhereClause($parseString);
392 if ($this->parse_error
) { return $this->parse_error
; }
394 } else return $this->parseError('No table found!',$parseString);
396 // Should be no more content now:
398 return $this->parseError('Still content in clause after parsing!',$parseString);
406 * Parsing EXPLAIN query
408 * @param string SQL string with EXPLAIN query to parse
409 * @return mixed Returns array with components of EXPLAIN query on success, otherwise an error message string.
412 function parseEXPLAIN($parseString) {
415 $parseString = $this->trimSQL($parseString);
416 $parseString = ltrim(substr($parseString,6)); // REMOVE eregi_replace('^EXPLAIN[[:space:]]+','',$parseString);
418 // Init output variable:
419 $result = $this->parseSELECT($parseString);
420 if (is_array($result)) {
421 $result['type'] = 'EXPLAIN';
428 * Parsing CREATE TABLE query
430 * @param string SQL string starting with CREATE TABLE
431 * @return mixed Returns array with components of CREATE TABLE query on success, otherwise an error message string.
432 * @see compileCREATETABLE()
434 function parseCREATETABLE($parseString) {
436 // Removing CREATE TABLE
437 $parseString = $this->trimSQL($parseString);
438 $parseString = ltrim(substr(ltrim(substr($parseString,6)),5)); // REMOVE eregi_replace('^CREATE[[:space:]]+TABLE[[:space:]]+','',$parseString);
440 // Init output variable:
442 $result['type'] = 'CREATETABLE';
445 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]*\(',TRUE);
447 if ($result['TABLE']) {
449 // While the parseString is not yet empty:
450 while(strlen($parseString)>0) {
451 if ($key = $this->nextPart($parseString, '^(KEY|PRIMARY KEY)([[:space:]]+|\()')) { // Getting key
452 $key = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$key));
456 $result['KEYS'][$key] = $this->getValue($parseString,'_LIST');
457 if ($this->parse_error
) { return $this->parse_error
; }
460 if ($keyName = $this->nextPart($parseString, '^([[:alnum:]_]+)([[:space:]]+|\()')) {
461 $result['KEYS'][$keyName] = $this->getValue($parseString,'_LIST');
462 if ($this->parse_error
) { return $this->parse_error
; }
463 } else return $this->parseError('No keyname found',$parseString);
466 } elseif ($fieldName = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+')) { // Getting field:
467 $result['FIELDS'][$fieldName]['definition'] = $this->parseFieldDef($parseString);
468 if ($this->parse_error
) { return $this->parse_error
; }
471 // Finding delimiter:
472 $delim = $this->nextPart($parseString, '^(,|\))');
474 return $this->parseError('No delimiter found',$parseString);
475 } elseif ($delim==')') {
480 // Finding what is after the table definition - table type in MySQL
482 if ($this->nextPart($parseString, '^(TYPE[[:space:]]*=)')) {
483 $result['tableType'] = $parseString;
486 } else return $this->parseError('No fieldname found!',$parseString);
488 // Getting table type
489 } else return $this->parseError('No table found!',$parseString);
491 // Should be no more content now:
493 return $this->parseError('Still content in clause after parsing!',$parseString);
500 * Parsing ALTER TABLE query
502 * @param string SQL string starting with ALTER TABLE
503 * @return mixed Returns array with components of ALTER TABLE query on success, otherwise an error message string.
504 * @see compileALTERTABLE()
506 function parseALTERTABLE($parseString) {
508 // Removing ALTER TABLE
509 $parseString = $this->trimSQL($parseString);
510 $parseString = ltrim(substr(ltrim(substr($parseString,5)),5)); // REMOVE eregi_replace('^ALTER[[:space:]]+TABLE[[:space:]]+','',$parseString);
512 // Init output variable:
514 $result['type'] = 'ALTERTABLE';
517 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
519 if ($result['TABLE']) {
520 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:]]+|\()')) {
521 $actionKey = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$result['action']));
524 if (t3lib_div
::inList('ADDPRIMARYKEY,DROPPRIMARYKEY',$actionKey) ||
$fieldKey = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+')) {
528 $result['FIELD'] = $fieldKey;
529 $result['definition'] = $this->parseFieldDef($parseString);
530 if ($this->parse_error
) { return $this->parse_error
; }
534 $result['FIELD'] = $fieldKey;
537 $result['FIELD'] = $fieldKey;
538 if ($result['newField'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+')) {
539 $result['definition'] = $this->parseFieldDef($parseString);
540 if ($this->parse_error
) { return $this->parse_error
; }
541 } else return $this->parseError('No NEW field name found',$parseString);
545 case 'ADDPRIMARYKEY':
546 $result['KEY'] = $fieldKey;
547 $result['fields'] = $this->getValue($parseString,'_LIST');
548 if ($this->parse_error
) { return $this->parse_error
; }
551 $result['KEY'] = $fieldKey;
553 case 'DROPPRIMARYKEY':
557 } else return $this->parseError('No field name found',$parseString);
558 } else return $this->parseError('No action CHANGE, DROP or ADD found!',$parseString);
559 } else return $this->parseError('No table found!',$parseString);
561 // Should be no more content now:
563 return $this->parseError('Still content in clause after parsing!',$parseString);
570 * Parsing DROP TABLE query
572 * @param string SQL string starting with DROP TABLE
573 * @return mixed Returns array with components of DROP TABLE query on success, otherwise an error message string.
575 function parseDROPTABLE($parseString) {
577 // Removing DROP TABLE
578 $parseString = $this->trimSQL($parseString);
579 $parseString = ltrim(substr(ltrim(substr($parseString,4)),5)); // eregi_replace('^DROP[[:space:]]+TABLE[[:space:]]+','',$parseString);
581 // Init output variable:
583 $result['type'] = 'DROPTABLE';
586 $result['ifExists'] = $this->nextPart($parseString, '^(IF[[:space:]]+EXISTS[[:space:]]+)');
589 $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
591 if ($result['TABLE']) {
593 // Should be no more content now:
595 return $this->parseError('Still content in clause after parsing!',$parseString);
599 } else return $this->parseError('No table found!',$parseString);
603 * Parsing CREATE DATABASE query
605 * @param string SQL string starting with CREATE DATABASE
606 * @return mixed Returns array with components of CREATE DATABASE query on success, otherwise an error message string.
608 function parseCREATEDATABASE($parseString) {
610 // Removing CREATE DATABASE
611 $parseString = $this->trimSQL($parseString);
612 $parseString = ltrim(substr(ltrim(substr($parseString,6)),8)); // eregi_replace('^CREATE[[:space:]]+DATABASE[[:space:]]+','',$parseString);
614 // Init output variable:
616 $result['type'] = 'CREATEDATABASE';
619 $result['DATABASE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
621 if ($result['DATABASE']) {
623 // Should be no more content now:
625 return $this->parseError('Still content in clause after parsing!',$parseString);
629 } else return $this->parseError('No database found!',$parseString);
646 /**************************************
648 * SQL Parsing, helper functions for parts of queries
650 **************************************/
653 * Parsing the fields in the "SELECT [$selectFields] FROM" part of a query into an array.
654 * The output from this function can be compiled back into a field list with ->compileFieldList()
655 * 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!
657 * @param string The string with fieldnames, eg. "title, uid AS myUid, max(tstamp), count(*)" etc. NOTICE: passed by reference!
658 * @param string Regular expressing to STOP parsing, eg. '^(FROM)([[:space:]]*)'
659 * @return array If successful parsing, returns an array, otherwise an error string.
660 * @see compileFieldList()
662 function parseFieldList(&$parseString, $stopRegex='') {
664 // Prepare variables:
665 $parseString = $this->trimSQL($parseString);
666 $this->lastStopKeyWord
= '';
667 $this->parse_error
= '';
670 $stack = array(); // Contains the parsed content
671 $pnt = 0; // Pointer to positions in $stack
672 $level = 0; // Indicates the parenthesis level we are at.
673 $loopExit = 0; // Recursivity brake.
675 // $parseString is continously shortend by the process and we keep parsing it till it is zero:
676 while (strlen($parseString)) {
678 // Checking if we are inside / outside parenthesis (in case of a function like count(), max(), min() etc...):
679 if ($level>0) { // Inside parenthesis here (does NOT detect if values in quotes are used, the only token is ")" or "("):
681 // Accumulate function content until next () parenthesis:
682 $funcContent = $this->nextPart($parseString,'^([^()]*.)');
683 $stack[$pnt]['func_content.'][] = array(
685 'func_content' => substr($funcContent,0,-1)
687 $stack[$pnt]['func_content'].= $funcContent;
690 switch(substr($stack[$pnt]['func_content'],-1)) {
696 if (!$level) { // If this was the last parenthesis:
697 $stack[$pnt]['func_content'] = substr($stack[$pnt]['func_content'],0,-1);
698 $parseString = ltrim($parseString); // Remove any whitespace after the parenthesis.
702 } else { // Outside parenthesis, looking for next field:
704 // Looking for a known function (only known functions supported)
705 $func = $this->nextPart($parseString,'^(count|max|min|floor|sum|avg)[[:space:]]*\(');
707 $parseString = trim(substr($parseString,1)); // Strip of "("
708 $stack[$pnt]['type'] = 'function';
709 $stack[$pnt]['function'] = $func;
710 $level++
; // increse parenthesis level counter.
712 // Otherwise, look for regular fieldname:
713 if ($fieldName = $this->nextPart($parseString,'^([[:alnum:]\*._]+)(,|[[:space:]]+)')) {
714 $stack[$pnt]['type'] = 'field';
716 // Explode fieldname into field and table:
717 $tableField = explode('.',$fieldName,2);
718 if (count($tableField)==2) {
719 $stack[$pnt]['table'] = $tableField[0];
720 $stack[$pnt]['field'] = $tableField[1];
722 $stack[$pnt]['table'] = '';
723 $stack[$pnt]['field'] = $tableField[0];
726 return $this->parseError('No field name found as expected',$parseString);
731 // After a function or field we look for "AS" alias and a comma to separate to the next field in the list:
734 // Looking for "AS" alias:
735 if ($as = $this->nextPart($parseString,'^(AS)[[:space:]]+')) {
736 $stack[$pnt]['as'] = $this->nextPart($parseString,'^([[:alnum:]_]+)(,|[[:space:]]+)');
737 $stack[$pnt]['as_keyword'] = $as;
740 // Looking for "ASC" or "DESC" keywords (for ORDER BY)
741 if ($sDir = $this->nextPart($parseString,'^(ASC|DESC)([[:space:]]+|,)')) {
742 $stack[$pnt]['sortDir'] = $sDir;
745 // Looking for stop-keywords:
746 if ($stopRegex && $this->lastStopKeyWord
= $this->nextPart($parseString, $stopRegex)) {
747 $this->lastStopKeyWord
= strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$this->lastStopKeyWord
));
751 // Looking for comma (since the stop-keyword did not trigger a return...)
752 if (strlen($parseString) && !$this->nextPart($parseString,'^(,)')) {
753 return $this->parseError('No comma found as expected',$parseString);
756 // Increasing pointer:
760 // Check recursivity brake:
763 return $this->parseError('More than 500 loops, exiting prematurely...',$parseString);
767 // Return result array:
772 * Parsing the tablenames in the "FROM [$parseString] WHERE" part of a query into an array.
773 * The success of this parsing determines if that part of the query is supported by TYPO3.
775 * @param string list of tables, eg. "pages, tt_content" or "pages A, pages B". NOTICE: passed by reference!
776 * @param string Regular expressing to STOP parsing, eg. '^(WHERE)([[:space:]]*)'
777 * @return array If successful parsing, returns an array, otherwise an error string.
778 * @see compileFromTables()
780 function parseFromTables(&$parseString, $stopRegex='') {
782 // Prepare variables:
783 $parseString = $this->trimSQL($parseString);
784 $this->lastStopKeyWord
= '';
785 $this->parse_error
= '';
787 $stack = array(); // Contains the parsed content
788 $pnt = 0; // Pointer to positions in $stack
789 $loopExit = 0; // Recursivity brake.
791 // $parseString is continously shortend by the process and we keep parsing it till it is zero:
792 while (strlen($parseString)) {
793 // Looking for the table:
794 if ($stack[$pnt]['table'] = $this->nextPart($parseString,'^([[:alnum:]_]+)(,|[[:space:]]+)')) {
795 $stack[$pnt]['as'] = $this->nextPart($parseString,'^([[:alnum:]_]+)[[:space:]]*');
796 } else return $this->parseError('No table name found as expected!',$parseString);
799 if ($join = $this->nextPart($parseString,'^(JOIN|LEFT[[:space:]]+JOIN)[[:space:]]+')) {
800 $stack[$pnt]['JOIN']['type'] = $join;
801 if ($stack[$pnt]['JOIN']['withTable'] = $this->nextPart($parseString,'^([[:alnum:]_]+)[[:space:]]+ON[[:space:]]+',1)) {
802 $field1 = $this->nextPart($parseString,'^([[:alnum:]_.]+)[[:space:]]*=[[:space:]]*',1);
803 $field2 = $this->nextPart($parseString,'^([[:alnum:]_.]+)[[:space:]]+');
804 if ($field1 && $field2) {
805 $stack[$pnt]['JOIN']['ON'] = array($field1,$field2);
806 } else return $this->parseError('No join fields found!',$parseString);
807 } else return $this->parseError('No join table found!',$parseString);
810 // Looking for stop-keywords:
811 if ($stopRegex && $this->lastStopKeyWord
= $this->nextPart($parseString, $stopRegex)) {
812 $this->lastStopKeyWord
= strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$this->lastStopKeyWord
));
816 // Looking for comma:
817 if (strlen($parseString) && !$this->nextPart($parseString,'^(,)')) {
818 return $this->parseError('No comma found as expected',$parseString);
821 // Increasing pointer:
824 // Check recursivity brake:
827 return $this->parseError('More than 500 loops, exiting prematurely...',$parseString);
831 // Return result array:
836 * Parsing the WHERE clause fields in the "WHERE [$parseString] ..." part of a query into a multidimensional array.
837 * The success of this parsing determines if that part of the query is supported by TYPO3.
839 * @param string WHERE clause to parse. NOTICE: passed by reference!
840 * @param string Regular expressing to STOP parsing, eg. '^(GROUP BY|ORDER BY|LIMIT)([[:space:]]*)'
841 * @return mixed If successful parsing, returns an array, otherwise an error string.
843 function parseWhereClause(&$parseString, $stopRegex='') {
845 // Prepare variables:
846 $parseString = $this->trimSQL($parseString);
847 $this->lastStopKeyWord
= '';
848 $this->parse_error
= '';
850 $stack = array(0 => array()); // Contains the parsed content
851 $pnt = array(0 => 0); // Pointer to positions in $stack
852 $level = 0; // Determines parenthesis level
853 $loopExit = 0; // Recursivity brake.
855 // $parseString is continously shortend by the process and we keep parsing it till it is zero:
856 while (strlen($parseString)) {
858 // Look for next parenthesis level:
859 $newLevel = $this->nextPart($parseString,'^([(])');
860 if ($newLevel=='(') { // If new level is started, manage stack/pointers:
861 $level++
; // Increase level
862 $pnt[$level] = 0; // Reset pointer for this level
863 $stack[$level] = array(); // Reset stack for this level
864 } else { // If no new level is started, just parse the current level:
866 // Find "modifyer", eg. "NOT or !"
867 $stack[$level][$pnt[$level]]['modifier'] = trim($this->nextPart($parseString,'^(!|NOT[[:space:]]+)'));
870 if ($fieldName = $this->nextPart($parseString,'^([[:alnum:]._]+)([[:space:]]+|&|<=|>=|<|>|=|!=|IS)')) {
872 // Parse field name into field and table:
873 $tableField = explode('.',$fieldName,2);
874 if (count($tableField)==2) {
875 $stack[$level][$pnt[$level]]['table'] = $tableField[0];
876 $stack[$level][$pnt[$level]]['field'] = $tableField[1];
878 $stack[$level][$pnt[$level]]['table'] = '';
879 $stack[$level][$pnt[$level]]['field'] = $tableField[0];
882 return $this->parseError('No field name found as expected',$parseString);
885 // See if the value is calculated. Support only for "&" (boolean AND) at the moment:
886 $stack[$level][$pnt[$level]]['calc'] = $this->nextPart($parseString,'^(&)');
887 if (strlen($stack[$level][$pnt[$level]]['calc'])) {
888 // Finding value for calculation:
889 $stack[$level][$pnt[$level]]['calc_value'] = $this->getValue($parseString);
892 // Find "comparator":
893 $stack[$level][$pnt[$level]]['comparator'] = $this->nextPart($parseString,'^(<=|>=|<|>|=|!=|NOT[[:space:]]+IN|IN|NOT[[:space:]]+LIKE|LIKE|IS)');
894 if (strlen($stack[$level][$pnt[$level]]['comparator'])) {
895 // Finding value for comparator:
896 $stack[$level][$pnt[$level]]['value'] = $this->getValue($parseString,$stack[$level][$pnt[$level]]['comparator']);
897 if ($this->parse_error
) { return $this->parse_error
; }
900 // Finished, increase pointer:
903 // Checking if the current level is ended, in that case do stack management:
904 while ($this->nextPart($parseString,'^([)])')) {
905 $level--; // Decrease level:
906 $stack[$level][$pnt[$level]]['sub'] = $stack[$level+
1]; // Copy stack
907 $pnt[$level]++
; // Increase pointer of the new level
909 // Make recursivity check:
912 return $this->parseError('More than 500 loops (in search for exit parenthesis), exiting prematurely...',$parseString);
916 // Detecting the operator for the next level; support for AND, OR and &&):
917 $op = $this->nextPart($parseString,'^(AND|OR|AND[[:space:]]+NOT)(\(|[[:space:]]+)');
919 $stack[$level][$pnt[$level]]['operator'] = $op;
920 } elseif (strlen($parseString)) {
922 // Looking for stop-keywords:
923 if ($stopRegex && $this->lastStopKeyWord
= $this->nextPart($parseString, $stopRegex)) {
924 $this->lastStopKeyWord
= strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$this->lastStopKeyWord
));
927 return $this->parseError('No operator, but parsing not finished.',$parseString);
932 // Make recursivity check:
935 return $this->parseError('More than 500 loops, exiting prematurely...',$parseString);
939 // Return the stacks lowest level:
944 * Parsing the WHERE clause fields in the "WHERE [$parseString] ..." part of a query into a multidimensional array.
945 * The success of this parsing determines if that part of the query is supported by TYPO3.
947 * @param string WHERE clause to parse. NOTICE: passed by reference!
948 * @param string Regular expressing to STOP parsing, eg. '^(GROUP BY|ORDER BY|LIMIT)([[:space:]]*)'
949 * @return mixed If successful parsing, returns an array, otherwise an error string.
951 function parseFieldDef(&$parseString, $stopRegex='') {
952 // Prepare variables:
953 $parseString = $this->trimSQL($parseString);
954 $this->lastStopKeyWord
= '';
955 $this->parse_error
= '';
960 if ($result['fieldType'] = $this->nextPart($parseString,'^(int|smallint|tinyint|mediumint|bigint|double|numeric|decimal|varchar|char|text|tinytext|mediumtext|longtext|blob|tinyblob|mediumblob|longblob)([[:space:]]+|\()')) {
962 // Looking for value:
963 if (substr($parseString,0,1)=='(') {
964 $parseString = substr($parseString,1);
965 if ($result['value'] = $this->nextPart($parseString,'^([^)]*)')) {
966 $parseString = ltrim(substr($parseString,1));
967 } else return $this->parseError('No end-parenthesis for value found!',$parseString);
970 // Looking for keywords
971 while($keyword = $this->nextPart($parseString,'^(DEFAULT|NOT[[:space:]]+NULL|AUTO_INCREMENT|UNSIGNED)([[:space:]]+|,|\))')) {
972 $keywordCmp = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$keyword));
974 $result['featureIndex'][$keywordCmp]['keyword'] = $keyword;
976 switch($keywordCmp) {
978 $result['featureIndex'][$keywordCmp]['value'] = $this->getValue($parseString);
982 } else return $this->parseError('Field type unknown!',$parseString);
997 /************************************
999 * Parsing: Helper functions
1001 ************************************/
1004 * Strips of a part of the parseString and returns the matching part.
1005 * Helper function for the parsing methods.
1007 * @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.
1008 * @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())
1009 * @param boolean If set the full match of the regex is stripped of the beginning of the string!
1010 * @return string The value of the first parenthesis level of the REGEX.
1012 function nextPart(&$parseString,$regex,$trimAll=FALSE) {
1013 //if (eregi($regex,$parseString.' ', $reg)) { // Adding space char because [[:space:]]+ is often a requirement in regex's
1014 if (preg_match('/'.$regex.'/i',$parseString.' ', $reg)) { // Adding space char because [[:space:]]+ is often a requirement in regex's
1015 $parseString = ltrim(substr($parseString,strlen($reg[$trimAll?
0:1])));
1021 * Finds value in beginning of $parseString, returns result and strips it of parseString
1023 * @param string The parseString, eg. "(0,1,2,3) ..." or "('asdf','qwer') ..." or "1234 ..." or "'My string value here' ..."
1024 * @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)
1025 * @return string The value (string/integer). Otherwise an array with error message in first key (0)
1027 function getValue(&$parseString,$comparator='') {
1028 //if (t3lib_div::inList('NOTIN,IN,_LIST',strtoupper(ereg_replace('[[:space:]]','',$comparator)))) { // List of values:
1029 if (t3lib_div
::inList('NOTIN,IN,_LIST',strtoupper(str_replace(array(' ',"\n","\r","\t"),'',$comparator)))) { // List of values:
1030 if ($this->nextPart($parseString,'^([(])')) {
1031 $listValues = array();
1034 while($comma==',') {
1035 $listValues[] = $this->getValue($parseString);
1036 $comma = $this->nextPart($parseString,'^([,])');
1039 $out = $this->nextPart($parseString,'^([)])');
1041 if ($comparator=='_LIST') {
1043 foreach ($listValues as $vArr) {
1044 $kVals[] = $vArr[0];
1050 } else return array($this->parseError('No ) parenthesis in list',$parseString));
1051 } else return array($this->parseError('No ( parenthesis starting the list',$parseString));
1053 } else { // Just plain string value, in quotes or not:
1056 $firstChar = substr($parseString,0,1);
1058 switch($firstChar) {
1060 return array($this->getValueInQuotes($parseString,'"'),'"');
1063 return array($this->getValueInQuotes($parseString,"'"),"'");
1066 if (preg_match('/^([[:alnum:]._-]+)/i',$parseString, $reg)) {
1067 $parseString = ltrim(substr($parseString,strlen($reg[0])));
1068 return array($reg[1]);
1076 * Get value in quotes from $parseString.
1077 * NOTICE: If a query being parsed was prepared for another database than MySQL this function should probably be changed
1079 * @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.
1080 * @param string The quote used; input either " or '
1081 * @return string The value, passed through stripslashes() !
1083 function getValueInQuotes(&$parseString,$quote) {
1085 $parts = explode($quote,substr($parseString,1));
1087 foreach($parts as $k => $v) {
1091 //preg_match('/[\]*$/',$v,$reg); // does not work. what is the *exact* meaning of the next line?
1092 ereg('[\]*$',$v,$reg);
1093 if (strlen($reg[0])%2
) {
1096 $parseString = ltrim(substr($parseString,strlen($buffer)+
2));
1097 return $this->parseStripslashes($buffer);
1103 * Strip slashes function used for parsing
1104 * NOTICE: If a query being parsed was prepared for another database than MySQL this function should probably be changed
1106 * @param string Input string
1107 * @return string Output string
1109 function parseStripslashes($str) {
1110 $search = array('\\\\', '\\\'', '\\"', '\0', '\n', '\r', '\Z');
1111 $replace = array('\\', '\'', '"', "\x00", "\x0a", "\x0d", "\x1a");
1113 return str_replace($search, $replace, $str);
1117 * Add slashes function used for compiling queries
1118 * NOTICE: If a query being parsed was prepared for another database than MySQL this function should probably be changed
1120 * @param string Input string
1121 * @return string Output string
1123 function compileAddslashes($str) {
1125 $search = array('\\', '\'', '"', "\x00", "\x0a", "\x0d", "\x1a");
1126 $replace = array('\\\\', '\\\'', '\\"', '\0', '\n', '\r', '\Z');
1128 return str_replace($search, $replace, $str);
1132 * Setting the internal error message value, $this->parse_error and returns that value.
1134 * @param string Input error message
1135 * @param string Remaining query to parse.
1136 * @return string Error message.
1138 function parseError($msg,$restQuery) {
1139 $this->parse_error
= 'SQL engine parse ERROR: '.$msg.': near "'.substr($restQuery,0,50).'"';
1140 return $this->parse_error
;
1144 * Trimming SQL as preparation for parsing.
1145 * ";" in the end is stripped of.
1146 * White space is trimmed away around the value
1147 * A single space-char is added in the end
1149 * @param string Input string
1150 * @return string Output string
1152 function trimSQL($str) {
1153 return trim(rtrim($str, "; \r\n\t")).' ';
1154 //return trim(ereg_replace('[[:space:];]*$','',$str)).' ';
1168 /*************************
1172 *************************/
1175 * Compiles an SQL query from components
1177 * @param array Array of SQL query components
1178 * @return string SQL query
1181 function compileSQL($components) {
1182 switch($components['type']) {
1184 $query = $this->compileSELECT($components);
1187 $query = $this->compileUPDATE($components);
1190 $query = $this->compileINSERT($components);
1193 $query = $this->compileDELETE($components);
1196 $query = 'EXPLAIN '.$this->compileSELECT($components);
1199 $query = 'DROP TABLE'.($components['ifExists']?
' IF EXISTS':'').' '.$components['TABLE'];
1202 $query = $this->compileCREATETABLE($components);
1205 $query = $this->compileALTERTABLE($components);
1213 * Compiles a SELECT statement from components array
1215 * @param array Array of SQL query components
1216 * @return string SQL SELECT query
1217 * @see parseSELECT()
1219 function compileSELECT($components) {
1222 $where = $this->compileWhereClause($components['WHERE']);
1223 $groupBy = $this->compileFieldList($components['GROUPBY']);
1224 $orderBy = $this->compileFieldList($components['ORDERBY']);
1225 $limit = $components['LIMIT'];
1228 $query = 'SELECT '.($components['STRAIGHT_JOIN'] ?
$components['STRAIGHT_JOIN'].'' : '').'
1229 '.$this->compileFieldList($components['SELECT']).'
1230 FROM '.$this->compileFromTables($components['FROM']).
1232 WHERE '.$where : '').
1234 GROUP BY '.$groupBy : '').
1236 ORDER BY '.$orderBy : '').
1238 LIMIT '.$limit : '');
1244 * Compiles an UPDATE statement from components array
1246 * @param array Array of SQL query components
1247 * @return string SQL UPDATE query
1248 * @see parseUPDATE()
1250 function compileUPDATE($components) {
1253 $where = $this->compileWhereClause($components['WHERE']);
1257 foreach($components['FIELDS'] as $fN => $fV) {
1258 $fields[]=$fN.'='.$fV[1].$this->compileAddslashes($fV[0]).$fV[1];
1262 $query = 'UPDATE '.$components['TABLE'].' SET
1266 WHERE '.$where : '');
1272 * Compiles an INSERT statement from components array
1274 * @param array Array of SQL query components
1275 * @return string SQL INSERT query
1276 * @see parseINSERT()
1278 function compileINSERT($components) {
1280 if ($components['VALUES_ONLY']) {
1283 foreach($components['VALUES_ONLY'] as $fV) {
1284 $fields[]=$fV[1].$this->compileAddslashes($fV[0]).$fV[1];
1288 $query = 'INSERT INTO '.$components['TABLE'].'
1295 foreach($components['FIELDS'] as $fN => $fV) {
1296 $fields[$fN]=$fV[1].$this->compileAddslashes($fV[0]).$fV[1];
1300 $query = 'INSERT INTO '.$components['TABLE'].'
1302 ',array_keys($fields)).')
1312 * Compiles an DELETE statement from components array
1314 * @param array Array of SQL query components
1315 * @return string SQL DELETE query
1316 * @see parseDELETE()
1318 function compileDELETE($components) {
1321 $where = $this->compileWhereClause($components['WHERE']);
1324 $query = 'DELETE FROM '.$components['TABLE'].
1326 WHERE '.$where : '');
1332 * Compiles a CREATE TABLE statement from components array
1334 * @param array Array of SQL query components
1335 * @return string SQL CREATE TABLE query
1336 * @see parseCREATETABLE()
1338 function compileCREATETABLE($components) {
1340 // Create fields and keys:
1341 $fieldsKeys = array();
1342 foreach($components['FIELDS'] as $fN => $fCfg) {
1343 $fieldsKeys[]=$fN.' '.$this->compileFieldCfg($fCfg['definition']);
1345 foreach($components['KEYS'] as $kN => $kCfg) {
1346 if ($kN == 'PRIMARYKEY') {
1347 $fieldsKeys[]='PRIMARY KEY ('.implode(',', $kCfg).')';
1349 $fieldsKeys[]='KEY '.$kN.' ('.implode(',', $kCfg).')';
1354 $query = 'CREATE TABLE '.$components['TABLE'].' (
1357 )'.($components['tableType'] ?
' TYPE='.$components['tableType'] : '');
1363 * Compiles an ALTER TABLE statement from components array
1365 * @param array Array of SQL query components
1366 * @return string SQL ALTER TABLE query
1367 * @see parseALTERTABLE()
1369 function compileALTERTABLE($components) {
1372 $query = 'ALTER TABLE '.$components['TABLE'].' '.$components['action'].' '.($components['FIELD']?
$components['FIELD']:$components['KEY']);
1374 // Based on action, add the final part:
1375 switch(strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$components['action']))) {
1377 $query.=' '.$this->compileFieldCfg($components['definition']);
1380 $query.=' '.$components['newField'].' '.$this->compileFieldCfg($components['definition']);
1386 case 'ADDPRIMARYKEY':
1387 $query.=' ('.implode(',',$components['fields']).')';
1408 /**************************************
1410 * Compiling queries, helper functions for parts of queries
1412 **************************************/
1415 * Compiles a "SELECT [output] FROM..:" field list based on input array (made with ->parseFieldList())
1416 * Can also compile field lists for ORDER BY and GROUP BY.
1418 * @param array Array of select fields, (made with ->parseFieldList())
1419 * @return string Select field string
1420 * @see parseFieldList()
1422 function compileFieldList($selectFields) {
1424 // Prepare buffer variable:
1425 $outputParts = array();
1427 // Traverse the selectFields if any:
1428 if (is_array($selectFields)) {
1429 foreach($selectFields as $k => $v) {
1432 switch($v['type']) {
1434 $outputParts[$k] = $v['function'].'('.$v['func_content'].')';
1437 $outputParts[$k] = ($v['table']?
$v['table'].'.':'').$v['field'];
1443 $outputParts[$k].= ' '.$v['as_keyword'].' '.$v['as'];
1446 // Specifically for ORDER BY and GROUP BY field lists:
1447 if ($v['sortDir']) {
1448 $outputParts[$k].= ' '.$v['sortDir'];
1453 // Return imploded buffer:
1454 return implode(', ',$outputParts);
1458 * Compiles a "FROM [output] WHERE..:" table list based on input array (made with ->parseFromTables())
1460 * @param array Array of table names, (made with ->parseFromTables())
1461 * @return string Table name string
1462 * @see parseFromTables()
1464 function compileFromTables($tablesArray) {
1466 // Prepare buffer variable:
1467 $outputParts = array();
1469 // Traverse the table names:
1470 if (is_array($tablesArray)) {
1471 foreach($tablesArray as $k => $v) {
1474 $outputParts[$k] = $v['table'];
1476 // Add alias AS if there:
1478 $outputParts[$k].= ' '.$v['as_keyword'].' '.$v['as'];
1481 if (is_array($v['JOIN'])) {
1482 $outputParts[$k].= ' '.$v['JOIN']['type'].' '.$v['JOIN']['withTable'].' ON '.implode('=',$v['JOIN']['ON']);
1488 // Return imploded buffer:
1489 return implode(', ',$outputParts);
1493 * Implodes an array of WHERE clause configuration into a WHERE clause.
1494 * NOTICE: MIGHT BY A TEMPORARY FUNCTION. Use for debugging only!
1495 * BUT IT IS NEEDED FOR DBAL - MAKE IT PERMANENT?!?!
1497 * @param array WHERE clause configuration
1498 * @return string WHERE clause as string.
1499 * @see explodeWhereClause()
1501 function compileWhereClause($clauseArray) {
1503 // Prepare buffer variable:
1506 // Traverse clause array:
1507 if (is_array($clauseArray)) {
1508 foreach($clauseArray as $k => $v) {
1511 $output.=$v['operator'] ?
' '.$v['operator'] : '';
1513 // Look for sublevel:
1514 if (is_array($v['sub'])) {
1515 $output.=' ('.trim($this->compileWhereClause($v['sub'])).')';
1518 // Set field/table with modifying prefix if any:
1519 $output.=' '.trim($v['modifier'].' '.($v['table']?
$v['table'].'.':'').$v['field']);
1521 // Set calculation, if any:
1523 $output.=$v['calc'].$v['calc_value'][1].$this->compileAddslashes($v['calc_value'][0]).$v['calc_value'][1];
1527 if ($v['comparator']) {
1528 $output.=' '.$v['comparator'];
1530 // Detecting value type; list or plain:
1531 if (t3lib_div
::inList('NOTIN,IN',strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$v['comparator'])))) {
1532 $valueBuffer = array();
1533 foreach($v['value'] as $realValue) {
1534 $valueBuffer[]=$realValue[1].$this->compileAddslashes($realValue[0]).$realValue[1];
1536 $output.=' ('.trim(implode(',',$valueBuffer)).')';
1538 $output.=' '.$v['value'][1].$this->compileAddslashes($v['value'][0]).$v['value'][1];
1545 // Return output buffer:
1550 * Compile field definition
1552 * @param array Field definition parts
1553 * @return string Field definition string
1555 function compileFieldCfg($fieldCfg) {
1558 $cfg = $fieldCfg['fieldType'];
1560 // Add value, if any:
1561 if (strlen($fieldCfg['value'])) {
1562 $cfg.='('.$fieldCfg['value'].')';
1565 // Add additional features:
1566 if (is_array($fieldCfg['featureIndex'])) {
1567 foreach($fieldCfg['featureIndex'] as $featureDef) {
1568 $cfg.=' '.$featureDef['keyword'];
1570 // Add value if found:
1571 if (is_array($featureDef['value'])) {
1572 $cfg.=' '.$featureDef['value'][1].$this->compileAddslashes($featureDef['value'][0]).$featureDef['value'][1];
1577 // Return field definition string:
1591 /*************************
1595 *************************/
1598 * Check parsability of input SQL part string; Will parse and re-compile after which it is compared
1600 * @param string Part definition of string; "SELECT" = fieldlist (also ORDER BY and GROUP BY), "FROM" = table list, "WHERE" = Where clause.
1601 * @param string SQL string to verify parsability of
1602 * @return mixed Returns array with string 1 and 2 if error, otherwise false
1604 function debug_parseSQLpart($part,$str) {
1607 return $this->debug_parseSQLpartCompare($str,$this->compileFieldList($this->parseFieldList($str)));
1610 return $this->debug_parseSQLpartCompare($str,$this->compileFromTables($this->parseFromTables($str)));
1613 return $this->debug_parseSQLpartCompare($str,$this->compileWhereClause($this->parseWhereClause($str)));
1619 * Compare two query strins by stripping away whitespace.
1621 * @param string SQL String 1
1622 * @param string SQL string 2
1623 * @param boolean If true, the strings are compared insensitive to case
1624 * @return mixed Returns array with string 1 and 2 if error, otherwise false
1626 function debug_parseSQLpartCompare($str,$newStr,$caseInsensitive=FALSE) {
1627 if ($caseInsensitive) {
1628 $str1 = strtoupper($str);
1629 $str2 = strtoupper($newStr);
1635 // Fixing escaped chars:
1636 $search = array('\0', '\n', '\r', '\Z');
1637 $replace = array("\x00", "\x0a", "\x0d", "\x1a");
1638 $str1 = str_replace($search, $replace, $str1);
1639 $str2 = str_replace($search, $replace, $str2);
1641 # Normally, commented out since they are needed only in tricky cases...
1642 # $str1 = stripslashes($str1);
1643 # $str2 = stripslashes($str2);
1645 if (strcmp(str_replace(array(' ',"\t","\r","\n"),'',$this->trimSQL($str1)),str_replace(array(' ',"\t","\r","\n"),'',$this->trimSQL($str2)))) {
1647 str_replace(array(' ',"\t","\r","\n"),' ',$str),
1648 str_replace(array(' ',"\t","\r","\n"),' ',$newStr),
1654 * 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
1656 * @param string SQL query
1657 * @return string Query if all is well, otherwise exit.
1659 function debug_testSQL($SQLquery) {
1661 #debug(array($SQLquery));
1663 // Getting result array:
1664 $parseResult = $this->parseSQL($SQLquery);
1666 // If result array was returned, proceed. Otherwise show error and exit.
1667 if (is_array($parseResult)) {
1669 // Re-compile query:
1670 $newQuery = $this->compileSQL($parseResult);
1672 // TEST the new query:
1673 $testResult = $this->debug_parseSQLpartCompare($SQLquery, $newQuery);
1675 // Return new query if OK, otherwise show error and exit:
1676 if (!is_array($testResult)) {
1679 debug(array('ERROR MESSAGE'=>'Input query did not match the parsed and recompiled query exactly (not observing whitespace)', 'TEST result' => $testResult),'SQL parsing failed:');
1683 debug(array('query' => $SQLquery, 'ERROR MESSAGE'=>$parseResult),'SQL parsing failed:');
1690 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_sqlparser.php']) {
1691 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_sqlparser.php']);