Skip to content

Commit

Permalink
新增支持查询器别名,减少查询器连贯操作重复构建步骤
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Oct 25, 2019
1 parent 6e84883 commit a8b6e5e
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Db/Query/Builder/InsertBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function build(...$args)
else
{
$fields[] = $this->parseKeyword($k);
$valueParam = $this->query->getAutoParamName();
$valueParam = ':' . $k;
$valueParams[] = $valueParam;
$this->params[$valueParam] = $v;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Query/Builder/ReplaceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function build(...$args)
}
else
{
$valueParam = $this->query->getAutoParamName();
$valueParam = ':' . $k;
$this->params[$valueParam] = $v;
$setStrs[] = $this->parseKeyword($k) . ' = ' . $valueParam;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Query/Builder/UpdateBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function build(...$args)
}
else
{
$valueParam = $this->query->getAutoParamName();
$valueParam = ':' . $k;
$valueParams[] = $valueParam;
$this->params[$valueParam] = $v;
$setStrs[] = $this->parseKeyword($k) . ' = ' . $valueParam;
Expand Down
14 changes: 14 additions & 0 deletions src/Db/Query/Interfaces/IQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,5 +594,19 @@ public function setFieldInc($fieldName, float $incValue = 1);
*/
public function setFieldDec($fieldName, float $decValue = 1);

/**
* 获取自动起名的参数名称
* @return string
*/
public function getAutoParamName();

/**
* 查询器别名
*
* @param string $name
* @param callable $callable
* @return static
*/
public function alias($name, $callable);

}
129 changes: 118 additions & 11 deletions src/Db/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ class Query implements IQuery
*/
protected $dbParamInc = 0;

/**
* 查询器别名集合
*
* @var \Imi\Db\Query\Interfaces\IQuery[]
*/
protected static $aliasMap = [];

/**
* 当前别名
*
* @var string
*/
protected $alias;

/**
* 别名 Sql 集合
*
* @var string[]
*/
protected static $aliasSqls = [];

public function __construct(IDb $db = null, $modelClass = null, $poolName = null, $queryType = null)
{
$this->db = $db;
Expand All @@ -111,6 +132,13 @@ public function __init()
}
}

public function __clone()
{
$this->isInitDb = false;
$this->db = null;
$this->option = clone $this->option;
}

/**
* 获取所有操作的记录
* @return QueryOption
Expand Down Expand Up @@ -856,8 +884,15 @@ public function getBinds()
*/
public function select(): IResult
{
$builder = new SelectBuilder($this);
$sql = $builder->build();
if($this->alias && isset(static::$aliasSqls[$this->alias]))
{
$sql = static::$aliasSqls[$this->alias];
}
else
{
$builder = new SelectBuilder($this);
static::$aliasSqls[$this->alias] = $sql = $builder->build();
}
if(!$this->isInitQueryType && !$this->isInTransaction())
{
$this->queryType = QueryType::READ;
Expand Down Expand Up @@ -899,8 +934,29 @@ public function paginate($page, $count, $options = []): IPaginateResult
*/
public function insert($data = null): IResult
{
$builder = new InsertBuilder($this);
$sql = $builder->build($data);
if($this->alias && isset(static::$aliasSqls[$this->alias]))
{
$sql = static::$aliasSqls[$this->alias];
$bindValues = [];
$numberKey = isset($data[0]);
foreach($data as $k => $v)
{
if($numberKey)
{
$bindValues[':' . ($k + 1)] = $v;
}
else
{
$bindValues[':' . $k] = $v;
}
}
$this->bindValues($bindValues);
}
else
{
$builder = new InsertBuilder($this);
static::$aliasSqls[$this->alias] = $sql = $builder->build($data);
}
return $this->execute($sql);
}

Expand All @@ -926,8 +982,21 @@ public function batchInsert($data = null): IResult
*/
public function update($data = null): IResult
{
$builder = new UpdateBuilder($this);
$sql = $builder->build($data);
if($this->alias && isset(static::$aliasSqls[$this->alias]))
{
$sql = static::$aliasSqls[$this->alias];
$bindValues = [];
foreach($data as $k => $v)
{
$bindValues[':' . $k] = $v;
}
$this->bindValues($bindValues);
}
else
{
$builder = new UpdateBuilder($this);
static::$aliasSqls[$this->alias] = $sql = $builder->build($data);
}
return $this->execute($sql);
}

Expand All @@ -939,8 +1008,21 @@ public function update($data = null): IResult
*/
public function replace($data = null): IResult
{
$builder = new ReplaceBuilder($this);
$sql = $builder->build($data);
if($this->alias && isset(static::$aliasSqls[$this->alias]))
{
$sql = static::$aliasSqls[$this->alias];
$bindValues = [];
foreach($data as $k => $v)
{
$bindValues[':' . $k] = $v;
}
$this->bindValues($bindValues);
}
else
{
$builder = new ReplaceBuilder($this);
static::$aliasSqls[$this->alias] = $sql = $builder->build($data);
}
return $this->execute($sql);
}

Expand All @@ -950,10 +1032,16 @@ public function replace($data = null): IResult
*/
public function delete(): IResult
{
$builder = new DeleteBuilder($this);
$sql = $builder->build();
if($this->alias && isset(static::$aliasSqls[$this->alias]))
{
$sql = static::$aliasSqls[$this->alias];
}
else
{
$builder = new DeleteBuilder($this);
static::$aliasSqls[$this->alias] = $sql = $builder->build();
}
$result = $this->execute($sql);
$this->__init();
return $result;
}

Expand Down Expand Up @@ -1158,4 +1246,23 @@ private function isInTransaction()
return false;
}
}

/**
* 查询器别名
*
* @param string $name
* @param callable $callable
* @return static
*/
public function alias($name, $callable)
{
if(!isset(static::$aliasMap[$name]))
{
$callable($this);
$this->alias = $name;
static::$aliasMap[$name] = $this;
}
return clone static::$aliasMap[$name];
}

}
6 changes: 6 additions & 0 deletions src/Db/Query/QueryOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@ public function __construct()
{
$this->table = new Table();
}

public function __clone()
{
$this->table = clone $this->table;
}

}
Loading

0 comments on commit a8b6e5e

Please sign in to comment.