-
Notifications
You must be signed in to change notification settings - Fork 0
/
bccdata.go
244 lines (190 loc) · 6.21 KB
/
bccdata.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package bccdata
import (
"database/sql"
"fmt"
"time"
)
type DatabaseContext struct {
Database *sql.DB
EntityDescriptions map[string]EntityDescription
}
type EntityRelationship struct {
EntityName string
JoinTableName string
ForeignKey string
TargetKey string
}
type EntityDescription struct {
Name string
TableName string
PrimaryKey string
Relationships map[string]EntityRelationship
InsertStatement *sql.Stmt
CreateZeroInstance func() Entity
Context *DatabaseContext
}
type Entity interface {
ScanFromRow(*sql.Rows) (bool, error)
}
// Entity Descriptions
func (databaseContext *DatabaseContext) RegisterEntityDescription(entityDescription EntityDescription) {
if databaseContext.EntityDescriptions == nil {
databaseContext.EntityDescriptions = make(map[string]EntityDescription)
}
entityDescription.Context = databaseContext
databaseContext.EntityDescriptions[entityDescription.Name] = entityDescription
}
func (databaseContext *DatabaseContext) EntityDescriptionForName(entityName string) (entityDescription EntityDescription) {
return databaseContext.EntityDescriptions[entityName]
}
// Entity Relationships
func (entityDescription *EntityDescription) RegisterRelationship(entityRelationship EntityRelationship) {
if entityDescription.Relationships == nil {
entityDescription.Relationships = make(map[string]EntityRelationship)
}
entityDescription.Relationships[entityRelationship.EntityName] = entityRelationship
}
func (entityDescription *EntityDescription) RelationshipForName(entityName string) (entityRelationship EntityRelationship) {
return entityDescription.Relationships[entityName]
}
// Entity Creation
func (entityDescription *EntityDescription) Create(transaction *sql.Tx, args ...interface{}) (entity Entity, err error) {
var (
commitAtEnd bool
insertStatement *sql.Stmt
result sql.Result
objectID int64
createdTime int64
tableName string
updateCreatedDateSQL string
querySQL string
rows *sql.Rows
scanSuccess bool
)
commitAtEnd = false
if transaction == nil {
transaction, err = entityDescription.Context.Database.Begin()
commitAtEnd = true
if err != nil {
goto cleanup
}
commitAtEnd = true
}
insertStatement = transaction.Stmt(entityDescription.InsertStatement)
result, err = insertStatement.Exec(args...)
if err != nil {
goto cleanup
}
objectID, err = result.LastInsertId()
if err != nil {
goto cleanup
}
tableName = entityDescription.TableName
createdTime = time.Now().Unix()
updateCreatedDateSQL = fmt.Sprintf("UPDATE %s SET createdDate=? WHERE id=?", tableName)
result, err = transaction.Exec(updateCreatedDateSQL, createdTime, objectID)
if err != nil {
goto cleanup
}
querySQL = fmt.Sprintf("SELECT * FROM %s WHERE id=?", tableName)
rows, err = transaction.Query(querySQL, objectID)
if err != nil {
goto cleanup
}
entity = entityDescription.CreateZeroInstance()
scanSuccess, err = entity.ScanFromRow(rows)
if !scanSuccess {
goto cleanup
}
cleanup:
if rows != nil {
defer rows.Close()
}
if insertStatement != nil {
defer insertStatement.Close()
}
if commitAtEnd {
if err != nil {
transaction.Rollback()
} else {
transaction.Commit()
}
}
return entity, err
}
func (entityDescription *EntityDescription) CreateFromRows(rows *sql.Rows) (entities []Entity, err error) {
var (
scanSuccess bool
)
for {
entity := entityDescription.CreateZeroInstance()
scanSuccess, err = entity.ScanFromRow(rows)
if !scanSuccess {
break
}
entities = append(entities, entity)
}
return entities, err
}
// Entity Find
func (entityDescription *EntityDescription) FindEntity(transaction *sql.Tx, keyName *string, value interface{}) (entity Entity, err error) {
entities, err := entityDescription.FindEntities(transaction, keyName, value)
return entities[0], err
}
func (entityDescription *EntityDescription) FindEntities(transaction *sql.Tx, keyName *string, value interface{}) (entities []Entity, err error) {
var (
tableName string
columnName string
selectStatement string
rows *sql.Rows
)
tableName = entityDescription.TableName
if keyName == nil {
columnName = entityDescription.PrimaryKey
} else {
columnName = *keyName
}
selectStatement = fmt.Sprintf("SELECT * FROM %s WHERE %s=?", tableName, columnName)
if transaction != nil {
rows, err = transaction.Query(selectStatement, value)
} else {
rows, err = entityDescription.Context.Database.Query(selectStatement, value)
}
if err != nil {
goto cleanup
}
entities, err = entityDescription.CreateFromRows(rows)
if err != nil {
goto cleanup
}
cleanup:
defer rows.Close()
return entities, err
}
func (entityDescription *EntityDescription) FindRelatedEntity(transaction *sql.Tx, targetEntityName string, queryKey string, queryValue interface{}) (entities []Entity, err error) {
var (
relationship EntityRelationship
targetEntityDescription EntityDescription
joinTableName string
targetTableName string
joinTableForeignKey string
targetTableKey string
selectStatement string
rows *sql.Rows
)
relationship = entityDescription.RelationshipForName(targetEntityName)
targetEntityDescription = entityDescription.Context.EntityDescriptionForName(targetEntityName)
joinTableName = relationship.JoinTableName
targetTableName = targetEntityDescription.TableName
joinTableForeignKey = relationship.ForeignKey
targetTableKey = relationship.TargetKey
// SELECT * FROM lists_placemarks LEFT OUTER JOIN placemarks ON lists_placemarks.placemarksID=placemarks.id WHERE lists_placemarks.listsID=1
selectStatement = fmt.Sprintf("SELECT %s.* FROM %s LEFT OUTER JOIN %s ON %s.%s=%s.%s WHERE %s.%s=?", targetTableName, joinTableName, targetTableName, joinTableName, joinTableForeignKey, targetTableName, targetTableKey, joinTableName, queryKey)
if transaction != nil {
rows, err = transaction.Query(selectStatement, queryValue)
} else {
rows, err = entityDescription.Context.Database.Query(selectStatement, queryValue)
}
entities, err = targetEntityDescription.CreateFromRows(rows)
return entities, err
}