Fixed bug #13504: Cannot parse TRUNCATE TABLE in install tool
authorXavier Perseguers <typo3@perseguers.ch>
Wed, 10 Feb 2010 20:59:12 +0000 (20:59 +0000)
committerXavier Perseguers <typo3@perseguers.ch>
Wed, 10 Feb 2010 20:59:12 +0000 (20:59 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@6895 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/class.t3lib_db.php
t3lib/class.t3lib_sqlparser.php

index 9928a3b..c1ef19f 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2010-02-10  Xavier Perseguers  <typo3@perseguers.ch>
+
+       * Fixed bug #13504: Cannot parse TRUNCATE TABLE in install tool
+
 2010-02-09  Xavier Perseguers  <typo3@perseguers.ch>
 
        * Fixed bug #13490: Where clause 0=0 cannot be parsed
index 4d2e5e9..fb89169 100644 (file)
@@ -382,6 +382,20 @@ class t3lib_DB {
                return $count;
        }
 
+       /**
+        * Truncates a table.
+        * 
+        * @param       string          Database tablename
+        * @return      mixed           Result from handler
+        */
+       public function exec_TRUNCATETABLEquery($table) {
+               $res = mysql_query($this->TRUNCATETABLEquery($table), $this->link);
+               if ($this->debugOutput) {
+                       $this->debug('exec_TRUNCATETABLEquery');
+               }
+               return $res;
+       }
+
 
 
 
@@ -589,6 +603,25 @@ class t3lib_DB {
        }
 
        /**
+        * Creates a TRUNCATE TABLE SQL-statement
+        * 
+        * @param       string          See exec_TRUNCATETABLEquery()
+        * @return      string          Full SQL query for TRUNCATE TABLE
+        */
+       public function TRUNCATETABLEquery($table) {
+                       // Table should be "SQL-injection-safe" when supplied to this function
+                       // Build basic query:
+               $query = 'TRUNCATE TABLE ' . $table;
+
+                       // Return query:
+               if ($this->debugOutput || $this->store_lastBuiltQuery) {
+                       $this->debug_lastBuiltQuery = $query;
+               }
+
+               return $query;
+       }
+
+       /**
         * Returns a WHERE clause that can find a value ($value) in a list field ($field)
         * For instance a record in the database might contain a list of numbers,
         * "34,234,5" (with no spaces between). This query would be able to select that
index 03e2a99..c82c461 100644 (file)
@@ -134,7 +134,7 @@ class t3lib_sqlparser {
 
                        // Finding starting keyword of string:
                $_parseString = $parseString;   // Protecting original string...
-               $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:]]+');
+               $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|TRUNCATE[[:space:]]+TABLE)[[:space:]]+');
                $keyword = strtoupper(str_replace(array(' ',"\t","\r","\n"),'',$keyword));
 
                switch($keyword)        {
@@ -174,6 +174,10 @@ class t3lib_sqlparser {
                                        // Parsing CREATE DATABASE query:
                                $result = $this->parseCREATEDATABASE($parseString);
                        break;
+                       case 'TRUNCATETABLE':
+                                       // Parsing TRUNCATE TABLE query:
+                               $result = $this->parseTRUNCATETABLE($parseString);
+                       break;
                        default:
                                $result = $this->parseError('"'.$keyword.'" is not a keyword',$parseString);
                        break;
@@ -664,6 +668,38 @@ class t3lib_sqlparser {
                } else return $this->parseError('No database found!',$parseString);
        }
 
+       /**
+        * Parsing TRUNCATE TABLE query
+        * 
+        * @param       string          SQL string starting with TRUNCATE TABLE
+        * @return      mixed           Returns array with components of TRUNCATE TABLE query on success, otherwise an error message string.
+        */
+       protected function parseTRUNCATETABLE($parseString) {
+
+                       // Removing TRUNCATE TABLE
+               $parseString = $this->trimSQL($parseString);
+               $parseString = ltrim(substr(ltrim(substr($parseString, 8)), 5));
+
+                       // Init output variable:
+               $result = array();
+               $result['type'] = 'TRUNCATETABLE';
+
+                       // Get table:
+               $result['TABLE'] = $this->nextPart($parseString, '^([[:alnum:]_]+)[[:space:]]+');
+
+               if ($result['TABLE']) {
+
+                               // Should be no more content now:
+                       if ($parseString) {
+                               return $this->parseError('Still content in clause after parsing!', $parseString);
+                       }
+
+                       return $result;
+               } else {
+                       return $this->parseError('No table found!', $parseString);
+               }
+       }
+
 
 
 
@@ -1460,6 +1496,9 @@ class t3lib_sqlparser {
                        case 'ALTERTABLE':
                                $query = $this->compileALTERTABLE($components);
                        break;
+                       case 'TRUNCATETABLE':
+                               $query = $this->compileTRUNCATETABLE($components);
+                       break;
                }
 
                return $query;
@@ -1652,6 +1691,22 @@ class t3lib_sqlparser {
                return $query;
        }
 
+       /**
+        * Compiles a TRUNCATE TABLE statement from components array
+        * 
+        * @param       array           Array of SQL query components
+        * @return      string          SQL TRUNCATE TABLE query
+        * @see parseTRUNCATETABLE()
+        */
+       protected function compileTRUNCATETABLE(array $components) {
+
+                       // Make query:
+               $query = 'TRUNCATE TABLE ' . $components['TABLE'];
+
+                       // Return query
+               return $query;
+       }
+