eee
This commit is contained in:
+1
-4
@@ -10,18 +10,15 @@ declare(strict_types=1);
|
|||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
use Database\Traits\QueryTrait;
|
use Database\Traits\QueryTrait;
|
||||||
use Exception;
|
|
||||||
use JetBrains\PhpStorm\ArrayShape;
|
use JetBrains\PhpStorm\ArrayShape;
|
||||||
use Kiri\Abstracts\Component;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ActiveQuery
|
* Class ActiveQuery
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
class ActiveQuery extends Component implements ISqlBuilder
|
class ActiveQuery extends QueryTrait implements ISqlBuilder
|
||||||
{
|
{
|
||||||
|
|
||||||
use QueryTrait;
|
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
public bool $asArray = FALSE;
|
public bool $asArray = FALSE;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace Database\Base;
|
namespace Database\Base;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
|
use Database\Traits\QueryTrait;
|
||||||
|
|
||||||
interface ActiveQueryInterface
|
interface ActiveQueryInterface
|
||||||
{
|
{
|
||||||
@@ -11,261 +12,257 @@ interface ActiveQueryInterface
|
|||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param callable $callable
|
* @param callable $callable
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function when(string $column, callable $callable): static;
|
public function when(string $column, callable $callable): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $whereRaw
|
* @param string $whereRaw
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereRaw(string $whereRaw): static;
|
public function whereRaw(string $whereRaw): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereLocate(string $column, string $value): static;
|
public function whereLocate(string $column, string $value): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereNull(string $column): static;
|
public function whereNull(string $column): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereEmpty(string $column): static;
|
public function whereEmpty(string $column): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereNotEmpty(string $column): static;
|
public function whereNotEmpty(string $column): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereNotNull(string $column): static;
|
public function whereNotNull(string $column): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return QueryTrait select * from tableName as t1
|
||||||
*
|
*
|
||||||
* select * from tableName as t1
|
* select * from tableName as t1
|
||||||
*/
|
*/
|
||||||
public function alias(string $alias = 't1'): static;
|
public function alias(string $alias = 't1'): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|Closure $tableName
|
* @param string|Closure $tableName
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function from(string|Closure $tableName): static;
|
public function from(string|Closure $tableName): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $tableName
|
* @param string $tableName
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @param $onCondition
|
* @param string|array $onCondition
|
||||||
* @param null $param
|
* @param array $param
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
* @throws
|
|
||||||
*/
|
*/
|
||||||
public function leftJoin(string $tableName, string $alias, $onCondition, $param = NULL): static;
|
public function leftJoin(string $tableName, string $alias, string|array $onCondition, array $param = []): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $tableName
|
* @param string $tableName
|
||||||
* @param $alias
|
* @param string $alias
|
||||||
* @param $onCondition
|
* @param string|array $onCondition
|
||||||
* @param null $param
|
* @param array $param
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
* @throws
|
|
||||||
*/
|
*/
|
||||||
public function rightJoin($tableName, $alias, $onCondition, $param = NULL): static;
|
public function rightJoin(string $tableName, string $alias, string|array $onCondition, array $param = []): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $tableName
|
* @param string $tableName
|
||||||
* @param $alias
|
* @param string $alias
|
||||||
* @param $onCondition
|
* @param string|array $onCondition
|
||||||
* @param null $param
|
* @param array $param
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
* @throws
|
|
||||||
*/
|
*/
|
||||||
public function innerJoin($tableName, $alias, $onCondition, $param = NULL): static;
|
public function innerJoin(string $tableName, string $alias, string|array $onCondition, array $param = []): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function sum(string $field): static;
|
public function sum(string $field): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function max(string $field): static;
|
public function max(string $field): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $lngField
|
* @param string $lngField
|
||||||
* @param string $latField
|
* @param string $latField
|
||||||
* @param int $lng1
|
* @param int|float $lng1
|
||||||
* @param int $lat1
|
* @param int|float $lat1
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function distance(string $lngField, string $latField, int $lng1, int $lat1): static;
|
public function distance(string $lngField, string $latField, int|float $lng1, int|float $lat1): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|array $column
|
* @param string|array $column
|
||||||
* @param string $sort
|
* @param string $sort
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return QueryTrait [
|
||||||
*
|
*
|
||||||
* [
|
* [
|
||||||
* 'addTime',
|
* 'addTime',
|
||||||
* 'descTime desc'
|
* 'descTime desc'
|
||||||
* ]
|
* ]
|
||||||
*/
|
*/
|
||||||
public function orderBy(string|array $column, string $sort = 'DESC'): static;
|
public function orderBy(string|array $column, string $sort = 'DESC'): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|string $column
|
* @param array|string $column
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function select(array|string $column = '*'): static;
|
public function select(array|string $column = '*'): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function orderRand(): static;
|
public function orderRand(): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|Closure|string $conditionArray
|
* @param array|Closure|string $conditionArray
|
||||||
* @param string $opera
|
* @param string $opera
|
||||||
* @param null $value
|
* @param null $value
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereOr(array|Closure|string $conditionArray = [], string $opera = '=', $value = null): static;
|
public function whereOr(array|Closure|string $conditionArray = [], string $opera = '=', $value = null): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereLike(string $column, string $value): static;
|
public function whereLike(string $column, string $value): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereLeftLike(string $column, string $value): static;
|
public function whereLeftLike(string $column, string $value): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereRightLike(string $column, string $value): static;
|
public function whereRightLike(string $column, string $value): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereNotLike(string $column, string $value): static;
|
public function whereNotLike(string $column, string $value): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $columns
|
* @param string $columns
|
||||||
* @param array|Closure $value
|
* @param array|Closure $value
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
* @throws
|
|
||||||
*/
|
*/
|
||||||
public function whereIn(string $columns, array|Closure $value): static;
|
public function whereIn(string $columns, array|Closure $value): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $columns
|
* @param string $columns
|
||||||
* @param array $value
|
* @param array $value
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereNotIn(string $columns, array $value): static;
|
public function whereNotIn(string $columns, array $value): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param int $start
|
* @param int|float $start
|
||||||
* @param int $end
|
* @param int|float $end
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereBetween(string $column, int $start, int $end): static;
|
public function whereBetween(string $column, int|float $start, int|float $end): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param int $start
|
* @param int|float $start
|
||||||
* @param int $end
|
* @param int|float $end
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereNotBetween(string $column, int $start, int $end): static;
|
public function whereNotBetween(string $column, int|float $start, int|float $end): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $column
|
* @param array $column
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function where(array $column): static;
|
public function where(array $column): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param string $opera
|
* @param string $opera
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereMath(string $column, string $opera, mixed $value): static;
|
public function whereMath(string $column, string $opera, mixed $value): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Closure $closure
|
* @param Closure $closure
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function whereClosure(Closure $closure): static;
|
public function whereClosure(Closure $closure): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string|null $having
|
* @param string|null $having
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function groupBy(string $name, string $having = NULL): static;
|
public function groupBy(string $name, string $having = NULL): QueryTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $limit
|
* @param int $limit
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function limit(int $limit = 20): static;
|
public function limit(int $limit = 20): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $offset
|
* @param int $offset
|
||||||
* @return $this
|
* @return QueryTrait
|
||||||
*/
|
*/
|
||||||
public function offset(int $offset): static;
|
public function offset(int $offset): QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -21,10 +21,8 @@ use Throwable;
|
|||||||
* Class Db
|
* Class Db
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
class Db implements ISqlBuilder
|
class Db extends QueryTrait implements ISqlBuilder
|
||||||
{
|
{
|
||||||
use QueryTrait;
|
|
||||||
|
|
||||||
|
|
||||||
private static bool $_inTransaction = false;
|
private static bool $_inTransaction = false;
|
||||||
|
|
||||||
|
|||||||
@@ -136,9 +136,9 @@ class Model extends Base\Model
|
|||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param array $arguments
|
* @param array $arguments
|
||||||
* @return mixed
|
* @return Model|ActiveQuery
|
||||||
*/
|
*/
|
||||||
public static function __callStatic(string $name, array $arguments)
|
public static function __callStatic(string $name, array $arguments): static|ActiveQuery
|
||||||
{
|
{
|
||||||
// TODO: Change the autogenerated stub
|
// TODO: Change the autogenerated stub
|
||||||
if (method_exists(static::class, $name)) {
|
if (method_exists(static::class, $name)) {
|
||||||
|
|||||||
@@ -16,10 +16,14 @@ use Database\Traits\QueryTrait;
|
|||||||
* Class Query
|
* Class Query
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
class Query implements ISqlBuilder
|
class Query extends QueryTrait implements ISqlBuilder
|
||||||
{
|
{
|
||||||
|
|
||||||
use QueryTrait;
|
|
||||||
|
/**
|
||||||
|
* @var SqlBuilder
|
||||||
|
*/
|
||||||
|
protected SqlBuilder $builder;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,6 +32,8 @@ class Query implements ISqlBuilder
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->builder = SqlBuilder::builder($this);
|
$this->builder = SqlBuilder::builder($this);
|
||||||
|
|
||||||
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+23
-25
@@ -12,39 +12,37 @@ namespace Database\Traits;
|
|||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
|
use Database\Base\ActiveQueryInterface;
|
||||||
|
use Database\ISqlBuilder;
|
||||||
use Database\ModelInterface;
|
use Database\ModelInterface;
|
||||||
use Database\Query;
|
use Database\Query;
|
||||||
use Database\SqlBuilder;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
|
use Kiri\Abstracts\Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trait QueryTrait
|
* Trait QueryTrait
|
||||||
* @package Database\Traits
|
* @package Database\Traits
|
||||||
*/
|
*/
|
||||||
trait QueryTrait
|
class QueryTrait extends Component implements ActiveQueryInterface, ISqlBuilder
|
||||||
{
|
{
|
||||||
public array $where = [];
|
protected array $where = [];
|
||||||
public array $select = [];
|
protected array $select = [];
|
||||||
public array $join = [];
|
protected array $join = [];
|
||||||
public array $order = [];
|
protected array $order = [];
|
||||||
public int $offset = 0;
|
protected int $offset = 0;
|
||||||
public int $limit = 0;
|
protected int $limit = 0;
|
||||||
public string $group = '';
|
protected string $group = '';
|
||||||
public string $from = '';
|
protected string $from = '';
|
||||||
public string $alias = 't1';
|
protected string $alias = 't1';
|
||||||
public array $filter = [];
|
protected array $filter = [];
|
||||||
|
protected bool $lock = false;
|
||||||
|
|
||||||
|
|
||||||
public bool $lock = false;
|
|
||||||
|
|
||||||
|
|
||||||
private SqlBuilder $builder;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ModelInterface|string|null
|
* @var ModelInterface|string|null
|
||||||
*/
|
*/
|
||||||
public ModelInterface|string|null $modelClass;
|
protected ModelInterface|string|null $modelClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* clear
|
* clear
|
||||||
@@ -337,7 +335,7 @@ trait QueryTrait
|
|||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function distance(string $lngField, string $latField, int $lng1, int $lat1): static
|
public function distance(string $lngField, string $latField, int|float $lng1, int|float $lat1): static
|
||||||
{
|
{
|
||||||
$sql = "ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(($lat1 * PI() / 180 - $lat1 * PI() / 180) / 2),2) + COS($lat1 * PI() / 180) * COS($latField * PI() / 180) * POW(SIN(($lng1 * PI() / 180 - $lngField * PI() / 180) / 2),2))) * 1000) AS distance";
|
$sql = "ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(($lat1 * PI() / 180 - $lat1 * PI() / 180) / 2),2) + COS($lat1 * PI() / 180) * COS($latField * PI() / 180) * POW(SIN(($lng1 * PI() / 180 - $lngField * PI() / 180) / 2),2))) * 1000) AS distance";
|
||||||
$this->select[] = $sql;
|
$this->select[] = $sql;
|
||||||
@@ -552,11 +550,11 @@ trait QueryTrait
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param int $start
|
* @param int|float $start
|
||||||
* @param int $end
|
* @param int|float $end
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function whereBetween(string $column, int $start, int $end): static
|
public function whereBetween(string $column, int|float $start, int|float $end): static
|
||||||
{
|
{
|
||||||
if (empty($column) || empty($start) || empty($end)) {
|
if (empty($column) || empty($start) || empty($end)) {
|
||||||
return $this;
|
return $this;
|
||||||
@@ -571,11 +569,11 @@ trait QueryTrait
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param int $start
|
* @param int|float $start
|
||||||
* @param int $end
|
* @param int|float $end
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function whereNotBetween(string $column, int $start, int $end): static
|
public function whereNotBetween(string $column, int|float $start, int|float $end): static
|
||||||
{
|
{
|
||||||
if (empty($column) || empty($start) || empty($end)) {
|
if (empty($column) || empty($start) || empty($end)) {
|
||||||
return $this;
|
return $this;
|
||||||
|
|||||||
Reference in New Issue
Block a user