forked from c9s/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
66 lines (58 loc) · 1.84 KB
/
load.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
package gatsby
import (
"database/sql"
"github.com/c9s/gatsby/sqlutils"
)
type WhereMap map[string]interface{}
// Load record by primary key value.
var loadQueryCache = map[string]string{}
/*
Fill a record object by executing QueryRow from sql.DB object,
this method is faster than the DB.Query method.
*/
func LoadFromQueryRow(db *sql.DB, val PtrRecord, sqlstring string, args ...interface{}) *Result {
var err error
var row = db.QueryRow(sqlstring, args...)
if err = FillFromRows(val, row); err != nil {
if err == sql.ErrNoRows {
res := NewResult(sqlstring)
res.IsEmpty = true
return res
}
return NewErrorResult(err, sqlstring)
}
return NewResult(sqlstring)
}
/*
Fill a record object by executing a SQL query.
*/
func LoadFromQuery(db *sql.DB, val PtrRecord, sqlstring string, args ...interface{}) *Result {
var err error
row := db.QueryRow(sqlstring, args...)
if err = FillFromRows(val, row); err != nil {
if err == sql.ErrNoRows {
res := NewResult(sqlstring)
res.IsEmpty = true
return res
}
return NewErrorResult(err, sqlstring)
}
return NewResult(sqlstring)
}
func LoadWith(db *sql.DB, val PtrRecord, postQuery string, args ...interface{}) *Result {
var sqlstring = sqlutils.BuildSelectClause(val) + " " + postQuery + sqlutils.BuildLimitClause(1)
return LoadFromQueryRow(db, val, sqlstring, args...)
}
func Load(db *sql.DB, val PtrRecord, pkId int64) *Result {
var sqlstring = sqlutils.BuildLoadClause(val)
return LoadFromQueryRow(db, val, sqlstring, pkId)
}
/*
Load record from a where condition map
*/
func LoadByCols(db *sql.DB, val PtrRecord, cols WhereMap) *Result {
var sqlstring = sqlutils.BuildSelectClause(val)
whereSql, args := sqlutils.BuildWhereClauseWithAndOp(cols, GetHolderTypeByDriver(driverType))
sqlstring += whereSql + sqlutils.BuildLimitClause(1)
return LoadFromQueryRow(db, val, sqlstring, args...)
}