Remove redundant docblocks, bump php version requirement

This commit is contained in:
Timothy Warren 2022-09-29 10:18:26 -04:00
parent 69c27e2415
commit 3013242106
26 changed files with 74 additions and 777 deletions

View File

@ -24,11 +24,11 @@
"config": {
"lock": false,
"platform": {
"php": "7.4"
"php": "8.1"
}
},
"require": {
"php": ">=7.4",
"php": ">=8.1",
"ext-pdo": "*"
},
"require-dev": {

View File

@ -26,13 +26,11 @@ final class ConnectionManager {
/**
* Map of named database connections
* @var array
*/
private array $connections = [];
/**
* Class instance variable
* @var ConnectionManager|null
*/
private static ?ConnectionManager $instance = NULL;
@ -69,7 +67,6 @@ final class ConnectionManager {
* Make sure serialize/deserialize doesn't work
*
* @throws DomainException
* @return void
*/
public function __wakeup(): void
{
@ -80,7 +77,6 @@ final class ConnectionManager {
* Return a connection manager instance
*
* @staticvar null $instance
* @return ConnectionManager
*/
public static function getInstance(): ConnectionManager
{
@ -96,7 +92,6 @@ final class ConnectionManager {
* Returns the connection specified by the name given
*
* @param string|array|object $name
* @return QueryBuilderInterface
* @throws Exception\NonExistentConnectionException
*/
public function getConnection($name = ''): QueryBuilderInterface
@ -121,7 +116,6 @@ final class ConnectionManager {
*
* @param array|object $params
* @throws Exception\BadDBDriverException
* @return QueryBuilderInterface
*/
public function connect($params): QueryBuilderInterface
{
@ -131,9 +125,9 @@ final class ConnectionManager {
$driver = "\\Query\\Drivers\\{$dbType}\\Driver";
// Create the database connection
$db = ! empty($params->user)
? new $driver($dsn, $params->user, $params->pass, $options)
: new $driver($dsn, '', '', $options);
$db = empty($params->user)
? new $driver($dsn, '', '', $options)
: new $driver($dsn, $params->user, $params->pass, $options);
// Set the table prefix, if it exists
if (isset($params->prefix))
@ -162,8 +156,8 @@ final class ConnectionManager {
* Parses params into a dsn and option array
*
* @param array|object $rawParams
* @return array
* @throws Exception\BadDBDriverException
* @return mixed[]
*/
public function parseParams($rawParams): array
{
@ -187,14 +181,7 @@ final class ConnectionManager {
}
// Create the dsn for the database to connect to
if(strtolower($dbType) === 'sqlite')
{
$dsn = $params->file;
}
else
{
$dsn = $this->createDsn($dbType, $params);
}
$dsn = strtolower($dbType) === 'sqlite' ? $params->file : $this->createDsn($dbType, $params);
return [$dsn, $dbType, $params, $options];
@ -204,9 +191,6 @@ final class ConnectionManager {
* Create the dsn from the db type and params
*
* @codeCoverageIgnore
* @param string $dbType
* @param stdClass $params
* @return string
*/
private function createDsn(string $dbType, stdClass $params): string
{

View File

@ -35,59 +35,46 @@ abstract class AbstractDriver
/**
* Reference to the last executed query
* @var PDOStatement
*/
protected PDOStatement $statement;
/**
* Start character to escape identifiers
* @var string
*/
protected string $escapeCharOpen = '"';
/**
* End character to escape identifiers
* @var string
*/
protected string $escapeCharClose = '"';
/**
* Reference to sql class
* @var SQLInterface
*/
protected SQLInterface $driverSQL;
/**
* Reference to util class
* @var AbstractUtil
*/
protected AbstractUtil $util;
/**
* Last query executed
* @var string
*/
protected string $lastQuery = '';
/**
* Prefix to apply to table names
* @var string
*/
protected string $tablePrefix = '';
/**
* Whether the driver supports 'TRUNCATE'
* @var boolean
*/
protected bool $hasTruncate = TRUE;
/**
* PDO constructor wrapper
*
* @param string $dsn
* @param string $username
* @param string $password
* @param array $driverOptions
*/
public function __construct(string $dsn, string $username=NULL, string $password=NULL, array $driverOptions=[])
{
@ -100,13 +87,11 @@ abstract class AbstractDriver
/**
* Loads the subclasses for the driver
*
* @return void
*/
protected function _loadSubClasses(): void
{
// Load the sql and util class for the driver
$thisClass = \get_class($this);
$thisClass = $this::class;
$nsArray = explode("\\", $thisClass);
array_pop($nsArray);
$driver = array_pop($nsArray);
@ -121,8 +106,6 @@ abstract class AbstractDriver
* Allow invoke to work on table object
*
* @codeCoverageIgnore
* @param string $name
* @param array $args
* @return mixed
*/
public function __call(string $name, array $args = [])
@ -142,11 +125,8 @@ abstract class AbstractDriver
// --------------------------------------------------------------------------
// ! Accessors / Mutators
// --------------------------------------------------------------------------
/**
* Get the last sql query executed
*
* @return string
*/
public function getLastQuery(): string
{
@ -155,9 +135,6 @@ abstract class AbstractDriver
/**
* Set the last query sql
*
* @param string $queryString
* @return void
*/
public function setLastQuery(string $queryString): void
{
@ -166,8 +143,6 @@ abstract class AbstractDriver
/**
* Get the SQL class for the current driver
*
* @return SQLInterface
*/
public function getSql(): SQLInterface
{
@ -176,8 +151,6 @@ abstract class AbstractDriver
/**
* Get the Util class for the current driver
*
* @return AbstractUtil
*/
public function getUtil(): AbstractUtil
{
@ -186,9 +159,6 @@ abstract class AbstractDriver
/**
* Set the common table name prefix
*
* @param string $prefix
* @return void
*/
public function setTablePrefix(string $prefix): void
{
@ -198,12 +168,9 @@ abstract class AbstractDriver
// --------------------------------------------------------------------------
// ! Concrete functions that can be overridden in child classes
// --------------------------------------------------------------------------
/**
* Simplifies prepared statements for database queries
*
* @param string $sql
* @param array $data
* @return PDOStatement | FALSE
* @throws InvalidArgumentException
*/
@ -230,10 +197,7 @@ abstract class AbstractDriver
/**
* Create and execute a prepared statement with the provided parameters
*
* @param string $sql
* @param array $params
* @throws InvalidArgumentException
* @return PDOStatement
*/
public function prepareExecute(string $sql, array $params): PDOStatement
{
@ -245,8 +209,6 @@ abstract class AbstractDriver
/**
* Returns number of rows affected by an INSERT, UPDATE, DELETE type query
*
* @return int
*/
public function affectedRows(): int
{
@ -256,8 +218,6 @@ abstract class AbstractDriver
/**
* Prefixes a table if it is not already prefixed
* @param string $table
* @return string
*/
public function prefixTable(string $table): string
{
@ -285,9 +245,6 @@ abstract class AbstractDriver
/**
* Quote database table name, and set prefix
*
* @param string $table
* @return string
*/
public function quoteTable(string $table): string
{
@ -301,9 +258,8 @@ abstract class AbstractDriver
* Surrounds the string with the databases identifier escape characters
*
* @param mixed $identifier
* @return string|array
*/
public function quoteIdent($identifier)
public function quoteIdent($identifier): string|array
{
if (is_array($identifier))
{
@ -314,7 +270,7 @@ abstract class AbstractDriver
$identifier = (string)$identifier;
// Handle comma-separated identifiers
if (strpos($identifier, ',') !== FALSE)
if (str_contains($identifier, ','))
{
$parts = array_map('mb_trim', explode(',', $identifier));
$parts = array_map([$this, __METHOD__], $parts);
@ -440,7 +396,6 @@ abstract class AbstractDriver
/**
* Retrieve column information for the current database table
*
* @param string $table
* @return array
*/
public function getColumns(string $table): ?array
@ -451,7 +406,6 @@ abstract class AbstractDriver
/**
* Retrieve foreign keys for the table
*
* @param string $table
* @return array
*/
public function getFks(string $table): ?array
@ -462,7 +416,6 @@ abstract class AbstractDriver
/**
* Retrieve indexes for the table
*
* @param string $table
* @return array
*/
public function getIndexes(string $table): ?array
@ -482,8 +435,6 @@ abstract class AbstractDriver
/**
* Get the version of the database engine
*
* @return string
*/
public function getVersion(): string
{
@ -495,7 +446,6 @@ abstract class AbstractDriver
*
* @param string|array|null $query
* @param bool $filteredIndex
* @return array|null
*/
public function driverQuery($query, $filteredIndex=TRUE): ?array
{
@ -525,7 +475,6 @@ abstract class AbstractDriver
* Return the number of rows returned for a SELECT query
*
* @see http://us3.php.net/manual/en/pdostatement.rowcount.php#87110
* @return int|null
*/
public function numRows(): ?int
{
@ -544,7 +493,6 @@ abstract class AbstractDriver
/**
* Create sql for batch insert
*
* @param string $table
* @param mixed $data
* @return array<string|array|null>
*/
@ -557,7 +505,7 @@ abstract class AbstractDriver
$vals = [];
foreach($data as $group)
{
$vals = array_merge($vals, array_values($group));
$vals = [...$vals, ...array_values($group)];
}
$table = $this->quoteTable($table);
@ -598,7 +546,7 @@ abstract class AbstractDriver
// Get the keys of the current set of data, except the one used to
// set the update condition
$fields = array_unique(
array_reduce($data, static function ($previous, $current) use (&$affectedRows, $where) {
array_reduce($data, static function ($previous, $current) use (&$affectedRows, $where): array {
$affectedRows++;
$keys = array_diff(array_keys($current), [$where]);
@ -653,9 +601,6 @@ abstract class AbstractDriver
/**
* Empty the passed table
*
* @param string $table
* @return PDOStatement
*/
public function truncate(string $table): PDOStatement
{
@ -671,10 +616,6 @@ abstract class AbstractDriver
/**
* Generate the returning clause for the current database
*
* @param string $query
* @param string $select
* @return string
*/
public function returning(string $query, string $select): string
{
@ -694,7 +635,7 @@ abstract class AbstractDriver
// that value, otherwise, return the original value
return (
is_string($str)
&& strpos($str, $this->escapeCharOpen) !== 0
&& ( ! str_starts_with($str, $this->escapeCharOpen))
&& strrpos($str, $this->escapeCharClose) !== 0
)
? "{$this->escapeCharOpen}{$str}{$this->escapeCharClose}"
@ -704,14 +645,11 @@ abstract class AbstractDriver
/**
* Sets the table prefix on the passed string
*
* @param string $str
* @return string
*/
protected function _prefix(string $str): string
{
// Don't prefix an already prefixed table
if (strpos($str, $this->tablePrefix) !== FALSE)
if (str_contains($str, $this->tablePrefix))
{
return $str;
}

View File

@ -23,10 +23,7 @@ abstract class AbstractSQL implements SQLInterface {
/**
* Limit clause
*
* @param string $sql
* @param int $limit
* @param int $offset
* @return string
*/
public function limit(string $sql, int $limit, ?int $offset=NULL): string
{

View File

@ -20,26 +20,15 @@ namespace Query\Drivers;
*/
abstract class AbstractUtil {
/**
* Reference to the current connection object
* @var DriverInterface
*/
private DriverInterface $connection;
/**
* Save a reference to the connection object for later use
*
* @param DriverInterface $connection
*/
public function __construct(DriverInterface $connection)
public function __construct(private DriverInterface $connection)
{
$this->connection = $connection;
}
/**
* Get the driver object for the current connection
*
* @return DriverInterface
*/
public function getDriver(): DriverInterface
{
@ -51,9 +40,7 @@ abstract class AbstractUtil {
*
* @param string $name
* @param array $fields
* @param array $constraints
* @param bool $ifNotExists
* @return string
*/
public function createTable($name, $fields, array $constraints=[], $ifNotExists=TRUE): string
{
@ -93,7 +80,6 @@ abstract class AbstractUtil {
* Drop the selected table
*
* @param string $name
* @return string
*/
public function deleteTable($name): string
{
@ -103,12 +89,10 @@ abstract class AbstractUtil {
// --------------------------------------------------------------------------
// ! Abstract Methods
// --------------------------------------------------------------------------
/**
* Return an SQL file with the database table structure
*
* @abstract
* @return string
*/
abstract public function backupStructure(): string;
@ -116,7 +100,6 @@ abstract class AbstractUtil {
* Return an SQL file with the database data as insert statements
*
* @abstract
* @return string
*/
abstract public function backupData(): string;

View File

@ -40,19 +40,12 @@ interface DriverInterface /* extends the interface of PDO */ {
/**
* Constructor/Connection method
*
* @param string $dsn
* @param string $username
* @param string $password
* @param array $driverOptions
*/
public function __construct(string $dsn, string $username=NULL, string $password=NULL, array $driverOptions = []);
/**
* Simplifies prepared statements for database queries
*
* @param string $sql
* @param array $data
* @return PDOStatement|null
* @throws InvalidArgumentException
*/
@ -61,7 +54,6 @@ interface DriverInterface /* extends the interface of PDO */ {
/**
* Retrieve column information for the current database table
*
* @param string $table
* @return array
*/
public function getColumns(string $table): ?array;
@ -76,7 +68,6 @@ interface DriverInterface /* extends the interface of PDO */ {
/**
* Retrieve indexes for the table
*
* @param string $table
* @return array
*/
public function getIndexes(string $table): ?array;
@ -84,7 +75,6 @@ interface DriverInterface /* extends the interface of PDO */ {
/**
* Retrieve foreign keys for the table
*
* @param string $table
* @return array
*/
public function getFks(string $table): ?array;
@ -156,26 +146,16 @@ interface DriverInterface /* extends the interface of PDO */ {
/**
* Surrounds the string with the databases identifier escape characters
*
* @param string|array $ident
* @return string|array
*/
public function quoteIdent($ident);
public function quoteIdent(string|array $ident): string|array;
/**
* Quote database table name, and set prefix
*
* @param string $table
* @return string
*/
public function quoteTable(string $table): string;
/**
* Create and execute a prepared statement with the provided parameters
*
* @param string $sql
* @param array $params
* @return PDOStatement
*/
public function prepareExecute(string $sql, array $params): PDOStatement;
@ -190,8 +170,6 @@ interface DriverInterface /* extends the interface of PDO */ {
/**
* Returns number of rows affected by an INSERT, UPDATE, DELETE type query
*
* @return int
*/
public function affectedRows(): int;
@ -205,81 +183,52 @@ interface DriverInterface /* extends the interface of PDO */ {
/**
* Prefixes a table if it is not already prefixed
*
* @param string $table
* @return string
*/
public function prefixTable(string $table): string;
/**
* Create sql for batch insert
*
* @param string $table
* @param array $data
* @return array
*/
public function insertBatch(string $table, array $data=[]): array;
/**
* Creates a batch update, and executes it.
* Returns the number of affected rows
*
* @param string $table
* @param array $data
* @param string $where
* @return array
*/
public function updateBatch(string $table, array $data, string $where): array;
/**
* Empty the passed table
*
* @param string $table
* @return PDOStatement
*/
public function truncate(string $table): PDOStatement;
/**
* Get the SQL class for the current driver
*
* @return SQLInterface
*/
public function getSql(): SQLInterface;
/**
* Get the Util class for the current driver
*
* @return AbstractUtil
*/
public function getUtil(): AbstractUtil;
/**
* Get the version of the database engine
*
* @return string
*/
public function getVersion(): string;
/**
* Get the last sql query executed
*
* @return string
*/
public function getLastQuery(): string;
/**
* Set the last query sql
*
* @param string $queryString
* @return void
*/
public function setLastQuery(string $queryString): void;
/**
* Set the common table name prefix
*
* @param string $prefix
* @return void
*/
public function setTablePrefix(string $prefix): void;
}

View File

@ -26,15 +26,11 @@ class Driver extends AbstractDriver {
/**
* Set the backtick as the MySQL escape character
*
* @var string
*/
protected string $escapeCharOpen = '`';
/**
* Set the backtick as the MySQL escape character
*
* @var string
*/
protected string $escapeCharClose = '`';
@ -42,10 +38,6 @@ class Driver extends AbstractDriver {
* Connect to MySQL Database
*
* @codeCoverageIgnore
* @param string $dsn
* @param string $username
* @param string $password
* @param array $options
*/
public function __construct(string $dsn, string $username=NULL, string $password=NULL, array $options=[])
{
@ -57,7 +49,7 @@ class Driver extends AbstractDriver {
]);
}
if (strpos($dsn, 'mysql') === FALSE)
if ( ! str_contains($dsn, 'mysql'))
{
$dsn = 'mysql:'.$dsn;
}
@ -67,10 +59,6 @@ class Driver extends AbstractDriver {
/**
* Generate the returning clause for the current database
*
* @param string $query
* @param string $select
* @return string
*/
public function returning(string $query, string $select): string
{

View File

@ -25,10 +25,7 @@ class SQL extends AbstractSQL {
/**
* Limit clause
*
* @param string $sql
* @param int $limit
* @param int|boolean $offset
* @return string
*/
public function limit(string $sql, int $limit, ?int $offset=NULL): string
{
@ -42,9 +39,6 @@ class SQL extends AbstractSQL {
/**
* Get the query plan for the sql query
*
* @param string $sql
* @return string
*/
public function explain(string $sql): string
{
@ -53,8 +47,6 @@ class SQL extends AbstractSQL {
/**
* Random ordering keyword
*
* @return string
*/
public function random(): string
{
@ -63,8 +55,6 @@ class SQL extends AbstractSQL {
/**
* Returns sql to list other databases
*
* @return string
*/
public function dbList(): string
{
@ -78,7 +68,6 @@ SQL;
* Returns sql to list tables
*
* @param string $database
* @return string
*/
public function tableList($database=''): string
{
@ -94,8 +83,6 @@ SQL;
/**
* Overridden in MySQL class
*
* @return string
*/
public function systemTableList(): string
{
@ -107,8 +94,6 @@ SQL;
/**
* Returns sql to list views
*
* @return string
*/
public function viewList(): string
{
@ -117,8 +102,6 @@ SQL;
/**
* Returns sql to list triggers
*
* @return string
*/
public function triggerList(): string
{
@ -127,8 +110,6 @@ SQL;
/**
* Return sql to list functions
*
* @return string
*/
public function functionList(): string
{
@ -137,8 +118,6 @@ SQL;
/**
* Return sql to list stored procedures
*
* @return string
*/
public function procedureList(): string
{
@ -157,8 +136,6 @@ SQL;
/**
* SQL to show list of field types
*
* @return string
*/
public function typeList(): string
{
@ -167,9 +144,6 @@ SQL;
/**
* SQL to show information about columns in a table
*
* @param string $table
* @return string
*/
public function columnList(string $table): string
{
@ -179,9 +153,6 @@ SQL;
/**
* Get the list of foreign keys for the current
* table
*
* @param string $table
* @return string
*/
public function fkList(string $table): string
{
@ -204,9 +175,6 @@ SQL;
/**
* Get the list of indexes for the current table
*
* @param string $table
* @return string
*/
public function indexList(string $table): string
{

View File

@ -25,8 +25,6 @@ class Util extends AbstractUtil {
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backupStructure(): string
{
@ -69,9 +67,6 @@ class Util extends AbstractUtil {
/**
* Create an SQL backup file for the current database's data
*
* @param array $exclude
* @return string
*/
public function backupData(array $exclude=[]): string
{
@ -94,7 +89,7 @@ class Util extends AbstractUtil {
$rows = $res->fetchAll(PDO::FETCH_ASSOC);
// Skip empty tables
if (count($rows) < 1)
if ((is_countable($rows) ? count($rows) : 0) < 1)
{
continue;
}

View File

@ -26,14 +26,10 @@ class Driver extends AbstractDriver {
* Connect to a PosgreSQL database
*
* @codeCoverageIgnore
* @param string $dsn
* @param string $username
* @param string $password
* @param array $options
*/
public function __construct(string $dsn, string $username=NULL, string $password=NULL, array $options=[])
{
if (strpos($dsn, 'pgsql') === FALSE)
if ( ! str_contains($dsn, 'pgsql'))
{
$dsn = 'pgsql:'.$dsn;
}
@ -60,10 +56,9 @@ SQL;
/**
* Retrieve foreign keys for the table
*
* @param string $table
* @return array
* @return mixed[]|null
*/
public function getFks($table): array
public function getFks(string $table): array
{
$valueMap = [
'c' => 'CASCADE',

View File

@ -24,9 +24,6 @@ class SQL extends AbstractSQL {
/**
* Get the query plan for the sql query
*
* @param string $sql
* @return string
*/
public function explain(string $sql): string
{
@ -35,8 +32,6 @@ class SQL extends AbstractSQL {
/**
* Random ordering keyword
*
* @return string
*/
public function random(): string
{
@ -45,8 +40,6 @@ class SQL extends AbstractSQL {
/**
* Returns sql to list other databases
*
* @return string
*/
public function dbList(): string
{
@ -59,8 +52,6 @@ SQL;
/**
* Returns sql to list tables
*
* @return string
*/
public function tableList(): string
{
@ -75,8 +66,6 @@ SQL;
/**
* Returns sql to list system tables
*
* @return string
*/
public function systemTableList(): string
{
@ -91,8 +80,6 @@ SQL;
/**
* Returns sql to list views
*
* @return string
*/
public function viewList(): string
{
@ -107,8 +94,6 @@ SQL;
/**
* Returns sql to list triggers
*
* @return string
*/
public function triggerList(): string
{
@ -132,8 +117,6 @@ SQL;
/**
* Return sql to list stored procedures
*
* @return string
*/
public function procedureList(): string
{
@ -148,8 +131,6 @@ SQL;
/**
* Return sql to list sequences
*
* @return string
*/
public function sequenceList(): string
{
@ -163,9 +144,6 @@ SQL;
/**
* Return sql to list columns of the specified table
*
* @param string $table
* @return string
*/
public function columnList(string $table): string
{
@ -185,8 +163,6 @@ SQL;
/**
* SQL to show list of field types
*
* @return string
*/
public function typeList(): string
{
@ -201,9 +177,6 @@ SQL;
/**
* Get the list of foreign keys for the current
* table
*
* @param string $table
* @return string
*/
public function fkList(string $table): string
{
@ -243,9 +216,6 @@ SQL;
/**
* Get the list of indexes for the current table
*
* @param string $table
* @return string
*/
public function indexList(string $table): string
{

View File

@ -25,8 +25,6 @@ class Util extends AbstractUtil {
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backupStructure(): string
{
@ -36,9 +34,6 @@ class Util extends AbstractUtil {
/**
* Create an SQL backup file for the current database's data
*
* @param array $exclude
* @return string
*/
public function backupData(array $exclude=[]): string
{
@ -60,7 +55,7 @@ class Util extends AbstractUtil {
$objRes = $res->fetchAll(PDO::FETCH_ASSOC);
// Don't add to the file if the table is empty
if (count($objRes) < 1)
if ((is_countable($objRes) ? count($objRes) : 0) < 1)
{
continue;
}

View File

@ -22,54 +22,36 @@ interface SQLInterface {
/**
* Get database specific sql for limit clause
*
* @param string $sql
* @param int $limit
* @param int|null $offset
* @return string
*/
public function limit(string $sql, int $limit, ?int $offset=NULL): string;
/**
* Modify the query to get the query plan
*
* @param string $sql
* @return string
*/
public function explain(string $sql): string;
/**
* Get the sql for random ordering
*
* @return string
*/
public function random(): string;
/**
* Returns sql to list other databases
*
* @return string
*/
public function dbList(): string;
/**
* Returns sql to list tables
*
* @return string
*/
public function tableList(): string;
/**
* Returns sql to list system tables
*
* @return string|array
*/
public function systemTableList();
public function systemTableList(): string|array;
/**
* Returns sql to list views
*
* @return string
*/
public function viewList(): string;
@ -103,34 +85,23 @@ interface SQLInterface {
/**
* Return sql to list database field types
*
* @return string|array
*/
public function typeList();
public function typeList(): string|array;
/**
* Get information about the columns in the
* specified table
*
* @param string $table
* @return string
*/
public function columnList(string $table): string;
/**
* Get the list of foreign keys for the current
* table
*
* @param string $table
* @return string
*/
public function fkList(string $table): string;
/**
* Get the list of indexes for the current table
*
* @param string $table
* @return string
*/
public function indexList(string $table): string;
}

View File

@ -29,21 +29,15 @@ class Driver extends AbstractDriver {
/**
* SQLite has a truncate optimization,
* but no support for the actual keyword
* @var boolean
*/
protected bool $hasTruncate = FALSE;
/**
* Open SQLite Database
*
* @param string $dsn
* @param string $user
* @param string $pass
* @param array $driverOptions
*/
public function __construct(string $dsn, string $user=NULL, string $pass=NULL, array $driverOptions=[])
{
if (strpos($dsn, 'sqlite:') === FALSE)
if ( ! str_contains($dsn, 'sqlite:'))
{
$dsn = "sqlite:{$dsn}";
}
@ -53,8 +47,6 @@ class Driver extends AbstractDriver {
/**
* Return list of dbs for the current connection, if possible. Meaningless for SQLite.
*
* @return array | null
*/
public function getDbs(): ?array
{
@ -64,7 +56,7 @@ class Driver extends AbstractDriver {
/**
* List tables for the current database
*
* @return mixed
* @return mixed[]
*/
public function getTables(): array
{
@ -76,10 +68,9 @@ class Driver extends AbstractDriver {
/**
* Retrieve foreign keys for the table
*
* @param string $table
* @return array
* @return array<int, array{child_column: mixed, parent_table: mixed, parent_column: mixed, update: mixed, delete: mixed}>
*/
public function getFks($table): array
public function getFks(string $table): array
{
$returnRows = [];
@ -101,9 +92,7 @@ class Driver extends AbstractDriver {
* Create sql for batch insert
*
* @codeCoverageIgnore
* @param string $table
* @param array $data
* @return array
* @return mixed[][]|string[]|null[]|string[]|null[]
*/
public function insertBatch(string $table, array $data=[]): array
{
@ -148,10 +137,6 @@ class Driver extends AbstractDriver {
/**
* Generate the returning clause for the current database
*
* @param string $query
* @param string $select
* @return string
*/
public function returning(string $query, string $select): string
{

View File

@ -25,9 +25,6 @@ class SQL extends AbstractSQL {
/**
* Get the query plan for the sql query
*
* @param string $sql
* @return string
*/
public function explain(string $sql): string
{
@ -36,8 +33,6 @@ class SQL extends AbstractSQL {
/**
* Random ordering keyword
*
* @return string
*/
public function random(): string
{
@ -47,8 +42,6 @@ class SQL extends AbstractSQL {
/**
* Returns sql to list other databases. Meaningless for SQLite, as this
* just returns the database(s) that we are currently connected to.
*
* @return string
*/
public function dbList(): string
{
@ -57,8 +50,6 @@ class SQL extends AbstractSQL {
/**
* Returns sql to list tables
*
* @return string
*/
public function tableList(): string
{
@ -89,8 +80,6 @@ SQL;
/**
* Returns sql to list views
*
* @return string
*/
public function viewList(): string
{
@ -101,8 +90,6 @@ SQL;
/**
* Returns sql to list triggers
*
* @return string
*/
public function triggerList(): string
{
@ -115,7 +102,6 @@ SQL;
* Return sql to list functions
*
* @throws NotImplementedException
* @return string
*/
public function functionList(): string
{
@ -126,7 +112,6 @@ SQL;
* Return sql to list stored procedures
*
* @throws NotImplementedException
* @return string
*/
public function procedureList(): string
{
@ -135,8 +120,6 @@ SQL;
/**
* Return sql to list sequences
*
* @return string
*/
public function sequenceList(): string
{
@ -155,42 +138,33 @@ SQL;
/**
* SQL to show information about columns in a table
*
* @param string $table
* @return string
*/
public function columnList(string $table): string
{
return <<<SQL
PRAGMA table_info("$table")
PRAGMA table_info("{$table}")
SQL;
}
/**
* Get the list of foreign keys for the current
* table
*
* @param string $table
* @return string
*/
public function fkList(string $table): string
{
return <<<SQL
PRAGMA foreign_key_list("$table")
PRAGMA foreign_key_list("{$table}")
SQL;
}
/**
* Get the list of indexes for the current table
*
* @param string $table
* @return string
*/
public function indexList(string $table): string
{
return <<<SQL
PRAGMA index_list("$table")
PRAGMA index_list("{$table}")
SQL;
}
}

View File

@ -25,9 +25,6 @@ class Util extends AbstractUtil {
/**
* Create an SQL backup file for the current database's data
*
* @param array $excluded
* @return string
*/
public function backupData(array $excluded=[]): string
{
@ -98,8 +95,6 @@ class Util extends AbstractUtil {
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backupStructure(): string
{

View File

@ -19,9 +19,9 @@ namespace Query;
* 'Enum' of join types
*/
class JoinType {
public const CROSS = 'cross';
public const INNER = 'inner';
public const OUTER = 'outer';
public const LEFT = 'left';
public const RIGHT = 'right';
public final const CROSS = 'cross';
public final const INNER = 'inner';
public final const OUTER = 'outer';
public final const LEFT = 'left';
public final const RIGHT = 'right';
}

View File

@ -19,7 +19,7 @@ namespace Query;
* 'Enum' of join types
*/
class LikeType {
public const BEFORE = 'before';
public const AFTER = 'after';
public const BOTH = 'both';
public final const BEFORE = 'before';
public final const AFTER = 'after';
public final const BOTH = 'both';
}

View File

@ -19,10 +19,10 @@ namespace Query;
* 'Enum' of query map types
*/
class MapType {
public const GROUP_END = 'group_end';
public const GROUP_START = 'group_start';
public const JOIN = 'join';
public const LIKE = 'like';
public const WHERE = 'where';
public const WHERE_IN = 'where_in';
public final const GROUP_END = 'group_end';
public final const GROUP_START = 'group_start';
public final const JOIN = 'join';
public final const LIKE = 'like';
public final const WHERE = 'where';
public final const WHERE_IN = 'where_in';
}

View File

@ -28,12 +28,8 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
// --------------------------------------------------------------------------
// ! Select Queries
// --------------------------------------------------------------------------
/**
* Specifies rows to select in a query
*
* @param string $fields
* @return self
*/
public function select(string $fields): self
{
@ -46,7 +42,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
{
if (stripos($field, 'as') !== FALSE)
{
$fieldsArray[$key] = preg_split('` as `i', $field);
$fieldsArray[$key] = preg_split('` as `i', (string) $field);
$fieldsArray[$key] = array_map('mb_trim', $fieldsArray[$key]);
}
}
@ -73,9 +69,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Selects the maximum value of a field from a query
*
* @param string $field
* @param string|bool $as
* @return self
*/
public function selectMax(string $field, $as=FALSE): self
{
@ -87,9 +81,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Selects the minimum value of a field from a query
*
* @param string $field
* @param string|bool $as
* @return self
*/
public function selectMin(string $field, $as=FALSE): self
{
@ -101,9 +93,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Selects the average value of a field from a query
*
* @param string $field
* @param string|bool $as
* @return self
*/
public function selectAvg(string $field, $as=FALSE): self
{
@ -115,9 +105,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Selects the sum of a field from a query
*
* @param string $field
* @param string|bool $as
* @return self
*/
public function selectSum(string $field, $as=FALSE): self
{
@ -129,7 +117,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Add a 'returning' clause to an insert,update, or delete query
*
* @param string $fields
* @return $this
*/
public function returning(string $fields = ''): self
@ -147,8 +134,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Adds the 'distinct' keyword to a query
*
* @return self
*/
public function distinct(): self
{
@ -159,8 +144,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Tell the database to give you the query plan instead of result set
*
* @return self
*/
public function explain(): self
{
@ -174,7 +157,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
* Alias of `from` method to better match CodeIgniter 4
*
* @param string $tableName
* @return self
*/
public function table(string $tableName): self
{
@ -183,9 +165,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Specify the database table to select from
*
* @param string $tableName
* @return self
*/
public function from(string $tableName): self
{
@ -206,14 +185,10 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
// --------------------------------------------------------------------------
// ! 'Like' methods
// --------------------------------------------------------------------------
/**
* Creates a Like clause in the sql statement
*
* @param string $field
* @param mixed $val
* @param string $pos
* @return self
*/
public function like(string $field, $val, string $pos=LikeType::BOTH): self
{
@ -223,10 +198,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Generates an OR Like clause
*
* @param string $field
* @param mixed $val
* @param string $pos
* @return self
*/
public function orLike(string $field, $val, string $pos=LikeType::BOTH): self
{
@ -236,10 +208,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Generates a NOT LIKE clause
*
* @param string $field
* @param mixed $val
* @param string $pos
* @return self
*/
public function notLike(string $field, $val, string $pos=LikeType::BOTH): self
{
@ -249,10 +218,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Generates a OR NOT LIKE clause
*
* @param string $field
* @param mixed $val
* @param string $pos
* @return self
*/
public function orNotLike(string $field, $val, string $pos=LikeType::BOTH): self
{
@ -262,13 +228,11 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
// --------------------------------------------------------------------------
// ! Having methods
// --------------------------------------------------------------------------
/**
* Generates a 'Having' clause
*
* @param mixed $key
* @param mixed $val
* @return self
*/
public function having($key, $val=[]): self
{
@ -280,7 +244,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
*
* @param mixed $key
* @param mixed $val
* @return self
*/
public function orHaving($key, $val=[]): self
{
@ -290,7 +253,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
// --------------------------------------------------------------------------
// ! 'Where' methods
// --------------------------------------------------------------------------
/**
* Specify condition(s) in the where clause of a query
* Note: this function works with key / value, or a
@ -299,7 +261,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
* @param mixed $key
* @param mixed $val
* @param mixed $escape
* @return self
*/
public function where($key, $val=[], $escape=NULL): self
{
@ -311,7 +272,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
*
* @param string $key
* @param mixed $val
* @return self
*/
public function orWhere($key, $val=[]): self
{
@ -323,7 +283,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
*
* @param mixed $field
* @param mixed $val
* @return self
*/
public function whereIn($field, $val=[]): self
{
@ -335,7 +294,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
*
* @param string $field
* @param mixed $val
* @return self
*/
public function orWhereIn($field, $val=[]): self
{
@ -347,7 +305,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
*
* @param string $field
* @param mixed $val
* @return self
*/
public function whereNotIn($field, $val=[]): self
{
@ -359,7 +316,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
*
* @param string $field
* @param mixed $val
* @return self
*/
public function orWhereNotIn($field, $val=[]): self
{
@ -369,24 +325,15 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
// --------------------------------------------------------------------------
// ! Other Query Modifier methods
// --------------------------------------------------------------------------
/**
* Sets values for inserts / updates / deletes
*
* @param mixed $key
* @param mixed $val
* @return self
*/
public function set($key, $val = NULL): self
{
if (is_scalar($key))
{
$pairs = [$key => $val];
}
else
{
$pairs = $key;
}
$pairs = is_scalar($key) ? [$key => $val] : $key;
$keys = array_keys($pairs);
$values = array_values($pairs);
@ -411,11 +358,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Creates a join phrase in a compiled query
*
* @param string $table
* @param string $condition
* @param string $type
* @return self
*/
public function join(string $table, string $condition, string $type=''): self
{
@ -438,7 +380,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
* Group the results by the selected field(s)
*
* @param mixed $field
* @return self
*/
public function groupBy($field): self
{
@ -462,10 +403,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Order the results by the selected field(s)
*
* @param string $field
* @param string $type
* @return self
*/
public function orderBy(string $field, string $type=''): self
{
@ -490,9 +427,9 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
}
// Set the final string
$orderString = ! isset($rand)
? "\nORDER BY ".implode(', ', $orderClauses)
: "\nORDER BY".$rand;
$orderString = isset($rand)
? "\nORDER BY".$rand
: "\nORDER BY ".implode(', ', $orderClauses);
$this->state->setOrderString($orderString);
@ -501,10 +438,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Set a limit on the current sql statement
*
* @param int $limit
* @param int|null $offset
* @return self
*/
public function limit(int $limit, ?int $offset=NULL): self
{
@ -517,11 +450,8 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
// --------------------------------------------------------------------------
// ! Query Grouping Methods
// --------------------------------------------------------------------------
/**
* Adds a paren to the current query for query grouping
*
* @return self
*/
public function groupStart(): self
{
@ -535,8 +465,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'NOT'
*
* @return self
*/
public function notGroupStart(): self
{
@ -550,8 +478,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'OR'
*
* @return self
*/
public function orGroupStart(): self
{
@ -563,8 +489,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'OR NOT'
*
* @return self
*/
public function orNotGroupStart(): self
{
@ -575,8 +499,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Ends a query group
*
* @return self
*/
public function groupEnd(): self
{
@ -588,15 +510,9 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
// --------------------------------------------------------------------------
// ! Query execution methods
// --------------------------------------------------------------------------
/**
* Select and retrieve all records from the current table, and/or
* execute current compiled query
*
* @param string $table
* @param int|null $limit
* @param int|null $offset
* @return PDOStatement
*/
public function get(string $table='', ?int $limit=NULL, ?int $offset=NULL): PDOStatement
{
@ -618,11 +534,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Convenience method for get() with a where clause
*
* @param string $table
* @param mixed $where
* @param int|null $limit
* @param int|null $offset
* @return PDOStatement
*/
public function getWhere(string $table, $where=[], ?int $limit=NULL, ?int $offset=NULL): PDOStatement
{
@ -635,24 +547,17 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Retrieve the number of rows in the selected table
*
* @param string $table
* @return int
*/
public function countAll(string $table): int
{
$sql = 'SELECT * FROM '.$this->driver->quoteTable($table);
$res = $this->driver->query($sql);
return (int) count($res->fetchAll());
return (int) (is_countable($res->fetchAll()) ? count($res->fetchAll()) : 0);
}
/**
* Retrieve the number of results for the generated query - used
* in place of the get() method
*
* @param string $table
* @param boolean $reset
* @return int
*/
public function countAllResults(string $table='', bool $reset = TRUE): int
{
@ -671,9 +576,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Creates an insert clause, and executes it
*
* @param string $table
* @param mixed $data
* @return PDOStatement
*/
public function insert(string $table, $data=[]): PDOStatement
{
@ -688,7 +591,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Creates and executes a batch insertion query
*
* @param string $table
* @param array $data
* @return PDOStatement
*/
@ -705,9 +607,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Creates an update clause, and executes it
*
* @param string $table
* @param mixed $data
* @return PDOStatement
*/
public function update(string $table, $data=[]): PDOStatement
{
@ -722,11 +622,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Creates a batch update, and executes it.
* Returns the number of affected rows
*
* @param string $table
* @param array $data
* @param string $where
* @return int|null
*/
public function updateBatch(string $table, array $data, string $where): ?int
{
@ -745,9 +640,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Deletes data from a table
*
* @param string $table
* @param mixed $where
* @return PDOStatement
*/
public function delete(string $table, $where=''): PDOStatement
{
@ -763,13 +656,8 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
// --------------------------------------------------------------------------
// ! SQL Returning Methods
// --------------------------------------------------------------------------
/**
* Returns the generated 'select' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
public function getCompiledSelect(string $table='', bool $reset=TRUE): string
{
@ -784,10 +672,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Returns the generated 'insert' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
public function getCompiledInsert(string $table, bool $reset=TRUE): string
{
@ -796,10 +680,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Returns the generated 'update' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
public function getCompiledUpdate(string $table='', bool $reset=TRUE): string
{
@ -808,10 +688,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Returns the generated 'delete' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
public function getCompiledDelete(string $table='', bool $reset=TRUE): string
{

View File

@ -63,13 +63,11 @@ class QueryBuilderBase {
/**
* Convenience property for connection management
* @var string
*/
public string $connName = '';
/**
* List of queries executed
* @var array
*/
public array $queries = [
'total_time' => 0
@ -77,50 +75,27 @@ class QueryBuilderBase {
/**
* Whether to do only an explain on the query
* @var bool
*/
protected bool $explain = FALSE;
/**
* Whether to return data from a modification query
* @var bool
*/
protected bool $returning = FALSE;
/**
* The current database driver
* @var DriverInterface
*/
protected ?DriverInterface $driver;
/**
* Query parser class instance
* @var QueryParser
*/
protected QueryParser $parser;
/**
* Query Builder state
* @var State
*/
protected State $state;
// --------------------------------------------------------------------------
// ! Methods
// --------------------------------------------------------------------------
/**
* Constructor
*
* @param DriverInterface $driver
* @param QueryParser $parser
*/
public function __construct(DriverInterface $driver, QueryParser $parser)
public function __construct(protected ?\Query\Drivers\DriverInterface $driver, protected QueryParser $parser)
{
// Inject driver and parser
$this->driver = $driver;
$this->parser = $parser;
// Create new State object
$this->state = new State();
}
@ -138,8 +113,6 @@ class QueryBuilderBase {
* Calls a function further down the inheritance chain.
* 'Implements' methods on the driver object
*
* @param string $name
* @param array $params
* @return mixed
* @throws BadMethodCallException
*/
@ -155,8 +128,6 @@ class QueryBuilderBase {
/**
* Clear out the class variables, so the next query can be run
*
* @return void
*/
public function resetQuery(): void
{
@ -168,9 +139,7 @@ class QueryBuilderBase {
/**
* Method to simplify select_ methods
*
* @param string $field
* @param string|bool $as
* @return string
*/
protected function _select(string $field, $as = FALSE): string
{
@ -190,11 +159,6 @@ class QueryBuilderBase {
/**
* Helper function for returning sql strings
*
* @param string $type
* @param string $table
* @param bool $reset
* @return string
*/
protected function _getCompile(string $type, string $table, bool $reset): string
{
@ -212,12 +176,7 @@ class QueryBuilderBase {
/**
* Simplify 'like' methods
*
* @param string $field
* @param mixed $val
* @param string $pos
* @param string $like
* @param string $conj
* @return self
*/
protected function _like(string $field, $val, string $pos, string $like = 'LIKE', string $conj = 'AND'): self
{
@ -253,8 +212,6 @@ class QueryBuilderBase {
*
* @param mixed $key
* @param mixed $values
* @param string $conj
* @return self
*/
protected function _having($key, $values = [], string $conj = 'AND'): self
{
@ -289,7 +246,6 @@ class QueryBuilderBase {
*
* @param mixed $key
* @param mixed $val
* @return array
*/
protected function _where($key, $val = []): array
{
@ -318,8 +274,6 @@ class QueryBuilderBase {
*
* @param mixed $key
* @param mixed $values
* @param string $defaultConj
* @return self
*/
protected function _whereString($key, $values = [], string $defaultConj = 'AND'): self
{
@ -364,12 +318,11 @@ class QueryBuilderBase {
* @param mixed $val
* @param string $in - The (not) in fragment
* @param string $conj - The where in conjunction
* @return self
*/
protected function _whereIn($key, $val = [], string $in = 'IN', string $conj = 'AND'): self
{
$key = $this->driver->quoteIdent($key);
$params = array_fill(0, count($val), '?');
$params = array_fill(0, is_countable($val) ? count($val) : 0, '?');
$this->state->appendWhereValues($val);
$conjunction = empty($this->state->getQueryMap()) ? ' WHERE ' : " {$conj} ";
@ -383,12 +336,7 @@ class QueryBuilderBase {
/**
* Executes the compiled query
*
* @param string $type
* @param string $table
* @param string $sql
* @param array|null $vals
* @param boolean $reset
* @return PDOStatement
*/
protected function _run(string $type, string $table, string $sql = NULL, array $vals = NULL, bool $reset = TRUE): PDOStatement
{
@ -425,11 +373,6 @@ class QueryBuilderBase {
/**
* Convert the prepared statement into readable sql
*
* @param array $values
* @param string $sql
* @param int $totalTime
* @return void
*/
protected function _appendQuery(array $values, string $sql, int $totalTime): void
{
@ -439,9 +382,9 @@ class QueryBuilderBase {
// Quote string values
foreach ($evals as &$v)
{
$v = ( ! is_numeric($v))
? htmlentities($this->driver->quote($v), ENT_NOQUOTES, 'utf-8')
: $v;
$v = ( is_numeric($v))
? $v
: htmlentities($this->driver->quote($v), ENT_NOQUOTES, 'utf-8');
}
unset($v);
@ -465,9 +408,6 @@ class QueryBuilderBase {
* Sub-method for generating sql strings
*
* @codeCoverageIgnore
* @param string $type
* @param string $table
* @return string
*/
protected function _compileType(string $type = QueryType::SELECT, string $table = ''): string
{
@ -475,7 +415,7 @@ class QueryBuilderBase {
switch ($type)
{
case QueryType::INSERT:
$paramCount = count($setArrayKeys);
$paramCount = is_countable($setArrayKeys) ? count($setArrayKeys) : 0;
$params = array_fill(0, $paramCount, '?');
$sql = "INSERT INTO {$table} ("
. implode(',', $setArrayKeys)
@ -512,10 +452,6 @@ class QueryBuilderBase {
/**
* String together the sql statements for sending to the db
*
* @param string $type
* @param string $table
* @return string
*/
protected function _compile(string $type = '', string $table = ''): string
{
@ -568,10 +504,6 @@ class QueryBuilderBase {
/**
* Generate returning clause of query
*
* @param string $sql
* @param string $type
* @return string
*/
protected function _compileReturning(string $sql, string $type): string
{
@ -597,8 +529,6 @@ class QueryBuilderBase {
switch ($type)
{
case QueryType::INSERT:
// @TODO figure out a good response for insert query
break;
case QueryType::UPDATE:
// @TODO figure out a good response for update query
@ -606,8 +536,6 @@ class QueryBuilderBase {
case QueryType::INSERT_BATCH:
case QueryType::UPDATE_BATCH:
// @TODO figure out a good response for batch queries
break;
default:
// On Delete queries, what would we return?

View File

@ -62,62 +62,46 @@ interface QueryBuilderInterface {
// --------------------------------------------------------------------------
// ! Select Queries
// --------------------------------------------------------------------------
/**
* Specifies rows to select in a query
*
* @param string $fields
* @return self
*/
public function select(string $fields): self;
/**
* Selects the maximum value of a field from a query
*
* @param string $field
* @param string|bool $as
* @return self
*/
public function selectMax(string $field, $as=FALSE): self;
/**
* Selects the minimum value of a field from a query
*
* @param string $field
* @param string|bool $as
* @return self
*/
public function selectMin(string $field, $as=FALSE): self;
/**
* Selects the average value of a field from a query
*
* @param string $field
* @param string|bool $as
* @return self
*/
public function selectAvg(string $field, $as=FALSE): self;
/**
* Selects the sum of a field from a query
*
* @param string $field
* @param string|bool $as
* @return self
*/
public function selectSum(string $field, $as=FALSE): self;
/**
* Adds the 'distinct' keyword to a query
*
* @return self
*/
public function distinct(): self;
/**
* Shows the query plan for the query
*
* @return self
*/
public function explain(): self;
@ -127,72 +111,53 @@ interface QueryBuilderInterface {
* Alias of `from` method to better match CodeIgniter 4
*
* @param string $tableName
* @return self
*/
public function table(string $tableName): self;
/**
* Specify the database table to select from
*
* @param string $tableName
* @return self
*/
public function from(string $tableName): self;
// --------------------------------------------------------------------------
// ! 'Like' methods
// --------------------------------------------------------------------------
/**
* Creates a Like clause in the sql statement
*
* @param string $field
* @param mixed $values
* @param string $pos
* @return self
*/
public function like(string $field, $values, string $pos=LikeType::BOTH): self;
/**
* Generates an OR Like clause
*
* @param string $field
* @param mixed $values
* @param string $pos
* @return self
*/
public function orLike(string $field, $values, string $pos=LikeType::BOTH): self;
/**
* Generates a NOT LIKE clause
*
* @param string $field
* @param mixed $values
* @param string $pos
* @return self
*/
public function notLike(string $field, $values, string $pos=LikeType::BOTH): self;
/**
* Generates a OR NOT LIKE clause
*
* @param string $field
* @param mixed $values
* @param string $pos
* @return self
*/
public function orNotLike(string $field, $values, string $pos=LikeType::BOTH): self;
// --------------------------------------------------------------------------
// ! Having methods
// --------------------------------------------------------------------------
/**
* Generates a 'Having' clause
*
* @param mixed $key
* @param mixed $values
* @return self
*/
public function having($key, $values=[]): self;
@ -201,14 +166,12 @@ interface QueryBuilderInterface {
*
* @param mixed $key
* @param mixed $values
* @return self
*/
public function orHaving($key, $values=[]): self;
// --------------------------------------------------------------------------
// ! 'Where' methods
// --------------------------------------------------------------------------
/**
* Specify condition(s) in the where clause of a query
* Note: this function works with key / value, or a
@ -217,7 +180,6 @@ interface QueryBuilderInterface {
* @param mixed $key
* @param mixed $values
* @param bool $escape
* @return self
*/
public function where($key, $values=[], $escape = NULL): self;
@ -226,7 +188,6 @@ interface QueryBuilderInterface {
*
* @param string $key
* @param mixed $values
* @return self
*/
public function orWhere($key, $values=[]): self;
@ -235,7 +196,6 @@ interface QueryBuilderInterface {
*
* @param mixed $field
* @param mixed $values
* @return self
*/
public function whereIn($field, $values=[]): self;
@ -244,7 +204,6 @@ interface QueryBuilderInterface {
*
* @param string $field
* @param mixed $values
* @return self
*/
public function orWhereIn($field, $values=[]): self;
@ -253,7 +212,6 @@ interface QueryBuilderInterface {
*
* @param string $field
* @param mixed $values
* @return self
*/
public function whereNotIn($field, $values=[]): self;
@ -262,30 +220,22 @@ interface QueryBuilderInterface {
*
* @param string $field
* @param mixed $values
* @return self
*/
public function orWhereNotIn($field, $values=[]): self;
// --------------------------------------------------------------------------
// ! Other Query Modifier methods
// --------------------------------------------------------------------------
/**
* Sets values for inserts / updates / deletes
*
* @param mixed $key
* @param mixed $values
* @return self
*/
public function set($key, $values = NULL): self;
/**
* Creates a join phrase in a compiled query
*
* @param string $table
* @param string $condition
* @param string $type
* @return self
*/
public function join(string $table, string $condition, string $type=JoinType::INNER): self;
@ -293,101 +243,68 @@ interface QueryBuilderInterface {
* Group the results by the selected field(s)
*
* @param mixed $field
* @return self
*/
public function groupBy($field): self;
/**
* Order the results by the selected field(s)
*
* @param string $field
* @param string $type
* @return self
*/
public function orderBy(string $field, string $type=''): self;
/**
* Set a limit on the current sql statement
*
* @param int $limit
* @param int|null $offset
* @return self
*/
public function limit(int $limit, ?int $offset=NULL): self;
// --------------------------------------------------------------------------
// ! Query Grouping Methods
// --------------------------------------------------------------------------
/**
* Adds a paren to the current query for query grouping
*
* @return self
*/
public function groupStart(): self;
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'NOT'
*
* @return self
*/
public function notGroupStart(): self;
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'OR'
*
* @return self
*/
public function orGroupStart(): self;
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'OR NOT'
*
* @return self
*/
public function orNotGroupStart(): self;
/**
* Ends a query group
*
* @return self
*/
public function groupEnd(): self;
// --------------------------------------------------------------------------
// ! Query execution methods
// --------------------------------------------------------------------------
/**
* Select and retrieve all records from the current table, and/or
* execute current compiled query
*
* @param string $table
* @param int|null $limit
* @param int|null $offset
* @return PDOStatement
*/
public function get(string $table='', ?int $limit=NULL, ?int $offset=NULL): PDOStatement;
/**
* Convenience method for get() with a where clause
*
* @param string $table
* @param array $where
* @param int|null $limit
* @param int|null $offset
* @return PDOStatement
*/
public function getWhere(string $table, $where=[], ?int $limit=NULL, ?int $offset=NULL): PDOStatement;
/**
* Retrieve the number of rows in the selected table
*
* @param string $table
* @return int
*/
public function countAll(string $table): int;
@ -395,36 +312,28 @@ interface QueryBuilderInterface {
* Retrieve the number of results for the generated query - used
* in place of the get() method
*
* @param string $table
* @param bool $reset - Whether to keep the query after counting the results
* @return int
*/
public function countAllResults(string $table='', bool $reset=TRUE): int;
/**
* Creates an insert clause, and executes it
*
* @param string $table
* @param mixed $data
* @return PDOStatement
*/
public function insert(string $table, $data=[]): PDOStatement;
/**
* Creates and executes a batch insertion query
*
* @param string $table
* @param array $data
* @return PDOStatement | null
*/
public function insertBatch(string $table, $data=[]): ?PDOStatement;
/**
* Creates an update clause, and executes it
*
* @param string $table
* @param mixed $data
* @return PDOStatement
*/
public function update(string $table, $data=[]): PDOStatement;
@ -435,67 +344,44 @@ interface QueryBuilderInterface {
* @param string $table The table to update
* @param array $data an array of update values
* @param string $where The where key
* @return int|null
*/
public function updateBatch(string $table, array $data, string $where): ?int;
/**
* Deletes data from a table
*
* @param string $table
* @param mixed $where
* @return PDOStatement
*/
public function delete(string $table, $where=''): PDOStatement;
// --------------------------------------------------------------------------
// ! SQL Returning Methods
// --------------------------------------------------------------------------
/**
* Returns the generated 'select' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
public function getCompiledSelect(string $table='', bool $reset=TRUE): string;
/**
* Returns the generated 'insert' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
public function getCompiledInsert(string $table, bool $reset=TRUE): string;
/**
* Returns the generated 'update' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
public function getCompiledUpdate(string $table='', bool $reset=TRUE): string;
/**
* Returns the generated 'delete' sql query
*
* @param string $table
* @param bool $reset
* @return string
*/
public function getCompiledDelete(string $table='', bool $reset=TRUE): string;
// --------------------------------------------------------------------------
// ! Miscellaneous Methods
// --------------------------------------------------------------------------
/**
* Clear out the class variables, so the next query can be run
*
* @return void
*/
public function resetQuery(): void;
}

View File

@ -22,17 +22,8 @@ use Query\Drivers\DriverInterface;
*/
class QueryParser {
/**
* DB Driver
*
* @var DriverInterface
*/
private DriverInterface $db;
/**
* Regex patterns for various syntax components
*
* @var array
*/
private array $matchPatterns = [
'function' => '([a-zA-Z0-9_]+\((.*?)\))',
@ -42,8 +33,6 @@ class QueryParser {
/**
* Regex matches
*
* @var array
*/
public array $matches = [
'functions' => [],
@ -54,19 +43,15 @@ class QueryParser {
/**
* Constructor/entry point into parser
*
* @param DriverInterface $db
*/
public function __construct(DriverInterface $db)
public function __construct(private DriverInterface $db)
{
$this->db = $db;
}
/**
* Parser method for setting the parse string
*
* @param string $sql
* @return array
* @return mixed[][]
*/
public function parseJoin(string $sql): array
{
@ -87,14 +72,11 @@ class QueryParser {
/**
* Compiles a join condition after parsing
*
* @param string $condition
* @return string
*/
public function compileJoin(string $condition): string
{
$parts = $this->parseJoin($condition);
$count = count($parts['identifiers']);
$count = is_countable($parts['identifiers']) ? count($parts['identifiers']) : 0;
// Go through and quote the identifiers
for($i=0; $i <= $count; $i++)
@ -111,8 +93,7 @@ class QueryParser {
/**
* Returns a more useful match array
*
* @param array $array
* @return array
* @return mixed[]
*/
protected function filterArray(array $array): array
{

View File

@ -19,10 +19,10 @@ namespace Query;
* 'Enum' of query types
*/
class QueryType {
public const SELECT = 'select';
public const INSERT = 'insert';
public const INSERT_BATCH = 'insert_batch';
public const UPDATE = 'update';
public const UPDATE_BATCH = 'update_batch';
public const DELETE = 'delete';
public final const SELECT = 'select';
public final const INSERT = 'insert';
public final const INSERT_BATCH = 'insert_batch';
public final const UPDATE = 'update';
public final const UPDATE_BATCH = 'update_batch';
public final const DELETE = 'delete';
}

View File

@ -49,84 +49,69 @@ class State {
// --------------------------------------------------------------------------
// ! SQL Clause Strings
// --------------------------------------------------------------------------
/**
* Compiled 'select' clause
* @var string
*/
protected string $selectString = '';
/**
* Compiled 'from' clause
* @var string
*/
protected string $fromString = '';
/**
* Compiled arguments for insert / update
* @var string
*/
protected string $setString = '';
/**
* Order by clause
* @var string
*/
protected string $orderString = '';
/**
* Group by clause
* @var string
*/
protected string $groupString = '';
// --------------------------------------------------------------------------
// ! SQL Clause Arrays
// --------------------------------------------------------------------------
/**
* Keys for insert/update statement
* @var array
*/
protected array $setArrayKeys = [];
/**
* Key/val pairs for order by clause
* @var array
*/
protected array $orderArray = [];
/**
* Key/val pairs for group by clause
* @var array
*/
protected array $groupArray = [];
// --------------------------------------------------------------------------
// ! Other Class vars
// --------------------------------------------------------------------------
/**
* Values to apply to prepared statements
* @var array
*/
protected array $values = [];
/**
* Values to apply to where clauses in prepared statements
* @var array
*/
protected array $whereValues = [];
/**
* Value for limit string
* @var int
*/
protected ?int $limit = NULL;
/**
* Value for offset in limit string
* @var int
*/
protected ?int $offset = NULL;
@ -140,20 +125,17 @@ class State {
* 'conjunction' => ' AND ',
* 'string' => 'k=?'
* ]
*
* @var array
*/
protected array $queryMap = [];
/**
* Map for having clause
* @var array
*/
protected array $havingMap = [];
public function __call(string $name, array $arguments)
{
if (strpos($name, 'get', 0) === 0)
if (str_starts_with($name, 'get'))
{
$maybeProp = lcfirst(substr($name, 3));
if (isset($this->$maybeProp))
@ -162,7 +144,7 @@ class State {
}
}
if (strpos($name, 'set', 0) === 0)
if (str_starts_with($name, 'set'))
{
$maybeProp = lcfirst(substr($name, 3));
if (isset($this->$maybeProp))
@ -175,20 +157,12 @@ class State {
return NULL;
}
/**
* @param string $str
* @return State
*/
public function appendSelectString(string $str): self
{
$this->selectString .= $str;
return $this;
}
/**
* @param array $setArrayKeys
* @return State
*/
public function appendSetArrayKeys(array $setArrayKeys): self
{
$this->setArrayKeys = array_merge($this->setArrayKeys, $setArrayKeys);
@ -196,9 +170,7 @@ class State {
}
/**
* @param string $key
* @param mixed $orderArray
* @return State
*/
public function setOrderArray(string $key, $orderArray): self
{
@ -206,20 +178,12 @@ class State {
return $this;
}
/**
* @param string $groupArray
* @return State
*/
public function appendGroupArray(string $groupArray): self
{
$this->groupArray[] = $groupArray;
return $this;
}
/**
* @param array $values
* @return State
*/
public function appendValues(array $values): self
{
$this->values = array_merge($this->values, $values);
@ -228,7 +192,6 @@ class State {
/**
* @param mixed $val
* @return State
*/
public function appendWhereValues($val): self
{
@ -248,11 +211,6 @@ class State {
/**
* Add an additional set of mapping pairs to a internal map
*
* @param string $conjunction
* @param string $string
* @param string $type
* @return State
*/
public function appendMap(string $conjunction = '', string $string = '', string $type = ''): self
{
@ -264,10 +222,6 @@ class State {
return $this;
}
/**
* @param array $item
* @return State
*/
public function appendHavingMap(array $item): self
{
$this->havingMap[] = $item;

View File

@ -21,12 +21,8 @@ namespace {
/**
* Global functions that don't really fit anywhere else
*/
/**
* Multibyte-safe trim function
*
* @param string $string
* @return string
*/
function mb_trim(string $string): string
{
@ -36,9 +32,7 @@ namespace {
/**
* Filter out db rows into one array
*
* @param array $array
* @param mixed $index
* @return array
*/
function dbFilter(array $array, $index): array
{
@ -57,9 +51,6 @@ namespace {
*
* The $zipperInput array is an array of arrays indexed by their place in the output
* array.
*
* @param array $zipperInput
* @return array
*/
function arrayZipper(array $zipperInput): array
{
@ -83,10 +74,6 @@ namespace {
/**
* Determine whether a value in the passed array matches the pattern
* passed
*
* @param array $array
* @param string $pattern
* @return bool
*/
function regexInArray(array $array, string $pattern): bool
{
@ -97,7 +84,7 @@ namespace {
foreach ($array as $item)
{
if (is_scalar($item) && preg_match($pattern, $item))
if (is_scalar($item) && preg_match($pattern, (string) $item))
{
return TRUE;
}
@ -113,11 +100,9 @@ namespace {
* the array or object has an 'alias' parameter, passing that string to this
* function will return that connection. Passing no parameters returns the last
* connection created.
*
* @param string|object|array $params
* @return QueryBuilderInterface|null
*/
function Query($params = ''): ?QueryBuilderInterface
function Query(string|object|array|null $params = ''): ?QueryBuilderInterface
{
if ($params === NULL)
{
@ -127,7 +112,7 @@ namespace {
$manager = ConnectionManager::getInstance();
// If you are getting a previously created connection
if (is_scalar($params))
if (is_string($params))
{
return $manager->getConnection($params);
}