| Versionen | |
|---|---|
| drupal7 | public InsertQuery::execute() |
Executes the insert query.
The last insert ID of the query, if one exists. If the query was given multiple sets of values to insert, the return value is undefined. If the query is flagged "delayed", then the insert ID won't be created until later when the query actually runs so the return value is also undefined. If no fields are specified, this method will do nothing and return NULL. That makes it safe to use in multi-insert loops.
includes/
<?php
public function execute() {
$last_insert_id = 0;
// Confirm that the user did not try to specify an identical
// field and default field.
if (array_intersect($this->insertFields, $this->defaultFields)) {
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) {
return NULL;
}
// Don't execute query without values.
if (!isset($this->insertValues[0]) && count($this->insertFields) > 0) {
return NULL;
}
// Each insert happens in its own query in the degenerate case. However,
// we wrap it in a transaction so that it is atomic where possible. On many
// databases, such as SQLite, this is also a notable performance boost.
$transaction = $this->connection->startTransaction();
$sql = (string) $this;
foreach ($this->insertValues as $insert_values) {
$last_insert_id = $this->connection->query($sql, $insert_values, $this->queryOptions);
}
// Re-initialize the values array so that we can re-use this query.
$this->insertValues = array();
// Transaction commits here where $transaction looses scope.
return $last_insert_id;
}
?>
Kommentare
Kommentar hinzufügen