Fixed bug #13504: Cannot parse TRUNCATE TABLE in install tool
authorXavier Perseguers <typo3@perseguers.ch>
Wed, 10 Feb 2010 21:11:29 +0000 (21:11 +0000)
committerXavier Perseguers <typo3@perseguers.ch>
Wed, 10 Feb 2010 21:11:29 +0000 (21:11 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Extensions/dbal/trunk@29911 735d13b6-9817-0410-8766-e36946ffe9aa

typo3/sysext/dbal/ChangeLog
typo3/sysext/dbal/class.ux_t3lib_db.php
typo3/sysext/dbal/tests/db_oracle_testcase.php
typo3/sysext/dbal/tests/sqlparser_general_testcase.php

index 640d2ed..3ef5466 100644 (file)
@@ -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>
 
        * Added unit test for bug #13490: Where clause 0=0 cannot be parsed
index 5dc723f..7d3f056 100644 (file)
@@ -754,6 +754,62 @@ class ux_t3lib_DB extends t3lib_DB {
        }
 
        /**
+        * Truncates a table.
+        * 
+        * @param       string          Database tablename
+        * @return      mixed           Result from handler
+        */
+       public function exec_TRUNCATETABLEquery($table) {
+               if ($this->debug) {
+                       $pt = t3lib_div::milliseconds();
+               }
+
+                       // Do table/field mapping:
+               $ORIG_tableName = $table;
+               if ($tableArray = $this->map_needMapping($table)) {
+                               // Table name:
+                       if ($this->mapping[$table]['mapTableName']) {
+                               $table = $this->mapping[$table]['mapTableName'];
+                       }
+               }
+
+                       // Select API
+               $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
+               switch ((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
+                       case 'native':
+                               $this->lastQuery = $this->TRUNCATETABLEquery($table);
+                               $sqlResult = mysql_query($this->lastQuery, $this->handlerInstance[$this->lastHandlerKey]['link']);
+                               break;
+                       case 'adodb':
+                               $this->lastQuery = $this->TRUNCATETABLEquery($table);
+                               $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery, FALSE);
+                               break;
+                       case 'userdefined':
+                               $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->exec_TRUNCATETABLEquery($table,$where);
+                               break;
+               }
+
+               if ($this->printErrors && $this->sql_error()) {
+                       debug(array($this->lastQuery, $this->sql_error()));
+               }
+
+               if ($this->debug) {
+                       $this->debugHandler(
+                               'exec_TRUNCATETABLEquery',
+                               t3lib_div::milliseconds() - $pt,
+                               array(
+                                       'handlerType' => $hType,
+                                       'args' => array($table),
+                                       'ORIG_from_table' => $ORIG_tableName
+                               )
+                       );
+               }
+
+                       // Return result:
+               return $sqlResult;
+       }
+
+       /**
         * Executes a query.
         * EXPERIMENTAL since TYPO3 4.4.
         * 
@@ -801,6 +857,10 @@ class ux_t3lib_DB extends t3lib_DB {
                                $table = $queryParts['TABLE'];
                                $whereClause = isset($queryParts['WHERE']) ? $this->SQLparser->compileWhereClause($queryParts['WHERE']) : '1=1';
                                return $this->exec_DELETEquery($table, $whereClause);
+
+                       case 'TRUNCATETABLE':
+                               $table = $queryParts['TABLE'];
+                               return $this->exec_TRUNCATETABLEquery($table);
                }
        }
 
@@ -1042,6 +1102,25 @@ class ux_t3lib_DB extends t3lib_DB {
                return $query;
        }
 
+       /**
+        * Creates a TRUNCATE TABLE SQL-statement
+        * 
+        * @param       string          See exec_TRUNCATETABLEquery()
+        * @return      string          Full SQL query for TRUNCATE TABLE
+        */
+       public function TRUNCATETABLEquery($table) {
+               $table = $this->quoteFromTables($table);
+
+                       // Call parent method to build actual query
+               $query = parent::TRUNCATETABLEquery($table);
+
+               if ($this->debugOutput || $this->store_lastBuiltQuery) {
+                       $this->debug_lastBuiltQuery = $query;
+               }
+
+               return $query;
+       }
+
 
        /**************************************
        *
@@ -2238,6 +2317,7 @@ class ux_t3lib_DB extends t3lib_DB {
                                        $this->map_genericQueryParsed($parsedQuery);
                                        break;
                                case 'INSERT':
+                               case 'TRUNCATETABLE':
                                        $this->map_genericQueryParsed($parsedQuery);
                                        break;
                                case 'CREATEDATABASE':
@@ -2266,8 +2346,11 @@ class ux_t3lib_DB extends t3lib_DB {
                                case 'adodb':
                                                // Compiling query:
                                        $compiledQuery =  $this->SQLparser->compileSQL($this->lastParsedAndMappedQueryArray);
-                                       if ($this->lastParsedAndMappedQueryArray['type']=='INSERT') {
-                                               return $this->exec_INSERTquery($this->lastParsedAndMappedQueryArray['TABLE'],$compiledQuery);
+                                       switch ($this->lastParsedAndMappedQueryArray['type']) {
+                                               case 'INSERT':
+                                                       return $this->exec_INSERTquery($this->lastParsedAndMappedQueryArray['TABLE'], $compiledQuery);
+                                               case 'TRUNCATETABLE':
+                                                       return $this->exec_TRUNCATETABLEquery($this->lastParsedAndMappedQueryArray['TABLE']);
                                        }
                                        return $this->handlerInstance[$this->lastHandlerKey]->DataDictionary->ExecuteSQLArray($compiledQuery);
                                        break;
@@ -2729,7 +2812,7 @@ class ux_t3lib_DB extends t3lib_DB {
                                        $t = $sqlPartArray[$k]['table'] ? $sqlPartArray[$k]['table'] : $defaultTable;
 
                                                // Mapping field name, if set:
-                                       if (is_array($this->mapping[$t]['mapFieldNames']) && $this->mapping[$t]['mapFieldNames'][$sqlPartArray[$k]['field']]) {
+                                       if (is_array($this->mapping[$t]['mapFieldNames']) && isset($this->mapping[$t]['mapFieldNames'][$sqlPartArray[$k]['field']])) {
                                                $sqlPartArray[$k]['field'] = $this->mapping[$t]['mapFieldNames'][$sqlPartArray[$k]['field']];
                                        }
 
index ec82e5f..c1df513 100644 (file)
@@ -227,6 +227,16 @@ class db_oracle_testcase extends BaseTestCase {
 
        /**
         * @test
+        * http://bugs.typo3.org/view.php?id=13504
+        */
+       public function truncateTableQueryIsProperlyQuoted() {
+               $query = $this->cleanSql($GLOBALS['TYPO3_DB']->TRUNCATETABLEquery('be_users'));
+               $expected = 'TRUNCATE TABLE "be_users"';
+               $this->assertEquals($expected, $query);
+       }
+
+       /**
+        * @test
         * @see http://bugs.typo3.org/view.php?id=2438
         */
        public function distinctFieldIsProperlyQuoted() {
index bccab7c..003640d 100644 (file)
@@ -192,6 +192,18 @@ class sqlparser_general_testcase extends BaseTestCase {
 
        /**
         * @test
+        * http://bugs.typo3.org/view.php?id=13504
+        */
+       public function canParseTruncateTable() {
+               $sql = 'TRUNCATE TABLE be_users';
+               $expected = $sql;
+               $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
+
+               $this->assertEquals($expected, $actual);
+       }
+
+       /**
+        * @test
         * @see http://bugs.typo3.org/view.php?id=13412
         */
        public function canParseAndCompileBetweenOperator() {