Minor code cleanup

This commit is contained in:
Timothy Warren 2020-04-23 18:16:32 -04:00
parent 519b39e75e
commit 4201a7c42c
7 changed files with 107 additions and 27 deletions

View File

@ -15,11 +15,13 @@
*/
namespace Query\Drivers;
use function dbFilter;
use InvalidArgumentException;
use PDO;
use PDOStatement;
use function call_user_func_array;
use function dbFilter;
use function is_object;
use function is_string;
/**
@ -127,11 +129,11 @@ abstract class AbstractDriver
{
if (
isset($this->$name)
&& \is_object($this->$name)
&& is_object($this->$name)
&& method_exists($this->$name, '__invoke')
)
{
return \call_user_func_array([$this->$name, '__invoke'], $args);
return call_user_func_array([$this->$name, '__invoke'], $args);
}
return NULL;
@ -287,7 +289,7 @@ abstract class AbstractDriver
* @param string $table
* @return string
*/
public function quoteTable($table): string
public function quoteTable(string $table): string
{
$table = $this->prefixTable($table);
@ -333,7 +335,7 @@ abstract class AbstractDriver
{
// Unquote the function
// Quote the inside identifiers
$raw = str_replace(array($f[0], $f[3]), array($f[1], $this->quoteIdent($f[3])), $raw);
$raw = str_replace([$f[0], $f[3]], [$f[1], $this->quoteIdent($f[3])], $raw);
}
return $raw;

View File

@ -165,10 +165,10 @@ interface DriverInterface /* extends the interface of PDO */ {
/**
* Quote database table name, and set prefix
*
* @param string|array $table
* @return string|array
* @param string $table
* @return string
*/
public function quoteTable($table);
public function quoteTable(string $table): string;
/**
* Create and execute a prepared statement with the provided parameters

27
src/JoinType.php Normal file
View File

@ -0,0 +1,27 @@
<?php declare(strict_types=1);
/**
* Query
*
* SQL Query Builder / Database Abstraction Layer
*
* PHP version 7.4
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2012 - 2020 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat/Query
* @version 3.0.0
*/
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';
}

25
src/LikeType.php Normal file
View File

@ -0,0 +1,25 @@
<?php declare(strict_types=1);
/**
* Query
*
* SQL Query Builder / Database Abstraction Layer
*
* PHP version 7.4
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2012 - 2020 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat/Query
* @version 3.0.0
*/
namespace Query;
/**
* 'Enum' of join types
*/
class LikeType {
public const BEFORE = 'before';
public const AFTER = 'after';
public const BOTH = 'both';
}

View File

@ -17,6 +17,7 @@ namespace Query;
use function is_array;
use function is_int;
use function mb_trim;
use PDOStatement;
@ -170,14 +171,27 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
/**
* Specify the database table to select from
*
* @param string $tblname
* Alias of `from` method to better match CodeIgniter 4
*
* @param string $tableName
* @return self
*/
public function from(string $tblname): self
public function table(string $tableName): self
{
return $this->from($tableName);
}
/**
* Specify the database table to select from
*
* @param string $tableName
* @return self
*/
public function from(string $tableName): self
{
// Split identifiers on spaces
$identArray = explode(' ', \mb_trim($tblname));
$identArray = array_map('\\mb_trim', $identArray);
$identArray = explode(' ', mb_trim($tableName));
$identArray = array_map('mb_trim', $identArray);
// Quote the identifiers
$identArray[0] = $this->driver->quoteTable($identArray[0]);
@ -201,7 +215,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
* @param string $pos
* @return self
*/
public function like(string $field, $val, string $pos='both'): self
public function like(string $field, $val, string $pos=LikeType::BOTH): self
{
return $this->_like($field, $val, $pos);
}
@ -214,7 +228,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
* @param string $pos
* @return self
*/
public function orLike(string $field, $val, string $pos='both'): self
public function orLike(string $field, $val, string $pos=LikeType::BOTH): self
{
return $this->_like($field, $val, $pos, 'LIKE', 'OR');
}
@ -227,7 +241,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
* @param string $pos
* @return self
*/
public function notLike(string $field, $val, string $pos='both'): self
public function notLike(string $field, $val, string $pos=LikeType::BOTH): self
{
return $this->_like($field, $val, $pos, 'NOT LIKE');
}
@ -240,7 +254,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface {
* @param string $pos
* @return self
*/
public function orNotLike(string $field, $val, string $pos='both'): self
public function orNotLike(string $field, $val, string $pos=LikeType::BOTH): self
{
return $this->_like($field, $val, $pos, 'NOT LIKE', 'OR');
}

View File

@ -226,13 +226,15 @@ class QueryBuilderBase {
// Add the like string into the order map
$like = $field . " {$like} ?";
if ($pos === 'before')
if ($pos === LikeType::BEFORE)
{
$val = "%{$val}";
} elseif ($pos === 'after')
}
elseif ($pos === LikeType::AFTER)
{
$val = "{$val}%";
} else
}
else
{
$val = "%{$val}%";
}

View File

@ -124,10 +124,20 @@ interface QueryBuilderInterface {
/**
* Specify the database table to select from
*
* @param string $tblname
* Alias of `from` method to better match CodeIgniter 4
*
* @param string $tableName
* @return self
*/
public function from(string $tblname): 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
@ -141,7 +151,7 @@ interface QueryBuilderInterface {
* @param string $pos
* @return self
*/
public function like(string $field, $values, string $pos='both'): self;
public function like(string $field, $values, string $pos=LikeType::BOTH): self;
/**
* Generates an OR Like clause
@ -151,7 +161,7 @@ interface QueryBuilderInterface {
* @param string $pos
* @return self
*/
public function orLike(string $field, $values, string $pos='both'): self;
public function orLike(string $field, $values, string $pos=LikeType::BOTH): self;
/**
* Generates a NOT LIKE clause
@ -161,7 +171,7 @@ interface QueryBuilderInterface {
* @param string $pos
* @return self
*/
public function notLike(string $field, $values, string $pos='both'): self;
public function notLike(string $field, $values, string $pos=LikeType::BOTH): self;
/**
* Generates a OR NOT LIKE clause
@ -171,7 +181,7 @@ interface QueryBuilderInterface {
* @param string $pos
* @return self
*/
public function orNotLike(string $field, $values, string $pos='both'): self;
public function orNotLike(string $field, $values, string $pos=LikeType::BOTH): self;
// --------------------------------------------------------------------------
// ! Having methods
@ -277,7 +287,7 @@ interface QueryBuilderInterface {
* @param string $type
* @return self
*/
public function join(string $table, string $condition, string $type=''): self;
public function join(string $table, string $condition, string $type=JoinType::INNER): self;
/**
* Group the results by the selected field(s)