Skip to content

Commit

Permalink
修复 setFieldInc、setFieldDec (#487)
Browse files Browse the repository at this point in the history
* 修复 setFieldInc、setFieldDec

* 重构代码

* 修复

* 修复测试

* 更新文档
  • Loading branch information
Yurunsoft authored Apr 3, 2023
1 parent ee9b1c2 commit 5503b63
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
19 changes: 15 additions & 4 deletions doc/components/db/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,9 @@ Db::query()->field(['id', 'name1 name', 'age1 as age']);
// 传入参数原样代入到SQL中
Db::query()->fieldRaw('id, name1 name, age1 as age');

// 传入参数原样代入到SQL中,也支持参数绑定
// 传入参数原样代入到SQL中,也支持参数绑定(不支持同时使用 ? 和 :xxx)
Db::query()->fieldRaw('id, name1 name, age1 as age, ? as value', null, [123]);
Db::query()->fieldRaw('id, name1 name, age1 as age, :value as value', null, [':value' => 123]);
```

### 条件 where
Expand Down Expand Up @@ -441,10 +442,12 @@ Db::query()->whereRaw('id >= 1');
Db::query()->whereRaw('id >= 1', 'or');
Db::query()->orWhereRaw('id >= 1');

// 支持参数绑定
// 支持参数绑定(不支持同时使用 ? 和 :xxx)
// 传入参数原样代入到SQL中,并且为or条件
Db::query()->whereRaw('id >= ?', 'or', [1]);
Db::query()->orWhereRaw('id >= ?', [1]);
Db::query()->whereRaw('id >= :value', 'or', [':value' => 1]);
Db::query()->orWhereRaw('id >= :value', [':value' => 1]);
```

#### whereBrackets
Expand Down Expand Up @@ -531,9 +534,11 @@ Db::query()->table('tb_test1')->join('tb_test2', 'tb_test1.aid', '=', 'tb_test2.
// select * from tb_test1 left join tb_test2 on tb_test1.aid = tb_test2.bid
Db::query()->table('tb_test1')->joinRaw('left join tb_test2 on tb_test1.aid = tb_test2.bid');

// 支持参数绑定
// 支持参数绑定(不支持同时使用 ? 和 :xxx)
// select * from tb_test1 left join tb_test2 on tb_test1.aid = tb_test2.bid and tb_test2.xxx = ?
Db::query()->table('tb_test1')->joinRaw('left join tb_test2 on tb_test1.aid = tb_test2.bid and tb_test2.xxx = ?', [123]);
// select * from tb_test1 left join tb_test2 on tb_test1.aid = tb_test2.bid and tb_test2.xxx = :value
Db::query()->table('tb_test1')->joinRaw('left join tb_test2 on tb_test1.aid = tb_test2.bid and tb_test2.xxx = :value', [':value' => 123]);

// 下面三种用法,第5个参数都支持传Where
// left join
Expand All @@ -556,6 +561,9 @@ Db::query()->orderRaw('id desc');
// order by id desc, 1 asc
Db::query()->orderRaw('id desc, ? asc', [1]);

// order by id desc, 1 asc
Db::query()->orderRaw('id desc, :value asc', [':value' => 1]);

// JSON 类型参数排序
Db::query()->order('field1->uid', 'desc');
```
Expand All @@ -570,7 +578,10 @@ Db::query()->group('id', 'name');
Db::query()->groupRaw('sum(id)');

// group by sum(id), ?
Db::query()->groupRaw('sum(id), ?', 123);
Db::query()->groupRaw('sum(id), ?', [123]);

// group by sum(id), :value
Db::query()->groupRaw('sum(id), :value', [':value' => 123]);
```

### having
Expand Down
12 changes: 6 additions & 6 deletions src/Components/pgsql/tests/Unit/Db/QueryCurdBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,16 @@ public function testSetFieldExp(): void

$query = Db::query()->from('test')->setFieldInc('a', 1)
->setFieldDec('b', 2)
->setFieldExp('c', 'c + ?', [3])
->setFieldExp('c', 'c + :c', [':c' => 3])
;
$this->assertEquals('update "test" set "a" = "a" + ?,"b" = "b" - ?,"c" = c + ?', $query->buildUpdateSql());
$this->assertEquals([1, 2, 3], $query->getBinds());
$this->assertEquals('update "test" set "a" = "a" + :fip1,"b" = "b" - :fdp2,"c" = c + :c', $query->buildUpdateSql());
$this->assertEquals([':fip1' => 1, ':fdp2' => 2, ':c' => 3], $query->getBinds());

$query = Db::query()->from('test')->setFieldInc('a', 4)
->setFieldDec('b', 5)
->setFieldExp('c', 'c + ?', [6])
->setFieldExp('c', 'c + :c', [':c' => 6])
;
$this->assertEquals('replace into "test" set "a" = "a" + ?,"b" = "b" - ?,"c" = c + ?', $query->buildReplaceSql());
$this->assertEquals([4, 5, 6], $query->getBinds());
$this->assertEquals('replace into "test" set "a" = "a" + :fip1,"b" = "b" - :fdp2,"c" = c + :c', $query->buildReplaceSql());
$this->assertEquals([':fip1' => 4, ':fdp2' => 5, ':c' => 6], $query->getBinds());
}
}
2 changes: 1 addition & 1 deletion src/Db/Query/Interfaces/IQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ public function setFieldDec(string $fieldName, float $decValue = 1): self;
/**
* 获取自动起名的参数名称.
*/
public function getAutoParamName(): string;
public function getAutoParamName(string $prefix = ':p'): string;

/**
* 查询器别名.
Expand Down
19 changes: 8 additions & 11 deletions src/Db/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,16 +911,9 @@ protected function executeEx(string $sql, string $resultClass)
/**
* {@inheritDoc}
*/
public function getAutoParamName(): string
public function getAutoParamName(string $prefix = ':p'): string
{
$dbParamInc = &$this->dbParamInc;
if ($dbParamInc >= 65535)
{ // 限制dechex()结果最长为ffff,一般一个查询也不会用到这么多参数,足够了
$dbParamInc = 0;
}
++$dbParamInc;

return ':p' . dechex($dbParamInc);
return $prefix . dechex(++$this->dbParamInc);
}

/**
Expand Down Expand Up @@ -958,15 +951,19 @@ public function setFieldExp(string $fieldName, string $exp, array $binds = []):
*/
public function setFieldInc(string $fieldName, float $incValue = 1): self
{
return $this->setFieldExp($fieldName, $this->fieldQuote($fieldName) . ' + ?', [$incValue]);
$name = $this->getAutoParamName(':fip');

return $this->setFieldExp($fieldName, $this->fieldQuote($fieldName) . ' + ' . $name, [$name => $incValue]);
}

/**
* {@inheritDoc}
*/
public function setFieldDec(string $fieldName, float $decValue = 1): self
{
return $this->setFieldExp($fieldName, $this->fieldQuote($fieldName) . ' - ?', [$decValue]);
$name = $this->getAutoParamName(':fdp');

return $this->setFieldExp($fieldName, $this->fieldQuote($fieldName) . ' - ' . $name, [$name => $decValue]);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/Component/Tests/Db/QueryCurdBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,16 +483,16 @@ public function testSetFieldExp(): void

$query = Db::query()->from('test')->setFieldInc('a', 1)
->setFieldDec('b', 2)
->setFieldExp('c', 'c + ?', [3])
->setFieldExp('c', 'c + :c', [':c' => 3])
;
$this->assertEquals('update `test` set `a` = `a` + ?,`b` = `b` - ?,`c` = c + ?', $query->buildUpdateSql());
$this->assertEquals([1, 2, 3], $query->getBinds());
$this->assertEquals('update `test` set `a` = `a` + :fip1,`b` = `b` - :fdp2,`c` = c + :c', $query->buildUpdateSql());
$this->assertEquals([':fip1' => 1, ':fdp2' => 2, ':c' => 3], $query->getBinds());

$query = Db::query()->from('test')->setFieldInc('a', 4)
->setFieldDec('b', 5)
->setFieldExp('c', 'c + ?', [6])
->setFieldExp('c', 'c + :c', [':c' => 6])
;
$this->assertEquals('replace into `test` set `a` = `a` + ?,`b` = `b` - ?,`c` = c + ?', $query->buildReplaceSql());
$this->assertEquals([4, 5, 6], $query->getBinds());
$this->assertEquals('replace into `test` set `a` = `a` + :fip1,`b` = `b` - :fdp2,`c` = c + :c', $query->buildReplaceSql());
$this->assertEquals([':fip1' => 4, ':fdp2' => 5, ':c' => 6], $query->getBinds());
}
}
19 changes: 19 additions & 0 deletions tests/unit/Component/Tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1170,4 +1170,23 @@ public function testId(): void
$record2 = ArticleId::find($record1->id);
$this->assertEquals($record1->toArray(), $record2->toArray());
}

public function testIncAndDec(): void
{
$record = VirtualColumn::newInstance();
$record->amount = 1;
$record->insert();

VirtualColumn::query()->where('id', '=', $record->id)
->setFieldInc('amount')
->update();
$record = VirtualColumn::find($record->id);
$this->assertEquals(2, $record->amount);

VirtualColumn::query()->where('id', '=', $record->id)
->setFieldDec('amount')
->update();
$record = VirtualColumn::find($record->id);
$this->assertEquals(1, $record->amount);
}
}

0 comments on commit 5503b63

Please sign in to comment.