- Patch #481288 by Berdir: add support for INSERT INTO ... SELECT FROM ... queries.

merge-requests/26/head
Dries Buytaert 2009-06-05 16:55:45 +00:00
parent 41204a5a82
commit 9079d78481
5 changed files with 54 additions and 6 deletions

View File

@ -22,12 +22,12 @@ class InsertQuery_mysql extends InsertQuery {
throw new PDOException('You may not specify the same field to have a value and a schema-default value.');
}
if (count($this->insertFields) + count($this->defaultFields) == 0) {
if (count($this->insertFields) + count($this->defaultFields) == 0 && empty($this->fromQuery)) {
return NULL;
}
// Don't execute query without values.
if (!isset($this->insertValues[0]) && count($this->insertFields) > 0) {
if (!isset($this->insertValues[0]) && count($this->insertFields) > 0 && empty($this->fromQuery)) {
return NULL;
}
@ -56,6 +56,10 @@ class InsertQuery_mysql extends InsertQuery {
// Default fields are always placed first for consistency.
$insert_fields = array_merge($this->defaultFields, $this->insertFields);
if (!empty($this->fromQuery)) {
return "INSERT $delay INTO {" . $this->table . '} (' . implode(', ', $insert_fields) . ') ' . $this->fromQuery;
}
$query = "INSERT $delay INTO {" . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
$max_placeholder = 0;

View File

@ -22,12 +22,12 @@ class InsertQuery_pgsql extends InsertQuery {
throw new PDOException('You may not specify the same field to have a value and a schema-default value.');
}
if (count($this->insertFields) + count($this->defaultFields) == 0) {
if (count($this->insertFields) + count($this->defaultFields) == 0 && empty($this->fromQuery)) {
return NULL;
}
// Don't execute query without values.
if (!isset($this->insertValues[0]) && count($this->insertFields) > 0) {
if (!isset($this->insertValues[0]) && count($this->insertFields) > 0 && empty($this->fromQuery)) {
return NULL;
}
@ -82,6 +82,10 @@ class InsertQuery_pgsql extends InsertQuery {
// Default fields are always placed first for consistency.
$insert_fields = array_merge($this->defaultFields, $this->insertFields);
if (!empty($this->fromQuery)) {
return "INSERT INTO {" . $this->table . '} (' . implode(', ', $insert_fields) . ') ' . $this->fromQuery;
}
$query = "INSERT INTO {" . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
$max_placeholder = 0;

View File

@ -287,6 +287,12 @@ class InsertQuery extends Query {
*/
protected $insertValues = array();
/**
* A SelectQuery object to fetch the rows that should be inserted.
*
*/
protected $fromQuery;
public function __construct($connection, $table, array $options = array()) {
if (!isset($options['return'])) {
$options['return'] = Database::RETURN_INSERT_ID;
@ -410,6 +416,11 @@ class InsertQuery extends Query {
return $this;
}
public function from(SelectQueryInterface $query) {
$this->fromQuery = $query;
return $this;
}
/**
* Executes the insert query.
*
@ -426,6 +437,11 @@ class InsertQuery extends Query {
$last_insert_id = 0;
// Check if a SelectQuery is passed in and use that.
if (!empty($this->fromQuery)) {
return $this->connection->query((string) $this, array(), $this->queryOptions);
}
// Confirm that the user did not try to specify an identical
// field and default field.
if (array_intersect($this->insertFields, $this->defaultFields)) {
@ -463,6 +479,10 @@ class InsertQuery extends Query {
// Default fields are always placed first for consistency.
$insert_fields = array_merge($this->defaultFields, $this->insertFields);
if (!empty($this->fromQuery)) {
return "INSERT $delay INTO {" . $this->table . '} (' . implode(', ', $insert_fields) . ') ' . $this->fromQuery;
}
// For simplicity, we will use the $placeholders array to inject
// default keywords even though they are not, strictly speaking,
// placeholders for prepared statements.

View File

@ -21,11 +21,11 @@
class InsertQuery_sqlite extends InsertQuery {
public function execute() {
if (count($this->insertFields) + count($this->defaultFields) == 0) {
if (count($this->insertFields) + count($this->defaultFields) == 0 && empty($this->fromQuery)) {
return NULL;
}
// Don't execute query without values.
if (!isset($this->insertValues[0]) && count($this->insertFields) > 0) {
if (!isset($this->insertValues[0]) && count($this->insertFields) > 0 && empty($this->fromQuery)) {
return NULL;
}
if (count($this->insertFields)) {
@ -39,6 +39,11 @@ class InsertQuery_sqlite extends InsertQuery {
public function __toString() {
// Produce as many generic placeholders as necessary.
$placeholders = array_fill(0, count($this->insertFields), '?');
if (!empty($this->fromQuery)) {
return "INSERT INTO {" . $this->table . '} (' . implode(', ', $this->insertFields) . ') ' . $this->fromQuery;
}
return 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') VALUES (' . implode(', ', $placeholders) . ')';
}

View File

@ -513,6 +513,21 @@ class DatabaseInsertTestCase extends DatabaseTestCase {
$this->assertIdentical($id, '5', t('Auto-increment ID returned successfully.'));
}
/**
* Test that the INSERT INTO ... SELECT ... syntax works.
*/
function testInsertSelect() {
$query = db_select('test_people', 'tp')->fields('tp', array('name', 'age', 'job'));
db_insert('test')
->fields(array('name', 'age', 'job'))
->from($query)
->execute();
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Meredith'))->fetchField();
$this->assertIdentical($saved_age, '30', t('Can retrieve after inserting.'));
}
}
/**