Browse Source

Adding tests for modifiers in INSERT queries

Jose Lorenzo Rodriguez 10 years ago
parent
commit
4123ba1a7c
2 changed files with 39 additions and 1 deletions
  1. 5 1
      src/Database/QueryCompiler.php
  2. 34 0
      tests/TestCase/Database/QueryTest.php

+ 5 - 1
src/Database/QueryCompiler.php

@@ -70,7 +70,7 @@ class QueryCompiler
      *
      * @var array
      */
-    protected $_insertParts = ['insert', 'modifier', 'values', 'epilog'];
+    protected $_insertParts = ['insert', 'values', 'epilog'];
 
     /**
      * Indicate whether or not this query dialect supports ordered unions.
@@ -289,6 +289,10 @@ class QueryCompiler
         $columns = $this->_stringifyExpressions($parts[1], $generator);
         $modifiers = $this->_buildModifierPart($query->clause('modifier'), $query, $generator);
 
+        if ($modifiers !== null) {
+            $modifiers .= ' ';
+        }
+
         return sprintf('INSERT %sINTO %s (%s)', $modifiers, $table, implode(', ', $columns));
     }
 

+ 34 - 0
tests/TestCase/Database/QueryTest.php

@@ -3636,6 +3636,40 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Test use of modifiers in a INSERT query
+     *
+     * Testing the generated SQL since the modifiers are usually different per driver
+     *
+     * @return void
+     */
+    public function testInsertModifiers()
+    {
+        $query = new Query($this->connection);
+        $result = $query
+            ->insert(['title'])
+            ->into('articles')
+            ->values(['title' => 'foo'])
+            ->modifier('IGNORE');
+        $this->assertQuotedQuery(
+            'INSERT IGNORE INTO <articles> \(<title>\) VALUES \(:c0\)',
+            $result->sql(),
+            !$this->autoQuote
+        );
+
+        $query = new Query($this->connection);
+        $result = $query
+            ->insert(['title'])
+            ->into('articles')
+            ->values(['title' => 'foo'])
+            ->modifier(['IGNORE', 'LOW_PRIORITY']);
+        $this->assertQuotedQuery(
+            'INSERT IGNORE LOW_PRIORITY INTO <articles> \(<title>\) VALUES \(:c0\)',
+            $result->sql(),
+            !$this->autoQuote
+        );
+    }
+
+    /**
      * Assertion for comparing a table's contents with what is in it.
      *
      * @param string $table