This repository has been archived by the owner on May 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opal.go
339 lines (283 loc) · 7.85 KB
/
opal.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* Date: 8/02/14
* Time: 11:22 AM
*/
package opal
import (
"database/sql"
"fmt"
_ "github.com/twinj/version"
"log"
"reflect"
)
const (
// Group magic number
group = 0xAA7E5441
// Magic number specific to Opal
opalMagic = 0x88280BA1
// Version information
release = 1
iteration = 0
revision = 0
api = 1
)
var (
// The current instance - instances can be swapped out
// TODO create a package only handle set by the user
currentGem *Gem
)
func init() {
//version.Init(group, opalMagic, release, iteration, revision, api, "OPAL")
}
// Copy a Gem into the packages address to be used as the current service
func SwitchGem(pGem Gem) bool {
*currentGem = pGem
return true
}
// Retrieve a copy of Gem
func GetGem() Gem {
return *currentGem
}
// ******************************************** Data Access
// ActiveRecordDAO acts as a data provider for a Model's Entity.
// It is a limited version of the full Domain access object.
type ActiveRecordDAO interface {
// Takes a Model saves it as a new data-store entity and
// returns a result
Insert(pModel Model) Result
// Takes a Model, updates an existing entity from the
// data-store and returns a result
Save(pModel Model) Result
// Takes a Model and removes an existing entity from the
// data-store
Delete(pModel Model) Result
}
// ActiveRecordDAO acts as a data provider for a Model's Entity.
// It is a limited version of the full Domain access object.
type ActiveRecord interface {
// Takes a Model saves it as a new data-store entity and
// returns a result
Insert(pModel Model) Result
// Takes a Model, updates an existing entity from the
// data-store and returns a result
Save(pModel Model) Result
// Takes a Model and removes an existing entity from the
// data-store
Delete(pModel Model) Result
}
// ModelDAO acts a data provider for a Model's domain
// Methods are
type ModelDAO interface {
Opal
ActiveRecordDAO
// Find all models within the domain
FindAllModels() []Model
// Find a specific Model using its keys
FindModel(pKeys ...interface{}) Model
// Create a Sql Builder for the specified Model
SqlBuilder() *SqlBuilder
// Returns the ModelName associated with this instance
Model() ModelName
}
// ModelIDAO Implements ModelDAO
type ModelIDAO struct {
gem *Gem
model ModelName
}
func (o ModelIDAO) Model() ModelName {
return o.model
}
// Opal partially implements the opal OPAL interface
func (ModelIDAO) opal() OpalMagic {
return opal
}
// Opal partially implements the opal OPAL interface
func (ModelIDAO) Kind() reflect.Kind {
return DAO
}
// TODO betterway to handle DAO
func (o ModelIDAO) Gem() Gem {
return *o.gem
}
func (o ModelIDAO) FindAllModels() []Model {
meta := o.gem.allModelsMetadata[o.Model()]
// TODO what if lose connection
stmt := meta.preparedStatements[findAll]
rows, err := stmt.Query()
if err != nil {
log.Print(err)
return nil // TODO handle err
}
defer rows.Close()
var models []Model
for rows.Next() {
model, args := meta.ScanInto()
rows.Scan(args...)
models = append(models, model)
}
return models
}
// TODO better key solution
func (o ModelIDAO) FindModel(pKeys ...interface{}) Model {
meta := o.gem.allModelsMetadata[o.Model()]
stmt := meta.preparedStatements[find]
row := stmt.QueryRow(pKeys...)
model, args := meta.ScanInto()
err := row.Scan(args...)
if err != nil {
//TODO determine how errors should be handled
fmt.Println(err)
return nil
}
return model
}
func (o *ModelIDAO) SqlBuilder() *SqlBuilder {
meta := o.gem.allModelsMetadata[o.Model()]
builder := new(SqlBuilder)
builder.ModelMetadata = &meta
builder.Dialect = o.gem.Dialect
return builder
}
// TODO find why insert prepared statement does not work
// TODO fix persis
// gets a primary key unique constraint error
func (o *ModelIDAO) Insert(pModel Model) Result {
if o.gem.tx == nil {
// TODO remove?
builder := o.SqlBuilder().Insert().Values()
fPre, fPost := insertHooks(pModel)
if fPre != nil {
err := fPre()
if err != nil {
return Result{nil, err}
}
}
result, err := o.gem.Exec(builder, insertArgs(pModel)...)
if err != nil {
return Result{result, err}
}
// TODO dialect for Id
if id, err := result.LastInsertId(); err == nil {
// TODO compound key
v, ok := pModel.Keys()[0].(*AutoIncrement)
if ok {
v.A(id)
}
v2, ok := pModel.Keys()[0].(*Int64)
if ok {
v2.A(id)
}
}
if fPost != nil {
err := fPost()
if err != nil {
return Result{nil, err}
}
}
return Result{result, err}
}
return persist(o, pModel)
}
func (o *ModelIDAO) Save(pModel Model) Result {
return merge(o, pModel)
}
func (o *ModelIDAO) Delete(pModel Model) Result {
return remove(o, pModel)
}
func (o *ModelIDAO) ExecorStmt(pModel ModelName, pNamedStmt string) *sql.Stmt {
// TODO handle disconnections
stmt := o.gem.allModelsMetadata[pModel].preparedStatements[pNamedStmt]
if o.gem.tx == nil {
return stmt
}
return o.gem.tx.stmt(stmt)
}
// Future type for using when the opal sql has more of its own nuances
type OpalSql string
func (o OpalSql) String() string {
return string(o)
}
type Rows struct {
*sql.Rows // TODO explore embedded is wasteful?
}
type Sql interface {
String() string
}
type StartArgs struct {
BaseModel BaseModel
DB *sql.DB
Dialect Dialect
CreateEntity func(ModelName) Entity
Id *OpalMagic
}
func GEM(o StartArgs) *Gem {
// TODO panic on nil options
gem := new(Gem)
gem.Dialect = o.Dialect
gem.dao = &ModelIDAO{gem: gem}
gem.DB = o.DB
gem.funcCreateDomainEntity = o.CreateEntity
SetMagic(o.Id)
if gem.funcCreateDomainEntity == nil {
gem.funcCreateDomainEntity = NewEntity
}
models := o.BaseModel.Models()
gem.allModelsMetadata = make(map[ModelName]ModelMetadata, len(models))
gem.allModelsEntity = make(map[ModelName]*Entity, len(models))
gem.txPreparedStatements = make(map[*sql.Stmt]*sql.Stmt)
currentGem = gem
for _, face := range models {
model, ok := face.(Model)
if !ok {
panic("Opal.Start: You cannot pass a type which does not implement the Model interface.")
}
// TODO option for fuller path name
t := reflect.TypeOf(model).Elem()
// Create the ModelMetadata and gather the
// table and column information
meta := NewMetadata(model, t)
// Gather the metadata and save into the ModelMetadata holder
name, entity, modelDAOf := model.Gather(meta) // TODO somehow detach Gather from model and initialise another way
// Inject OpalDAOs into Model DAOs
// TODO report
modelDAO := modelDAOf(&ModelIDAO{gem, name})
// Add the ModelName to the map for retrieving metadata
gem.modelNames = append(gem.modelNames, modelDAO.Model())
gem.allModelsMetadata[modelDAO.Model()] = *meta
gem.allModelsEntity[modelDAO.Model()] = entity
// Save an entity instance into the provided address
*gem.allModelsEntity[modelDAO.Model()] = gem.funcCreateDomainEntity(modelDAO.Model())
// Generate prepared statements
builder := modelDAO.SqlBuilder()
// Create tables if necessary
table := builder.Create().Sql()
log.Printf("Opal.Start: Create table statement: %s", table.String())
gem.Exec(table)
// Add these first run
meta.addStmt(gem.DB, findAll, builder.Select().Sql())
meta.addStmt(gem.DB, find, builder.Select().WherePk().Sql())
meta.addStmt(gem.DB, insert, builder.Insert().Values().Sql())
meta.addStmt(gem.DB, update, builder.Update().WherePk().Sql())
meta.addStmt(gem.DB, delete, builder.Delete().WherePk().Sql())
}
return currentGem
}
// Base Model statement names
const (
find = "find"
findAll = "findAll"
insert = "insert"
update = "update"
delete = "delete"
)
type SqlBuilderDialectEncoder func(*SqlBuilder, DialectEncoder) *SqlBuilder
type ModifyDB (func(ModelName, Model) (Result, error))
// ********************************************* SPECIAL TYPES
type PreparedQuery interface {
}
type ModelQueries interface {
NamedQueries() []PreparedQuery
DerivedQueries() []PreparedQuery
}
type Validation interface {
}