Skip to content

Commit

Permalink
add db unit
Browse files Browse the repository at this point in the history
  • Loading branch information
sakuraovq committed Aug 18, 2019
1 parent cac7e13 commit 94c7171
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/db/src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ public function setTablePrefix($prefix): self
* @return Expression
* @throws ContainerException
* @throws ReflectionException
*
* @deprecated This method unsafe, This connection unreleased
*/
public function raw($value): Expression
{
Expand Down Expand Up @@ -446,6 +448,7 @@ public function cursor(string $query, array $bindings = [], bool $useReadPdo = t
return $statement;
});

/** @var PDOStatement $statement */
while ($record = $statement->fetch()) {
yield $record;
}
Expand Down
1 change: 1 addition & 0 deletions src/db/src/Connection/ConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public function release(bool $final = false): void
$tsKey = sprintf('%d.transaction.%d', Co::tid(), Co::id());

$ordConnections = $this->get($ordKey, []);

foreach ($ordConnections as $poolName => $ordPoolConnection) {
foreach ($ordPoolConnection as $ordConId => $ordConnection) {
if (!$ordConnection instanceof Connection) {
Expand Down
28 changes: 15 additions & 13 deletions src/db/src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,21 +291,23 @@ public function whereProp($column, $operator = null, $value = null, string $bool
// Get `@Column` Prop Mapping
$props = EntityRegister::getProps($this->model->getClassName());

if (is_string($column)) {
$column = $props[$column] ?? $column;
} elseif (is_array($column)) {
$newColumns = [];
foreach ($column as $k => $v) {
$k = $props[$k] ?? $k;

if (isset($v[0]) && is_scalar($v[0])) {
$kv = $v[0];
$v[0] = $props[$kv] ?? $kv;
if ($props) {
if (is_string($column)) {
$column = $props[$column] ?? $column;
} elseif (is_array($column)) {
$newColumns = [];
foreach ($column as $k => $v) {
$k = $props[$k] ?? $k;

if (isset($v[0]) && is_scalar($v[0])) {
$kv = $v[0];
$v[0] = $props[$kv] ?? $kv;
}

$newColumns[$k] = $v;
}

$newColumns[$k] = $v;
$column = $newColumns;
}
$column = $newColumns;
}

$this->where($column, $operator, $value, $boolean);
Expand Down
9 changes: 5 additions & 4 deletions src/db/src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3121,7 +3121,9 @@ public function updateAllCounters(array $where, array $counters, array $extra =
{
// Convert counters to expression
foreach ($counters as $column => $value) {
$counters[$column] = Expression::new("$column + $value");
$wrapped = $this->grammar->wrap($column);

$counters[$column] = $this->raw("$wrapped + $value");
}

if (!empty($where)) {
Expand Down Expand Up @@ -3269,12 +3271,11 @@ protected function forSubQuery(): self
*
* @return Expression
* @throws ContainerException
* @throws DbException
* @throws ReflectionException
*/
public function raw($value)
public function raw($value): Expression
{
return $this->getConnection()->raw($value);
return Expression::new($value);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/db/src/Query/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function processSelect(Builder $query, $results): array
public function processInsertGetId(Builder $query, $sql, $values, string $sequence = null): string
{
$id = $query->getConnection()->insertGetId($sql, $values, $sequence);
return (string)$id;
return $id;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/db/test/testing/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ public function setHahh(?int $hahh): void
*/
private $testJson;

/**
*
*
* @Column()
* @var float|null
*/
private $amount;

/**
* @return float|null
*/
public function getAmount(): ?float
{
return $this->amount;
}

/**
* @param float|null $amount
*/
public function setAmount(?float $amount): void
{
$this->amount = $amount;
}

/**
* @return null|array
*/
Expand Down
25 changes: 23 additions & 2 deletions src/db/test/unit/Eloquent/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ReflectionException;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Db\DB;
use Swoft\Db\Eloquent\Builder;
use Swoft\Db\Eloquent\Collection;
use Swoft\Db\Exception\DbException;
use Swoft\Db\Query\Expression;
Expand Down Expand Up @@ -838,10 +839,30 @@ public function testWhereProp()

$where = [
'pwd' => md5(uniqid()),
['udesc', 'like', 'swoft%']
['udesc', 'like', 'swoft%'],
[
function (\Swoft\Db\Query\Builder $builder) {
echo $builder->toSql();
}
],
['whereIn', 'id', [1]]
];
$expectSql2 = 'select * from `user` where (`password` = ? and `user_desc` like ?)';
$expectSql2 = 'select * from `user` where (`password` = ? and `user_desc` like ? and `id` in (?))';
$resSql2 = User::whereProp($where)->toSql();
$this->assertEquals($expectSql2, $resSql2);
}

public function testWhereCall()
{
$toSql = 'select * from `user` where (`id` in (?) or `id` = ? or `status` > ? and `age` between ? and ?)';
$where = [
['whereIn', 'id', [1]],
['orWhere', 'id', 2],
['orWhere', 'status', '>', -1],
['whereBetween', 'age', [18, 25]]
];
$sql = User::where($where)->toSql();

$this->assertEquals($sql, $toSql);
}
}

0 comments on commit 94c7171

Please sign in to comment.