neo4j-core 3.0.0.alpha.13 → 3.0.0.alpha.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +60 -49
  3. data/lib/mydb/active_tx_log +1 -0
  4. data/lib/mydb/index/lucene-store.db +0 -0
  5. data/lib/mydb/index/lucene.log.1 +0 -0
  6. data/lib/mydb/index/lucene.log.active +0 -0
  7. data/lib/mydb/lock +0 -0
  8. data/lib/mydb/messages.log +313 -0
  9. data/lib/mydb/neostore +0 -0
  10. data/lib/mydb/neostore.id +0 -0
  11. data/lib/mydb/neostore.labeltokenstore.db +0 -0
  12. data/lib/mydb/neostore.labeltokenstore.db.id +0 -0
  13. data/lib/mydb/neostore.labeltokenstore.db.names +0 -0
  14. data/lib/mydb/neostore.labeltokenstore.db.names.id +0 -0
  15. data/lib/mydb/neostore.nodestore.db +0 -0
  16. data/lib/mydb/neostore.nodestore.db.id +0 -0
  17. data/lib/mydb/neostore.nodestore.db.labels +0 -0
  18. data/lib/mydb/neostore.nodestore.db.labels.id +0 -0
  19. data/lib/mydb/neostore.propertystore.db +0 -0
  20. data/lib/mydb/neostore.propertystore.db.arrays +0 -0
  21. data/lib/mydb/neostore.propertystore.db.arrays.id +0 -0
  22. data/lib/mydb/neostore.propertystore.db.id +0 -0
  23. data/lib/mydb/neostore.propertystore.db.index +0 -0
  24. data/lib/mydb/neostore.propertystore.db.index.id +0 -0
  25. data/lib/mydb/neostore.propertystore.db.index.keys +0 -0
  26. data/lib/mydb/neostore.propertystore.db.index.keys.id +0 -0
  27. data/lib/mydb/neostore.propertystore.db.strings +0 -0
  28. data/lib/mydb/neostore.propertystore.db.strings.id +0 -0
  29. data/lib/mydb/neostore.relationshipstore.db +0 -0
  30. data/lib/mydb/neostore.relationshipstore.db.id +0 -0
  31. data/lib/mydb/neostore.relationshiptypestore.db +0 -0
  32. data/lib/mydb/neostore.relationshiptypestore.db.id +0 -0
  33. data/lib/mydb/neostore.relationshiptypestore.db.names +0 -0
  34. data/lib/mydb/neostore.relationshiptypestore.db.names.id +0 -0
  35. data/lib/mydb/neostore.schemastore.db +0 -0
  36. data/lib/mydb/neostore.schemastore.db.id +0 -0
  37. data/lib/mydb/nioneo_logical.log.1 +0 -0
  38. data/lib/mydb/nioneo_logical.log.active +0 -0
  39. data/lib/mydb/schema/label/lucene/write.lock +0 -0
  40. data/lib/mydb/store_lock +0 -0
  41. data/lib/mydb/tm_tx_log.1 +0 -0
  42. data/lib/neo4j-core.rb +1 -2
  43. data/lib/neo4j-core/query_builder.rb +200 -0
  44. data/lib/neo4j-core/version.rb +1 -1
  45. data/lib/neo4j-embedded.rb +1 -0
  46. data/lib/neo4j-embedded/cypher_response.rb +89 -0
  47. data/lib/neo4j-embedded/cypher_response.rb~ +85 -0
  48. data/lib/neo4j-embedded/embedded_node.rb +12 -0
  49. data/lib/neo4j-embedded/embedded_node.rb~ +201 -0
  50. data/lib/neo4j-embedded/embedded_relationship.rb +8 -0
  51. data/lib/neo4j-embedded/embedded_relationship.rb~ +62 -0
  52. data/lib/neo4j-embedded/embedded_session.rb +20 -12
  53. data/lib/neo4j-embedded/embedded_session.rb~ +145 -0
  54. data/lib/neo4j-server/cypher_relationship.rb +6 -11
  55. data/lib/neo4j-server/cypher_response.rb +43 -5
  56. data/lib/neo4j-server/cypher_response.rb~ +155 -0
  57. data/lib/neo4j-server/cypher_session.rb +54 -19
  58. data/lib/neo4j/label.rb +7 -51
  59. data/lib/neo4j/node.rb +21 -19
  60. data/lib/neo4j/relationship.rb +7 -7
  61. data/lib/neo4j/session.rb +70 -28
  62. data/lib/neo4j/session.rb~ +202 -0
  63. data/neo4j-core.gemspec +0 -1
  64. metadata +49 -16
data/lib/neo4j/node.rb CHANGED
@@ -20,20 +20,20 @@ module Neo4j
20
20
  include Wrapper
21
21
  include PropertyContainer
22
22
 
23
- # @return [Hash] all properties of the node
23
+ # @return [Hash<Symbol, Object>] all properties of the node
24
24
  def props()
25
25
  raise 'not implemented'
26
26
  end
27
27
 
28
28
  # replace all properties with new properties
29
- # @param [Hash] hash a hash of properties the node should have
30
- def props=(hash)
29
+ # @param [Hash<Symbol, Object>] properties a hash of properties the node should have
30
+ def props=(properties)
31
31
  raise 'not implemented'
32
32
  end
33
33
 
34
34
  # Updates the properties, keeps old properties
35
- # @param [Hash] hash hash of properties that should be updated on the node
36
- def update_props(hash)
35
+ # @param [Hash<Symbol, Object>] properties hash of properties that should be updated on the node
36
+ def update_props(properties)
37
37
  raise 'not implemented'
38
38
  end
39
39
 
@@ -43,14 +43,14 @@ module Neo4j
43
43
  end
44
44
 
45
45
  # Directly set the property on the node (low level method, may need transaction)
46
- # @param [Hash, String] key
46
+ # @param [Symbol, String] key
47
47
  # @param value see Neo4j::PropertyValidator::VALID_PROPERTY_VALUE_CLASSES for valid values
48
48
  def set_property(key, value)
49
49
  raise 'not implemented'
50
50
  end
51
51
 
52
52
  # Directly get the property on the node (low level method, may need transaction)
53
- # @param [Hash, String] key
53
+ # @param [Symbol, String] key
54
54
  # @return the value of the key
55
55
  def get_property(key, value)
56
56
  raise 'not implemented'
@@ -59,7 +59,7 @@ module Neo4j
59
59
  # Creates a relationship of given type to other_node with optionally properties
60
60
  # @param [Symbol] type the type of the relation between the two nodes
61
61
  # @param [Neo4j::Node] other_node the other node
62
- # @param [Hash] props optionally properties for the created relationship
62
+ # @param [Hash<Symbol, Object>] props optionally properties for the created relationship
63
63
  def create_rel(type, other_node, props = nil)
64
64
  raise 'not implemented'
65
65
  end
@@ -68,11 +68,11 @@ module Neo4j
68
68
  # Returns an enumeration of relationships.
69
69
  # It always returns relationships of depth one.
70
70
  #
71
- # @param [Hash] opts the options to create a message with.
72
- # @option opts [Symbol] :dir dir the direction of the relationship, allowed values: :both, :incoming, :outgoing.
73
- # @option opts [Symbol] :type the type of relationship to navigate
74
- # @option opts [Symbol] :between return all the relationships between this and given node
75
- # @return [Enumerable] of Neo4j::Relationship objects
71
+ # @param [Hash] match the options to create a message with.
72
+ # @option match [Symbol] :dir dir the direction of the relationship, allowed values: :both, :incoming, :outgoing.
73
+ # @option match [Symbol] :type the type of relationship to navigate
74
+ # @option match [Symbol] :between return all the relationships between this and given node
75
+ # @return [Enumerable<Neo4j::Relationship>] of Neo4j::Relationship objects
76
76
  #
77
77
  # @example Return both incoming and outgoing relationships of any type
78
78
  # node_a.rels
@@ -88,11 +88,13 @@ module Neo4j
88
88
  end
89
89
 
90
90
  # Adds one or more Neo4j labels on the node
91
+ # @param [Array<Symbol>] labels one or more labels to add
91
92
  def add_label(*labels)
92
93
  raise 'not implemented'
93
94
  end
94
95
 
95
96
  # Sets label on the node. Any old labels will be removed
97
+ # @param [Array<Symbol>] labels one or more labels to set
96
98
  def set_label(*labels)
97
99
  raise 'not implemented'
98
100
  end
@@ -103,7 +105,7 @@ module Neo4j
103
105
  end
104
106
 
105
107
  #
106
- # @return all labels on the node
108
+ # @return [Array<Symbol>]all labels on the node
107
109
  def labels()
108
110
  raise 'not implemented'
109
111
  end
@@ -118,7 +120,7 @@ module Neo4j
118
120
  raise 'not implemented'
119
121
  end
120
122
 
121
- # @returns all the Neo4j labels for this node
123
+ # @return all the Neo4j labels for this node
122
124
  def labels
123
125
  raise 'not implemented'
124
126
  end
@@ -150,12 +152,12 @@ module Neo4j
150
152
  end
151
153
 
152
154
  # Returns true or false if there is one or more relationships
153
- # Same as `!! #rel()`
155
+ # @return [Boolean]
154
156
  def rel?(spec = {})
155
157
  raise 'not implemented'
156
158
  end
157
159
 
158
- # Same as Neo4j::Node#exist?
160
+ # @return [Boolean] true if the node exists
159
161
  def exist?
160
162
  raise 'not implemented'
161
163
  end
@@ -164,8 +166,8 @@ module Neo4j
164
166
  # It does try to load a Ruby wrapper around each node
165
167
  # @abstract
166
168
  # @param (see #rels)
167
- # @return [Enumerable] an Enumeration of either Neo4j::Node objects or wrapped Neo4j::Node objects
168
- # @notice it's possible that the same node is returned more then once because of several relationship reaching to the same node, see #outgoing for alternative
169
+ # @return [Enumerable<Neo4j::Node>] an Enumeration of either Neo4j::Node objects or wrapped Neo4j::Node objects
170
+ # @note it's possible that the same node is returned more than once because of several relationship reaching to the same node, see #outgoing for alternative
169
171
  def nodes(specs = {})
170
172
  #rels(specs).map{|n| n.other_node(self)}
171
173
  end
@@ -27,20 +27,20 @@ module Neo4j
27
27
  include EntityEquality
28
28
  include Wrapper
29
29
 
30
- # @return [Hash] all properties of the relationship
30
+ # @return [Hash<Symbol,Object>] all properties of the relationship
31
31
  def props()
32
32
  raise 'not implemented'
33
33
  end
34
34
 
35
35
  # replace all properties with new properties
36
- # @param [Hash] hash a hash of properties the relationship should have
37
- def props=(hash)
36
+ # @param [Hash] properties a hash of properties the relationship should have
37
+ def props=(properties)
38
38
  raise 'not implemented'
39
39
  end
40
40
 
41
41
  # Updates the properties, keeps old properties
42
- # @param [Hash] hash hash of properties that should be updated on the relationship
43
- def update_props(hash)
42
+ # @param [Hash<Symbol,Object>] properties hash of properties that should be updated on the relationship
43
+ def update_props(properties)
44
44
  raise 'not implemented'
45
45
  end
46
46
 
@@ -70,7 +70,7 @@ module Neo4j
70
70
  end
71
71
 
72
72
  # Same as #start_node but does not wrap the node
73
- # @returns [Neo4j::Node]
73
+ # @return [Neo4j::Node]
74
74
  def _start_node
75
75
  raise 'not implemented'
76
76
  end
@@ -82,7 +82,7 @@ module Neo4j
82
82
  end
83
83
 
84
84
  # Same as #end_node but does not wrap the node
85
- # @returns [Neo4j::Node]
85
+ # @return [Neo4j::Node]
86
86
  def _end_node
87
87
  raise 'not implemented'
88
88
  end
data/lib/neo4j/session.rb CHANGED
@@ -46,39 +46,72 @@ module Neo4j
46
46
  end
47
47
  end
48
48
 
49
- # Executes a Cypher Query
50
- # Returns an enumerable of hash values, column => value
49
+ # Executes a Cypher Query.
50
+ # Returns an enumerable of hash values where each hash corresponds to a row unless +return+ or +map_return+
51
+ # is not an array. The search result can be mapped to Neo4j::Node or Neo4j::Relationship is your own Ruby wrapper class
52
+ # by specifying a map_return parameter.
51
53
  #
52
- # @example Using the Cypher DSL
53
- # q = session.query("START n=node({param}) RETURN n", :param => 0)
54
- # q.first[:n] #=> the node
55
- # q.columns.first => :n
54
+ # @param [Hash, String] q the cypher query, as a pure string query or a hash which will generate a cypher string.
55
+ # @option q [Hash] :params cypher parameters
56
+ # @option q [Symbol,Hash] :label the label to match. You can specify several labels by using a hash of variable names and labels.
57
+ # @option q [Symbol] :conditions key and value of properties which the label nodes must match
58
+ # @option q [Hash] :conditions key and value of properties which the label nodes must match
59
+ # @option q [String, Array] :match the cypher match clause
60
+ # @option q [String, Array] :where the cypher where clause
61
+ # @option q [String, Array, Symbol] :return the cypher where clause
62
+ # @option q [String, Hash, Symbol] :map_return mapping of the returned values, e.g. :id_to_node, :id_to_rel, or :value
63
+ # @option q [Hash<Symbol, Proc>] :map_return_procs custom mapping functions of :map_return types
64
+ # @option q [String,Symbol,Array<Hash>] :order the order
65
+ # @option q [Fixnum] :limit enables the return of only subsets of the total result.
66
+ # @option q [Fixnum] :skip enables the return of only subsets of the total result.
67
+ # @return [Enumerable] the result, an enumerable of Neo4j::Node objects unless a pure cypher string is given or return/map_returns is specified, see examples.
68
+ # @raise CypherError if invalid cypher
69
+ # @example Cypher String and parameters
70
+ # Neo4j::Session.query("START n=node({p}) RETURN ID(n)", params: {p: 42})
56
71
  #
57
- # @example Using the Cypher DSL
58
- # q = session.query{ match node(3) <=> node(:x); ret :x}
59
- # q.first[:n] #=> the @node
60
- # q.columns.first => :n
72
+ # @example label
73
+ # # If there is no :return parameter it will try to return Neo4j::Node objects
74
+ # # Default parameter is :n in the generated cypher
75
+ # Neo4j::Session.query(label: :person) # => MATCH (n:`person`) RETURN ID(n) # or RETURN n for embedded
61
76
  #
62
- # @example Using the Cypher DSL and one parameter (n=Neo4j.ref_node)
63
- # q = session.query(Neo4j.ref_node){|n| n <=> node(:x); :x}
64
- # q.first[:n] #=> the @node
65
- # q.columns.first => :n
77
+ # @example to_s
78
+ # # What Cypher is returned ? check with to_s
79
+ # Neo4j::Session.query(label: :person).to_s # =>
66
80
  #
67
- # @example Using an array of nodes
68
- # # same as - two_nodes=node(Neo4j.ref_node.neo_id, node_b.neo_id), b = node(b.neo_id)
69
- # q = session.query([Neo4j.ref_node, node_b], node_c){|two_nodes, b| two_nodes <=> b; b}
81
+ # @example return
82
+ # Neo4j::Session.query(label: :person, return: :age) # returns age properties
83
+ # Neo4j::Session.query(label: :person, return: [:name, :age]) # returns a hash of name and age properties
84
+ # Neo4j::Session.query(label: :person, return: 'count(n) AS c')
85
+ #
86
+ # @example map_return - an Enumerable of names (String)
87
+ # Neo4j::Session.query("START n=node(42) RETURN n.name", map_return: :value)
88
+ #
89
+ # @example map_return - Enumerable of an Hash with name property, Neo4j::Relationship and Neo4j::Node as values
90
+ # Neo4j::Session.query("START n=node(42) MATCH n-[r]->[x] RETURN n.name as N, ID(r) as R, ID(x) as X",
91
+ # map_return: {N: :value, R: :id_to_rel, X: :id_to_node})
92
+ #
93
+ # @example map_return, only for embedded_db, to_rel, and to_node allows direct mapping to Neo4j::Node and Neo4j::Relationship without ID(n)
94
+ # Neo4j::Session.query("START n=node(42) MATCH n-[r]->[x] RETURN n.name as N, r, x", map_return: {N: :value, r: :to_rel, x: :to_node})
95
+ #
96
+ # @example map_return_procs, custom mapping function
97
+ # Neo4j::Session.query(label: :person, map_return: :age_times_two, map_return_procs: {age_times_two: ->(row){(row[:age] || 0) * 2}})
98
+ #
99
+ # @example match
100
+ # Neo4j::Session.query(label: :person, match: 'n--m')
101
+ #
102
+ # @example where
103
+ # Neo4j::Session.query(label: :person, where: 'n.age > 40')
104
+ # Neo4j::Session.query(label: :person, where: 'n.age > {age}', params: {age: 40})
105
+ #
106
+ # @example condition
107
+ # Neo4j::Session.query(label: :person, conditions: {age: 42})
108
+ # Neo4j::Session.query(label: :person, conditions: {name: /foo?bar.*/})
70
109
  #
71
- # @see Cypher
72
110
  # @see http://docs.neo4j.org/chunked/milestone/cypher-query-lang.html The Cypher Query Language Documentation
73
- # @note Returns a read-once only forward iterable.
74
- # @param params parameter for the query_dsl block
75
- # @return [Neo4j::Cypher::ResultWrapper] a forward read once only Enumerable, containing hash values.
111
+ # @note Returns a read-once only forward iterable for the embedded database.
76
112
  #
77
- # @abstract
78
- def query(*params, &query_dsl)
79
- cypher_params = params.pop if params.last.is_a?(Hash)
80
- q = query_dsl ? Neo4j::Cypher.query(*params, &query_dsl).to_s : params[0]
81
- _query(q, cypher_params)
113
+ def query(q)
114
+ raise 'not implemented, abstract'
82
115
  end
83
116
 
84
117
  # Same as #query but does not accept an DSL and returns the raw result from the database.
@@ -92,7 +125,7 @@ module Neo4j
92
125
  # Creates a new session to Neo4j
93
126
  # @see also Neo4j::Server::CypherSession#open for :server_db params
94
127
  # @param db_type the type of database, e.g. :embedded_db, or :server_db
95
- def open(db_type, *params)
128
+ def open(db_type=:server_db, *params)
96
129
  register(create_session(db_type, params))
97
130
  end
98
131
 
@@ -112,6 +145,11 @@ module Neo4j
112
145
  @@current_session
113
146
  end
114
147
 
148
+ # @see Neo4j::Session#query
149
+ def query(*params)
150
+ current.query(*params)
151
+ end
152
+
115
153
  def named(name)
116
154
  @@all_sessions[name] || raise("No session named #{name}.")
117
155
  end
@@ -147,8 +185,12 @@ module Neo4j
147
185
  @@current_session = nil if @@current_session == session
148
186
  end
149
187
 
188
+ def inspect
189
+ "Neo4j::Session available: #{@@factories && @@factories.keys}"
190
+ end
191
+
150
192
  def register_db(db, &session_factory)
151
- raise "Factory for #{db} already exists" if @@factories[db]
193
+ puts "replace factory for #{db}" if @@factories[db]
152
194
  @@factories[db] = session_factory
153
195
  end
154
196
  end
@@ -0,0 +1,202 @@
1
+ module Neo4j
2
+ class Session
3
+
4
+ @@current_session = nil
5
+ @@all_sessions = {}
6
+ @@factories = {}
7
+
8
+ # @abstract
9
+ def close
10
+ self.class.unregister(self)
11
+ end
12
+
13
+ # Only for embedded database
14
+ # @abstract
15
+ def start
16
+ raise "not impl."
17
+ end
18
+
19
+ # Only for embedded database
20
+ # @abstract
21
+ def shutdown
22
+ raise "not impl."
23
+ end
24
+
25
+ # Only for embedded database
26
+ # @abstract
27
+ def running
28
+ raise "not impl."
29
+ end
30
+
31
+ def auto_commit?
32
+ true # TODO
33
+ end
34
+
35
+ # @abstract
36
+ def begin_tx
37
+ raise "not impl."
38
+ end
39
+
40
+ class CypherError < StandardError
41
+ attr_reader :error_msg, :error_status, :error_code
42
+ def initialize(error_msg, error_code, error_status)
43
+ super(error_msg)
44
+ @error_msg = error_msg
45
+ @error_status = error_status
46
+ end
47
+ end
48
+
49
+ # Executes a Cypher Query.
50
+ # Returns an enumerable of hash values where each hash corresponds to a row unless +return+ or +map_return+
51
+ # is not an array. The search result can be mapped to Neo4j::Node or Neo4j::Relationship is your own Ruby wrapper class
52
+ # by specifying a map_return parameter.
53
+ #
54
+ # @param [Hash, String] q the cypher query, as a pure string query or a hash which will generate a cypher string.
55
+ # @option q [Hash] :params cypher parameters
56
+ # @option q [Symbol,Hash] :label the label to match. You can specify several labels by using a hash of variable names and labels.
57
+ # @option q [Symbol] :conditions key and value of properties which the label nodes must match
58
+ # @option q [Hash] :conditions key and value of properties which the label nodes must match
59
+ # @option q [String, Array] :match the cypher match clause
60
+ # @option q [String, Array] :where the cypher where clause
61
+ # @option q [String, Array, Symbol] :return the cypher where clause
62
+ # @option q [String, Hash, Symbol] :map_return mapping of the returned values, e.g. :id_to_node, :id_to_rel, or :value
63
+ # @option q [Hash<Symbol, Proc>] :map_return_procs custom mapping functions of :map_return types
64
+ # @option q [String,Symbol,Array<Hash>] :order the order
65
+ # @option q [Fixnum] :limit enables the return of only subsets of the total result.
66
+ # @option q [Fixnum] :skip enables the return of only subsets of the total result.
67
+ # @return [Enumerable] the result, an enumerable of Neo4j::Node objects unless a pure cypher string is given or return/map_returns is specified, see examples.
68
+ # @raise CypherError if invalid cypher
69
+ # @example Cypher String and parameters
70
+ # Neo4j::Session.query("START n=node({p}) RETURN ID(n)", params: {p: 42})
71
+ #
72
+ # @example label
73
+ # # If there is no :return parameter it will try to return Neo4j::Node objects
74
+ # # Default parameter is :n in the generated cypher
75
+ # Neo4j::Session.query(label: :person) # => MATCH (n:`person`) RETURN ID(n) # or RETURN n for embedded
76
+ #
77
+ # @example to_s
78
+ # # What Cypher is returned ? check with to_s
79
+ # Neo4j::Session.query(label: :person).to_s # =>
80
+ #
81
+ # @example return
82
+ # Neo4j::Session.query(label: :person, return: :age) # returns age properties
83
+ # Neo4j::Session.query(label: :person, return: [:name, :age]) # returns a hash of name and age properties
84
+ # Neo4j::Session.query(label: :person, return: 'count(n) AS c')
85
+ #
86
+ # @example map_return - an Enumerable of names (String)
87
+ # Neo4j::Session.query("START n=node(42) RETURN n.name", map_return: :value)
88
+ #
89
+ # @example map_return - Enumerable of an Hash with name property, Neo4j::Relationship and Neo4j::Node as values
90
+ # Neo4j::Session.query("START n=node(42) MATCH n-[r]->[x] RETURN n.name as N, ID(r) as R, ID(x) as X",
91
+ # map_return: {N: :value, R: :id_to_rel, X: :id_to_node})
92
+ #
93
+ # @example map_return, only for embedded_db, to_rel, and to_node allows direct mapping to Neo4j::Node and Neo4j::Relationship without ID(n)
94
+ # Neo4j::Session.query("START n=node(42) MATCH n-[r]->[x] RETURN n.name as N, r, x", map_return: {N: :value, r: :to_rel, x: :to_node})
95
+ #
96
+ # @example map_return_procs, custom mapping function
97
+ # Neo4j::Session.query(label: :person, map_return: :age_times_two, map_return_procs: {age_times_two: ->(row){(row[:age] || 0) * 2}})
98
+ #
99
+ # @example match
100
+ # Neo4j::Session.query(label: :person, match: 'n--m')
101
+ #
102
+ # @example where
103
+ # Neo4j::Session.query(label: :person, where: 'n.age > 40')
104
+ # Neo4j::Session.query(label: :person, where: 'n.age > {age}', params: {age: 40})
105
+ #
106
+ # @example condition
107
+ # Neo4j::Session.query(label: :person, conditions: {age: 42})
108
+ # Neo4j::Session.query(label: :person, conditions: {name: /foo?bar.*/})
109
+ #
110
+ # @see http://docs.neo4j.org/chunked/milestone/cypher-query-lang.html The Cypher Query Language Documentation
111
+ # @note Returns a read-once only forward iterable for the embedded database.
112
+ #
113
+ def query(q)
114
+ raise 'not implemented, abstract'
115
+ end
116
+
117
+ # Same as #query but does not accept an DSL and returns the raw result from the database.
118
+ # Notice, it might return different values depending on which database is used, embedded or server.
119
+ # @abstract
120
+ def _query(*params)
121
+ raise 'not implemented'
122
+ end
123
+
124
+ class << self
125
+ # Creates a new session to Neo4j
126
+ # @see also Neo4j::Server::CypherSession#open for :server_db params
127
+ # @param db_type the type of database, e.g. :embedded_db, or :server_db
128
+ def open(db_type=:server_db, *params)
129
+ register(create_session(db_type, params))
130
+ end
131
+
132
+ def open_named(db_type, name, default = nil, *params)
133
+ raise "Multiple sessions is currently only supported for Neo4j Server connections." unless db_type == :server_db
134
+ register(create_session(db_type, params), name, default)
135
+ end
136
+
137
+ def create_session(db_type, params = {})
138
+ unless (@@factories[db_type])
139
+ raise "Can't connect to database '#{db_type}', available #{@@factories.keys.join(',')}"
140
+ end
141
+ @@factories[db_type].call(*params)
142
+ end
143
+
144
+ def current
145
+ @@current_session
146
+ end
147
+
148
+ # @see Neo4j::Session#query
149
+ def query(*params)
150
+ current.query(*params)
151
+ end
152
+
153
+ def named(name)
154
+ @@all_sessions[name] || raise("No session named #{name}.")
155
+ end
156
+
157
+ def set_current(session)
158
+ @@current_session = session
159
+ end
160
+
161
+ def add_listener(&listener)
162
+ self._listeners << listener
163
+ end
164
+
165
+ def _listeners
166
+ @@listeners ||= []
167
+ @@listeners
168
+ end
169
+
170
+ def _notify_listeners(event, data)
171
+ _listeners.each {|li| li.call(event, data)}
172
+ end
173
+
174
+ def register(session, name = nil, default = nil)
175
+ if default == true
176
+ set_current(session)
177
+ elsif default.nil?
178
+ set_current(session) unless @@current_session
179
+ end
180
+ @@all_sessions[name] = session if name
181
+ @@current_session
182
+ end
183
+
184
+ def unregister(session)
185
+ @@current_session = nil if @@current_session == session
186
+ end
187
+
188
+ def all_sessions
189
+ @@all_sessions
190
+ end
191
+
192
+ def inspect
193
+ "Neo4j::Session available: #{@@factories && @@factories.keys}"
194
+ end
195
+
196
+ def register_db(db, &session_factory)
197
+ puts "replace factory for #{db}" if @@factories[db]
198
+ @@factories[db] = session_factory
199
+ end
200
+ end
201
+ end
202
+ end