diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Insert.php b/core/lib/Drupal/Core/Database/Driver/mysql/Insert.php index 8501b14..3647125 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Insert.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Insert.php @@ -55,30 +55,7 @@ public function __toString() { $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES '; - $max_placeholder = 0; - $values = array(); - if (count($this->insertValues)) { - foreach ($this->insertValues as $insert_values) { - $placeholders = array(); - - // Default fields aren't really placeholders, but this is the most convenient - // way to handle them. - $placeholders = array_pad($placeholders, count($this->defaultFields), 'default'); - - $new_placeholder = $max_placeholder + count($insert_values); - for ($i = $max_placeholder; $i < $new_placeholder; ++$i) { - $placeholders[] = ':db_insert_placeholder_' . $i; - } - $max_placeholder = $new_placeholder; - $values[] = '(' . implode(', ', $placeholders) . ')'; - } - } - else { - // If there are no values, then this is a default-only query. We still need to handle that. - $placeholders = array_fill(0, count($this->defaultFields), 'default'); - $values[] = '(' . implode(', ', $placeholders) . ')'; - } - + $values = $this->prepareInsertValues(); $query .= implode(', ', $values); return $query; diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php b/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php index e29dac3..2230f6a 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php @@ -9,30 +9,16 @@ use Drupal\Core\Database\Query\Upsert as QueryUpsert; +/** + * Implements the Upsert query for the MySQL database driver. + */ class Upsert extends QueryUpsert { /** * {@inheritdoc} */ public function execute() { - if (!$this->preExecute()) { - return NULL; - } - - $max_placeholder = 0; - $values = array(); - foreach ($this->insertValues as $insert_values) { - foreach ($insert_values as $value) { - $values[':db_insert_placeholder_' . $max_placeholder++] = $value; - } - } - - $last_insert_id = $this->connection->query((string) $this, $values, $this->queryOptions); - - // Re-initialize the values array so that we can re-use this query. - $this->insertValues = array(); - - return $last_insert_id; + return parent::executeHelper(); } /** @@ -47,30 +33,7 @@ public function __toString() { $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES '; - $max_placeholder = 0; - $values = array(); - if ($this->insertValues) { - foreach ($this->insertValues as $insert_values) { - $placeholders = array(); - - // Default fields aren't really placeholders, but this is the most convenient - // way to handle them. - $placeholders = array_pad($placeholders, count($this->defaultFields), 'default'); - - $new_placeholder = $max_placeholder + count($insert_values); - for ($i = $max_placeholder; $i < $new_placeholder; ++$i) { - $placeholders[] = ':db_insert_placeholder_' . $i; - } - $max_placeholder = $new_placeholder; - $values[] = '(' . implode(', ', $placeholders) . ')'; - } - } - else { - // If there are no values, then this is a default-only query. We still need to handle that. - $placeholders = array_fill(0, count($this->defaultFields), 'default'); - $values[] = '(' . implode(', ', $placeholders) . ')'; - } - + $values = $this->prepareInsertValues(); $query .= implode(', ', $values); // Updating the unique / primary key is not necessary. diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Insert.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Insert.php index 7bffc9e..ac43478 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Insert.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Insert.php @@ -128,30 +128,7 @@ public function __toString() { $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES '; - $max_placeholder = 0; - $values = array(); - if (count($this->insertValues)) { - foreach ($this->insertValues as $insert_values) { - $placeholders = array(); - - // Default fields aren't really placeholders, but this is the most convenient - // way to handle them. - $placeholders = array_pad($placeholders, count($this->defaultFields), 'default'); - - $new_placeholder = $max_placeholder + count($insert_values); - for ($i = $max_placeholder; $i < $new_placeholder; ++$i) { - $placeholders[] = ':db_insert_placeholder_' . $i; - } - $max_placeholder = $new_placeholder; - $values[] = '(' . implode(', ', $placeholders) . ')'; - } - } - else { - // If there are no values, then this is a default-only query. We still need to handle that. - $placeholders = array_fill(0, count($this->defaultFields), 'default'); - $values[] = '(' . implode(', ', $placeholders) . ')'; - } - + $values = $this->prepareInsertValues(); $query .= implode(', ', $values); return $query; diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php index be2f942..b64869c 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php @@ -9,6 +9,9 @@ use Drupal\Core\Database\Query\Upsert as QueryUpsert; +/** + * Implements the Upsert query for the PostgreSQL database driver. + */ class Upsert extends QueryUpsert { /** diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Upsert.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Upsert.php index 7f24bdd..3d651d5 100644 --- a/core/lib/Drupal/Core/Database/Driver/sqlite/Upsert.php +++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Upsert.php @@ -9,30 +9,16 @@ use Drupal\Core\Database\Query\Upsert as QueryUpsert; +/** + * Implements the Upsert query for the SQLite database driver. + */ class Upsert extends QueryUpsert { /** * {@inheritdoc} */ public function execute() { - if (!$this->preExecute()) { - return NULL; - } - - $max_placeholder = 0; - $values = array(); - foreach ($this->insertValues as $insert_values) { - foreach ($insert_values as $value) { - $values[':db_insert_placeholder_' . $max_placeholder++] = $value; - } - } - - $last_insert_id = $this->connection->query((string) $this, $values, $this->queryOptions); - - // Re-initialize the values array so that we can re-use this query. - $this->insertValues = array(); - - return $last_insert_id; + return parent::executeHelper(); } /** @@ -47,30 +33,7 @@ public function __toString() { $query = $comments . 'INSERT OR REPLACE INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES '; - $max_placeholder = 0; - $values = array(); - if ($this->insertValues) { - foreach ($this->insertValues as $insert_values) { - $placeholders = array(); - - // Default fields aren't really placeholders, but this is the most convenient - // way to handle them. - $placeholders = array_pad($placeholders, count($this->defaultFields), 'default'); - - $new_placeholder = $max_placeholder + count($insert_values); - for ($i = $max_placeholder; $i < $new_placeholder; ++$i) { - $placeholders[] = ':db_insert_placeholder_' . $i; - } - $max_placeholder = $new_placeholder; - $values[] = '(' . implode(', ', $placeholders) . ')'; - } - } - else { - // If there are no values, then this is a default-only query. We still need to handle that. - $placeholders = array_fill(0, count($this->defaultFields), 'default'); - $values[] = '(' . implode(', ', $placeholders) . ')'; - } - + $values = $this->prepareInsertValues(); $query .= implode(', ', $values); return $query; diff --git a/core/lib/Drupal/Core/Database/Query/Insert.php b/core/lib/Drupal/Core/Database/Query/Insert.php index 453fbe3..b4ec4ef 100644 --- a/core/lib/Drupal/Core/Database/Query/Insert.php +++ b/core/lib/Drupal/Core/Database/Query/Insert.php @@ -14,45 +14,7 @@ * * @ingroup database */ -class Insert extends Query { - - /** - * The table on which to insert. - * - * @var string - */ - protected $table; - - /** - * An array of fields on which to insert. - * - * @var array - */ - protected $insertFields = array(); - - /** - * An array of fields that should be set to their database-defined defaults. - * - * @var array - */ - protected $defaultFields = array(); - - /** - * A nested array of values to insert. - * - * $insertValues is an array of arrays. Each sub-array is either an - * associative array whose keys are field names and whose values are field - * values to insert, or a non-associative array of values in the same order - * as $insertFields. - * - * Whether multiple insert sets will be run in a single query or multiple - * queries is left to individual drivers to implement in whatever manner is - * most appropriate. The order of values in each sub-array must match the - * order of fields in $insertFields. - * - * @var array - */ - protected $insertValues = array(); +class Insert extends InsertBase { /** * A SelectQuery object to fetch the rows that should be inserted. @@ -80,96 +42,6 @@ public function __construct($connection, $table, array $options = array()) { } /** - * Adds a set of field->value pairs to be inserted. - * - * This method may only be called once. Calling it a second time will be - * ignored. To queue up multiple sets of values to be inserted at once, - * use the values() method. - * - * @param $fields - * An array of fields on which to insert. This array may be indexed or - * associative. If indexed, the array is taken to be the list of fields. - * If associative, the keys of the array are taken to be the fields and - * the values are taken to be corresponding values to insert. If a - * $values argument is provided, $fields must be indexed. - * @param $values - * An array of fields to insert into the database. The values must be - * specified in the same order as the $fields array. - * - * @return \Drupal\Core\Database\Query\Insert - * The called object. - */ - public function fields(array $fields, array $values = array()) { - if (empty($this->insertFields)) { - if (empty($values)) { - if (!is_numeric(key($fields))) { - $values = array_values($fields); - $fields = array_keys($fields); - } - } - $this->insertFields = $fields; - if (!empty($values)) { - $this->insertValues[] = $values; - } - } - - return $this; - } - - /** - * Adds another set of values to the query to be inserted. - * - * If $values is a numeric-keyed array, it will be assumed to be in the same - * order as the original fields() call. If it is associative, it may be - * in any order as long as the keys of the array match the names of the - * fields. - * - * @param $values - * An array of values to add to the query. - * - * @return \Drupal\Core\Database\Query\Insert - * The called object. - */ - public function values(array $values) { - if (is_numeric(key($values))) { - $this->insertValues[] = $values; - } - else { - // Reorder the submitted values to match the fields array. - foreach ($this->insertFields as $key) { - $insert_values[$key] = $values[$key]; - } - // For consistency, the values array is always numerically indexed. - $this->insertValues[] = array_values($insert_values); - } - return $this; - } - - /** - * Specifies fields for which the database defaults should be used. - * - * If you want to force a given field to use the database-defined default, - * not NULL or undefined, use this method to instruct the database to use - * default values explicitly. In most cases this will not be necessary - * unless you are inserting a row that is all default values, as you cannot - * specify no values in an INSERT query. - * - * Specifying a field both in fields() and in useDefaults() is an error - * and will not execute. - * - * @param $fields - * An array of values for which to use the default values - * specified in the table definition. - * - * @return \Drupal\Core\Database\Query\Insert - * The called object. - */ - public function useDefaults(array $fields) { - $this->defaultFields = $fields; - return $this; - } - - /** * Sets the fromQuery on this InsertQuery object. * * @param \Drupal\Core\Database\Query\SelectInterface $query diff --git a/core/lib/Drupal/Core/Database/Query/InsertBase.php b/core/lib/Drupal/Core/Database/Query/InsertBase.php new file mode 100644 index 0000000..156dc19 --- /dev/null +++ b/core/lib/Drupal/Core/Database/Query/InsertBase.php @@ -0,0 +1,174 @@ +value pairs to be inserted. + * + * @param array $fields + * An array of fields on which to insert. This array may be indexed or + * associative. If indexed, the array is taken to be the list of fields. + * If associative, the keys of the array are taken to be the fields and + * the values are taken to be corresponding values to insert. If a + * $values argument is provided, $fields must be indexed. + * @param array $values + * (optional) An array of fields to insert into the database. The values + * must be specified in the same order as the $fields array. + * + * @return $this + * The called object. + */ + public function fields(array $fields, array $values = array()) { + if (!$this->insertFields) { + if (!$values) { + if (!is_numeric(array_keys($fields)[0])) { + $values = array_values($fields); + $fields = array_keys($fields); + } + } + $this->insertFields = $fields; + if (!empty($values)) { + $this->insertValues[] = $values; + } + } + return $this; + } + + /** + * Adds another set of values to the query to be inserted. + * + * If $values is a numeric-keyed array, it will be assumed to be in the same + * order as the original fields() call. If it is associative, it may be + * in any order as long as the keys of the array match the names of the + * fields. + * + * @param array $values + * An array of values to add to the query. + * + * @return $this + * The called object. + */ + public function values(array $values) { + if (is_numeric(key($values))) { + $this->insertValues[] = $values; + } + elseif ($this->insertFields) { + // Reorder the submitted values to match the fields array. + foreach ($this->insertFields as $key) { + $insert_values[$key] = $values[$key]; + } + // For consistency, the values array is always numerically indexed. + $this->insertValues[] = array_values($insert_values); + } + return $this; + } + + /** + * Specifies fields for which the database defaults should be used. + * + * If you want to force a given field to use the database-defined default, + * not NULL or undefined, use this method to instruct the database to use + * default values explicitly. In most cases this will not be necessary + * unless you are inserting a row that is all default values, as you cannot + * specify no values in an INSERT query. + * + * Specifying a field both in fields() and in useDefaults() is an error + * and will not execute. + * + * @param array $fields + * An array of values for which to use the default values + * specified in the table definition. + * + * @return $this + * The called object. + */ + public function useDefaults(array $fields) { + $this->defaultFields = $fields; + return $this; + } + + /** + * Prepares the placeholders for values that will be inserted. + * + * @return array + * An array of insert values. + */ + protected function prepareInsertValues() { + $max_placeholder = 0; + $values = array(); + if ($this->insertValues) { + foreach ($this->insertValues as $insert_values) { + $placeholders = array(); + + // Default fields aren't really placeholders, but this is the most convenient + // way to handle them. + $placeholders = array_pad($placeholders, count($this->defaultFields), 'default'); + + $new_placeholder = $max_placeholder + count($insert_values); + for ($i = $max_placeholder; $i < $new_placeholder; ++$i) { + $placeholders[] = ':db_insert_placeholder_' . $i; + } + $max_placeholder = $new_placeholder; + $values[] = '(' . implode(', ', $placeholders) . ')'; + } + } + else { + // If there are no values, then this is a default-only query. We still need to handle that. + $placeholders = array_fill(0, count($this->defaultFields), 'default'); + $values[] = '(' . implode(', ', $placeholders) . ')'; + } + + return $values; + } + +} diff --git a/core/lib/Drupal/Core/Database/Query/Upsert.php b/core/lib/Drupal/Core/Database/Query/Upsert.php index 7624042..d455961 100644 --- a/core/lib/Drupal/Core/Database/Query/Upsert.php +++ b/core/lib/Drupal/Core/Database/Query/Upsert.php @@ -18,14 +18,7 @@ * Insert except the rows will be set to the desired values even if the key * existed before. */ -class Upsert extends Query { - - /** - * The table to be used for INSERT and UPDATE. - * - * @var string - */ - protected $table; +class Upsert extends InsertBase { /** * The unique or primary key of the table. @@ -35,29 +28,6 @@ class Upsert extends Query { protected $key; /** - * An array of fields on which to insert. - * - * @var array - */ - protected $insertFields = array(); - - /** - * An array of fields which should be set to their database-defined defaults. - * - * Used on INSERT. - * - * @var array - */ - protected $defaultFields = array(); - - /** - * An array of values to be inserted. - * - * @var array - */ - protected $insertValues = array(); - - /** * Constructs an Upsert object. * * @param \Drupal\Core\Database\Connection $connection @@ -88,67 +58,6 @@ public function key($field) { } /** - * Adds a set of field->value pairs to be inserted. - * - * @param $fields - * An array of fields on which to insert. This array may be indexed or - * associative. If indexed, the array is taken to be the list of fields. - * If associative, the keys of the array are taken to be the fields and - * the values are taken to be corresponding values to insert. If a - * $values argument is provided, $fields must be indexed. - * @param $values - * An array of fields to insert into the database. The values must be - * specified in the same order as the $fields array. - * - * @return \Drupal\Core\Database\Query\Upsert - * The called object. - */ - public function fields(array $fields, array $values = array()) { - if (!$this->insertFields) { - if (!$values) { - if (!is_numeric(array_keys($fields)[0])) { - $values = array_values($fields); - $fields = array_keys($fields); - } - } - $this->insertFields = $fields; - if (!empty($values)) { - $this->insertValues[] = $values; - } - } - return $this; - } - - /** - * Adds another set of values to the query to be inserted. - * - * If $values is a numeric-keyed array, it will be assumed to be in the same - * order as the original fields() call. If it is associative, it may be - * in any order as long as the keys of the array match the names of the - * fields. - * - * @param array $values - * An array of values to add to the query. - * - * @return $this - * The called object. - */ - public function values(array $values) { - if (is_numeric(key($values))) { - $this->insertValues[] = $values; - } - elseif ($this->insertFields) { - // Reorder the submitted values to match the fields array. - foreach ($this->insertFields as $key) { - $insert_values[$key] = $values[$key]; - } - // For consistency, the values array is always numerically indexed. - $this->insertValues[] = array_values($insert_values); - } - return $this; - } - - /** * Preprocesses and validates the query. * * @return bool @@ -199,4 +108,33 @@ public function execute() { */ public function __toString() { } + /** + * Runs the query against the database. + * + * This implementation works for both MySQL and SQLite. + * + * @return \Drupal\Core\Database\StatementInterface|null + * A prepared statement, or NULL if the query is not valid. + */ + protected function executeHelper() { + if (!$this->preExecute()) { + return NULL; + } + + $max_placeholder = 0; + $values = array(); + foreach ($this->insertValues as $insert_values) { + foreach ($insert_values as $value) { + $values[':db_insert_placeholder_' . $max_placeholder++] = $value; + } + } + + $last_insert_id = $this->connection->query((string) $this, $values, $this->queryOptions); + + // Re-initialize the values array so that we can re-use this query. + $this->insertValues = array(); + + return $last_insert_id; + } + }