Skip to content

Commit 5503b63

Browse files
authored
修复 setFieldInc、setFieldDec (#487)
* 修复 setFieldInc、setFieldDec * 重构代码 * 修复 * 修复测试 * 更新文档
1 parent ee9b1c2 commit 5503b63

File tree

6 files changed

+55
-28
lines changed

6 files changed

+55
-28
lines changed

doc/components/db/index.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,9 @@ Db::query()->field(['id', 'name1 name', 'age1 as age']);
377377
// 传入参数原样代入到SQL中
378378
Db::query()->fieldRaw('id, name1 name, age1 as age');
379379

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

384385
### 条件 where
@@ -441,10 +442,12 @@ Db::query()->whereRaw('id >= 1');
441442
Db::query()->whereRaw('id >= 1', 'or');
442443
Db::query()->orWhereRaw('id >= 1');
443444

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

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

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

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

564+
// order by id desc, 1 asc
565+
Db::query()->orderRaw('id desc, :value asc', [':value' => 1]);
566+
559567
// JSON 类型参数排序
560568
Db::query()->order('field1->uid', 'desc');
561569
```
@@ -570,7 +578,10 @@ Db::query()->group('id', 'name');
570578
Db::query()->groupRaw('sum(id)');
571579

572580
// group by sum(id), ?
573-
Db::query()->groupRaw('sum(id), ?', 123);
581+
Db::query()->groupRaw('sum(id), ?', [123]);
582+
583+
// group by sum(id), :value
584+
Db::query()->groupRaw('sum(id), :value', [':value' => 123]);
574585
```
575586

576587
### having

src/Components/pgsql/tests/Unit/Db/QueryCurdBaseTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,16 +356,16 @@ public function testSetFieldExp(): void
356356

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

364364
$query = Db::query()->from('test')->setFieldInc('a', 4)
365365
->setFieldDec('b', 5)
366-
->setFieldExp('c', 'c + ?', [6])
366+
->setFieldExp('c', 'c + :c', [':c' => 6])
367367
;
368-
$this->assertEquals('replace into "test" set "a" = "a" + ?,"b" = "b" - ?,"c" = c + ?', $query->buildReplaceSql());
369-
$this->assertEquals([4, 5, 6], $query->getBinds());
368+
$this->assertEquals('replace into "test" set "a" = "a" + :fip1,"b" = "b" - :fdp2,"c" = c + :c', $query->buildReplaceSql());
369+
$this->assertEquals([':fip1' => 4, ':fdp2' => 5, ':c' => 6], $query->getBinds());
370370
}
371371
}

src/Db/Query/Interfaces/IQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ public function setFieldDec(string $fieldName, float $decValue = 1): self;
685685
/**
686686
* 获取自动起名的参数名称.
687687
*/
688-
public function getAutoParamName(): string;
688+
public function getAutoParamName(string $prefix = ':p'): string;
689689

690690
/**
691691
* 查询器别名.

src/Db/Query/Query.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -911,16 +911,9 @@ protected function executeEx(string $sql, string $resultClass)
911911
/**
912912
* {@inheritDoc}
913913
*/
914-
public function getAutoParamName(): string
914+
public function getAutoParamName(string $prefix = ':p'): string
915915
{
916-
$dbParamInc = &$this->dbParamInc;
917-
if ($dbParamInc >= 65535)
918-
{ // 限制dechex()结果最长为ffff,一般一个查询也不会用到这么多参数,足够了
919-
$dbParamInc = 0;
920-
}
921-
++$dbParamInc;
922-
923-
return ':p' . dechex($dbParamInc);
916+
return $prefix . dechex(++$this->dbParamInc);
924917
}
925918

926919
/**
@@ -958,15 +951,19 @@ public function setFieldExp(string $fieldName, string $exp, array $binds = []):
958951
*/
959952
public function setFieldInc(string $fieldName, float $incValue = 1): self
960953
{
961-
return $this->setFieldExp($fieldName, $this->fieldQuote($fieldName) . ' + ?', [$incValue]);
954+
$name = $this->getAutoParamName(':fip');
955+
956+
return $this->setFieldExp($fieldName, $this->fieldQuote($fieldName) . ' + ' . $name, [$name => $incValue]);
962957
}
963958

964959
/**
965960
* {@inheritDoc}
966961
*/
967962
public function setFieldDec(string $fieldName, float $decValue = 1): self
968963
{
969-
return $this->setFieldExp($fieldName, $this->fieldQuote($fieldName) . ' - ?', [$decValue]);
964+
$name = $this->getAutoParamName(':fdp');
965+
966+
return $this->setFieldExp($fieldName, $this->fieldQuote($fieldName) . ' - ' . $name, [$name => $decValue]);
970967
}
971968

972969
/**

tests/unit/Component/Tests/Db/QueryCurdBaseTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,16 +483,16 @@ public function testSetFieldExp(): void
483483

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

491491
$query = Db::query()->from('test')->setFieldInc('a', 4)
492492
->setFieldDec('b', 5)
493-
->setFieldExp('c', 'c + ?', [6])
493+
->setFieldExp('c', 'c + :c', [':c' => 6])
494494
;
495-
$this->assertEquals('replace into `test` set `a` = `a` + ?,`b` = `b` - ?,`c` = c + ?', $query->buildReplaceSql());
496-
$this->assertEquals([4, 5, 6], $query->getBinds());
495+
$this->assertEquals('replace into `test` set `a` = `a` + :fip1,`b` = `b` - :fdp2,`c` = c + :c', $query->buildReplaceSql());
496+
$this->assertEquals([':fip1' => 4, ':fdp2' => 5, ':c' => 6], $query->getBinds());
497497
}
498498
}

tests/unit/Component/Tests/ModelTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,4 +1170,23 @@ public function testId(): void
11701170
$record2 = ArticleId::find($record1->id);
11711171
$this->assertEquals($record1->toArray(), $record2->toArray());
11721172
}
1173+
1174+
public function testIncAndDec(): void
1175+
{
1176+
$record = VirtualColumn::newInstance();
1177+
$record->amount = 1;
1178+
$record->insert();
1179+
1180+
VirtualColumn::query()->where('id', '=', $record->id)
1181+
->setFieldInc('amount')
1182+
->update();
1183+
$record = VirtualColumn::find($record->id);
1184+
$this->assertEquals(2, $record->amount);
1185+
1186+
VirtualColumn::query()->where('id', '=', $record->id)
1187+
->setFieldDec('amount')
1188+
->update();
1189+
$record = VirtualColumn::find($record->id);
1190+
$this->assertEquals(1, $record->amount);
1191+
}
11731192
}

0 commit comments

Comments
 (0)