-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.go
More file actions
282 lines (230 loc) · 6.02 KB
/
driver.go
File metadata and controls
282 lines (230 loc) · 6.02 KB
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
package age
import (
"bytes"
"database/sql"
"fmt"
"reflect"
_ "github.com/lib/pq"
)
type Tx interface {
Commit() error
Rollback() error
Exec(columnCount int, cypher string, args ...interface{}) (*CypherCursor, error)
ExecMap(columnCount int, cypher string, args ...interface{}) (*CypherMapCursor, error)
}
type Driver interface {
Prepare() (bool, error)
Close() error
DB() *sql.DB
Begin() (Tx, error)
}
type CursorProvider func(columnCount int, rows *sql.Rows) Cursor
type Cursor interface {
Next() bool
Close() error
}
func execCypher(cursorProvider CursorProvider, tx *sql.Tx, graphName string, columnCount int, cypher string, args ...interface{}) (Cursor, error) {
var buf bytes.Buffer
cypherStmt := fmt.Sprintf(cypher, args...)
buf.WriteString("SELECT * from cypher(NULL,NULL) as (v0 agtype")
for i := 1; i < columnCount; i++ {
buf.WriteString(fmt.Sprintf(", v%d agtype", i))
}
buf.WriteString(");")
stmt := buf.String()
// Pass in the graph name and cypher statement via parameters to prepare
// the cypher function call for session info.
prepare_stmt := "SELECT * FROM age_prepare_cypher($1, $2);"
_, perr := tx.Exec(prepare_stmt, graphName, cypherStmt)
if perr != nil {
fmt.Println(prepare_stmt + " " + graphName + " " + cypher)
return nil, perr
}
if columnCount == 0 {
_, err := tx.Exec(stmt)
if err != nil {
fmt.Println(stmt)
return nil, err
}
return nil, nil
} else {
rows, err := tx.Query(stmt)
if err != nil {
fmt.Println(stmt)
return nil, err
}
return cursorProvider(columnCount, rows), nil
}
}
// Exec : execute cypher query
// CREATE , DROP ....
// MATCH .... RETURN ....
// CREATE , DROP .... RETURN ...
func exec(tx *sql.Tx, graphName string, columnCount int, cypher string, args ...interface{}) (*CypherCursor, error) {
cursor, err := execCypher(NewCypherCursor, tx, graphName, columnCount, cypher, args...)
var cypherCursor *CypherCursor
if cursor != nil {
cypherCursor = cursor.(*CypherCursor)
}
return cypherCursor, err
}
// ExecMap
// CREATE , DROP ....
// MATCH .... RETURN ....
// CREATE , DROP .... RETURN ...
func execMap(tx *sql.Tx, graphName string, columnCount int, cypher string, args ...interface{}) (*CypherMapCursor, error) {
cursor, err := execCypher(NewCypherMapCursor, tx, graphName, columnCount, cypher, args...)
var cypherMapCursor *CypherMapCursor
if cursor != nil {
cypherMapCursor = cursor.(*CypherMapCursor)
}
return cypherMapCursor, err
}
type age struct {
db *sql.DB
graphName string
}
type ageTx struct {
age *age
tx *sql.Tx
}
type Config struct {
GraphName string
Db *sql.DB
Dsn string
}
func New(cnf Config) Driver {
if cnf.Db == nil && cnf.Dsn != "" {
db, err := sql.Open("postgres", cnf.Dsn)
if err != nil {
panic(err)
}
cnf.Db = db
}
if cnf.Db == nil {
panic("age: Database connection is required")
}
if cnf.GraphName == "" {
panic("age: Graph name is required")
}
return &age{db: cnf.Db, graphName: cnf.GraphName}
}
func (age *age) Prepare() (bool, error) {
tx, err := age.db.Begin()
if err != nil {
return false, err
}
_, err = tx.Exec("LOAD 'age';")
if err != nil {
return false, err
}
_, err = tx.Exec("SET search_path = ag_catalog, '$user', public;")
if err != nil {
return false, err
}
var count int = 0
err = tx.QueryRow("SELECT count(*) FROM ag_graph WHERE name=$1", age.graphName).Scan(&count)
if err != nil {
return false, err
}
if count == 0 {
_, err = tx.Exec("SELECT create_graph($1);", age.graphName)
if err != nil {
return false, err
}
}
tx.Commit()
return true, nil
}
func (a *age) Close() error {
return a.db.Close()
}
func (a *age) DB() *sql.DB {
return a.db
}
func (a *age) Begin() (Tx, error) {
ageTx := &ageTx{age: a}
tx, err := a.db.Begin()
if err != nil {
return nil, err
}
ageTx.tx = tx
return ageTx, err
}
func (t *ageTx) Commit() error {
return t.tx.Commit()
}
func (t *ageTx) Rollback() error {
return t.tx.Rollback()
}
/** CREATE , DROP .... */
func (a *ageTx) Exec(columnCount int, cypher string, args ...interface{}) (*CypherCursor, error) {
return exec(a.tx, a.age.graphName, columnCount, cypher, args...)
}
func (a *ageTx) ExecMap(columnCount int, cypher string, args ...interface{}) (*CypherMapCursor, error) {
return execMap(a.tx, a.age.graphName, columnCount, cypher, args...)
}
type CypherCursor struct {
Cursor
columnCount int
rows *sql.Rows
unmarshaler Unmarshaller
}
func NewCypherCursor(columnCount int, rows *sql.Rows) Cursor {
return &CypherCursor{columnCount: columnCount, rows: rows, unmarshaler: NewAGUnmarshaler()}
}
func (c *CypherCursor) Next() bool {
return c.rows.Next()
}
func (c *CypherCursor) GetRow() ([]Entity, error) {
var gstrs = make([]interface{}, c.columnCount)
for i := 0; i < c.columnCount; i++ {
gstrs[i] = new(string)
}
err := c.rows.Scan(gstrs...)
if err != nil {
return nil, fmt.Errorf("CypherCursor.GetRow:: %s", err)
}
entArr := make([]Entity, c.columnCount)
for i := 0; i < c.columnCount; i++ {
gstr := gstrs[i].(*string)
e, err := c.unmarshaler.Unmarshal(*gstr)
if err != nil {
fmt.Println(i, ">>", gstr)
return nil, err
}
entArr[i] = e
}
return entArr, nil
}
func (c *CypherCursor) Close() error {
return c.rows.Close()
}
type CypherMapCursor struct {
CypherCursor
mapper *Mapper
}
func NewCypherMapCursor(columnCount int, rows *sql.Rows) Cursor {
mapper := NewMapper(make(map[string]reflect.Type))
pcursor := CypherCursor{columnCount: columnCount, rows: rows, unmarshaler: mapper}
return &CypherMapCursor{CypherCursor: pcursor, mapper: mapper}
}
func (c *CypherMapCursor) PutType(label string, tp reflect.Type) {
c.mapper.PutType(label, tp)
}
func (c *CypherMapCursor) GetRow() ([]interface{}, error) {
entities, err := c.CypherCursor.GetRow()
if err != nil {
return nil, fmt.Errorf("CypherMapCursor.GetRow:: %s", err)
}
elArr := make([]interface{}, c.columnCount)
for i := 0; i < c.columnCount; i++ {
ent := entities[i]
if ent.GType() == G_MAP_PATH {
elArr[i] = ent
} else {
elArr[i] = ent.(*SimpleEntity).Value()
}
}
return elArr, nil
}