master

laravel/framework

Last updated at: 29/12/2023 09:21

ConnectionInterface.php

TLDR

The ConnectionInterface interface defines a contract for interacting with a database connection. It includes methods for executing various types of SQL queries and transactions.

Methods

table

This method begins a fluent query against a database table. It accepts a closure, a query builder instance, or a table name as the first parameter and an optional table alias as the second parameter. It returns a Query\Builder instance.

raw

This method creates a new raw query expression. It accepts a value and returns an instance of Illuminate\Contracts\Database\Query\Expression.

selectOne

This method runs a select statement and returns a single result. It accepts a SQL query, an optional array of bindings, and a flag indicating whether to use the read PDO connection. It returns a single result.

scalar

This method runs a select statement and returns the first column of the first row. It accepts a SQL query, an optional array of bindings, and a flag indicating whether to use the read PDO connection. It returns a single value.

select

This method runs a select statement against the database. It accepts a SQL query, an optional array of bindings, and a flag indicating whether to use the read PDO connection. It returns an array of results.

cursor

This method runs a select statement against the database and returns a generator. It accepts a SQL query, an optional array of bindings, and a flag indicating whether to use the read PDO connection. It returns a generator that iterates over the rows of the result.

insert

This method runs an insert statement against the database. It accepts a SQL query and an optional array of bindings. It returns a boolean indicating whether the statement was successful.

update

This method runs an update statement against the database. It accepts a SQL query and an optional array of bindings. It returns the number of rows affected.

delete

This method runs a delete statement against the database. It accepts a SQL query and an optional array of bindings. It returns the number of rows affected.

statement

This method executes an SQL statement and returns a boolean indicating the result. It accepts a SQL query and an optional array of bindings.

affectingStatement

This method runs an SQL statement and returns the number of rows affected. It accepts a SQL query and an optional array of bindings.

unprepared

This method runs a raw, unprepared query against the PDO connection. It accepts a SQL query and returns a boolean indicating whether the statement was successful.

prepareBindings

This method prepares the query bindings for execution. It accepts an array of query bindings and returns an array of prepared bindings.

transaction

This method executes a closure within a transaction. It accepts a closure as the first parameter, and an optional number of attempts as the second parameter. It returns the result of the closure execution.

beginTransaction

This method starts a new database transaction.

commit

This method commits the active database transaction.

rollBack

This method rolls back the active database transaction.

transactionLevel

This method returns the number of active transactions.

pretend

This method executes the given callback in "dry run" mode. It accepts a closure and returns an array of queries that would have been executed.

getDatabaseName

This method returns the name of the connected database.

<?php

namespace Illuminate\Database;

use Closure;

interface ConnectionInterface
{
    /**
     * Begin a fluent query against a database table.
     *
     * @param  \Closure|\Illuminate\Database\Query\Builder|string  $table
     * @param  string|null  $as
     * @return \Illuminate\Database\Query\Builder
     */
    public function table($table, $as = null);

    /**
     * Get a new raw query expression.
     *
     * @param  mixed  $value
     * @return \Illuminate\Contracts\Database\Query\Expression
     */
    public function raw($value);

    /**
     * Run a select statement and return a single result.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @param  bool  $useReadPdo
     * @return mixed
     */
    public function selectOne($query, $bindings = [], $useReadPdo = true);

    /**
     * Run a select statement and return the first column of the first row.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @param  bool  $useReadPdo
     * @return mixed
     *
     * @throws \Illuminate\Database\MultipleColumnsSelectedException
     */
    public function scalar($query, $bindings = [], $useReadPdo = true);

    /**
     * Run a select statement against the database.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @param  bool  $useReadPdo
     * @return array
     */
    public function select($query, $bindings = [], $useReadPdo = true);

    /**
     * Run a select statement against the database and returns a generator.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @param  bool  $useReadPdo
     * @return \Generator
     */
    public function cursor($query, $bindings = [], $useReadPdo = true);

    /**
     * Run an insert statement against the database.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @return bool
     */
    public function insert($query, $bindings = []);

    /**
     * Run an update statement against the database.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @return int
     */
    public function update($query, $bindings = []);

    /**
     * Run a delete statement against the database.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @return int
     */
    public function delete($query, $bindings = []);

    /**
     * Execute an SQL statement and return the boolean result.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @return bool
     */
    public function statement($query, $bindings = []);

    /**
     * Run an SQL statement and get the number of rows affected.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @return int
     */
    public function affectingStatement($query, $bindings = []);

    /**
     * Run a raw, unprepared query against the PDO connection.
     *
     * @param  string  $query
     * @return bool
     */
    public function unprepared($query);

    /**
     * Prepare the query bindings for execution.
     *
     * @param  array  $bindings
     * @return array
     */
    public function prepareBindings(array $bindings);

    /**
     * Execute a Closure within a transaction.
     *
     * @param  \Closure  $callback
     * @param  int  $attempts
     * @return mixed
     *
     * @throws \Throwable
     */
    public function transaction(Closure $callback, $attempts = 1);

    /**
     * Start a new database transaction.
     *
     * @return void
     */
    public function beginTransaction();

    /**
     * Commit the active database transaction.
     *
     * @return void
     */
    public function commit();

    /**
     * Rollback the active database transaction.
     *
     * @return void
     */
    public function rollBack();

    /**
     * Get the number of active transactions.
     *
     * @return int
     */
    public function transactionLevel();

    /**
     * Execute the given callback in "dry run" mode.
     *
     * @param  \Closure  $callback
     * @return array
     */
    public function pretend(Closure $callback);

    /**
     * Get the name of the connected database.
     *
     * @return string
     */
    public function getDatabaseName();
}