Skip to content

Commit ea72b52

Browse files
committed
docs: 添加对 count(case ...) 语句的说明
1 parent bdbc5e5 commit ea72b52

File tree

6 files changed

+26
-9
lines changed

6 files changed

+26
-9
lines changed

docs/sqlbuilder.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,8 @@ list := make([]*User, 0, 10);
240240
// 查询符合以上条件的所有记录数
241241
builder.Count("count(*) AS cnt")
242242
count, err := builder.QueryInt("cnt")
243+
244+
// 也支持 case 作为 count 内容
245+
builder.Count("count(CASE WHEN age>18 THEN age ELSE NULL END) AS cnt")
246+
count, err := builder.QueryInt("cnt")
243247
```

fetch/object.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func convertError(field string, message error) error {
6060
// Count int `orm:"-"` // 不会匹配与该字段对应的列。
6161
// }
6262
//
63-
// 第一个参数用于表示有多少数据被正确导入到 obj 中
63+
// 第一个返回参数用于表示有多少数据被正确导入到 obj 中
6464
func Object(strict bool, rows *sql.Rows, obj any) (int, error) {
6565
val := reflect.ValueOf(obj)
6666

@@ -183,7 +183,7 @@ func getColumns(v reflect.Value, cols []string) ([]any, error) {
183183
}
184184

185185
// 将 rows 中的一条记录写入到 val 中,必须保证 val 的类型为 reflect.Struct。
186-
// 仅供 Obj() 调用。
186+
// 仅供 Object() 调用。
187187
func fetchOnceObj(strict bool, val reflect.Value, rows *sql.Rows) (int, error) {
188188
if !strict {
189189
return fetchOnceObjNoStrict(val, rows)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/lib/pq v1.10.9
1010
github.com/mattn/go-sqlite3 v1.14.24
1111
github.com/shopspring/decimal v1.4.0
12-
modernc.org/sqlite v1.33.1
12+
modernc.org/sqlite v1.34.4
1313
)
1414

1515
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
5959
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
6060
modernc.org/sortutil v1.2.0 h1:jQiD3PfS2REGJNzNCMMaLSp/wdMNieTbKX920Cqdgqc=
6161
modernc.org/sortutil v1.2.0/go.mod h1:TKU2s7kJMf1AE84OoiGppNHJwvB753OYfNl2WRb++Ss=
62-
modernc.org/sqlite v1.33.1 h1:trb6Z3YYoeM9eDL1O8do81kP+0ejv+YzgyFo+Gwy0nM=
63-
modernc.org/sqlite v1.33.1/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k=
62+
modernc.org/sqlite v1.34.4 h1:sjdARozcL5KJBvYQvLlZEmctRgW9xqIZc2ncN7PU0P8=
63+
modernc.org/sqlite v1.34.4/go.mod h1:3QQFCG2SEMtc2nv+Wq4cQCH7Hjcg+p/RMlS1XK+zwbk=
6464
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
6565
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
6666
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=

sqlbuilder/select.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,10 @@ func (stmt *SelectStmt) Limit(limit any, offset ...any) *SelectStmt {
401401
//
402402
// 如果指定了 count 表达式,则会造成 limit 失效,
403403
// 如果设置为空值,则取消 count,恢复普通的 select 。
404+
//
405+
// stmt := NewSelectStmt()
406+
// stmt.Count("count(*) as cnt")
407+
// stmt.Count("count(CASE WHEN xx) as cnt1, count(CASE WHEN yy) as cnt2")
404408
func (stmt *SelectStmt) Count(expr string) *SelectStmt {
405409
stmt.countExpr = expr
406410

sqlbuilder/select_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ func TestSelect(t *testing.T) {
3131
Desc("id")
3232

3333
id, err := stmt.QueryInt("id")
34-
a.NotError(err).
35-
Equal(id, 4)
34+
a.NotError(err).Equal(id, 4)
3635

3736
f, err := stmt.QueryFloat("id")
38-
a.NotError(err).
39-
Equal(f, 4.0)
37+
a.NotError(err).Equal(f, 4.0)
4038

4139
// 不存在的列
4240
f, err = stmt.QueryFloat("id_not_exists")
@@ -57,6 +55,17 @@ func TestSelect(t *testing.T) {
5755
cnt, err := stmt.Count("count(*) as cnt").QueryInt("cnt")
5856
a.NotError(err).Equal(cnt, 4)
5957

58+
type counter struct {
59+
Cnt1 int `orm:"name(cnt1)"`
60+
Cnt2 int `orm:"name(cnt2)"`
61+
}
62+
cntr := &counter{}
63+
size, err = stmt.Count("COUNT(CASE WHEN id>3 THEN id END) AS cnt1, COUNT(CASE WHEN id<3 THEN id END) AS cnt2").
64+
QueryObject(true, cntr)
65+
a.NotError(err).Equal(size, 1).
66+
Equal(cntr.Cnt1, 1). // stmt 中的条件是 id<5,所以只有一个符合条件的数据
67+
Equal(cntr.Cnt2, 2)
68+
6069
// 没有符合条件的数据
6170
stmt.Reset()
6271
stmt.Column("*").

0 commit comments

Comments
 (0)