@@ -11,16 +11,30 @@ import (
1111// TYPES
1212
1313type sqpreparefunc func (* Class , SQTransaction ) SQStatement
14+ type stkey uint
1415
1516///////////////////////////////////////////////////////////////////////////////
1617// GLOBALS
1718
19+ const (
20+ SQKeyNone stkey = iota
21+ SQKeySelect
22+ SQKeyInsert
23+ SQKeyDeleteRows
24+ SQKeyDeleteKeys
25+ SQKeyUpdateKeys
26+ SQKeyUpsertKeys
27+ SQKeyMax = SQKeyUpdateKeys
28+ )
29+
1830var (
19- statements = map [SQKey ]sqpreparefunc {
31+ statements = map [stkey ]sqpreparefunc {
2032 SQKeySelect : sqSelect ,
2133 SQKeyInsert : sqInsert ,
2234 SQKeyDeleteRows : sqDeleteRows ,
2335 SQKeyDeleteKeys : sqDeleteKeys ,
36+ SQKeyUpdateKeys : sqUpdateKeys ,
37+ SQKeyUpsertKeys : sqUpsertKeys ,
2438 }
2539)
2640
@@ -59,99 +73,27 @@ func sqDeleteKeys(class *Class, _ SQTransaction) SQStatement {
5973 return class .SQSource .Delete (cols ... )
6074}
6175
62- /*
63-
64- func (this *sqclass) addStatements(flags SQFlag) error {
65- this.RWMutex.Lock()
66- defer this.RWMutex.Unlock()
67-
68- // Create statments for create,insert and delete
69- this.addStatement(SQKeyCreate, this.sqCreate())
70- this.addStatement(SQKeyWrite, this.sqInsert(flags))
71- this.addStatement(SQKeyRead, this.sqSelect())
72-
73- // If we have primary keys, other operations are possible
74- if len(this.PrimaryColumnNames()) > 0 {
75- this.addStatement(SQKeyDelete, this.sqDelete())
76- this.addStatement(SQKeyGetRowId, this.sqGetRowId())
77- }
78-
79- // Create index statements
80- for _, index := range this.indexes {
81- this.addStatement(SQKeyCreate, this.sqIndex(index))
82- }
83-
84- // Return success
85- return nil
86- }
87-
88- func (this *sqclass) addStatement(key SQKey, st SQStatement) {
89- this.s[key] = append(this.s[key], st)
90- }
91-
92- func (this *sqclass) sqCreate() SQTable {
93- st := this.CreateTable(this.Columns()...).IfNotExists()
94- for _, column := range this.columns {
95- if column.Unique {
96- st = st.WithUnique(column.Field.Name)
97- } else if column.Index {
98- st = st.WithIndex(column.Field.Name)
76+ func sqUpdateKeys (class * Class , _ SQTransaction ) SQStatement {
77+ values := make ([]string , 0 , len (class .col ))
78+ keys := make ([]interface {}, 0 , len (class .col ))
79+ for _ , c := range class .col {
80+ if ! c .Primary {
81+ values = append (values , c .Col .Name ())
82+ } else {
83+ keys = append (keys , Q (N (c .Col .Name ()), "=" , P ))
9984 }
10085 }
101- return st
102- }
103-
104- func (this *sqclass) sqIndex(index *sqindex) SQStatement {
105- st := N(this.Name()+"_"+index.name).
106- WithSchema(this.Schema()).
107- CreateIndex(this.Name(), index.cols...).IfNotExists()
108- if index.unique {
109- st = st.WithUnique()
110- }
111- return st
86+ return class .SQSource .Update (values ... ).Where (keys ... )
11287}
11388
114- func (this *sqclass) sqInsert(flags SQFlag) SQStatement {
115- st := this.Insert(this.ColumnNames()...)
116-
117- // Add conflict resolution for any primary key field
118- st = st.WithConflictUpdate(this.PrimaryColumnNames()...)
119-
120- // Add conflict resolution for any unique fields
121- for _, column := range this.columns {
122- if column.Unique && flags&SQLITE_FLAG_UPDATEONINSERT != 0 {
123- st = st.WithConflictUpdate(column.Field.Name)
124- }
125- }
126-
127- // Add conflict for any unique indexes
128- for _, index := range this.indexes {
129- if index.unique && flags&SQLITE_FLAG_UPDATEONINSERT != 0 {
130- st = st.WithConflictUpdate(index.cols...)
89+ func sqUpsertKeys (class * Class , _ SQTransaction ) SQStatement {
90+ cols := make ([]string , len (class .col ))
91+ keys := make ([]string , 0 , len (class .col ))
92+ for i , col := range class .col {
93+ cols [i ] = col .Col .Name ()
94+ if col .Primary {
95+ keys = append (keys , col .Col .Name ())
13196 }
13297 }
133-
134- // Return success
135- return st
136- }
137-
138- func (this *sqclass) sqDelete() SQStatement {
139- expr := []interface{}{}
140- for _, name := range this.PrimaryColumnNames() {
141- expr = append(expr, Q(N(name), "=", P))
142- }
143- return this.Delete(expr...)
144- }
145-
146- func (this *sqclass) sqGetRowId() SQStatement {
147- expr := []interface{}{}
148- for _, name := range this.PrimaryColumnNames() {
149- expr = append(expr, Q(N(name), "=", P))
150- }
151- return S(this.SQSource).To(N("rowid")).Where(expr...)
152- }
153-
154- func (this *sqclass) sqSelect() SQStatement {
155- return S(this.SQSource).To(this.ColumnSources()...)
98+ return class .SQSource .Insert (cols ... ).WithConflictUpdate (keys ... )
15699}
157- */
0 commit comments