cypher-dsl 0.1.0.alpha.2 → 0.1.0

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
  SHA256:
3
- metadata.gz: 5430e442ecb75b449cefb44e0b4d1e06ccdc80ca8a4dc10a7f3da2366a8cb049
4
- data.tar.gz: 393b61210b500da83e53ea17706bc8e8e691ed3f813f69c1557c85a6be86e182
3
+ metadata.gz: 7cd03073fbe59b47c751c62cd953a3d4e53b009c29ba1c7c23cec907a5aa87ef
4
+ data.tar.gz: 164cea84a14542b0f348939a8b8bb47f893b618c36609fd37c8f135b473645f3
5
5
  SHA512:
6
- metadata.gz: 15d7b9bedab4f4d8072928d4d8dd207b3ab3fa4e7b4dc8c98422af314806cc5b00441a478b672640f626e4e2b0320202323990c710f0f6a1eac3c9abb52c8c8f
7
- data.tar.gz: d0634d9fc01f6375253637d9bdd4e94a1357dce52ab0be19188c8aed5fcecadd56932e0d88a60ce65a6b7e5f721be3161ce0e7c91b9f4ce7d3b529bfa6a94ea7
6
+ metadata.gz: 9470dc5e3ec3a582d2fecd247400d638886fd27dde0cbef2e233008b50e6983444173ec9327d2a516d1f52b454f08be86760742dd90bb9049d2aa9051a830d9a
7
+ data.tar.gz: 1faac1c67fa30d867dcf28ec1afad98c4efd343ef665fec79a8c3a6b00c007e85b3a8e8b1f0b6f7a74683d0e2887d28480bd8f7b63108718c657054926c7ee32
data/CHANGELOG.md CHANGED
@@ -7,6 +7,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.1.0] - 2026-11-09
11
+
12
+ First stable release. The public API is the one activegraph builds on; from here on,
13
+ breaking changes bump the minor version until 1.0.
14
+
15
+ ### Fixed
16
+
17
+ - A name projected by `WITH` is visible to that clause's own `WHERE`, so a pattern condition
18
+ keeps it: `WITH n AS m WHERE (m)-->()` no longer renders as `WHERE ()-->()` (#1).
19
+
20
+ ## [0.1.0.alpha.3] - 2026-11-01
21
+
22
+ The release activegraph's `ActiveGraph::Core::Query` builds on.
23
+
24
+ ### Added
25
+
26
+ - Raw fragments for host DSLs built on strings (Ruby-only, no Java counterpart): `Cypher.raw`
27
+ is accepted as a pattern element in MATCH/CREATE/MERGE, as a condition (also as operand of
28
+ `and`/`or`/`xor`/`not`), as a complete SET / ON CREATE SET / ON MATCH SET operation, and
29
+ `StatementBuilder#raw_clause(format, *args)` renders a whole clause verbatim.
30
+ - Raw operands of a compound condition are parenthesised unless they already form one group,
31
+ so `Cypher.raw('a OR b').as_condition.and(c)` renders `((a OR b) AND c)`.
32
+
10
33
  ## [0.1.0.alpha.2] - 2026-11-01
11
34
 
12
35
  Second pre-release. The whole upstream core test suite that applies to Ruby is now
@@ -79,6 +102,8 @@ collection, IssueRelatedIT, FunctionsIT).
79
102
  - Renderer with dialects (Neo4j 4, 5, 5.23+), pretty printing, identifier escaping,
80
103
  parameter collection (`Statement#catalog`).
81
104
 
82
- [Unreleased]: https://github.com/neo4jrb/cypher-dsl/compare/v0.1.0.alpha.2...HEAD
105
+ [Unreleased]: https://github.com/neo4jrb/cypher-dsl/compare/v0.1.0...HEAD
106
+ [0.1.0]: https://github.com/neo4jrb/cypher-dsl/compare/v0.1.0.alpha.3...v0.1.0
107
+ [0.1.0.alpha.3]: https://github.com/neo4jrb/cypher-dsl/compare/v0.1.0.alpha.2...v0.1.0.alpha.3
83
108
  [0.1.0.alpha.2]: https://github.com/neo4jrb/cypher-dsl/compare/v0.1.0.alpha.1...v0.1.0.alpha.2
84
109
  [0.1.0.alpha.1]: https://github.com/neo4jrb/cypher-dsl/releases/tag/v0.1.0.alpha.1
data/README.md CHANGED
@@ -75,6 +75,19 @@ Cypher.returning(Cypher.raw('($E + $E) * $factor', Cypher.parameter(:a), 2, Cyph
75
75
  .build.cypher # => "RETURN ($a + 2) * $factor AS r"
76
76
  ```
77
77
 
78
+ A raw fragment is also accepted as a pattern element, as a condition, as a `SET`
79
+ operation, and `raw_clause` renders a whole clause verbatim. These are escape hatches
80
+ for DSLs that have to pass user-written Cypher through; a raw condition combined with
81
+ others is parenthesised so its precedence survives:
82
+
83
+ ```ruby
84
+ Cypher.match(Cypher.raw('(n)-[:KNOWS]->(m)'))
85
+ .where(Cypher.property(:n, :age).gt(30).and(Cypher.raw('m.name STARTS WITH "A" OR m.vip')))
86
+ .raw_clause('CALL db.labels() YIELD label')
87
+ .returning(:n, :label).build.cypher
88
+ # => "MATCH (n)-[:KNOWS]->(m) WHERE (n.age > 30 AND (m.name STARTS WITH \"A\" OR m.vip)) CALL db.labels() YIELD label RETURN n, label"
89
+ ```
90
+
78
91
  Every expression and pattern answers `to_s` with its plain Cypher, e.g.
79
92
  `Cypher.node('Person').named(:n).to_s # => "(n:Person)"`.
80
93
 
@@ -125,7 +138,7 @@ The flags are `entity_names`, `parameter_names`, `all_aliases`, `internal_aliase
125
138
  Pre-releases are not picked up by a bare requirement, so name the version:
126
139
 
127
140
  ```ruby
128
- gem 'cypher-dsl', '0.1.0.alpha.2'
141
+ gem 'cypher-dsl', '0.1.0'
129
142
  ```
130
143
 
131
144
  Requires Ruby 3.4 or newer.
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CypherDSL
4
+ module Clauses
5
+ # A clause rendered verbatim from a {RawExpression}: the escape hatch for
6
+ # Cypher the builder does not model (`raw_clause('CALL db.labels()')`).
7
+ Raw = Data.define(:expression)
8
+ end
9
+ end
@@ -10,7 +10,7 @@ module CypherDSL
10
10
  case value
11
11
  when Condition then value
12
12
  when Relationship, RelationshipChain then RelationshipPatternCondition.new(value)
13
- when FunctionInvocation then ExpressionCondition.new(value) # boolean functions such as isEmpty()
13
+ when FunctionInvocation, RawExpression then ExpressionCondition.new(value) # isEmpty(), raw text
14
14
  else raise ArgumentError, "#{value.inspect} is not a condition"
15
15
  end
16
16
  end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CypherDSL
4
+ class Renderer
5
+ class Visitor
6
+ # Rendering of comparisons and compound conditions.
7
+ module Conditions
8
+ private
9
+
10
+ def render_comparison(comparison)
11
+ left = render_operand(comparison.left)
12
+ return "#{left} #{comparison.operator}" if comparison.unary?
13
+
14
+ "#{left} #{comparison.operator} #{render_operand(comparison.right)}"
15
+ end
16
+
17
+ # A comparison nested inside another comparison is parenthesised: `(1 > 2) = true`.
18
+ def render_operand(expression)
19
+ rendered = render_expression(expression)
20
+ expression.is_a?(Comparison) ? "(#{rendered})" : rendered
21
+ end
22
+
23
+ # `exists(n.prop)` on Neo4j 4, `n.prop IS NOT NULL` afterwards.
24
+ def render_property_exists(condition, negated: false)
25
+ property = render_expression(condition.property)
26
+ return "exists(#{property})" if @configuration.legacy_functions?
27
+
28
+ "#{property} IS #{'NOT ' unless negated}NULL"
29
+ end
30
+
31
+ # Pretty printed, each further operand starts an indented line: `(a\n AND b)`.
32
+ def render_compound_condition(condition)
33
+ if condition.negation?
34
+ negated = condition.conditions.first
35
+ if negated.is_a?(PropertyExistsCondition) && !@configuration.legacy_functions?
36
+ return render_property_exists(negated, negated: true)
37
+ end
38
+
39
+ rendered = render_expression(negated)
40
+ return negated.is_a?(RelationshipPatternCondition) ? "NOT #{rendered}" : "NOT (#{rendered})"
41
+ end
42
+
43
+ operands = nested { condition.conditions.map { |inner| render_compound_operand(inner) } }
44
+ "(#{operands.join("#{break_line(@level + 1)}#{condition.operator} ")})"
45
+ end
46
+
47
+ # A raw operand is parenthesised unless it already is one group, so that
48
+ # `Cypher.raw('a OR b').as_condition.and(c)` keeps its precedence. (Ruby addition; the
49
+ # Java renderer trusts raw text.)
50
+ def render_compound_operand(condition)
51
+ rendered = render_expression(condition)
52
+ raw_condition?(condition) && !parenthesised?(rendered) ? "(#{rendered})" : rendered
53
+ end
54
+
55
+ def raw_condition?(condition)
56
+ condition.is_a?(ExpressionCondition) && condition.expression.is_a?(RawExpression)
57
+ end
58
+
59
+ # True when the text is one parenthesised group: `(a OR b)` but not `(a) OR (b)`.
60
+ def parenthesised?(text)
61
+ return false unless text.start_with?('(') && text.end_with?(')')
62
+
63
+ depth = 0
64
+ text.each_char.with_index do |char, index|
65
+ depth += 1 if char == '('
66
+ depth -= 1 if char == ')'
67
+ return false if depth.zero? && index < text.size - 1
68
+ end
69
+ true
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -250,43 +250,6 @@ module CypherDSL
250
250
  "#{opening}#{break_line(@level + 1)}#{entries.join(",#{break_line(@level + 1)}")}#{break_line}#{closing}"
251
251
  end
252
252
 
253
- def render_comparison(comparison)
254
- left = render_operand(comparison.left)
255
- return "#{left} #{comparison.operator}" if comparison.unary?
256
-
257
- "#{left} #{comparison.operator} #{render_operand(comparison.right)}"
258
- end
259
-
260
- # A comparison nested inside another comparison is parenthesised: `(1 > 2) = true`.
261
- def render_operand(expression)
262
- rendered = render_expression(expression)
263
- expression.is_a?(Comparison) ? "(#{rendered})" : rendered
264
- end
265
-
266
- # Pretty printed, each further operand starts an indented line: `(a\n AND b)`.
267
- # `exists(n.prop)` on Neo4j 4, `n.prop IS NOT NULL` afterwards.
268
- def render_property_exists(condition, negated: false)
269
- property = render_expression(condition.property)
270
- return "exists(#{property})" if @configuration.legacy_functions?
271
-
272
- "#{property} IS #{'NOT ' unless negated}NULL"
273
- end
274
-
275
- def render_compound_condition(condition)
276
- if condition.negation?
277
- negated = condition.conditions.first
278
- if negated.is_a?(PropertyExistsCondition) && !@configuration.legacy_functions?
279
- return render_property_exists(negated, negated: true)
280
- end
281
-
282
- rendered = render_expression(negated)
283
- return negated.is_a?(RelationshipPatternCondition) ? "NOT #{rendered}" : "NOT (#{rendered})"
284
- end
285
-
286
- operands = nested { condition.conditions.map { |inner| render_expression(inner) } }
287
- "(#{operands.join("#{break_line(@level + 1)}#{condition.operator} ")})"
288
- end
289
-
290
253
  def render_function_invocation(invocation)
291
254
  arguments = invocation.arguments.map do |argument|
292
255
  rendered = render_expression(argument)
@@ -15,6 +15,7 @@ module CypherDSL
15
15
  when RelationshipChain then render_chain(element)
16
16
  when NamedPath then render_named_path(element)
17
17
  when QuantifiedPathPattern then render_quantified_path_pattern(element)
18
+ when RawExpression then render_raw(element) # `Cypher.raw('(n)-->(m)')` as a pattern
18
19
  else raise ArgumentError, "Cannot render pattern element #{element.inspect}"
19
20
  end
20
21
  end
@@ -8,6 +8,7 @@ module CypherDSL
8
8
  class Visitor
9
9
  include Patterns
10
10
  include Expressions
11
+ include Conditions
11
12
  include LabelRendering
12
13
 
13
14
  def initialize(configuration)
@@ -110,25 +111,29 @@ module CypherDSL
110
111
  case clause
111
112
  when Clauses::Match then render_match(clause)
112
113
  when Clauses::With then render_with(clause)
113
- when Clauses::Return then render_projection('RETURN', clause, nil)
114
+ when Clauses::Return then render_projection('RETURN', clause)
114
115
  when Clauses::RawReturn then clause.items.map { |item| render_projection_item(item) }.join(', ')
115
116
  when Clauses::Create then "CREATE #{render_pattern_elements(clause.pattern_elements)}"
116
117
  when Clauses::Merge then render_merge(clause)
117
118
  when Clauses::Set then "SET #{render_operations(clause.operations)}"
118
119
  when Clauses::Remove then "REMOVE #{render_operations(clause.items)}"
119
120
  when Clauses::Delete then render_delete(clause)
120
- when Clauses::Unwind
121
- "UNWIND #{render_expression(clause.expression)} AS #{render_expression(clause.variable)}"
121
+ when Clauses::Unwind then render_unwind(clause)
122
122
  when Clauses::Finish then 'FINISH'
123
123
  when Clauses::Foreach then render_foreach(clause)
124
124
  when Clauses::Call then render_call(clause)
125
125
  when Clauses::CallSubquery then render_call_subquery(clause)
126
126
  when Clauses::LoadCsv then render_load_csv(clause)
127
127
  when Clauses::Use then "USE #{clause.graph.is_a?(String) ? clause.graph : render_expression(clause.graph)}"
128
+ when Clauses::Raw then render_raw(clause.expression)
128
129
  else raise ArgumentError, "Cannot render clause #{clause.inspect}"
129
130
  end
130
131
  end
131
132
 
133
+ def render_unwind(clause)
134
+ "UNWIND #{render_expression(clause.expression)} AS #{render_expression(clause.variable)}"
135
+ end
136
+
132
137
  def render_load_csv(clause)
133
138
  parts = []
134
139
  if clause.periodic_commit
@@ -300,9 +305,16 @@ module CypherDSL
300
305
 
301
306
  # WITH ends a query part: only the names it projects stay in scope, so a
302
307
  # node that is not carried over renders with its labels again afterwards.
308
+ # The projected names are already known to the clause's own WHERE, next to
309
+ # the names of the part it ends: `WITH n AS m WHERE (m)-->()`. A `*` keeps
310
+ # the whole current scope.
303
311
  def render_with(clause)
304
- rendered = render_projection('WITH', clause, clause.where)
305
- @visited_names = clause.items.include?(Asterisk::INSTANCE) ? @visited_names : exported_names(clause.items)
312
+ projection = render_projection('WITH', clause)
313
+ exported = exported_names(clause.items)
314
+ exported.merge(@visited_names) if clause.items.include?(Asterisk::INSTANCE)
315
+ @visited_names.merge(exported)
316
+ rendered = "#{projection}#{render_where(clause.where)}"
317
+ @visited_names = exported
306
318
  rendered
307
319
  end
308
320
 
@@ -316,13 +328,13 @@ module CypherDSL
316
328
  end.to_set
317
329
  end
318
330
 
319
- def render_projection(keyword, clause, where)
331
+ def render_projection(keyword, clause)
320
332
  items = clause.items.map { |item| render_projection_item(item) }.join(', ')
321
333
  parts = ["#{keyword} #{'DISTINCT ' if clause.distinct}#{items}"]
322
334
  parts << "ORDER BY #{clause.order.items.map { |item| render_sort_item(item) }.join(', ')}" if clause.order
323
335
  parts << "SKIP #{render_expression(clause.skip)}" if clause.skip
324
336
  parts << "LIMIT #{render_expression(clause.limit)}" if clause.limit
325
- "#{parts.join(' ')}#{render_where(where)}"
337
+ parts.join(' ')
326
338
  end
327
339
 
328
340
  # In RETURN/WITH an aliased expression is defined once (`expr AS alias`);
@@ -177,6 +177,18 @@ module CypherDSL
177
177
  # The statement built so far as an `EXISTS { … }` condition.
178
178
  def as_condition = ExistsCondition.new(build)
179
179
 
180
+ # --- escape hatch ----------------------------------------------------
181
+
182
+ # A clause rendered verbatim, for Cypher the builder does not model, e.g.
183
+ # `raw_clause('CALL db.labels() YIELD label')`. Takes the arguments of
184
+ # `Cypher.raw` (a format with `$E` placeholders) or a ready {RawExpression}.
185
+ # The next clause starts afresh: a raw clause cannot be refined with WHERE,
186
+ # ORDER BY, SKIP or LIMIT.
187
+ def raw_clause(format, *)
188
+ expression = format.is_a?(RawExpression) ? format : RawExpression.create(format, *)
189
+ append(Clauses::Raw.new(expression:), nil)
190
+ end
191
+
180
192
  # --- finishing -------------------------------------------------------
181
193
 
182
194
  # Continues with the clauses of another statement, e.g. after a `CALL … YIELD`.
@@ -283,7 +295,8 @@ module CypherDSL
283
295
  arguments.each_slice(2).map { |target, value| Operation.new(target, '=', Expression.coerce(value)) }.freeze
284
296
  end
285
297
 
286
- def operation?(argument) = argument.is_a?(Operation) || argument.is_a?(LabelOperation)
298
+ # A raw fragment such as `Cypher.raw("n.name = 'x'")` counts as a complete operation.
299
+ def operation?(argument) = [Operation, LabelOperation, RawExpression].any? { |type| argument.is_a?(type) }
287
300
 
288
301
  def label_operation?(arguments)
289
302
  arguments.size > 1 && arguments.first.is_a?(Node) &&
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CypherDSL
4
- VERSION = '0.1.0.alpha.2'
4
+ VERSION = '0.1.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cypher-dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.alpha.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Heinrich Klobuczek
@@ -50,6 +50,7 @@ files:
50
50
  - lib/cypher_dsl/clauses/match.rb
51
51
  - lib/cypher_dsl/clauses/merge.rb
52
52
  - lib/cypher_dsl/clauses/order.rb
53
+ - lib/cypher_dsl/clauses/raw.rb
53
54
  - lib/cypher_dsl/clauses/raw_return.rb
54
55
  - lib/cypher_dsl/clauses/remove.rb
55
56
  - lib/cypher_dsl/clauses/return.rb
@@ -104,6 +105,7 @@ files:
104
105
  - lib/cypher_dsl/renderer/generated_name_resolver.rb
105
106
  - lib/cypher_dsl/renderer/name_resolver.rb
106
107
  - lib/cypher_dsl/renderer/visitor.rb
108
+ - lib/cypher_dsl/renderer/visitor/conditions.rb
107
109
  - lib/cypher_dsl/renderer/visitor/expressions.rb
108
110
  - lib/cypher_dsl/renderer/visitor/label_rendering.rb
109
111
  - lib/cypher_dsl/renderer/visitor/patterns.rb
@@ -142,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
144
  - !ruby/object:Gem::Version
143
145
  version: '0'
144
146
  requirements: []
145
- rubygems_version: 4.0.3
147
+ rubygems_version: 4.0.20
146
148
  specification_version: 4
147
149
  summary: A Ruby DSL (builder) for the Cypher query language
148
150
  test_files: []