function Insert::execute

Same name in this branch
  1. 11.x core/modules/mysql/src/Driver/Database/mysql/Insert.php \Drupal\mysql\Driver\Database\mysql\Insert::execute()
  2. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Insert.php \Drupal\pgsql\Driver\Database\pgsql\Insert::execute()
  3. 11.x core/lib/Drupal/Core/Database/Query/Insert.php \Drupal\Core\Database\Query\Insert::execute()
Same name and namespace in other branches
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Insert.php \Drupal\sqlite\Driver\Database\sqlite\Insert::execute()
  2. 9 core/modules/mysql/src/Driver/Database/mysql/Insert.php \Drupal\mysql\Driver\Database\mysql\Insert::execute()
  3. 9 core/modules/pgsql/src/Driver/Database/pgsql/Insert.php \Drupal\pgsql\Driver\Database\pgsql\Insert::execute()
  4. 9 core/lib/Drupal/Core/Database/Query/Insert.php \Drupal\Core\Database\Query\Insert::execute()
  5. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Insert.php \Drupal\Core\Database\Driver\sqlite\Insert::execute()
  6. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Insert.php \Drupal\Core\Database\Driver\mysql\Insert::execute()
  7. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Insert.php \Drupal\Core\Database\Driver\pgsql\Insert::execute()
  8. 8.9.x core/lib/Drupal/Core/Database/Query/Insert.php \Drupal\Core\Database\Query\Insert::execute()
  9. 10 core/modules/sqlite/src/Driver/Database/sqlite/Insert.php \Drupal\sqlite\Driver\Database\sqlite\Insert::execute()
  10. 10 core/modules/mysql/src/Driver/Database/mysql/Insert.php \Drupal\mysql\Driver\Database\mysql\Insert::execute()
  11. 10 core/modules/pgsql/src/Driver/Database/pgsql/Insert.php \Drupal\pgsql\Driver\Database\pgsql\Insert::execute()
  12. 10 core/lib/Drupal/Core/Database/Query/Insert.php \Drupal\Core\Database\Query\Insert::execute()

Overrides Insert::execute

File

core/modules/sqlite/src/Driver/Database/sqlite/Insert.php, line 20

Class

Insert
SQLite implementation of <a href="/api/drupal/core%21lib%21Drupal%21Core%21Database%21Query%21Insert.php/class/Insert/11.x" title="General class for an abstracted INSERT query." class="local">\Drupal\Core\Database\Query\Insert</a>.

Namespace

Drupal\sqlite\Driver\Database\sqlite

Code

public function execute() {
    if (!$this->preExecute()) {
        return NULL;
    }
    // If we're selecting from a SelectQuery, finish building the query and
    // pass it back, as any remaining options are irrelevant.
    if (!empty($this->fromQuery)) {
        // The SelectQuery may contain arguments, load and pass them through.
        return $this->connection
            ->query((string) $this, $this->fromQuery
            ->getArguments(), $this->queryOptions);
    }
    // If there are any fields in the query, execute normal INSERT statements.
    if (count($this->insertFields)) {
        $stmt = $this->connection
            ->prepareStatement((string) $this, $this->queryOptions);
        if (count($this->insertValues) === 1) {
            // Inserting a single row does not require a transaction to be atomic,
            // and executes faster without a transaction wrapper.
            $insert_values = $this->insertValues[0];
            try {
                $stmt->execute($insert_values, $this->queryOptions);
            } catch (\Exception $e) {
                $this->connection
                    ->exceptionHandler()
                    ->handleExecutionException($e, $stmt, $insert_values, $this->queryOptions);
            }
        }
        else {
            // Inserting multiple rows requires a transaction to be atomic, and
            // executes faster as a single transaction.
            try {
                $transaction = $this->connection
                    ->startTransaction();
            } catch (\PDOException $e) {
                // $this->connection->exceptionHandler()->handleExecutionException()
                // requires a $statement argument, so we cannot use that.
                throw new DatabaseExceptionWrapper($e->getMessage(), 0, $e);
            }
            foreach ($this->insertValues as $insert_values) {
                try {
                    $stmt->execute($insert_values, $this->queryOptions);
                } catch (\Exception $e) {
                    // One of the INSERTs failed, rollback the whole batch.
                    $transaction->rollBack();
                    $this->connection
                        ->exceptionHandler()
                        ->handleExecutionException($e, $stmt, $insert_values, $this->queryOptions);
                }
            }
        }
        // Re-initialize the values array so that we can re-use this query.
        $this->insertValues = [];
    }
    else {
        $stmt = $this->connection
            ->prepareStatement("INSERT INTO {{$this->table}} DEFAULT VALUES", $this->queryOptions);
        try {
            $stmt->execute(NULL, $this->queryOptions);
        } catch (\Exception $e) {
            $this->connection
                ->exceptionHandler()
                ->handleExecutionException($e, $stmt, [], $this->queryOptions);
        }
    }
    return $this->connection
        ->lastInsertId();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.