Select
+ Inheritance diagram for Select:

Description

Represents a SELECT query.

Protected Properties

string $alias
 Human-readable alias. More...
 
Closure $fetcher
 A callback to yield each result. More...
 
Column[] $refs = []
 Columns that can be accessed by an outer query. More...
 
AbstractTable $table
 The original table given to the constructor. More...
 
- Protected Properties inherited from AbstractTable
DB $db
 

Public Static Methods

magic static self factory (DB $db, string|AbstractTable $table, string|array $expressions=[' *'])
 

Public Methods

 __construct (DB $db, $table, $expressions=[' *'])
 
Statement __invoke (array $args=[])
 
string __toString ()
 Returns the alias. More...
 
int count (array $args=[])
 Clones the instance and selects COUNT(*), using the given execution arguments. More...
 
Statement execute (array $args=[])
 Executes the select, preparing a statement first if arguments are used. More...
 
array getAll (array $args=[])
 Executes and fetches all results. More...
 
Column[] getColumns ()
 
Generator getEach (array $args=[])
 Executes and yields from the fetcher. More...
 
mixed getFirst (array $args=[])
 Executes and returns from the fetcher. More...
 
Generator getIterator ()
 Executes without arguments and yields from the fetcher. More...
 
null mixed getResult (array $args=[])
 Executes and returns the first column of the first row. More...
 
$this group (string $column)
 Adds a column to the GROUP BY clause. More...
 
$this having (string ... $conditions)
 Adds conditions to the HAVING clause. More...
 
$this intersect (Select $select)
 INTERSECT SELECT ... More...
 
Predicate isEmpty ()
 NOT EXISTS (SELECT ...) More...
 
Predicate isNotEmpty ()
 EXISTS (SELECT ...) More...
 
$this join ($table, string ... $conditions)
 Adds INNER JOIN $table ON $conditions More...
 
$this joinLeft ($table, string ... $conditions)
 Adds LEFT JOIN $table ON $conditions More...
 
$this limit (int $limit, int $offset=0)
 Sets the LIMIT clause. More...
 
null Column offsetGet ($ref)
 Returns a reference Column for an outer query, qualified by the instance's alias. More...
 
$this order (string $order)
 Sets the ORDER BY clause. More...
 
Statement prepare ()
 
$this setAlias (string $alias)
 
$this setColumns ($expressions=[' *'])
 Compiles the column list and exposed reference columns. More...
 
$this setFetcher (Closure $fetcher)
 
string toSql ()
 SELECT ... More...
 
string toSubquery ()
 (SELECT ...) AS ALIAS More...
 
$this UNION (Select $select)
 UNION SELECT ... More...
 
$this unionAll (Select $select)
 UNION ALL SELECT ... More...
 
$this where (string ... $conditions)
 Adds conditions to the WHERE clause. More...
 
- Public Methods inherited from AbstractTable
 __construct (DB $db)
 
bool offsetExists ($column)
 
void offsetSet ($offset, $value)
 Throws. More...
 
void offsetUnset ($name)
 Throws. More...
 

Properties

◆ $alias

string $alias
protected

Human-readable alias.

◆ $fetcher

Closure $fetcher
protected

A callback to yield each result.

Defaults to yielding directly from the statement.

(Statement $statement):Generator

◆ $refs

Column [] $refs = []
protected

Columns that can be accessed by an outer query.

◆ $table

AbstractTable $table
protected

The original table given to the constructor.

Constructor

◆ __construct()

__construct ( DB  $db,
  $table,
  $expressions = ['*'] 
)
Parameters
DB$db
string | AbstractTable$table
string | string[]$expressions

Methods

◆ __invoke()

Statement __invoke ( array  $args = [])
Parameters
array$args
Returns
Statement

◆ __toString()

string __toString ( )
final

Returns the alias.

Returns
string

Reimplemented from AbstractTable.

◆ count()

int count ( array  $args = [])

Clones the instance and selects COUNT(*), using the given execution arguments.

Parameters
array$argsExecution arguments.
Returns
int

◆ execute()

Statement execute ( array  $args = [])

Executes the select, preparing a statement first if arguments are used.

Parameters
array$args
Returns
Statement

◆ factory()

magic static self factory ( DB  $db,
string|AbstractTable  $table,
string|array  $expressions = [' *'] 
)
static
Returns
self

◆ getAll()

array getAll ( array  $args = [])

Executes and fetches all results.

See also
fetcher
Parameters
array$argsExecution arguments.
Returns
array

◆ getColumns()

Column [] getColumns ( )
Returns
Column[]

Reimplemented from AbstractTable.

◆ getEach()

Generator getEach ( array  $args = [])

Executes and yields from the fetcher.

This is preferable over fetchAll() for iterating large result sets.

See also
fetcher
Parameters
array$argsExecution arguments.
Returns
Generator

◆ getFirst()

mixed getFirst ( array  $args = [])

Executes and returns from the fetcher.

See also
fetcher
Parameters
array$args
Returns
mixed

◆ getIterator()

Generator getIterator ( )

Executes without arguments and yields from the fetcher.

See also
fetcher
Returns
Generator

◆ getResult()

null mixed getResult ( array  $args = [])

Executes and returns the first column of the first row.

Use this for reductive queries that only have a single result.

Returns
null|mixed

◆ group()

$this group ( string  $column)

Adds a column to the GROUP BY clause.

Parameters
string$column
Returns
$this

◆ having()

$this having ( string ...  $conditions)

Adds conditions to the HAVING clause.

Parameters
string...$conditions
Returns
$this

◆ intersect()

$this intersect ( Select  $select)

INTERSECT SELECT ...

Note: MySQL does not support INTERSECT. An INNER JOIN on every column is used instead.

Parameters
Select$select
Returns
$this

◆ isEmpty()

Predicate isEmpty ( )

NOT EXISTS (SELECT ...)

Returns
Predicate

◆ isNotEmpty()

Predicate isNotEmpty ( )

EXISTS (SELECT ...)

Returns
Predicate

◆ join()

$this join (   $table,
string ...  $conditions 
)

Adds INNER JOIN $table ON $conditions

Parameters
string | Select$table
string...$conditions
Returns
$this

◆ joinLeft()

$this joinLeft (   $table,
string ...  $conditions 
)

Adds LEFT JOIN $table ON $conditions

Parameters
string | Select$table
string...$conditions
Returns
$this

◆ limit()

$this limit ( int  $limit,
int  $offset = 0 
)

Sets the LIMIT clause.

Parameters
int$limit
int$offset
Returns
$this

◆ offsetGet()

null Column offsetGet (   $ref)

Returns a reference Column for an outer query, qualified by the instance's alias.

Parameters
int | string$refOrdinal or reference name.
Returns
null|Column

Reimplemented from AbstractTable.

◆ order()

$this order ( string  $order)

Sets the ORDER BY clause.

Parameters
string$order
Returns
$this

◆ prepare()

Statement prepare ( )
Returns
Statement

◆ setAlias()

$this setAlias ( string  $alias)
Parameters
string$alias
Returns
$this

◆ setColumns()

$this setColumns (   $expressions = ['*'])

Compiles the column list and exposed reference columns.

Columns may be expressions, like COUNT(*) Unless an alias is given for such expressions, they can't be referenced externally.

Parameters
string | string[]$expressionsKeyed by alias if applicable.
Returns
$this

◆ setFetcher()

$this setFetcher ( Closure  $fetcher)
Parameters
Closure$fetcher
Returns
$this

◆ toSql()

string toSql ( )

SELECT ...

Returns
string

◆ toSubquery()

string toSubquery ( )

(SELECT ...) AS ALIAS

Returns
string

◆ UNION()

$this UNION ( Select  $select)

UNION SELECT ...

Parameters
Select$select
Returns
$this

◆ unionAll()

$this unionAll ( Select  $select)

UNION ALL SELECT ...

Parameters
Select$select
Returns
$this

◆ where()

$this where ( string ...  $conditions)

Adds conditions to the WHERE clause.

Parameters
string...$conditions
Returns
$this