-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
117 lines (109 loc) · 2.27 KB
/
exec.go
File metadata and controls
117 lines (109 loc) · 2.27 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cqlx
import (
"log"
"github.com/gocql/gocql"
"github.com/scylladb/gocqlx"
"github.com/scylladb/gocqlx/qb"
)
type QueryxType int
const (
Select = QueryxType(iota + 1)
Insert
Update
Delete
Batch
Raw
)
//// Executex
func executex(q *Queryx, item interface{}) error {
if q == nil {
return ErrInvalidQuery
}
if item == nil {
return q.ExecRelease()
}
switch q.typ {
case Select:
if isSlice(item) {
return q.SelectRelease(item)
}
return q.GetRelease(item)
case Raw:
if q.Statement()[0] == 'S' {
if isSlice(item) {
return q.SelectRelease(item)
}
return q.GetRelease(item)
}
fallthrough
case Insert, Update, Delete, Batch:
if m := asMap(item); m != nil {
return q.BindMap(m).ExecRelease()
}
return q.BindStruct(item).ExecRelease()
default:
log.Printf("CQLX: Unexpected query type -- %T", q.typ)
return ErrInvalidQueryType
}
}
//// Queryx
func queryx(sess *gocql.Session, qry interface{}, args ...interface{}) *Queryx {
if sess == nil {
return &Queryx{nil, 0}
}
var stmt string
var names []string
var err error
switch q := qry.(type) {
case *qb.SelectBuilder:
stmt, names = q.ToCql()
case *qb.InsertBuilder:
stmt, names = q.ToCql()
case *qb.UpdateBuilder:
stmt, names = q.ToCql()
case *qb.DeleteBuilder:
stmt, names = q.ToCql()
case *qb.BatchBuilder:
stmt, names = q.ToCql()
case string:
stmt, names, err = gocqlx.CompileNamedQuery([]byte(q))
if err != nil {
stmt, names = q, nil
}
default:
return &Queryx{nil, 0}
}
if m := asMap(args...); m != nil {
return &Queryx{gocqlx.Query(sess.Query(stmt), names).BindMap(m), queryxType(qry)}
}
if s := asStruct(args...); s != nil {
return &Queryx{gocqlx.Query(sess.Query(stmt), names).BindStruct(s), queryxType(qry)}
}
return &Queryx{gocqlx.Query(sess.Query(stmt, args...), names), queryxType(qry)}
}
//// Iterx
func iterx(qry *Queryx) *Iterx {
if qry.Queryx == nil {
return &Iterx{}
}
return &Iterx{qry.Queryx.Iter()}
}
//// QueryxType
func queryxType(qry interface{}) QueryxType {
switch qry.(type) {
case *qb.SelectBuilder:
return Select
case *qb.InsertBuilder:
return Insert
case *qb.UpdateBuilder:
return Update
case *qb.DeleteBuilder:
return Delete
case *qb.BatchBuilder:
return Batch
case string:
return Raw
default:
return 0
}
}