forked from bgentry/gls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gls.go
207 lines (181 loc) · 4.76 KB
/
gls.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
package gls
import (
"database/sql"
"fmt"
_ "github.com/bmizerany/pq"
"gls/logger"
"io"
"reflect"
"strings"
"time"
)
func OpenDB(connString string) (dbRef *sql.DB, err error) {
dbRef, err = sql.Open("postgres", connString)
if err != nil {
logger.LogEvent("fatal", "database_connection_error", "error", err.Error())
}
return
}
type pgTable struct {
parent *LockstepServer
name string
types map[string]reflect.Type
loaded bool
}
type LockstepServer struct {
db *sql.DB
tables map[string]*pgTable
}
func (l *LockstepServer) Stream(w io.Writer, tableName string) error {
finished := make(chan bool)
c, err := l.Query(tableName, finished)
if err != nil {
return err
}
for s := range c {
_, err = w.Write([]byte(s["name"].(string)))
if err != nil {
finished <- true
return err
}
}
return nil
}
func (l *LockstepServer) Query(tableName string, finished chan bool) (chan map[string]interface{}, error) {
if l.tables[tableName] == nil {
return nil, fmt.Errorf("invalid tableName: %q", tableName)
}
t := l.tables[tableName]
if !t.loaded {
// Table schema is not loaded, we need it before we can query
err := t.loadSchema()
if err != nil {
return nil, err
}
}
c := make(chan map[string]interface{})
go func(t *pgTable, c chan map[string]interface{}) {
defer close(c)
rows, err := t.startLockstepQuery()
if err != nil {
// no good way to send this error back?
fmt.Printf("Error in startLockstepQuery: %v\n", err)
return
}
// Figure out columns
cols, _ := rows.Columns()
for i, _ := range cols {
cols[i] = strings.ToLower(cols[i])
}
res := make(map[string]interface{}, len(cols))
var fargs []interface{}
for _, name := range cols {
if name == "txid_snapshot_xmin" {
res[name] = new(int64)
fargs = append(fargs, res[name])
} else {
res[name] = newValueFor(t.types[name])
fargs = append(fargs, res[name])
}
}
for rows.Next() {
err := rows.Scan(fargs...)
if err != nil {
// no good way to send this error back?
fmt.Printf("Error in Scan: %v\n", err)
return
}
for i, name := range cols {
switch fargs[i].(type) {
case **int64:
res[name] = **(reflect.ValueOf(fargs[i]).Interface().(**int64))
case **string:
res[name] = **(reflect.ValueOf(fargs[i]).Interface().(**string))
case **bool:
res[name] = **(reflect.ValueOf(fargs[i]).Interface().(**bool))
}
}
c <- res //name
}
}(t, c)
return c, nil
}
func newValueFor(k reflect.Type) interface{} {
return reflect.New(k).Interface()
}
func (l *LockstepServer) loadTables() error {
l.tables = make(map[string]*pgTable)
names, err := getTables(l.db)
if err != nil {
return err
}
for _, name := range names {
l.tables[name] = &pgTable{parent: l, name: name}
}
for _, t := range l.tables {
err = t.loadSchema()
if err != nil {
return err
}
}
return nil
}
func (t *pgTable) loadSchema() error {
r, err := describeTable(t.parent.db, t.name)
if err != nil {
return err
}
t.types = r
return nil
}
func getTables(db *sql.DB) ([]string, error) {
var tables []string
var table string
rows, err := db.Query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'")
if err != nil {
return nil, err
}
for rows.Next() {
rows.Scan(&table)
tables = append(tables, table)
}
return tables, nil
}
func describeTable(db *sql.DB, name string) (map[string]reflect.Type, error) {
rows, err := db.Query(fmt.Sprintf("SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '%s'", name))
if err != nil {
return nil, err
}
types := make(map[string]reflect.Type)
var colName, dtype string
for rows.Next() {
err := rows.Scan(&colName, &dtype)
if err != nil {
return nil, fmt.Errorf("Error describing table %s: %v", name, err.Error())
}
types[strings.ToLower(colName)] = getType(dtype)
}
return types, nil
}
func getType(pgtype string) reflect.Type {
switch pgtype {
case "character", "character varying", "text":
return reflect.TypeOf(new(string))
case "smallint", "integer", "bigint", "serial", "bigserial":
return reflect.TypeOf(new(int64))
case "boolean":
return reflect.TypeOf(new(bool))
// don't know how to deal w/ time..
case "time without time zone", "time with time zone", "timestamp without time zone", "timestamp with time zone":
return reflect.TypeOf(&time.Time{})
default:
fmt.Printf("Unknown type: %s\n", pgtype)
}
return reflect.TypeOf(new(string))
}
func startLockstepQuery(db *sql.DB, tableName string) (*sql.Rows, error) {
return db.Query(fmt.Sprintf("SELECT txid_snapshot_xmin(txid_current_snapshot()), * FROM %s", tableName))
}
func (t *pgTable) startLockstepQuery() (*sql.Rows, error) {
return t.parent.db.Query(fmt.Sprintf("SELECT txid_snapshot_xmin(txid_current_snapshot()), * FROM %s", t.name))
}