eee
This commit is contained in:
+1
-4
@@ -10,18 +10,15 @@ declare(strict_types=1);
|
||||
namespace Database;
|
||||
|
||||
use Database\Traits\QueryTrait;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use Kiri\Abstracts\Component;
|
||||
|
||||
/**
|
||||
* Class ActiveQuery
|
||||
* @package Database
|
||||
*/
|
||||
class ActiveQuery extends Component implements ISqlBuilder
|
||||
class ActiveQuery extends QueryTrait implements ISqlBuilder
|
||||
{
|
||||
|
||||
use QueryTrait;
|
||||
|
||||
/** @var bool */
|
||||
public bool $asArray = FALSE;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Database\Base;
|
||||
|
||||
use Closure;
|
||||
use Database\Traits\QueryTrait;
|
||||
|
||||
interface ActiveQueryInterface
|
||||
{
|
||||
@@ -11,261 +12,257 @@ interface ActiveQueryInterface
|
||||
/**
|
||||
* @param string $column
|
||||
* @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
|
||||
* @return $this
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function whereRaw(string $whereRaw): static;
|
||||
public function whereRaw(string $whereRaw): QueryTrait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @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
|
||||
* @return $this
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function whereNull(string $column): static;
|
||||
public function whereNull(string $column): QueryTrait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @return $this
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function whereEmpty(string $column): static;
|
||||
public function whereEmpty(string $column): QueryTrait;
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @return $this
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function whereNotEmpty(string $column): static;
|
||||
public function whereNotEmpty(string $column): QueryTrait;
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @return $this
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function whereNotNull(string $column): static;
|
||||
public function whereNotNull(string $column): QueryTrait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $alias
|
||||
*
|
||||
* @return $this
|
||||
* @return QueryTrait 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
|
||||
*
|
||||
* @return $this
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function from(string|Closure $tableName): static;
|
||||
public function from(string|Closure $tableName): QueryTrait;
|
||||
|
||||
/**
|
||||
* @param string $tableName
|
||||
* @param string $alias
|
||||
* @param $onCondition
|
||||
* @param null $param
|
||||
* @return $this
|
||||
* @throws
|
||||
* @param string|array $onCondition
|
||||
* @param array $param
|
||||
* @return QueryTrait
|
||||
*/
|
||||
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 $alias
|
||||
* @param $onCondition
|
||||
* @param null $param
|
||||
* @return $this
|
||||
* @throws
|
||||
* @param string $tableName
|
||||
* @param string $alias
|
||||
* @param string|array $onCondition
|
||||
* @param array $param
|
||||
* @return QueryTrait
|
||||
*/
|
||||
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 $alias
|
||||
* @param $onCondition
|
||||
* @param null $param
|
||||
* @return $this
|
||||
* @throws
|
||||
* @param string $tableName
|
||||
* @param string $alias
|
||||
* @param string|array $onCondition
|
||||
* @param array $param
|
||||
* @return QueryTrait
|
||||
*/
|
||||
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
|
||||
*
|
||||
* @return $this
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function sum(string $field): static;
|
||||
public function sum(string $field): QueryTrait;
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @return $this
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function max(string $field): static;
|
||||
public function max(string $field): QueryTrait;
|
||||
|
||||
/**
|
||||
* @param string $lngField
|
||||
* @param string $latField
|
||||
* @param int $lng1
|
||||
* @param int $lat1
|
||||
* @param int|float $lng1
|
||||
* @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 $sort
|
||||
*
|
||||
* @return $this
|
||||
* @return QueryTrait [
|
||||
*
|
||||
* [
|
||||
* 'addTime',
|
||||
* '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
|
||||
*
|
||||
* @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 string $opera
|
||||
* @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 $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 $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 $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 $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 array|Closure $value
|
||||
* @return $this
|
||||
* @throws
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function whereIn(string $columns, array|Closure $value): static;
|
||||
public function whereIn(string $columns, array|Closure $value): QueryTrait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $columns
|
||||
* @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 int $start
|
||||
* @param int $end
|
||||
* @return $this
|
||||
* @param int|float $start
|
||||
* @param int|float $end
|
||||
* @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 int $start
|
||||
* @param int $end
|
||||
* @return $this
|
||||
* @param int|float $start
|
||||
* @param int|float $end
|
||||
* @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
|
||||
* @return $this
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function where(array $column): static;
|
||||
public function where(array $column): QueryTrait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @param string $opera
|
||||
* @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
|
||||
* @return $this
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function whereClosure(Closure $closure): static;
|
||||
public function whereClosure(Closure $closure): QueryTrait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @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
|
||||
*
|
||||
* @return $this
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function limit(int $limit = 20): static;
|
||||
public function limit(int $limit = 20): QueryTrait;
|
||||
|
||||
|
||||
/**
|
||||
* @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
|
||||
* @package Database
|
||||
*/
|
||||
class Db implements ISqlBuilder
|
||||
class Db extends QueryTrait implements ISqlBuilder
|
||||
{
|
||||
use QueryTrait;
|
||||
|
||||
|
||||
private static bool $_inTransaction = false;
|
||||
|
||||
|
||||
@@ -136,9 +136,9 @@ class Model extends Base\Model
|
||||
/**
|
||||
* @param string $name
|
||||
* @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
|
||||
if (method_exists(static::class, $name)) {
|
||||
|
||||
@@ -16,10 +16,14 @@ use Database\Traits\QueryTrait;
|
||||
* Class Query
|
||||
* @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()
|
||||
{
|
||||
$this->builder = SqlBuilder::builder($this);
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+23
-25
@@ -12,39 +12,37 @@ namespace Database\Traits;
|
||||
|
||||
use Closure;
|
||||
use Database\ActiveQuery;
|
||||
use Database\Base\ActiveQueryInterface;
|
||||
use Database\ISqlBuilder;
|
||||
use Database\ModelInterface;
|
||||
use Database\Query;
|
||||
use Database\SqlBuilder;
|
||||
use Exception;
|
||||
use Kiri;
|
||||
use Kiri\Abstracts\Component;
|
||||
|
||||
/**
|
||||
* Trait QueryTrait
|
||||
* @package Database\Traits
|
||||
*/
|
||||
trait QueryTrait
|
||||
class QueryTrait extends Component implements ActiveQueryInterface, ISqlBuilder
|
||||
{
|
||||
public array $where = [];
|
||||
public array $select = [];
|
||||
public array $join = [];
|
||||
public array $order = [];
|
||||
public int $offset = 0;
|
||||
public int $limit = 0;
|
||||
public string $group = '';
|
||||
public string $from = '';
|
||||
public string $alias = 't1';
|
||||
public array $filter = [];
|
||||
protected array $where = [];
|
||||
protected array $select = [];
|
||||
protected array $join = [];
|
||||
protected array $order = [];
|
||||
protected int $offset = 0;
|
||||
protected int $limit = 0;
|
||||
protected string $group = '';
|
||||
protected string $from = '';
|
||||
protected string $alias = 't1';
|
||||
protected array $filter = [];
|
||||
protected bool $lock = false;
|
||||
|
||||
|
||||
public bool $lock = false;
|
||||
|
||||
|
||||
private SqlBuilder $builder;
|
||||
|
||||
/**
|
||||
* @var ModelInterface|string|null
|
||||
*/
|
||||
public ModelInterface|string|null $modelClass;
|
||||
protected ModelInterface|string|null $modelClass;
|
||||
|
||||
/**
|
||||
* clear
|
||||
@@ -337,7 +335,7 @@ trait QueryTrait
|
||||
*
|
||||
* @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";
|
||||
$this->select[] = $sql;
|
||||
@@ -552,11 +550,11 @@ trait QueryTrait
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @param int $start
|
||||
* @param int $end
|
||||
* @param int|float $start
|
||||
* @param int|float $end
|
||||
* @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)) {
|
||||
return $this;
|
||||
@@ -571,11 +569,11 @@ trait QueryTrait
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @param int $start
|
||||
* @param int $end
|
||||
* @param int|float $start
|
||||
* @param int|float $end
|
||||
* @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)) {
|
||||
return $this;
|
||||
|
||||
Reference in New Issue
Block a user