Index: t3lib/class.t3lib_sqlparser.php =================================================================== --- t3lib/class.t3lib_sqlparser.php (revision 6894) +++ t3lib/class.t3lib_sqlparser.php (working copy) @@ -134,7 +134,7 @@ // 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 @@ // 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,12 +668,37 @@ } 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); + } + } @@ -678,6 +707,13 @@ + + + + + + + /************************************** * * SQL Parsing, helper functions for parts of queries @@ -1460,6 +1496,9 @@ case 'ALTERTABLE': $query = $this->compileALTERTABLE($components); break; + case 'TRUNCATETABLE': + $query = $this->compileTRUNCATETABLE($components); + break; } return $query; @@ -1652,8 +1691,21 @@ 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; + } @@ -1665,6 +1717,9 @@ + + + /************************************** * * Compiling queries, helper functions for parts of queries Index: t3lib/class.t3lib_db.php =================================================================== --- t3lib/class.t3lib_db.php (revision 6894) +++ t3lib/class.t3lib_db.php (working copy) @@ -382,6 +382,19 @@ 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; + } @@ -392,6 +405,7 @@ + /************************************** * * Query building @@ -589,6 +603,25 @@ } /** + * 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