Query/src/QueryBuilderInterface.php

494 lines
12 KiB
PHP
Raw Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
2012-12-18 16:19:52 -05:00
/**
* Query
*
2016-09-07 13:17:17 -04:00
* SQL Query Builder / Database Abstraction Layer
2012-12-18 16:19:52 -05:00
*
2020-04-10 20:54:31 -04:00
* PHP version 7.4
2016-09-07 13:17:17 -04:00
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
2020-03-18 11:31:56 -04:00
* @copyright 2012 - 2020 Timothy J. Warren
2016-09-07 13:17:17 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2019-12-11 16:49:42 -05:00
* @link https://git.timshomepage.net/aviat/Query
* @version 3.0.0
2012-12-18 16:19:52 -05:00
*/
2014-04-02 17:08:50 -04:00
namespace Query;
2019-12-11 16:49:06 -05:00
use PDO;
2016-10-12 22:12:25 -04:00
use PDOStatement;
2012-12-18 16:19:52 -05:00
/**
* Interface defining the Query Builder class
2019-12-11 16:49:06 -05:00
*
* @method affectedRows(): int
* @method beginTransaction(): bool
* @method commit(): bool
* @method errorCode(): string
* @method errorInfo(): array
* @method exec(string $statement): int
* @method getAttribute(int $attribute)
* @method getColumns(string $table): array | null
* @method getDbs(): array | null
* @method getFks(string $table): array | null
* @method getFunctions(): array | null
* @method getIndexes(string $table): array | null
* @method getLastQuery(): string
* @method getProcedures(): array | null
* @method getSchemas(): array | null
* @method getSequences(): array | null
* @method getSystemTables(): array | null
* @method getTables(): array
* @method getTriggers(): array | null
* @method getTypes(): array | null
* @method getUtil(): \Query\Drivers\AbstractUtil
* @method getVersion(): string
2019-12-11 16:49:06 -05:00
* @method getViews(): array | null
* @method inTransaction(): bool
* @method lastInsertId(string $name = NULL): string
* @method numRows(): int | null
* @method prepare(string $statement, array $driver_options = []): PDOStatement
* @method prepareExecute(string $sql, array $params): PDOStatement
* @method prepareQuery(string $sql, array $data): PDOStatement
* @method query(string $statement): PDOStatement
* @method quote(string $string, int $parameter_type = PDO::PARAM_STR): string
* @method rollback(): bool
* @method setAttribute(int $attribute, $value): bool
* @method setTablePrefix(string $prefix): void
* @method truncate(string $table): PDOStatement
2012-12-18 16:19:52 -05:00
*/
2015-11-10 10:12:23 -05:00
interface QueryBuilderInterface {
2012-12-18 16:19:52 -05:00
// --------------------------------------------------------------------------
// ! Select Queries
// --------------------------------------------------------------------------
/**
* Specifies rows to select in a query
*
* @param string $fields
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function select(string $fields): self;
2012-12-18 16:19:52 -05:00
/**
* Selects the maximum value of a field from a query
*
* @param string $field
2014-04-23 16:27:43 -04:00
* @param string|bool $as
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function selectMax(string $field, $as=FALSE): self;
2012-12-18 16:19:52 -05:00
/**
* Selects the minimum value of a field from a query
*
* @param string $field
2014-04-23 16:27:43 -04:00
* @param string|bool $as
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function selectMin(string $field, $as=FALSE): self;
2012-12-18 16:19:52 -05:00
/**
* Selects the average value of a field from a query
*
* @param string $field
2014-04-23 16:27:43 -04:00
* @param string|bool $as
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function selectAvg(string $field, $as=FALSE): self;
2012-12-18 16:19:52 -05:00
/**
* Selects the sum of a field from a query
*
* @param string $field
2014-04-23 16:27:43 -04:00
* @param string|bool $as
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function selectSum(string $field, $as=FALSE): self;
2012-12-18 16:19:52 -05:00
/**
* Adds the 'distinct' keyword to a query
*
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function distinct(): self;
2014-02-04 20:59:30 -05:00
/**
* Shows the query plan for the query
*
2019-12-11 16:49:06 -05:00
* @return self
2014-02-04 20:59:30 -05:00
*/
2019-12-11 16:49:06 -05:00
public function explain(): self;
2012-12-18 16:19:52 -05:00
/**
* Specify the database table to select from
*
* @param string $tblname
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function from(string $tblname): self;
2012-12-18 16:19:52 -05:00
// --------------------------------------------------------------------------
// ! 'Like' methods
// --------------------------------------------------------------------------
/**
* Creates a Like clause in the sql statement
*
* @param string $field
* @param mixed $values
2012-12-18 16:19:52 -05:00
* @param string $pos
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function like(string $field, $values, string $pos='both'): self;
2012-12-18 16:19:52 -05:00
/**
* Generates an OR Like clause
*
* @param string $field
* @param mixed $values
2012-12-18 16:19:52 -05:00
* @param string $pos
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function orLike(string $field, $values, string $pos='both'): self;
2012-12-18 16:19:52 -05:00
/**
* Generates a NOT LIKE clause
*
* @param string $field
* @param mixed $values
2012-12-18 16:19:52 -05:00
* @param string $pos
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function notLike(string $field, $values, string $pos='both'): self;
2012-12-18 16:19:52 -05:00
/**
* Generates a OR NOT LIKE clause
*
* @param string $field
* @param mixed $values
2012-12-18 16:19:52 -05:00
* @param string $pos
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function orNotLike(string $field, $values, string $pos='both'): self;
2012-12-18 16:19:52 -05:00
// --------------------------------------------------------------------------
// ! Having methods
// --------------------------------------------------------------------------
/**
* Generates a 'Having' clause
*
* @param mixed $key
* @param mixed $values
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function having($key, $values=[]): self;
2012-12-18 16:19:52 -05:00
/**
* Generates a 'Having' clause prefixed with 'OR'
*
* @param mixed $key
* @param mixed $values
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function orHaving($key, $values=[]): self;
2012-12-18 16:19:52 -05:00
// --------------------------------------------------------------------------
// ! 'Where' methods
// --------------------------------------------------------------------------
/**
* Specify condition(s) in the where clause of a query
* Note: this function works with key / value, or a
* passed array with key / value pairs
*
* @param mixed $key
* @param mixed $values
* @param bool $escape
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function where($key, $values=[], $escape = NULL): self;
2012-12-18 16:19:52 -05:00
/**
* Where clause prefixed with "OR"
*
* @param string $key
* @param mixed $values
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function orWhere($key, $values=[]): self;
2012-12-18 16:19:52 -05:00
/**
* Where clause with 'IN' statement
*
* @param mixed $field
* @param mixed $values
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function whereIn($field, $values=[]): self;
2012-12-18 16:19:52 -05:00
/**
* Where in statement prefixed with "or"
*
* @param string $field
* @param mixed $values
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function orWhereIn($field, $values=[]): self;
2012-12-18 16:19:52 -05:00
/**
* WHERE NOT IN (FOO) clause
*
* @param string $field
* @param mixed $values
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function whereNotIn($field, $values=[]): self;
2012-12-18 16:19:52 -05:00
/**
* OR WHERE NOT IN (FOO) clause
*
* @param string $field
* @param mixed $values
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function orWhereNotIn($field, $values=[]): self;
2012-12-18 16:19:52 -05:00
// --------------------------------------------------------------------------
// ! Other Query Modifier methods
// --------------------------------------------------------------------------
/**
* Sets values for inserts / updates / deletes
*
* @param mixed $key
* @param mixed $values
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function set($key, $values = NULL): self;
2012-12-18 16:19:52 -05:00
/**
* Creates a join phrase in a compiled query
*
* @param string $table
* @param string $condition
* @param string $type
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function join(string $table, string $condition, string $type=''): self;
2012-12-18 16:19:52 -05:00
/**
* Group the results by the selected field(s)
*
* @param mixed $field
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function groupBy($field): self;
2012-12-18 16:19:52 -05:00
/**
* Order the results by the selected field(s)
*
* @param string $field
* @param string $type
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function orderBy(string $field, string $type=''): self;
2012-12-18 16:19:52 -05:00
/**
* Set a limit on the current sql statement
*
* @param int $limit
* @param int|null $offset
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
public function limit(int $limit, ?int $offset=NULL): self;
2012-12-18 16:19:52 -05:00
// --------------------------------------------------------------------------
// ! Query Grouping Methods
// --------------------------------------------------------------------------
/**
* Adds a paren to the current query for query grouping
*
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function groupStart(): self;
2012-12-18 16:19:52 -05:00
2016-09-07 13:10:03 -04:00
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'NOT'
2016-10-12 22:12:25 -04:00
*
2019-12-11 16:49:06 -05:00
* @return self
2016-09-07 13:10:03 -04:00
*/
2019-12-11 16:49:06 -05:00
public function notGroupStart(): self;
2016-09-07 13:10:03 -04:00
2012-12-18 16:19:52 -05:00
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'OR'
*
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function orGroupStart(): self;
2012-12-18 16:19:52 -05:00
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'OR NOT'
*
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function orNotGroupStart(): self;
2012-12-18 16:19:52 -05:00
/**
* Ends a query group
*
2019-12-11 16:49:06 -05:00
* @return self
2012-12-18 16:19:52 -05:00
*/
2019-12-11 16:49:06 -05:00
public function groupEnd(): self;
2012-12-18 16:19:52 -05:00
// --------------------------------------------------------------------------
// ! Query execution methods
// --------------------------------------------------------------------------
/**
* Select and retrieve all records from the current table, and/or
* execute current compiled query
*
2016-09-07 14:22:52 -04:00
* @param string $table
* @param int|null $limit
* @param int|null $offset
2016-10-12 22:12:25 -04:00
* @return PDOStatement
2012-12-18 16:19:52 -05:00
*/
public function get(string $table='', ?int $limit=NULL, ?int $offset=NULL): PDOStatement;
2012-12-18 16:19:52 -05:00
/**
2016-10-12 22:12:25 -04:00
* Convenience method for get() with a where clause
2012-12-18 16:19:52 -05:00
*
* @param string $table
* @param array $where
* @param int|null $limit
* @param int|null $offset
2016-10-12 22:12:25 -04:00
* @return PDOStatement
2012-12-18 16:19:52 -05:00
*/
public function getWhere(string $table, $where=[], ?int $limit=NULL, ?int $offset=NULL): PDOStatement;
2012-12-18 16:19:52 -05:00
/**
2014-04-23 16:27:43 -04:00
* Retrieve the number of rows in the selected table
2012-12-18 16:19:52 -05:00
*
* @param string $table
* @return int
*/
public function countAll(string $table): int;
2012-12-18 16:19:52 -05:00
/**
* Retrieve the number of results for the generated query - used
* in place of the get() method
*
* @param string $table
2016-09-07 13:10:03 -04:00
* @param bool $reset - Whether to keep the query after counting the results
2012-12-18 16:19:52 -05:00
* @return int
*/
2016-10-13 21:55:23 -04:00
public function countAllResults(string $table='', bool $reset=TRUE): int;
2012-12-18 16:19:52 -05:00
/**
* Creates an insert clause, and executes it
*
* @param string $table
* @param mixed $data
2016-10-12 22:12:25 -04:00
* @return PDOStatement
2012-12-18 16:19:52 -05:00
*/
public function insert(string $table, $data=[]): PDOStatement;
2014-03-28 15:16:39 -04:00
/**
* Creates and executes a batch insertion query
*
* @param string $table
* @param array $data
* @return PDOStatement | null
2014-03-28 15:16:39 -04:00
*/
public function insertBatch(string $table, $data=[]): ?PDOStatement;
2016-09-07 13:10:03 -04:00
2012-12-18 16:19:52 -05:00
/**
* Creates an update clause, and executes it
*
* @param string $table
* @param mixed $data
2016-10-12 22:12:25 -04:00
* @return PDOStatement
2012-12-18 16:19:52 -05:00
*/
public function update(string $table, $data=[]): PDOStatement;
2016-09-07 13:10:03 -04:00
/**
* Creates a batch update, and executes it.
* Returns the number of affected rows
*
2018-01-26 08:39:30 -05:00
* @param string $table The table to update
* @param array $data an array of update values
* @param string $where The where key
* @return int|null
2016-09-07 13:10:03 -04:00
*/
2018-01-26 08:39:30 -05:00
public function updateBatch(string $table, array $data, string $where): ?int;
2012-12-18 16:19:52 -05:00
/**
* Deletes data from a table
*
* @param string $table
* @param mixed $where
2016-10-12 22:12:25 -04:00
* @return PDOStatement
2012-12-18 16:19:52 -05:00
*/
public function delete(string $table, $where=''): PDOStatement;
2012-12-18 16:19:52 -05:00
// --------------------------------------------------------------------------
// ! SQL Returning Methods
// --------------------------------------------------------------------------
/**
* Returns the generated 'select' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
2016-10-13 21:55:23 -04:00
public function getCompiledSelect(string $table='', bool $reset=TRUE): string;
2012-12-18 16:19:52 -05:00
/**
* Returns the generated 'insert' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
2016-10-13 21:55:23 -04:00
public function getCompiledInsert(string $table, bool $reset=TRUE): string;
2012-12-18 16:19:52 -05:00
/**
* Returns the generated 'update' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
2016-10-13 21:55:23 -04:00
public function getCompiledUpdate(string $table='', bool $reset=TRUE): string;
2012-12-18 16:19:52 -05:00
/**
* Returns the generated 'delete' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
2016-10-13 21:55:23 -04:00
public function getCompiledDelete(string $table='', bool $reset=TRUE): string;
2012-12-18 16:19:52 -05:00
// --------------------------------------------------------------------------
// ! Miscellaneous Methods
// --------------------------------------------------------------------------
/**
* Clear out the class variables, so the next query can be run
*
* @return void
*/
2018-01-22 15:43:56 -05:00
public function resetQuery(): void;
2012-12-18 16:19:52 -05:00
}
2016-09-07 13:10:03 -04:00
// End of QueryBuilderInterface.php