Skip to content

Commit

Permalink
Add Query::into()
Browse files Browse the repository at this point in the history
Because of how identifier quoting works, the insert() method cannot be
overloaded as the table argument gets set by the identifier quoter. I'd
rather have additional infrequently used method vs. additional type
checks that add cruftyness.
  • Loading branch information
markstory committed Dec 29, 2013
1 parent 9672c53 commit 6cedf55
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 32 deletions.
3 changes: 2 additions & 1 deletion Cake/Database/Connection.php
Expand Up @@ -269,7 +269,8 @@ public function schemaCollection() {
*/
public function insert($table, array $data, array $types = []) {
$columns = array_keys($data);
return $this->newQuery()->insert($table, $columns, $types)
return $this->newQuery()->insert($columns, $types)
->into($table)
->values($data)
->execute();
}
Expand Down
2 changes: 1 addition & 1 deletion Cake/Database/IdentifierQuoter.php
Expand Up @@ -164,7 +164,7 @@ protected function _quoteInsert($query) {
$column = $this->_driver->quoteIdentifier($column);
}
}
$query->insert($table, $columns);
$query->insert($columns)->into($table);
}

/**
Expand Down
18 changes: 15 additions & 3 deletions Cake/Database/Query.php
Expand Up @@ -1292,15 +1292,14 @@ protected function _stringifyExpressions(array $expressions, ValueBinder $genera
* Note calling this method will reset any data previously set
* with Query::values()
*
* @param string $table The table name to insert into.
* @param array $columns The columns to insert into.
* @param array $types A map between columns & their datatypes.
* @return Query
*/
public function insert($table, $columns, $types = []) {
public function insert($columns, $types = []) {
$this->_dirty();
$this->_type = 'insert';
$this->_parts['insert'] = [$table, $columns];
$this->_parts['insert'][1] = $columns;

if (!$this->_parts['values']) {
$this->_parts['values'] = new ValuesExpression($columns, $types + $this->defaultTypes());
Expand All @@ -1309,6 +1308,19 @@ public function insert($table, $columns, $types = []) {
return $this;
}

/**
* Set the table name for insert queries.
*
* @param string $table The table name to insert into.
* @return Query
*/
public function into($table) {
$this->_dirty();
$this->_type = 'insert';
$this->_parts['insert'][0] = $table;
return $this;
}

/**
* Set the values for an insert query.
*
Expand Down
6 changes: 3 additions & 3 deletions Cake/ORM/Query.php
Expand Up @@ -986,12 +986,12 @@ public function delete($table = null) {
*
* @param array $columns The columns to insert into.
* @param array $types A map between columns & their datatypes.
* @param array $unused An unused parameter from the parent class' interface
* @return Query
*/
public function insert($columns, $types = [], $unused = []) {
public function insert($columns, $types = []) {
$table = $this->repository()->table();
return parent::insert($table, $columns, $types);
$this->into($table);
return parent::insert($columns, $types);
}

}
11 changes: 7 additions & 4 deletions Cake/Test/TestCase/Database/Driver/PostgresTest.php
@@ -1,7 +1,5 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand Down Expand Up @@ -150,13 +148,18 @@ public function testInsertReturning() {
[['log' => false]]
);
$query = new \Cake\Database\Query($connection);
$query->insert('articles', ['id', 'title'])->values([1, 'foo']);
$query->insert(['id', 'title'])
->into('articles')
->values([1, 'foo']);
$translator = $driver->queryTranslator('insert');
$query = $translator($query);
$this->assertEquals('RETURNING *', $query->clause('epilog'));

$query = new \Cake\Database\Query($connection);
$query->insert('articles', ['id', 'title'])->values([1, 'foo'])->epilog('FOO');
$query->insert(['id', 'title'])
->into('articles')
->values([1, 'foo'])
->epilog('FOO');
$query = $translator($query);
$this->assertEquals('FOO', $query->clause('epilog'));
}
Expand Down
5 changes: 4 additions & 1 deletion Cake/Test/TestCase/Database/Log/QueryLoggerTest.php
Expand Up @@ -91,10 +91,13 @@ public function testLogFunction() {
$query = new LoggedQuery;
$query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?';
$query->params = ['string', '3', null];

$engine = $this->getMock('\Cake\Log\Engine\BaseLog', ['write'], ['scopes' => ['queriesLog']]);
Log::engine('queryLoggerTest', $engine);

$engine2 = $this->getMock('\Cake\Log\Engine\BaseLog', ['write'], ['scopes' => ['foo']]);
Log::engine('queryLoggerTest2', $engine);
Log::engine('queryLoggerTest2', $engine2);

$engine2->expects($this->never())->method('write');
$logger->log($query);
}
Expand Down
26 changes: 17 additions & 9 deletions Cake/Test/TestCase/Database/QueryTest.php
Expand Up @@ -1702,7 +1702,8 @@ public function testInsertValuesBeforeInsertFailure() {
*/
public function testInsertSimple() {
$query = new Query($this->connection);
$query->insert('articles', ['title', 'body'])
$query->insert(['title', 'body'])
->into('articles')
->values([
'title' => 'mark',
'body' => 'test insert'
Expand Down Expand Up @@ -1738,7 +1739,8 @@ public function testInsertSimple() {
*/
public function testInsertSparseRow() {
$query = new Query($this->connection);
$query->insert('articles', ['title', 'body'])
$query->insert(['title', 'body'])
->into('articles')
->values([
'title' => 'mark',
]);
Expand Down Expand Up @@ -1772,7 +1774,8 @@ public function testInsertSparseRow() {
*/
public function testInsertMultipleRowsSparse() {
$query = new Query($this->connection);
$query->insert('articles', ['title', 'body'])
$query->insert(['title', 'body'])
->into('articles')
->values([
'body' => 'test insert'
])
Expand Down Expand Up @@ -1814,10 +1817,10 @@ public function testInsertFromSelect() {

$query = new Query($this->connection);
$query->insert(
'articles',
['title', 'body', 'author_id'],
['title' => 'string', 'body' => 'string', 'author_id' => 'integer']
)
->into('articles')
->values($select);

$result = $query->sql();
Expand Down Expand Up @@ -1853,7 +1856,8 @@ public function testInsertFromSelect() {
*/
public function testInsertFailureMixingTypesArrayFirst() {
$query = new Query($this->connection);
$query->insert('articles', ['name'])
$query->insert(['name'])
->into('articles')
->values(['name' => 'mark'])
->values(new Query($this->connection));
}
Expand All @@ -1865,7 +1869,8 @@ public function testInsertFailureMixingTypesArrayFirst() {
*/
public function testInsertFailureMixingTypesQueryFirst() {
$query = new Query($this->connection);
$query->insert('articles', ['name'])
$query->insert(['name'])
->into('articles')
->values(new Query($this->connection))
->values(['name' => 'mark']);
}
Expand Down Expand Up @@ -2015,7 +2020,8 @@ public function testAppendSelect() {
public function testAppendInsert() {
$query = new Query($this->connection);
$sql = $query
->insert('articles', ['id', 'title'])
->insert(['id', 'title'])
->into('articles')
->values([1, 'a title'])
->epilog('RETURNING id')
->sql();
Expand Down Expand Up @@ -2197,13 +2203,15 @@ public function testQuotingExpressions() {
public function testQuotingInsert() {
$this->connection->driver()->autoQuoting(true);
$query = new Query($this->connection);
$sql = $query->insert('foo', ['bar', 'baz'])
$sql = $query->insert(['bar', 'baz'])
->into('foo')
->where(['something' => 'value'])
->sql();
$this->assertQuotedQuery('INSERT INTO <foo> \(<bar>, <baz>\)', $sql);

$query = new Query($this->connection);
$sql = $query->insert('foo', [$query->newExpr()->add('bar')])
$sql = $query->insert([$query->newExpr()->add('bar')])
->into('foo')
->where(['something' => 'value'])
->sql();
$this->assertQuotedQuery('INSERT INTO <foo> \(\(bar\)\)', $sql);
Expand Down
27 changes: 19 additions & 8 deletions Cake/Test/TestCase/TestSuite/TestFixtureTest.php
Expand Up @@ -234,22 +234,28 @@ public function testInsert() {

$query->expects($this->once())
->method('insert')
->with('articles', ['name', 'created'], ['string', 'datetime'])
->with(['name', 'created'], ['string', 'datetime'])
->will($this->returnSelf());

$query->expects($this->once())
->method('into')
->with('articles')
->will($this->returnSelf());

$expected = [
['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'],
['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'],
['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00']
];
$query->expects($this->at(1))
$query->expects($this->at(2))
->method('values')
->with($expected[0])
->will($this->returnSelf());
$query->expects($this->at(2))
$query->expects($this->at(3))
->method('values')
->with($expected[1])
->will($this->returnSelf());
$query->expects($this->at(3))
$query->expects($this->at(4))
->method('values')
->with($expected[2])
->will($this->returnSelf());
Expand Down Expand Up @@ -279,23 +285,28 @@ public function testInsertStrings() {

$query->expects($this->once())
->method('insert')
->with('strings', ['name', 'email', 'age'], ['string', 'string', 'integer'])
->with(['name', 'email', 'age'], ['string', 'string', 'integer'])
->will($this->returnSelf());

$query->expects($this->once())
->method('into')
->with('strings')
->will($this->returnSelf());

$expected = [
['name' => 'Mark Doe', 'email' => 'mark.doe@email.com', 'age' => null],
['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20],
['name' => 'Jane Doe', 'email' => 'jane.doe@email.com', 'age' => 30],
];
$query->expects($this->at(1))
$query->expects($this->at(2))
->method('values')
->with($expected[0])
->will($this->returnSelf());
$query->expects($this->at(2))
$query->expects($this->at(3))
->method('values')
->with($expected[1])
->will($this->returnSelf());
$query->expects($this->at(3))
$query->expects($this->at(4))
->method('values')
->with($expected[2])
->will($this->returnSelf());
Expand Down
3 changes: 2 additions & 1 deletion Cake/TestSuite/Fixture/TestFixture.php
Expand Up @@ -276,7 +276,8 @@ public function insert(Connection $db) {
if (isset($this->records) && !empty($this->records)) {
list($fields, $values, $types) = $this->_getRecords();
$query = $db->newQuery()
->insert($this->table, $fields, $types);
->insert($fields, $types)
->into($this->table);

foreach ($values as $row) {
$query->values($row);
Expand Down
3 changes: 2 additions & 1 deletion Cake/Validation/Validator.php
Expand Up @@ -386,7 +386,8 @@ protected function _fieldIsEmpty($data) {
*/
protected function _processRules(ValidationSet $rules, $value, $newRecord) {
$errors = [];
$this->provider('default'); // Loading default provider in case there is none
// Loading default provider in case there is none
$this->provider('default');
foreach ($rules as $name => $rule) {
$result = $rule->process($value, $this->_providers, $newRecord);
if ($result === true) {
Expand Down

0 comments on commit 6cedf55

Please sign in to comment.