-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add querying and additional context to the enhanced Arel AST (#106)
* Add contextual information to transformer nodes * Sort rubocop * Fixed lint issues * Fix comparison for select,update,insert and delete manager * Added .query method to Node * Added AddchemaToTable transformer * Add support for schemas in function calls * Add missing attributes to the SelectCore dot visitor * Added TODO to Arel middleware
- Loading branch information
Showing
31 changed files
with
532 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Arel | ||
module Transformer | ||
class AddSchemaToTable | ||
attr_reader :schema_name | ||
|
||
def initialize(schema_name) | ||
@schema_name = schema_name | ||
end | ||
|
||
# https://github.com/mvgijssel/arel_toolkit/issues/110 | ||
def call(arel, _context) | ||
tree = Arel.transformer(arel) | ||
|
||
tree.query( | ||
class: Arel::Table, | ||
schema_name: nil, | ||
context: { range_variable: true }, | ||
).each do |node| | ||
node['schema_name'].replace(schema_name) | ||
end | ||
|
||
tree.object | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module Arel | ||
module Transformer | ||
module ContextEnhancer | ||
class ArelTable | ||
# rubocop:disable Metrics/PerceivedComplexity | ||
# rubocop:disable Metrics/CyclomaticComplexity | ||
# rubocop:disable Metrics/AbcSize | ||
def self.call(node) | ||
context = node.context.merge!(range_variable: false, column_reference: false) | ||
parent_object = node.parent.object | ||
|
||
# Using Arel::Table as SELECT ... FROM <table> | ||
if parent_object.is_a?(Arel::Nodes::JoinSource) | ||
context[:range_variable] = true | ||
|
||
# Using Arel::Table as SELECT ... FROM [<table>] | ||
elsif parent_object.is_a?(Array) && | ||
node.parent.parent.object.is_a?(Arel::Nodes::JoinSource) | ||
context[:range_variable] = true | ||
|
||
# Using Arel::Table as SELECT ... INNER JOIN <table> ON TRUE | ||
elsif parent_object.is_a?(Arel::Nodes::Join) | ||
context[:range_variable] = true | ||
|
||
# Using Arel::Table as an attribute SELECT <table>.id ... | ||
elsif parent_object.is_a?(Arel::Attributes::Attribute) | ||
context[:column_reference] = true | ||
|
||
# Using Arel::Table in an INSERT INTO <table> | ||
elsif parent_object.is_a?(Arel::Nodes::InsertStatement) | ||
context[:range_variable] = true | ||
|
||
# Using Arel::Table in an UPDATE <table> ... | ||
elsif parent_object.is_a?(Arel::Nodes::UpdateStatement) | ||
context[:range_variable] = true | ||
|
||
# Arel::Table in UPDATE ... FROM [<table>] | ||
elsif parent_object.is_a?(Array) && | ||
node.parent.parent.object.is_a?(Arel::Nodes::UpdateStatement) | ||
context[:range_variable] = true | ||
|
||
# Using Arel::Table in an DELETE FROM <table> | ||
elsif parent_object.is_a?(Arel::Nodes::DeleteStatement) | ||
context[:range_variable] = true | ||
|
||
# Arel::Table in DELETE ... USING [<table>] | ||
elsif parent_object.is_a?(Array) && | ||
node.parent.parent.object.is_a?(Arel::Nodes::DeleteStatement) | ||
context[:range_variable] = true | ||
|
||
# Using Arel::Table as an "alias" for WITH <table> AS (SELECT 1) SELECT 1 | ||
elsif parent_object.is_a?(Arel::Nodes::As) && | ||
node.parent.parent.parent.object.is_a?(Arel::Nodes::With) | ||
context[:alias] = true | ||
|
||
# Using Arel::Table as an "alias" for WITH RECURSIVE <table> AS (SELECT 1) SELECT 1 | ||
elsif parent_object.is_a?(Arel::Nodes::As) && | ||
node.parent.parent.parent.object.is_a?(Arel::Nodes::WithRecursive) | ||
context[:alias] = true | ||
|
||
# Using Arel::Table as an "alias" for SELECT INTO <table> ... | ||
elsif parent_object.is_a?(Arel::Nodes::Into) | ||
context[:alias] = true | ||
|
||
else | ||
raise "Unknown AST location for table #{node.inspect}, #{node.root_node.to_sql}" | ||
end | ||
end | ||
# rubocop:enable Metrics/PerceivedComplexity | ||
# rubocop:enable Metrics/CyclomaticComplexity | ||
# rubocop:enable Metrics/AbcSize | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Arel | ||
module Transformer | ||
class Query | ||
def self.call(node, kwargs) | ||
node_attributes = %i[context parent] | ||
node_args = kwargs.slice(*node_attributes) | ||
object_args = kwargs.except(*node_attributes) | ||
|
||
node.each.select do |child_node| | ||
next unless matches?(child_node, node_args) | ||
|
||
matches?(child_node.object, object_args) | ||
end | ||
end | ||
|
||
def self.matches?(object, test) | ||
case test | ||
when Hash | ||
case object | ||
when Hash | ||
test <= object | ||
else | ||
test.all? do |test_key, test_value| | ||
next false unless object.respond_to?(test_key) | ||
|
||
object_attribute_value = object.public_send(test_key) | ||
matches? object_attribute_value, test_value | ||
end | ||
end | ||
else | ||
object == test | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.