This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tables.go
183 lines (153 loc) · 3.92 KB
/
tables.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
/*
Copyright 2016 The Transicator Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"database/sql"
"fmt"
"strings"
"github.com/apigee-labs/transicator/common"
)
type tableInfo struct {
allColumns []string
primaryKeys []string
otherColumns []string
insert *sql.Stmt
update *sql.Stmt
delete *sql.Stmt
}
func scanTableInfo(db *sql.DB) (tables map[string]*tableInfo, err error) {
var rows *sql.Rows
rows, err = db.Query(`
select tableName, columnName, primaryKey from _transicator_tables
`)
if err == nil {
tables = make(map[string]*tableInfo)
defer rows.Close()
for rows.Next() {
var tn, cn string
var primary bool
err = rows.Scan(&tn, &cn, &primary)
if err != nil {
return
}
tps := strings.Split(tn, ".")
tn = tps[len(tps)-1]
table := tables[tn]
if table == nil {
table = &tableInfo{}
tables[tn] = table
}
table.allColumns = append(table.allColumns, cn)
if primary {
table.primaryKeys = append(table.primaryKeys, cn)
} else {
table.otherColumns = append(table.otherColumns, cn)
}
}
}
if err == nil {
for tn, table := range tables {
err = makeTableSQL(db, tn, table)
if err != nil {
return
}
}
}
return
}
func makeTableSQL(db *sql.DB, tn string, table *tableInfo) (err error) {
buf := &bytes.Buffer{}
buf.WriteString("insert into ")
buf.WriteString(tn)
buf.WriteString(" (")
buf.WriteString(strings.Join(table.allColumns, ", "))
buf.WriteString(") values(?")
buf.WriteString(strings.Repeat(", ?", len(table.allColumns)-1))
buf.WriteString(")")
fmt.Printf("Insert: %s\n", buf.String())
table.insert, err = db.Prepare(buf.String())
if err != nil {
return
}
buf = &bytes.Buffer{}
buf.WriteString("update ")
buf.WriteString(tn)
buf.WriteString(" set ")
for i, cn := range table.otherColumns {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(cn)
buf.WriteString(" = ?")
}
writePrimaryWhere(table, buf)
fmt.Printf("Update: %s\n", buf.String())
table.update, err = db.Prepare(buf.String())
if err != nil {
return
}
buf = &bytes.Buffer{}
buf.WriteString("delete from ")
buf.WriteString(tn)
writePrimaryWhere(table, buf)
fmt.Printf("Delete: %s\n", buf.String())
table.delete, err = db.Prepare(buf.String())
return
}
func writePrimaryWhere(table *tableInfo, buf *bytes.Buffer) {
buf.WriteString(" where ")
for i, pk := range table.primaryKeys {
if i > 0 {
buf.WriteString(" and ")
}
buf.WriteString(pk)
buf.WriteString(" = ?")
}
}
func (t *tableInfo) applyInsert(row common.Row) error {
var args []interface{}
// Insert SQL takes all columns in a row
for _, cn := range t.allColumns {
args = append(args, argOrNil(row, cn))
}
_, err := t.insert.Exec(args...)
return err
}
func (t *tableInfo) applyUpdate(row common.Row) error {
var args []interface{}
// Update SQL takes all non-primary key cols, then primary key
for _, cn := range t.otherColumns {
args = append(args, argOrNil(row, cn))
}
for _, cn := range t.primaryKeys {
args = append(args, argOrNil(row, cn))
}
_, err := t.update.Exec(args...)
return err
}
func (t *tableInfo) applyDelete(row common.Row) error {
var args []interface{}
// Delete just uses primary keys
for _, cn := range t.primaryKeys {
args = append(args, argOrNil(row, cn))
}
_, err := t.delete.Exec(args...)
return err
}
func argOrNil(row common.Row, n string) interface{} {
if row[n] == nil {
return nil
}
return row[n].Value
}