Skip to content
Snippets Groups Projects
Commit b2dc8aa5 authored by Dries Buytaert's avatar Dries Buytaert
Browse files

Issue #2056363 by yched, chx: Fixed INSERT INTO table SELECT * FROM ... not supported.

parent 439d53aa
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -49,7 +49,8 @@ public function __toString() { ...@@ -49,7 +49,8 @@ public function __toString() {
// If we're selecting from a SelectQuery, finish building the query and // If we're selecting from a SelectQuery, finish building the query and
// pass it back, as any remaining options are irrelevant. // pass it back, as any remaining options are irrelevant.
if (!empty($this->fromQuery)) { if (!empty($this->fromQuery)) {
return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') ' . $this->fromQuery; $insert_fields_string = $insert_fields ? ' (' . implode(', ', $insert_fields) . ') ' : ' ';
return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
} }
$query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES '; $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
......
...@@ -118,7 +118,8 @@ public function __toString() { ...@@ -118,7 +118,8 @@ public function __toString() {
// If we're selecting from a SelectQuery, finish building the query and // If we're selecting from a SelectQuery, finish building the query and
// pass it back, as any remaining options are irrelevant. // pass it back, as any remaining options are irrelevant.
if (!empty($this->fromQuery)) { if (!empty($this->fromQuery)) {
return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') ' . $this->fromQuery; $insert_fields_string = $insert_fields ? ' (' . implode(', ', $insert_fields) . ') ' : ' ';
return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
} }
$query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES '; $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
......
...@@ -40,7 +40,8 @@ public function __toString() { ...@@ -40,7 +40,8 @@ public function __toString() {
// If we're selecting from a SelectQuery, finish building the query and // If we're selecting from a SelectQuery, finish building the query and
// pass it back, as any remaining options are irrelevant. // pass it back, as any remaining options are irrelevant.
if (!empty($this->fromQuery)) { if (!empty($this->fromQuery)) {
return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') ' . $this->fromQuery; $insert_fields_string = $this->insertFields ? ' (' . implode(', ', $this->insertFields) . ') ' : ' ';
return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
} }
return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') VALUES (' . implode(', ', $placeholders) . ')'; return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') VALUES (' . implode(', ', $placeholders) . ')';
......
...@@ -284,11 +284,12 @@ public function preExecute() { ...@@ -284,11 +284,12 @@ public function preExecute() {
// first call to fields() does have an effect. // first call to fields() does have an effect.
$this->fields(array_merge(array_keys($this->fromQuery->getFields()), array_keys($this->fromQuery->getExpressions()))); $this->fields(array_merge(array_keys($this->fromQuery->getFields()), array_keys($this->fromQuery->getExpressions())));
} }
else {
// Don't execute query without fields. // Don't execute query without fields.
if (count($this->insertFields) + count($this->defaultFields) == 0) { if (count($this->insertFields) + count($this->defaultFields) == 0) {
throw new NoFieldsException('There are no fields available to insert with.'); throw new NoFieldsException('There are no fields available to insert with.');
} }
}
// If no values have been added, silently ignore this query. This can happen // If no values have been added, silently ignore this query. This can happen
// if values are added conditionally, so we don't want to throw an // if values are added conditionally, so we don't want to throw an
......
...@@ -24,6 +24,7 @@ function setUp() { ...@@ -24,6 +24,7 @@ function setUp() {
$this->installSchema('database_test', array( $this->installSchema('database_test', array(
'test', 'test',
'test_people', 'test_people',
'test_people_copy',
'test_one_blob', 'test_one_blob',
'test_two_blobs', 'test_two_blobs',
'test_task', 'test_task',
......
...@@ -142,9 +142,9 @@ function testInsertLastInsertID() { ...@@ -142,9 +142,9 @@ function testInsertLastInsertID() {
} }
/** /**
* Tests that the INSERT INTO ... SELECT ... syntax works. * Tests that the INSERT INTO ... SELECT (fields) ... syntax works.
*/ */
function testInsertSelect() { function testInsertSelectFields() {
$query = db_select('test_people', 'tp'); $query = db_select('test_people', 'tp');
// The query builder will always append expressions after fields. // The query builder will always append expressions after fields.
// Add the expression first to test that the insert fields are correctly // Add the expression first to test that the insert fields are correctly
...@@ -166,4 +166,26 @@ function testInsertSelect() { ...@@ -166,4 +166,26 @@ function testInsertSelect() {
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Meredith'))->fetchField(); $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Meredith'))->fetchField();
$this->assertIdentical($saved_age, '30', 'Can retrieve after inserting.'); $this->assertIdentical($saved_age, '30', 'Can retrieve after inserting.');
} }
/**
* Tests that the INSERT INTO ... SELECT * ... syntax works.
*/
function testInsertSelectAll() {
$query = db_select('test_people', 'tp')
->fields('tp')
->condition('tp.name', 'Meredith');
// The resulting query should be equivalent to:
// INSERT INTO test_people_copy
// SELECT *
// FROM test_people tp
// WHERE tp.name = 'Meredith'
db_insert('test_people_copy')
->from($query)
->execute();
$saved_age = db_query('SELECT age FROM {test_people_copy} WHERE name = :name', array(':name' => 'Meredith'))->fetchField();
$this->assertIdentical($saved_age, '30', 'Can retrieve after inserting.');
}
} }
...@@ -87,6 +87,37 @@ function database_test_schema() { ...@@ -87,6 +87,37 @@ function database_test_schema() {
), ),
); );
$schema['test_people_copy'] = array(
'description' => 'A duplicate version of the test_people table, used for additional tests.',
'fields' => array(
'name' => array(
'description' => "A person's name",
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'age' => array(
'description' => "The person's age",
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'job' => array(
'description' => "The person's job",
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
),
'primary key' => array('job'),
'indexes' => array(
'ages' => array('age'),
),
);
$schema['test_one_blob'] = array( $schema['test_one_blob'] = array(
'description' => 'A simple table including a BLOB field for testing BLOB behavior.', 'description' => 'A simple table including a BLOB field for testing BLOB behavior.',
'fields' => array( 'fields' => array(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment