-
Notifications
You must be signed in to change notification settings - Fork 8
/
gogm.go
167 lines (143 loc) · 4.28 KB
/
gogm.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
// MIT License
//
// Copyright (c) 2020 codingfinest
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package gogm
import (
"math"
"reflect"
"github.com/neo4j/neo4j-go-driver/neo4j"
)
const (
idPropertyName = "id"
labelsDelim = ":"
emptyString = ""
spaceString = " "
initialGraphID int64 = -1
infiniteDepth = -1
maxDepth = math.MaxInt32 / 2
defaultRelTypeDelim = "_"
indexDelim = ","
statementDelim = ";\n"
mapPropDelim = "."
)
const (
nodeCreateClause clause = iota
relationshipCreateClause
matchClause
setClause
deleteClause
)
const (
relatedGraph int = iota
labels
properties
)
var typeOfPublicNode = reflect.TypeOf(Node{})
var typeOfPublicRelationship = reflect.TypeOf(Relationship{})
var typeOfPrivateNode = reflect.TypeOf(&node{})
var typeOfPrivateRelationship = reflect.TypeOf(&relationship{})
var typeOfNodeMetadata = reflect.TypeOf(&nodeMetadata{})
var invalidValue = reflect.ValueOf(nil)
var directionTags = map[string]direction{
"<-": incoming,
"->": outgoing,
"--": undirected}
var clauseGroups = [5]clause{
matchClause,
nodeCreateClause,
relationshipCreateClause,
setClause,
deleteClause}
type clause int
type clauses map[clause][]string
type LogLevel int
const (
NONE LogLevel = 0
ERROR = 1
WARNING = 2
INFO = 3
DEBUG = 4
)
var logLevels = map[LogLevel]neo4j.LogLevel{
ERROR: neo4j.ERROR,
WARNING: neo4j.WARNING,
INFO: neo4j.INFO,
DEBUG: neo4j.DEBUG,
}
//Gogm is an instance of the OGM
type Gogm struct {
config *Config
driver neo4j.Driver
}
//New creates a new instance of the OGM
func New(config *Config) *Gogm {
return &Gogm{
config,
nil}
}
//NewSession creates a new session on an OGM instance
func (g *Gogm) NewSession(isWriteMode bool) (Session, error) {
var err error
var accessMode neo4j.AccessMode = neo4j.AccessModeRead
if isWriteMode {
accessMode = neo4j.AccessModeWrite
}
if g.driver == nil {
if g.driver, err = getDriver(g.config.URI, g.config.Username, g.config.Password, g.config.LogLevel); err != nil {
return nil, err
}
}
cypherExecutor := newCypherExecuter(g.driver, accessMode, nil)
registry := newRegistry(*cypherExecutor)
graphFactory := newGraphFactory(registry)
transactioner := newTransactioner(accessMode)
eventer := newEventer()
store := newstore(registry)
saver := newSaver(cypherExecutor, store, *eventer, registry, *graphFactory)
loader := newLoader(cypherExecutor, store, *eventer, registry, *graphFactory, g.config.AllowCyclicRef)
deleter := newDeleter(cypherExecutor, store, *eventer, registry, *graphFactory)
queryer := newQueryer(cypherExecutor, *graphFactory, registry)
return &sessionImpl{
cypherExecutor,
saver,
loader,
deleter,
queryer,
transactioner,
store,
registry,
g.driver,
eventer}, nil
}
func getDriver(uri string, username string, password string, logLevel LogLevel) (neo4j.Driver, error) {
var (
err error
driver neo4j.Driver
)
if driver, err = neo4j.NewDriver(uri, neo4j.BasicAuth(username, password, ""), func(config *neo4j.Config) {
if logLevel != NONE {
config.Log = neo4j.ConsoleLogger(logLevels[logLevel])
}
}); err != nil {
return nil, err
}
return driver, err
}