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
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 age
import (
"bytes"
"database/sql"
"fmt"
"reflect"
)
// GetReady prepare AGE extension
// load AGE extension
// set graph path
func GetReady(db *sql.DB, graphName string) (bool, error) {
tx, err := 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", graphName).Scan(&count)
if err != nil {
return false, err
}
if count == 0 {
_, err = tx.Exec("SELECT create_graph($1);", graphName)
if err != nil {
return false, err
}
}
tx.Commit()
return true, nil
}
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
}
}
// ExecCypher : execute cypher query
// CREATE , DROP ....
// MATCH .... RETURN ....
// CREATE , DROP .... RETURN ...
func ExecCypher(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
}
// ExecCypherMap
// CREATE , DROP ....
// MATCH .... RETURN ....
// CREATE , DROP .... RETURN ...
func ExecCypherMap(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
}
/**
@param dsn host=127.0.0.1 port=5432 dbname=postgres user=postgres password=agens sslmode=disable
*/
func ConnectAge(graphName string, dsn string) (*Age, error) {
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, err
}
age := &Age{db: db, graphName: graphName}
_, err = age.GetReady()
if err != nil {
db.Close()
age = nil
}
return age, err
}
func NewAge(graphName string, db *sql.DB) *Age {
return &Age{db: db, graphName: graphName}
}
func (age *Age) GetReady() (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() (*AgeTx, 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) ExecCypher(columnCount int, cypher string, args ...interface{}) (*CypherCursor, error) {
return ExecCypher(a.tx, a.age.graphName, columnCount, cypher, args...)
}
func (a *AgeTx) ExecCypherMap(columnCount int, cypher string, args ...interface{}) (*CypherMapCursor, error) {
return ExecCypherMap(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 *AGMapper
}
func NewCypherMapCursor(columnCount int, rows *sql.Rows) Cursor {
mapper := NewAGMapper(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
}
|