-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-repo-fetch-logic.go
69 lines (58 loc) · 1.67 KB
/
db-repo-fetch-logic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* Copyright (c) 2024.
* Created by Andy Pangaribuan <https://github.com/apangaribuan>.
*
* This product is protected by copyright and distributed under
* licenses restricting copying, distribution and decompilation.
* All Rights Reserved.
*/
package db
import (
"strings"
"github.com/andypangaribuan/gmod/fm"
"github.com/andypangaribuan/gmod/ice"
"github.com/andypangaribuan/gmod/mol"
)
func (slf *stuRepo[T]) fetches(isLimitOne bool, tx ice.DbTx, condition string, args []any) *stuRepoResult[T] {
var (
whereQuery = slf.getWhereQuery(condition, args)
endQuery = strings.TrimSpace(slf.getQuery("end-query", args) + fm.Ternary(isLimitOne, " LIMIT 1", ""))
fullQuery = slf.getQuery("full-query", args)
report = &stuReport{
tableName: slf.tableName,
insertColumn: slf.insertColumn,
insertArgSign: slf.insertArgSign,
args: slf.getArgs(args),
query: "SELECT * FROM ::tableName",
}
)
if whereQuery != "" {
report.query += " " + whereQuery
}
if endQuery != "" {
report.query += " " + endQuery
}
if fullQuery != "" {
report.query = fullQuery
}
var (
err error
out []*T
execReport *mol.DbExecReport
)
err = report.transform()
if err != nil {
return slf.result(report, err, nil, nil)
}
if tx != nil {
execReport, err = slf.ins.TxSelect(tx, &out, report.query, report.args...)
} else {
if slf.rwFetchWhenNull {
execReport, err = slf.ins.SelectR2(&out, report.query, report.args, fm.Ptr(func() bool { return len(out) > 0 }))
} else {
execReport, err = slf.ins.Select(&out, report.query, report.args...)
}
}
report.execReport = execReport
return slf.result(report, err, nil, out)
}