Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

When using 'oracle_enhanced', remove 'AS' on the table_name alias. #298

Merged
merged 2 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/closure_tree/finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def find_all_by_generation(generation_level)
WHERE ancestor_id = #{_ct.quote(self.id)}
GROUP BY descendant_id
HAVING MAX(#{_ct.quoted_hierarchy_table_name}.generations) = #{generation_level.to_i}
) AS descendants ON (#{_ct.quoted_table_name}.#{_ct.base_class.primary_key} = descendants.descendant_id)
) #{ _ct.t_alias_keyword } descendants ON (#{_ct.quoted_table_name}.#{_ct.base_class.primary_key} = descendants.descendant_id)
SQL
_ct.scope_with_order(s)
end
Expand Down Expand Up @@ -76,7 +76,7 @@ def leaves
FROM #{_ct.quoted_hierarchy_table_name}
GROUP BY ancestor_id
HAVING MAX(#{_ct.quoted_hierarchy_table_name}.generations) = 0
) AS leaves ON (#{_ct.quoted_table_name}.#{primary_key} = leaves.ancestor_id)
) #{ _ct.t_alias_keyword } leaves ON (#{_ct.quoted_table_name}.#{primary_key} = leaves.ancestor_id)
SQL
_ct.scope_with_order(s.readonly(false))
end
Expand All @@ -96,13 +96,13 @@ def find_all_by_generation(generation_level)
SELECT #{primary_key} as root_id
FROM #{_ct.quoted_table_name}
WHERE #{_ct.quoted_parent_column_name} IS NULL
) AS roots ON (1 = 1)
) #{ _ct.t_alias_keyword } roots ON (1 = 1)
INNER JOIN (
SELECT ancestor_id, descendant_id
FROM #{_ct.quoted_hierarchy_table_name}
GROUP BY ancestor_id, descendant_id
HAVING MAX(generations) = #{generation_level.to_i}
) AS descendants ON (
) #{ _ct.t_alias_keyword } descendants ON (
#{_ct.quoted_table_name}.#{primary_key} = descendants.descendant_id
AND roots.root_id = descendants.ancestor_id
)
Expand All @@ -122,7 +122,7 @@ def find_by_path(path, attributes = {}, parent_id = nil)
path.reverse.each_with_index do |ea, idx|
next_joined_table = "p#{idx}"
scope = scope.joins(<<-SQL.strip_heredoc)
INNER JOIN #{_ct.quoted_table_name} AS #{next_joined_table}
INNER JOIN #{_ct.quoted_table_name} #{ _ct.t_alias_keyword } #{next_joined_table}
ON #{next_joined_table}.#{_ct.quoted_id_column_name} =
#{connection.quote_table_name(last_joined_table)}.#{_ct.quoted_parent_column_name}
SQL
Expand Down
2 changes: 1 addition & 1 deletion lib/closure_tree/hash_tree_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def default_tree_scope(scope, limit_depth = nil)
FROM #{quoted_hierarchy_table_name}
GROUP BY descendant_id
#{having_clause}
) AS generation_depth
) #{ t_alias_keyword } generation_depth
ON #{quoted_table_name}.#{model_class.primary_key} = generation_depth.descendant_id
SQL
scope_with_order(scope.joins(generation_depth), 'generation_depth.depth')
Expand Down
2 changes: 1 addition & 1 deletion lib/closure_tree/hierarchy_maintenance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def delete_hierarchy_references
FROM #{_ct.quoted_hierarchy_table_name}
WHERE ancestor_id = #{_ct.quote(id)}
OR descendant_id = #{_ct.quote(id)}
) AS x )
) #{ _ct.t_alias_keyword } x )
SQL
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/closure_tree/numeric_deterministic_ordering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def roots_and_descendants_preordered
SELECT descendant_id, max(generations) AS max_depth
FROM #{_ct.quoted_hierarchy_table_name}
GROUP BY descendant_id
) AS depths ON depths.descendant_id = anc.#{_ct.quoted_id_column_name}
) #{ _ct.t_alias_keyword } depths ON depths.descendant_id = anc.#{_ct.quoted_id_column_name}
SQL
joins(join_sql)
.group("#{_ct.quoted_table_name}.#{_ct.quoted_id_column_name}")
Expand Down
5 changes: 5 additions & 0 deletions lib/closure_tree/support_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,10 @@ def quoted_order_column(include_table_name = true)
prefix = include_table_name ? "#{quoted_table_name}." : ""
"#{prefix}#{connection.quote_column_name(order_column)}"
end

# table_name alias keyword , like "AS". When used on table name alias, Oracle Database don't support used 'AS'
def t_alias_keyword
(ActiveRecord::Base.connection.adapter_name.to_sym == :OracleEnhanced) ? "" : :AS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why :AS is symbol instead of a string ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using #{} to embed string, there is no difference in symbol or string. Using symbols is my habit.

end
end
end