-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt_prepare.go
147 lines (135 loc) · 2.84 KB
/
stmt_prepare.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package gorb
import (
"database/sql"
"fmt"
)
type (
tableStmts struct {
stmtInfo *sql.Stmt
stmtSelect *sql.Stmt
stmtInsert *sql.Stmt
stmtUpdate *sql.Stmt
stmtRemove *sql.Stmt
stmtDelete *sql.Stmt
}
)
func (stmts *tableStmts) releaseStatements() {
if stmts.stmtInfo != nil {
stmts.stmtInfo.Close()
stmts.stmtInfo = nil
}
if stmts.stmtSelect != nil {
stmts.stmtSelect.Close()
stmts.stmtSelect = nil
}
if stmts.stmtInsert != nil {
stmts.stmtInsert.Close()
stmts.stmtInsert = nil
}
if stmts.stmtUpdate != nil {
stmts.stmtUpdate.Close()
stmts.stmtUpdate = nil
}
if stmts.stmtRemove != nil {
stmts.stmtRemove.Close()
stmts.stmtRemove = nil
}
if stmts.stmtDelete != nil {
stmts.stmtDelete.Close()
stmts.stmtDelete = nil
}
}
func (c *ChildTable) createStatements(db *sql.DB, tablePath []*ChildTable) error {
stmts := new(tableStmts)
var e error = nil
var query string
if e == nil {
query = c.getInfoQuery(tablePath)
stmts.stmtInfo, e = db.Prepare(query)
}
if e == nil {
query = c.getSelectQuery(tablePath)
stmts.stmtSelect, e = db.Prepare(query)
}
if e == nil {
query = c.getInsertQuery()
fmt.Println(query)
stmts.stmtInsert, e = db.Prepare(query)
}
if e == nil {
query = c.getUpdateQuery()
stmts.stmtUpdate, e = db.Prepare(query)
}
if e == nil {
query = c.getRemoveQuery()
stmts.stmtRemove, e = db.Prepare(query)
}
if e == nil {
query = c.getDeleteQuery(tablePath)
stmts.stmtDelete, e = db.Prepare(query)
}
if e != nil {
fmt.Printf("Invalid query: %s", query)
stmts.releaseStatements()
return e
}
if c.stmts != nil {
c.stmts.releaseStatements()
c.stmts = nil
}
c.stmts = stmts
for _, child := range c.Children {
e = child.createStatements(db, append(tablePath, c))
if e != nil {
return e
}
}
return nil
}
func (entity *Entity) createStatements(db *sql.DB) error {
stmts := new(tableStmts)
var e error = nil
var query string
entity.selectFields = entity.getSelectFields()
if e == nil {
query = entity.getInfoQuery()
stmts.stmtInfo, e = db.Prepare(query)
}
if e == nil {
query = entity.getSelectQuery()
stmts.stmtSelect, e = db.Prepare(query)
}
if e == nil {
query = entity.getInsertQuery()
stmts.stmtInsert, e = db.Prepare(query)
}
if e == nil {
query = entity.getUpdateQuery()
stmts.stmtUpdate, e = db.Prepare(query)
}
if e == nil {
query = entity.getRemoveQuery()
stmts.stmtRemove, e = db.Prepare(query)
}
if e == nil {
query = entity.getDeleteQuery()
stmts.stmtDelete, e = db.Prepare(query)
}
if e != nil {
fmt.Printf("Invalid query: %s", query)
stmts.releaseStatements()
return e
}
if entity.stmts != nil {
entity.stmts.releaseStatements()
entity.stmts = nil
}
entity.stmts = stmts
for _, child := range entity.Children {
e = child.createStatements(db, []*ChildTable{})
if e != nil {
return e
}
}
return nil
}