Do you wish to register an account?
Browse Source

Minor code cleanup

tags/v3.0.0^2
Timothy Warren 6 months ago
parent
commit
4201a7c42c
7 changed files with 107 additions and 27 deletions
  1. +8
    -6
      src/Drivers/AbstractDriver.php
  2. +3
    -3
      src/Drivers/DriverInterface.php
  3. +27
    -0
      src/JoinType.php
  4. +25
    -0
      src/LikeType.php
  5. +22
    -8
      src/QueryBuilder.php
  6. +5
    -3
      src/QueryBuilderBase.php
  7. +17
    -7
      src/QueryBuilderInterface.php

+ 8
- 6
src/Drivers/AbstractDriver.php 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;


+ 3
- 3
src/Drivers/DriverInterface.php 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
- 0
src/JoinType.php 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
- 0
src/LikeType.php 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';
}

+ 22
- 8
src/QueryBuilder.php 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 table(string $tableName): self
{
return $this->from($tableName);
}

/**
* Specify the database table to select from
*
* @param string $tableName
* @return self
*/
public function from(string $tblname): 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');
}


+ 5
- 3
src/QueryBuilderBase.php 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}%";
}


+ 17
- 7
src/QueryBuilderInterface.php 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 table(string $tableName): self;

/**
* Specify the database table to select from
*
* @param string $tableName
* @return self
*/
public function from(string $tblname): 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)


Loading…
Cancel
Save