cypher_builder 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cypher_builder.rb +2 -0
- data/lib/cypher_builder/alias.rb +2 -2
- data/lib/cypher_builder/and.rb +2 -2
- data/lib/cypher_builder/as_is.rb +16 -0
- data/lib/cypher_builder/context.rb +13 -0
- data/lib/cypher_builder/cypher.rb +2 -2
- data/lib/cypher_builder/eql.rb +4 -2
- data/lib/cypher_builder/field.rb +15 -1
- data/lib/cypher_builder/like.rb +4 -2
- data/lib/cypher_builder/match.rb +2 -2
- data/lib/cypher_builder/opt.rb +2 -2
- data/lib/cypher_builder/param.rb +1 -1
- data/lib/cypher_builder/resolver.rb +4 -2
- data/lib/cypher_builder/return.rb +2 -2
- data/lib/cypher_builder/runner.rb +2 -1
- data/lib/cypher_builder/version.rb +1 -1
- data/lib/cypher_builder/where.rb +2 -2
- data/spec/cypher_builder/cypher_spec.rb +8 -8
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a296dcf5a03aae2c6ad43c178ed2150808b7cf56
|
4
|
+
data.tar.gz: 586c6549b516bb866d454a3f1bdb26a9b9069bdd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bbc86c29b2ee15020301b4317a95bdd74889624a272e8917a3b4732f87a8ed060ed65aa987700aa408d3c19053b4f65d6b3fb2e4f560e1e2401105ecd5c9db82
|
7
|
+
data.tar.gz: c7b25acf4446a29861dadc71905c3af1458d05583c453be7f64481196ca4615b9a62dca49a9374a14208fa8d8045c6da17b03cce683bc1a7ed05f0ed6773dc5a
|
data/lib/cypher_builder.rb
CHANGED
@@ -12,10 +12,12 @@ if defined?(::Neography)
|
|
12
12
|
end
|
13
13
|
|
14
14
|
require 'cypher_builder/payload'
|
15
|
+
require 'cypher_builder/context'
|
15
16
|
require 'cypher_builder/resolver'
|
16
17
|
require 'cypher_builder/runner'
|
17
18
|
require 'cypher_builder/cypher'
|
18
19
|
|
20
|
+
require 'cypher_builder/as_is'
|
19
21
|
require 'cypher_builder/opt'
|
20
22
|
require 'cypher_builder/field'
|
21
23
|
require 'cypher_builder/node'
|
data/lib/cypher_builder/alias.rb
CHANGED
@@ -11,8 +11,8 @@ module CypherBuilder
|
|
11
11
|
@an_alias = an_alias
|
12
12
|
end
|
13
13
|
|
14
|
-
def as_cypher(
|
15
|
-
sprintf('%s AS %s', resolve(@field,
|
14
|
+
def as_cypher(payload:, context: )
|
15
|
+
sprintf('%s AS %s', resolve(@field, payload: payload, context: context.add(self)), @an_alias)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
data/lib/cypher_builder/and.rb
CHANGED
@@ -10,8 +10,8 @@ module CypherBuilder
|
|
10
10
|
@parts = wrap(*parts)
|
11
11
|
end
|
12
12
|
|
13
|
-
def as_cypher(
|
14
|
-
resolve(@parts, separator: ' AND ',
|
13
|
+
def as_cypher(payload:, context: )
|
14
|
+
resolve(@parts, separator: ' AND ', payload: payload, context: context.add(self))
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
data/lib/cypher_builder/eql.rb
CHANGED
@@ -10,8 +10,10 @@ module CypherBuilder
|
|
10
10
|
@left, @right = wrap(left, right)
|
11
11
|
end
|
12
12
|
|
13
|
-
def as_cypher(
|
14
|
-
sprintf('%s = %s',
|
13
|
+
def as_cypher(payload:, context: )
|
14
|
+
sprintf('%s = %s',
|
15
|
+
resolve(@left, payload: payload, context: context.add(self)),
|
16
|
+
resolve(@right, payload: payload, context: context.add(self)))
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
data/lib/cypher_builder/field.rb
CHANGED
@@ -4,12 +4,26 @@ module CypherBuilder
|
|
4
4
|
end
|
5
5
|
|
6
6
|
class Field
|
7
|
+
attr_reader :name
|
8
|
+
|
7
9
|
def initialize(prefix = nil, name)
|
8
10
|
@prefix, @name = prefix, name
|
9
11
|
end
|
10
12
|
|
11
|
-
def as_cypher(
|
13
|
+
def as_cypher(payload:, context:)
|
14
|
+
if context.ancestor?(Return) && !context.ancestor?(Alias)
|
15
|
+
aliased
|
16
|
+
else
|
17
|
+
prefixed_name
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def prefixed_name
|
12
22
|
[@prefix, @name].compact.join('.')
|
13
23
|
end
|
24
|
+
|
25
|
+
def aliased
|
26
|
+
sprintf('%s AS %s', prefixed_name, name)
|
27
|
+
end
|
14
28
|
end
|
15
29
|
end
|
data/lib/cypher_builder/like.rb
CHANGED
@@ -10,8 +10,10 @@ module CypherBuilder
|
|
10
10
|
@left, @right = wrap(left, right)
|
11
11
|
end
|
12
12
|
|
13
|
-
def as_cypher(
|
14
|
-
sprintf('%s LIKE %s',
|
13
|
+
def as_cypher(payload:, context: )
|
14
|
+
sprintf('%s LIKE %s',
|
15
|
+
resolve(@left, payload: payload, context: context.add(self)),
|
16
|
+
resolve(@right, payload: payload, context: context.add(self)))
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
data/lib/cypher_builder/match.rb
CHANGED
@@ -10,8 +10,8 @@ module CypherBuilder
|
|
10
10
|
@parts = wrap(*parts)
|
11
11
|
end
|
12
12
|
|
13
|
-
def as_cypher(
|
14
|
-
resolve(@parts, format: 'MATCH (%s)', separator: ', ',
|
13
|
+
def as_cypher(payload:, context: )
|
14
|
+
resolve(@parts, format: 'MATCH (%s)', separator: ', ', payload: payload, context: context.add(self))
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
data/lib/cypher_builder/opt.rb
CHANGED
@@ -10,12 +10,12 @@ module CypherBuilder
|
|
10
10
|
@params_and_parts = Hash[params_and_parts.map { |k, v| [k, wrap(v)] }]
|
11
11
|
end
|
12
12
|
|
13
|
-
def as_cypher(payload:)
|
13
|
+
def as_cypher(payload:, context:)
|
14
14
|
param, part = @params_and_parts.find { |k, _| payload.include?(k) }
|
15
15
|
part = @params_and_parts.values.first unless part
|
16
16
|
payload.already_used(param) if param
|
17
17
|
|
18
|
-
resolve(part, payload: payload)
|
18
|
+
resolve(part, payload: payload, context: context.add(self))
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
data/lib/cypher_builder/param.rb
CHANGED
@@ -9,7 +9,9 @@ module CypherBuilder::Resolver
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
def resolve(parts, format: '%s', separator: ' ',
|
13
|
-
Array(parts).map
|
12
|
+
def resolve(parts, format: '%s', separator: ' ', ** opts)
|
13
|
+
Array(parts).map do |p|
|
14
|
+
sprintf(format, p.as_cypher(** opts))
|
15
|
+
end.join(separator).strip
|
14
16
|
end
|
15
17
|
end
|
@@ -10,8 +10,8 @@ module CypherBuilder
|
|
10
10
|
@parts = wrap(*parts)
|
11
11
|
end
|
12
12
|
|
13
|
-
def as_cypher(
|
14
|
-
sprintf('RETURN %s', resolve(@parts, separator: ', ',
|
13
|
+
def as_cypher(payload:, context: )
|
14
|
+
sprintf('RETURN %s', resolve(@parts, separator: ', ', payload: payload, context: context.add(self)))
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -16,7 +16,8 @@ module CypherBuilder
|
|
16
16
|
|
17
17
|
def execute(** params)
|
18
18
|
payload = Payload.new(params)
|
19
|
-
|
19
|
+
context = Context.new
|
20
|
+
cypher = self.class.cypher.as_cypher(payload: payload, context: context)
|
20
21
|
@adapter.execute(cypher, payload.necessary)
|
21
22
|
end
|
22
23
|
end
|
data/lib/cypher_builder/where.rb
CHANGED
@@ -10,8 +10,8 @@ module CypherBuilder
|
|
10
10
|
@parts = wrap(*parts)
|
11
11
|
end
|
12
12
|
|
13
|
-
def as_cypher(
|
14
|
-
resolve(@parts, format: 'WHERE %s', separator: ' AND ',
|
13
|
+
def as_cypher(payload:, context: )
|
14
|
+
resolve(@parts, format: 'WHERE %s', separator: ' AND ', payload: payload, context: context.add(self))
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -8,7 +8,7 @@ describe Cypher do
|
|
8
8
|
cypher_class = Cypher(Match(c),
|
9
9
|
Return(c.name))
|
10
10
|
cypher_class.exec(adapter)
|
11
|
-
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name', {})
|
11
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name AS name', {})
|
12
12
|
end
|
13
13
|
end
|
14
14
|
describe '#execute' do
|
@@ -22,7 +22,7 @@ describe Cypher do
|
|
22
22
|
cypher_class = Cypher(Match(c),
|
23
23
|
Return(c.name))
|
24
24
|
cypher_class.new(adapter).execute
|
25
|
-
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name', {})
|
25
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name AS name', {})
|
26
26
|
end
|
27
27
|
it 'executes the most complex query possible (exercises everything currently implemented)' do
|
28
28
|
c = Node('c', labels: 'what')
|
@@ -31,7 +31,7 @@ describe Cypher do
|
|
31
31
|
Like(c.staff, 'test%'))),
|
32
32
|
Return(c.name, Alias(c.stuff, 'something')))
|
33
33
|
cypher_class.new(adapter).execute(thing: 'of course')
|
34
|
-
expect(adapter).to have_received(:execute).with('MATCH (c:what) WHERE c.stuff = {thing} AND c.staff LIKE "test%" RETURN c.name, c.stuff AS something', {thing: 'of course'})
|
34
|
+
expect(adapter).to have_received(:execute).with('MATCH (c:what) WHERE c.stuff = {thing} AND c.staff LIKE "test%" RETURN c.name AS name, c.stuff AS something', {thing: 'of course'})
|
35
35
|
end
|
36
36
|
context 'with Opt' do
|
37
37
|
before do
|
@@ -47,23 +47,23 @@ describe Cypher do
|
|
47
47
|
end
|
48
48
|
it 'generates first option' do
|
49
49
|
@cypher_class.new(adapter).execute(name: true)
|
50
|
-
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name', {})
|
50
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name AS name', {})
|
51
51
|
end
|
52
52
|
it 'generates first option with multiple uses' do
|
53
53
|
@cypher_class2.new(adapter).execute(name: 'Testing Test')
|
54
|
-
expect(adapter).to have_received(:execute).with('MATCH (c) WHERE c.name = {name} RETURN c.name', {name: 'Testing Test'})
|
54
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) WHERE c.name = {name} RETURN c.name AS name', {name: 'Testing Test'})
|
55
55
|
end
|
56
56
|
it 'generates second option' do
|
57
57
|
@cypher_class.new(adapter).execute(thing: true)
|
58
|
-
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.thing', {})
|
58
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.thing AS thing', {})
|
59
59
|
end
|
60
60
|
it 'generates second option with multiple uses' do
|
61
61
|
@cypher_class2.new(adapter).execute(thing: 123)
|
62
|
-
expect(adapter).to have_received(:execute).with('MATCH (c) WHERE c.thing = {thing} RETURN c.thing', {thing: 123})
|
62
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) WHERE c.thing = {thing} RETURN c.thing AS thing', {thing: 123})
|
63
63
|
end
|
64
64
|
it 'defaults to firts option' do
|
65
65
|
@cypher_class.new(adapter).execute
|
66
|
-
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name', {})
|
66
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name AS name', {})
|
67
67
|
end
|
68
68
|
end
|
69
69
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cypher_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ronie Uliana
|
@@ -86,6 +86,8 @@ files:
|
|
86
86
|
- lib/cypher_builder/adapter/neography.rb
|
87
87
|
- lib/cypher_builder/alias.rb
|
88
88
|
- lib/cypher_builder/and.rb
|
89
|
+
- lib/cypher_builder/as_is.rb
|
90
|
+
- lib/cypher_builder/context.rb
|
89
91
|
- lib/cypher_builder/cypher.rb
|
90
92
|
- lib/cypher_builder/eql.rb
|
91
93
|
- lib/cypher_builder/field.rb
|