neo4j-core 3.0.0.alpha.6 → 3.0.0.alpha.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0169ab5b121c40e20ed52eef9788cf4f18f46c2a
4
- data.tar.gz: 3d193639061208fe216802c04d420faa20b9e0a9
3
+ metadata.gz: 8d1f4739d5310c9630aa978a30139764ca721ba8
4
+ data.tar.gz: 288f2ce9b48122d99c4edbce147e5278f933bd6b
5
5
  SHA512:
6
- metadata.gz: a1d763665cbe45cfff3765e46d9346e21c379ea0f65930a3405a90632bdd59e8139318fd4a4e15dd3b41795c9d57e87f751ea81dfef365fe7dce3a449bb61d8b
7
- data.tar.gz: 85e2bf6b4819f47556b5a33e137bc9980226f75a5c95aa0375e24734f231c1c49af269fcd0962556a1d58100a3b2aa356f91b7384db322a3622701a70ee14e8e
6
+ metadata.gz: fd729be854e8df8c59e4f2e96f8720f0a09d5bc66aed7b2a13c5abfa1ef7146a1997f6042302139968180b6a8a6a59aae08908ef79fab5d7d5a005a4585d3fff
7
+ data.tar.gz: 394e87ad9bfb563d1d27d61bc5e4bcb884b23cb319d8d78a4489b457398e37ebbb44bd0b45f27b2d98640653a5e9b8e2d34507148aa1ace1ce7c8a511719fa7a
data/README.md CHANGED
@@ -73,10 +73,23 @@ The default session is used by all operation unless specified as the last argume
73
73
  For example create a node with a different session:
74
74
 
75
75
  ```ruby
76
- my_session = Neo4j::Session.open(:server_db, "http://localhost:7474")
76
+ my_session = Neo4j::Session.create_session(:server_db, "http://localhost:7474")
77
77
  Neo4j::Node.create(name: 'kalle', my_session)
78
78
  ```
79
79
 
80
+ When using the Neo4j Server: `:server_db`, mutliple sessions are supported. They
81
+ can be created using the open_named method. This method takes two extra parameters,
82
+ the second prameter is the session name, and the third parameter is whether the new session should over-ride
83
+ the default session (becoming the session returned by calling `Neo4j::Session.current`).
84
+ Valid options are true (always become current), false (never become current) and nil (become current if no
85
+ existing current session).
86
+
87
+ ```ruby
88
+ Neo4j::Session.open_named(:server_db, :test, true, "https://localhost:7474")
89
+ session = Neo4j::Session.named :test # Returns the session named :test.
90
+ session = Neo4j::Session.current # Returns the session named :test, because the 'default' flag was set to true.
91
+ ```
92
+
80
93
 
81
94
  ### Label and Index Support
82
95
 
@@ -125,6 +138,7 @@ Setting properties
125
138
  node[:name] # => 'changed name'
126
139
  node.props # => {name: 'changed name'}
127
140
  node.props={ foo: 42} # replace all properties
141
+ node.update_props( bar: 42) # keeps old properties (unlike #props=) update with given hash
128
142
  ```
129
143
 
130
144
  Notice properties are never stored in ruby objects, instead they are always fetched from the database.
@@ -192,8 +206,13 @@ How to create a relationship between node n1 and node n2 with one property
192
206
  n1 = Neo4j::Node.create
193
207
  n2 = Neo4j::Node.create
194
208
  rel = n1.create_rel(:knows, n2, since: 1994)
209
+
210
+ # Alternative
211
+ Neo4j::Relationship.create(:knows, n1, n2, since: 1994)
195
212
  ```
196
213
 
214
+ Setting properties on relationships works like setting properties on nodes.
215
+
197
216
  Finding relationships
198
217
 
199
218
  ```ruby
@@ -234,15 +253,22 @@ Delete relationship
234
253
  rel = n1.rel(:outgoing, :know) # expects only one relationship
235
254
  rel.del
236
255
  ```
237
- ### Identity
238
256
 
239
- NOT WORKING YET, TODO.
240
- By default the identity for a node is the same as the native Neo4j id.
241
- You can specify your own identity of nodes.
257
+
258
+ ### Cypher
259
+
260
+ Example
242
261
 
243
262
  ```ruby
244
- session = Neo4j::CypherDatabase.connect('URL')
245
- session.config.node_identity = '_my_id'
263
+ session = Neo4j::Session.current
264
+
265
+ session.query("CREATE (n {mydata: 'Hello'}) RETURN ID(n) AS id")
266
+
267
+ # Parameters
268
+ session.query("START n=node({a_parameter}) RETURN ID(n)", a_parameter: 0)
269
+
270
+ # DSL
271
+ session.query(a_parameter: 0) { node("{a_parameter}").neo_id.as(:foo) }
246
272
  ```
247
273
 
248
274
  ## Implementation:
@@ -282,7 +308,7 @@ The testing will be using much more mocking.
282
308
 
283
309
  * The `unit` rspec folder only contains testing for one Ruby module. All other modules should be mocked.
284
310
  * The `integration` rspec folder contains testing for two or more modules but mocks the neo4j database access.
285
- * The `e2e` rspec folder for use the real database (or Neo4j's ImpermanentDatabase (todo))
311
+ * The `e2e` rspec folder for use the real database (or Neo4j's ImpermanentDatabase)
286
312
  * The `shared_examples` common specs for different types of databases
287
313
 
288
314
 
@@ -4,7 +4,7 @@ module Neo4j::Core
4
4
  def escape_value(value)
5
5
  case value
6
6
  when String
7
- "'#{value}'" # TODO escape ' and "
7
+ "'#{value.gsub("'", %q(\\\'))}'"
8
8
  else
9
9
  value
10
10
  end
@@ -1,5 +1,5 @@
1
1
  module Neo4j
2
2
  module Core
3
- VERSION = "3.0.0.alpha.6"
3
+ VERSION = "3.0.0.alpha.7"
4
4
  end
5
5
  end
@@ -28,14 +28,22 @@ module Neo4j::Embedded::Property
28
28
  property_keys.each do |key|
29
29
  remove_property(key)
30
30
  end
31
+ _update_props(hash)
32
+ hash
33
+ end
34
+ tx_methods :props=
31
35
 
36
+ def _update_props(hash)
32
37
  hash.each_pair do |k,v|
33
38
  set_property(k,v)
34
39
  end
35
40
  hash
36
41
  end
37
- tx_methods :props=
38
42
 
43
+ def update_props(hash)
44
+ _update_props(hash)
45
+ end
46
+ tx_methods :update_props
39
47
 
40
48
  def neo_id
41
49
  get_id
@@ -51,6 +51,16 @@ module Neo4j::Server
51
51
  properties
52
52
  end
53
53
 
54
+ # (see Neo4j::Node#update_props)
55
+ def update_props(properties)
56
+ return if properties.empty?
57
+ q = "START n=node(#{neo_id}) SET " + properties.keys.map do |k|
58
+ "n.`#{k}`= #{escape_value(properties[k])}"
59
+ end.join(',')
60
+ @session._query_or_fail(q)
61
+ properties
62
+ end
63
+
54
64
  # (see Neo4j::Node#get_property)
55
65
  def get_property(key)
56
66
  @session._query_or_fail("START n=node(#{neo_id}) RETURN n.`#{key}`", true)
@@ -2,6 +2,7 @@ module Neo4j::Server
2
2
 
3
3
  class CypherRelationship < Neo4j::Relationship
4
4
  include Neo4j::Server::Resource
5
+ include Neo4j::Core::CypherTranslator
5
6
 
6
7
  def initialize(session, id)
7
8
  @session = session
@@ -60,6 +61,29 @@ module Neo4j::Server
60
61
  expect_response_code(r.response, 200)
61
62
  end
62
63
 
64
+ # (see Neo4j::Relationship#props)
65
+ def props
66
+ props = @session._query_or_fail("START n=relationship(#{neo_id}) RETURN n", true)['data']
67
+ props.keys.inject({}){|hash,key| hash[key.to_sym] = props[key]; hash}
68
+ end
69
+
70
+ # (see Neo4j::Relationship#props=)
71
+ def props=(properties)
72
+ @session._query_or_fail("START n=relationship(#{neo_id}) SET n = { props }", false, {props: properties})
73
+ properties
74
+ end
75
+
76
+ # (see Neo4j::Relationship#update_props)
77
+ def update_props(properties)
78
+ return if properties.empty?
79
+ q = "START n=relationship(#{neo_id}) SET " + properties.keys.map do |k|
80
+ "n.`#{k}`= #{escape_value(properties[k])}"
81
+ end.join(',')
82
+ @session._query_or_fail(q)
83
+ properties
84
+ end
85
+
86
+
63
87
  def del
64
88
  id = neo_id
65
89
  @session._query_internal{rel(id).del}.raise_unless_response_code(200)
data/lib/neo4j/node.rb CHANGED
@@ -26,11 +26,17 @@ module Neo4j
26
26
  end
27
27
 
28
28
  # replace all properties with new properties
29
- # @param hash a hash of properties the node should have
29
+ # @param [Hash] hash a hash of properties the node should have
30
30
  def props=(hash)
31
31
  raise 'not implemented'
32
32
  end
33
33
 
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)
37
+ raise 'not implemented'
38
+ end
39
+
34
40
  # Directly remove the property on the node (low level method, may need transaction)
35
41
  def remove_property(key)
36
42
  raise 'not implemented'
@@ -16,12 +16,53 @@ module Neo4j
16
16
  def wrapper
17
17
  self
18
18
  end
19
+
20
+ def neo4j_obj
21
+ self
22
+ end
23
+
19
24
  end
20
25
 
21
26
  include PropertyContainer
22
27
  include EntityEquality
23
28
  include Wrapper
24
29
 
30
+ # @return [Hash] all properties of the relationship
31
+ def props()
32
+ raise 'not implemented'
33
+ end
34
+
35
+ # replace all properties with new properties
36
+ # @param [Hash] hash a hash of properties the relationship should have
37
+ def props=(hash)
38
+ raise 'not implemented'
39
+ end
40
+
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)
44
+ raise 'not implemented'
45
+ end
46
+
47
+ # Directly remove the property on the relationship (low level method, may need transaction)
48
+ def remove_property(key)
49
+ raise 'not implemented'
50
+ end
51
+
52
+ # Directly set the property on the relationship (low level method, may need transaction)
53
+ # @param [Hash, String] key
54
+ # @param value see Neo4j::PropertyValidator::VALID_PROPERTY_VALUE_CLASSES for valid values
55
+ def set_property(key, value)
56
+ raise 'not implemented'
57
+ end
58
+
59
+ # Directly get the property on the relationship (low level method, may need transaction)
60
+ # @param [Hash, String] key
61
+ # @return the value of the key
62
+ def get_property(key, value)
63
+ raise 'not implemented'
64
+ end
65
+
25
66
  # Returns the start node of this relationship.
26
67
  # @return [Neo4j::Node,Object] the node or wrapped node
27
68
  def start_node
@@ -106,6 +147,10 @@ module Neo4j
106
147
 
107
148
 
108
149
  class << self
150
+ def create(rel_type, from_node, other_node, props = {})
151
+ from_node.neo4j_obj.create_rel(rel_type, other_node, props)
152
+ end
153
+
109
154
  def load(neo_id, session = Neo4j::Session.current)
110
155
  session.load_relationship(neo_id)
111
156
  end
data/lib/neo4j/session.rb CHANGED
@@ -2,6 +2,7 @@ module Neo4j
2
2
  class Session
3
3
 
4
4
  @@current_session = nil
5
+ @@all_sessions = {}
5
6
  @@factories = {}
6
7
 
7
8
  # @abstract
@@ -91,17 +92,29 @@ module Neo4j
91
92
  # Creates a new session
92
93
  # @param db_type the type of database, e.g. :embedded_db, or :server_db
93
94
  def open(db_type, *params)
95
+ register(create_session(db_type, params))
96
+ end
97
+
98
+ def open_named(db_type, name, default = nil, *params)
99
+ raise "Multiple sessions is currently only supported for Neo4j Server connections." unless db_type == :server_db
100
+ register(create_session(db_type, params), name, default)
101
+ end
102
+
103
+ def create_session(db_type, params)
94
104
  unless (@@factories[db_type])
95
105
  raise "Can't connect to database '#{db_type}', available #{@@factories.keys.join(',')}"
96
106
  end
97
- session = @@factories[db_type].call(*params)
98
- register(session)
107
+ @@factories[db_type].call(*params)
99
108
  end
100
109
 
101
110
  def current
102
111
  @@current_session
103
112
  end
104
113
 
114
+ def named(name)
115
+ @@all_sessions[name] || raise("No session named #{name}.")
116
+ end
117
+
105
118
  def set_current(session)
106
119
  @@current_session = session
107
120
  end
@@ -119,8 +132,13 @@ module Neo4j
119
132
  _listeners.each {|li| li.call(event, data)}
120
133
  end
121
134
 
122
- def register(session)
123
- set_current(session) unless @@current_session
135
+ def register(session, name = nil, default = nil)
136
+ if default == true
137
+ set_current(session)
138
+ elsif default.nil?
139
+ set_current(session) unless @@current_session
140
+ end
141
+ @@all_sessions[name] = session if name
124
142
  @@current_session
125
143
  end
126
144
 
@@ -134,4 +152,4 @@ module Neo4j
134
152
  end
135
153
  end
136
154
  end
137
- end
155
+ end
data/lib/test.rb ADDED
@@ -0,0 +1,11 @@
1
+ class Hej
2
+ attr_accessor :values
3
+
4
+ def []=(key, v)
5
+ self.values = *v
6
+ end
7
+ end
8
+
9
+ h = Hej.new
10
+ h[:koo] = 1
11
+ puts "h: #{h.values.inspect}"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neo4j-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.alpha.6
4
+ version: 3.0.0.alpha.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Ronge
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-07 00:00:00.000000000 Z
11
+ date: 2014-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -96,6 +96,7 @@ files:
96
96
  - lib/neo4j-server/cypher_transaction.rb
97
97
  - lib/neo4j-server/resource.rb
98
98
  - lib/neo4j-server.rb
99
+ - lib/test.rb
99
100
  - README.md
100
101
  - Gemfile
101
102
  - neo4j-core.gemspec