-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
67 lines (61 loc) · 1.33 KB
/
session.go
File metadata and controls
67 lines (61 loc) · 1.33 KB
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
package lorm
import (
"context"
"database/sql"
)
type session struct {
engine *Engine
tx *sql.Tx
}
func (s *session) Exec(ctx context.Context, query string, args ...any) (result sql.Result, err error) {
proxy := s.proxy()
if len(args) > 0 {
query, err = s.engine.Placeholder().ReplacePlaceholders(query)
if err != nil {
return
}
args = adaptDBArgs(args)
}
return proxy.ExecContext(ctx, query, args...)
}
func (s *session) Query(ctx context.Context, query string, args ...any) (rows *sql.Rows, err error) {
proxy := s.proxy()
if len(args) > 0 {
query, err = s.engine.Placeholder().ReplacePlaceholders(query)
if err != nil {
return
}
args = adaptDBArgs(args)
}
return proxy.QueryContext(ctx, query, args...)
}
func (s *session) Exist(ctx context.Context, query string, args ...any) (exist bool, err error) {
proxy := s.proxy()
var rows *sql.Rows
if len(args) == 0 {
rows, err = proxy.QueryContext(ctx, query)
} else {
query, err = s.engine.Placeholder().ReplacePlaceholders(query)
if err != nil {
return
}
args = adaptDBArgs(args)
rows, err = proxy.QueryContext(ctx, query, args...)
}
if err != nil {
return
}
defer rows.Close()
if rows.Next() {
exist = true
return
}
err = rows.Err()
return
}
func (s *session) proxy() DBProxy {
if s.tx != nil {
return s.tx
}
return s.engine.db
}