Class: Cyrel::AST::Compiler
- Inherits:
-
Object
- Object
- Cyrel::AST::Compiler
- Defined in:
- lib/cyrel/ast/compiler.rb
Overview
Compiles AST nodes into Cypher queries with optional thread-safety It’s like Google Translate, but for graph databases and with more reliable results
Direct Known Subclasses
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Instance Method Summary collapse
-
#compile(node_or_nodes) ⇒ Object
Compile an AST node or array of nodes Returns [cypher_string, parameters_hash].
-
#initialize ⇒ Compiler
constructor
A new instance of Compiler.
-
#visit_call_node(node) ⇒ Object
Visit a CALL clause node (procedure call) Invoking the stored procedures of the graph database.
-
#visit_call_subquery_node(node) ⇒ Object
Visit a CALL subquery node Queries all the way down.
-
#visit_create_node(node) ⇒ Object
Visit a CREATE clause node Making nodes and relationships appear out of thin air.
-
#visit_delete_node(node) ⇒ Object
Visit a DELETE clause node Making nodes disappear, potentially with their relationships.
-
#visit_foreach_node(node) ⇒ Object
Visit a FOREACH node Iterating through lists like a database therapist.
-
#visit_limit_node(node) ⇒ Object
Visit a LIMIT node Because sometimes less is more, except in this comment.
-
#visit_literal_node(node) ⇒ Object
Visit a literal value node The most honest node in the entire tree.
-
#visit_load_csv_node(node) ⇒ Object
Visit a LOAD CSV node Reading CSV files like it’s 1999.
-
#visit_match_node(node) ⇒ Object
Visit a MATCH clause node Finding nodes in the graph, one pattern at a time.
-
#visit_merge_node(node) ⇒ Object
Visit a MERGE clause node Finding or creating, because commitment issues.
-
#visit_order_by_node(node) ⇒ Object
Visit an ORDER BY node Because even chaos needs some structure sometimes.
-
#visit_remove_node(node) ⇒ Object
Visit a REMOVE clause node Tidying up properties and labels.
-
#visit_return_node(node) ⇒ Object
Visit a RETURN node Where your data comes home to roost.
-
#visit_set_node(node) ⇒ Object
Visit a SET node Where change happens, one property at a time.
-
#visit_skip_node(node) ⇒ Object
Visit a SKIP node For when you want to jump ahead in your results.
-
#visit_union_node(node) ⇒ Object
Visit a UNION node Combining queries like a Cypher mixologist.
-
#visit_unwind_node(node) ⇒ Object
Visit an UNWIND node Unpacking arrays like unwrapping presents.
-
#visit_where_node(node) ⇒ Object
Visit a WHERE node Because sometimes you need to be selective about your relationships.
-
#visit_with_node(node) ⇒ Object
Visit a WITH node Because sometimes you need to pass data along for the next part of your journey.
Constructor Details
#initialize ⇒ Compiler
Returns a new instance of Compiler.
12 13 14 15 16 17 18 |
# File 'lib/cyrel/ast/compiler.rb', line 12 def initialize @output = StringIO.new @parameters = {} @param_counter = 0 @first_clause = true @loop_variables = Set.new # Track loop variables that shouldn't be parameterized end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
10 11 12 |
# File 'lib/cyrel/ast/compiler.rb', line 10 def output @output end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
10 11 12 |
# File 'lib/cyrel/ast/compiler.rb', line 10 def parameters @parameters end |
Instance Method Details
#compile(node_or_nodes) ⇒ Object
Compile an AST node or array of nodes Returns [cypher_string, parameters_hash]
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cyrel/ast/compiler.rb', line 22 def compile(node_or_nodes) nodes = Array(node_or_nodes) nodes.each do |node| add_clause_separator unless @first_clause node.accept(self) @first_clause = false end [@output.string, @parameters] end |
#visit_call_node(node) ⇒ Object
Visit a CALL clause node (procedure call) Invoking the stored procedures of the graph database
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/cyrel/ast/compiler.rb', line 228 def visit_call_node(node) @output << "CALL #{node.procedure_name}" if node.arguments.any? @output << '(' node.arguments.each_with_index do |arg, i| @output << ', ' if i.positive? render_expression(Expression.coerce(arg)) end @output << ')' end return unless node.yield_items @output << ' YIELD ' if node.yield_items.is_a?(Hash) # YIELD items with aliases node.yield_items.each_with_index do |(item, alias_name), i| @output << ', ' if i.positive? @output << item.to_s @output << " AS #{alias_name}" if alias_name && alias_name != item end else # Simple yield list Array(node.yield_items).each_with_index do |item, i| @output << ', ' if i.positive? @output << item.to_s end end end |
#visit_call_subquery_node(node) ⇒ Object
Visit a CALL subquery node Queries all the way down
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/cyrel/ast/compiler.rb', line 261 def visit_call_subquery_node(node) @output << "CALL {\n" # Render subquery clauses directly without going through the cache # to avoid recursive locking issues subquery = node.subquery subquery_clauses = subquery.clauses.sort_by { |clause| subquery.send(:clause_order, clause) } subquery_clauses.each_with_index do |clause, index| @output << "\n" if index.positive? # Add newline between clauses if clause.is_a?(AST::ClauseAdapter) # For AST-based clauses, compile directly without cache # Create a proxy object that forwards parameter registration to this compiler parameter_proxy = Object.new parent_compiler = self parameter_proxy.define_singleton_method(:register_parameter) do |value| parent_compiler.send(:register_parameter, value) end subquery_compiler = QueryIntegratedCompiler.new(parameter_proxy) clause_cypher, = subquery_compiler.compile(clause.ast_node) @output << clause_cypher.split("\n").map { |line| " #{line}" }.join("\n") else # For legacy clauses, render normally clause_output = clause.render(subquery) @output << clause_output.split("\n").map { |line| " #{line}" }.join("\n") unless clause_output.blank? # Merge subquery parameters subquery.parameters.each_value do |value| register_parameter(value) end end end @output << "\n}" end |
#visit_create_node(node) ⇒ Object
Visit a CREATE clause node Making nodes and relationships appear out of thin air
168 169 170 171 |
# File 'lib/cyrel/ast/compiler.rb', line 168 def visit_create_node(node) @output << 'CREATE ' render_pattern(node.pattern) end |
#visit_delete_node(node) ⇒ Object
Visit a DELETE clause node Making nodes disappear, potentially with their relationships
194 195 196 197 198 199 200 201 202 |
# File 'lib/cyrel/ast/compiler.rb', line 194 def visit_delete_node(node) @output << 'DETACH ' if node.detach @output << 'DELETE ' node.variables.each_with_index do |var, i| @output << ', ' if i.positive? @output << var.to_s end end |
#visit_foreach_node(node) ⇒ Object
Visit a FOREACH node Iterating through lists like a database therapist
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/cyrel/ast/compiler.rb', line 346 def visit_foreach_node(node) @output << "FOREACH (#{node.variable} IN " # Handle the expression - could be an array literal or an expression if node.expression.is_a?(Array) # Array literal - convert to parameter param_key = register_parameter(node.expression) @output << "$#{param_key}" elsif node.expression.is_a?(Symbol) # Symbol reference to parameter param_key = register_parameter(node.expression) @output << "$#{param_key}" else # Other expressions render_expression(node.expression) end @output << ' | ' # Track the loop variable so it doesn't get parameterized in inner clauses old_loop_variables = @loop_variables.dup @loop_variables.add(node.variable.to_sym) # Render update clauses without duplication node.update_clauses.each_with_index do |clause, index| @output << ' ' if index.positive? raise "Unexpected clause type in FOREACH: #{clause.class}" unless clause.is_a?(AST::ClauseAdapter) # For AST-based clauses, compile just the inner content # Create a proxy object that forwards parameter registration to this compiler # and inherits the loop variable context parameter_proxy = Object.new parent_compiler = self current_loop_variables = @loop_variables.dup parameter_proxy.define_singleton_method(:register_parameter) do |value| # Check if this is a loop variable that shouldn't be parameterized if value.is_a?(Symbol) && current_loop_variables.include?(value) value # Return the symbol itself, not a parameter key else parent_compiler.send(:register_parameter, value) end end inner_compiler = QueryIntegratedCompiler.new(parameter_proxy) # Pass the loop variables context to the inner compiler inner_compiler.instance_variable_set(:@loop_variables, @loop_variables.dup) clause_cypher, = inner_compiler.compile([clause.ast_node]) @output << clause_cypher # For other clause types, render directly end # Restore previous loop variables context @loop_variables = old_loop_variables @output << ')' end |
#visit_limit_node(node) ⇒ Object
Visit a LIMIT node Because sometimes less is more, except in this comment
36 37 38 39 |
# File 'lib/cyrel/ast/compiler.rb', line 36 def visit_limit_node(node) @output << 'LIMIT ' render_expression(node.expression) end |
#visit_literal_node(node) ⇒ Object
Visit a literal value node The most honest node in the entire tree
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/cyrel/ast/compiler.rb', line 431 def visit_literal_node(node) if node.value.is_a?(Symbol) # Check if this symbol is a loop variable @output << if @loop_variables.include?(node.value) # Loop variables are rendered as-is, not as parameters node.value.to_s else # Symbols are parameter references, not values to be parameterized "$#{node.value}" end else # All other literals become parameters for consistency with existing behavior param_key = register_parameter(node.value) @output << "$#{param_key}" end end |
#visit_load_csv_node(node) ⇒ Object
Visit a LOAD CSV node Reading CSV files like it’s 1999
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/cyrel/ast/compiler.rb', line 407 def visit_load_csv_node(node) @output << 'LOAD CSV' @output << ' WITH HEADERS' if node.with_headers @output << ' FROM ' # URL can be a string or expression if node.url.is_a?(String) param_key = register_parameter(node.url) @output << "$#{param_key}" else render_expression(node.url) end @output << " AS #{node.variable}" return unless node.fieldterminator @output << ' FIELDTERMINATOR ' param_key = register_parameter(node.fieldterminator) @output << "$#{param_key}" end |
#visit_match_node(node) ⇒ Object
Visit a MATCH clause node Finding nodes in the graph, one pattern at a time
156 157 158 159 160 161 162 163 164 |
# File 'lib/cyrel/ast/compiler.rb', line 156 def visit_match_node(node) @output << (node.optional ? 'OPTIONAL MATCH ' : 'MATCH ') # Handle path variable assignment if present @output << "#{node.path_variable} = " if node.path_variable # Render the pattern render_pattern(node.pattern) end |
#visit_merge_node(node) ⇒ Object
Visit a MERGE clause node Finding or creating, because commitment issues
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/cyrel/ast/compiler.rb', line 175 def visit_merge_node(node) @output << 'MERGE ' render_pattern(node.pattern) # Handle ON CREATE SET if node.on_create @output << "\nON CREATE SET " render_merge_assignments(node.on_create) end # Handle ON MATCH SET return unless node.on_match @output << "\nON MATCH SET " render_merge_assignments(node.on_match) end |
#visit_order_by_node(node) ⇒ Object
Visit an ORDER BY node Because even chaos needs some structure sometimes
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cyrel/ast/compiler.rb', line 50 def visit_order_by_node(node) @output << 'ORDER BY ' @in_order_by = true node.items.each_with_index do |(expr, direction), index| @output << ', ' if index.positive? render_expression(expr) @output << " #{direction.to_s.upcase}" if direction && direction != :asc end @in_order_by = false end |
#visit_remove_node(node) ⇒ Object
Visit a REMOVE clause node Tidying up properties and labels
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/cyrel/ast/compiler.rb', line 206 def visit_remove_node(node) @output << 'REMOVE ' node.items.each_with_index do |item, i| @output << ', ' if i.positive? case item when Expression::PropertyAccess # Remove property render_expression(item) when Array # Remove label [variable, label] variable, label = item @output << "#{variable}:#{label}" else raise "Unknown REMOVE item type: #{item.class}" end end end |
#visit_return_node(node) ⇒ Object
Visit a RETURN node Where your data comes home to roost
83 84 85 86 87 88 89 90 91 |
# File 'lib/cyrel/ast/compiler.rb', line 83 def visit_return_node(node) @output << 'RETURN ' @output << 'DISTINCT ' if node.distinct node.items.each_with_index do |item, index| @output << ', ' if index.positive? render_expression(item) end end |
#visit_set_node(node) ⇒ Object
Visit a SET node Where change happens, one property at a time
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cyrel/ast/compiler.rb', line 95 def visit_set_node(node) return if node.assignments.empty? @output << 'SET ' node.assignments.each_with_index do |assignment, index| @output << ', ' if index.positive? render_assignment(assignment) end end |
#visit_skip_node(node) ⇒ Object
Visit a SKIP node For when you want to jump ahead in your results
43 44 45 46 |
# File 'lib/cyrel/ast/compiler.rb', line 43 def visit_skip_node(node) @output << 'SKIP ' render_expression(node.amount) end |
#visit_union_node(node) ⇒ Object
Visit a UNION node Combining queries like a Cypher mixologist
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/cyrel/ast/compiler.rb', line 301 def visit_union_node(node) # UNION is special - it combines complete queries # We need to render each query's clauses directly to avoid recursive locking node.queries.each_with_index do |query, index| if index.positive? @output << "\n" @output << 'UNION' @output << ' ALL' if node.all @output << "\n" end # Render query clauses directly without going through cache query_clauses = query.clauses.sort_by { |clause| query.send(:clause_order, clause) } query_clauses.each_with_index do |clause, clause_index| @output << "\n" if clause_index.positive? if clause.is_a?(AST::ClauseAdapter) # For AST-based clauses, compile directly without cache # Create a proxy object that forwards parameter registration to this compiler parameter_proxy = Object.new parent_compiler = self parameter_proxy.define_singleton_method(:register_parameter) do |value| parent_compiler.send(:register_parameter, value) end clause_compiler = QueryIntegratedCompiler.new(parameter_proxy) clause_cypher, = clause_compiler.compile(clause.ast_node) @output << clause_cypher else # For legacy clauses, render normally clause_output = clause.render(query) @output << clause_output unless clause_output.blank? # Merge query parameters query.parameters.each_value do |value| register_parameter(value) end end end end end |
#visit_unwind_node(node) ⇒ Object
Visit an UNWIND node Unpacking arrays like unwrapping presents
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/cyrel/ast/compiler.rb', line 135 def visit_unwind_node(node) @output << 'UNWIND ' # Render the expression to unwind if node.expression.is_a?(Array) # Array literal @output << format_array_literal(node.expression) elsif node.expression.is_a?(Symbol) # Parameter reference param_key = register_parameter(node.expression) @output << "$#{param_key}" else # Other expressions render_expression(node.expression) end @output << " AS #{node.alias_name}" end |
#visit_where_node(node) ⇒ Object
Visit a WHERE node Because sometimes you need to be selective about your relationships
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cyrel/ast/compiler.rb', line 65 def visit_where_node(node) return if node.conditions.empty? @output << 'WHERE ' if node.conditions.length == 1 render_expression(node.conditions.first) else # Combine multiple conditions with AND node.conditions.each_with_index do |condition, index| @output << ' AND ' if index.positive? render_expression(condition) end end end |
#visit_with_node(node) ⇒ Object
Visit a WITH node Because sometimes you need to pass data along for the next part of your journey
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/cyrel/ast/compiler.rb', line 108 def visit_with_node(node) @output << 'WITH ' @output << 'DISTINCT ' if node.distinct node.items.each_with_index do |item, index| @output << ', ' if index.positive? render_expression(item) end # Add WHERE clause if present return unless node.where_conditions && !node.where_conditions.empty? @output << "\nWHERE " if node.where_conditions.length == 1 render_expression(node.where_conditions.first) else # Combine multiple conditions with AND node.where_conditions.each_with_index do |condition, index| @output << ' AND ' if index.positive? render_expression(condition) end end end |