Skip to content

Commit d49cddf

Browse files
committed
Monkey-patch Oracle adapter to fix illegal zero-length identifier
ORA-01741: illegal zero-length identifier This error is caused by composite key feature: rails/rails@c18a95e PR to fix it in oracle-enhanced: rsim/oracle-enhanced#2471
1 parent 6d61f77 commit d49cddf

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

config/initializers/oracle.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ def column(name, type, **options)
2323
ENV['NLS_LANG'] ||= 'AMERICAN_AMERICA.UTF8'
2424

2525
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do
26+
# Fixing OCIError: ORA-01741: illegal zero-length identifier
27+
# because of https://github.com/rails/rails/commit/c18a95e38e9860953236aed94c1bfb877fa3be84
28+
# the value of `columns` is [ "\"ACCOUNTS\".\"ID\"" ] which forms an incorrect query
29+
# ... OVER (PARTITION BY ["\"ACCOUNTS\".\"ID\""] ORDER BY "ACCOUNTS"."ID") ...
30+
# Will not be needed after https://github.com/rsim/oracle-enhanced/pull/2471 is merged and Rails upgraded
31+
def columns_for_distinct(columns, orders) # :nodoc:
32+
# construct a valid columns name for DISTINCT clause,
33+
# ie. one that includes the ORDER BY columns, using FIRST_VALUE such that
34+
# the inclusion of these columns doesn't invalidate the DISTINCT
35+
#
36+
# It does not construct DISTINCT clause. Just return column names for distinct.
37+
order_columns = orders.reject(&:blank?).map { |s|
38+
s = visitor.compile(s) unless s.is_a?(String)
39+
# remove any ASC/DESC modifiers
40+
s.gsub(/\s+(ASC|DESC)\s*?/i, "")
41+
}.reject(&:blank?).map.with_index { |column, i|
42+
"FIRST_VALUE(#{column}) OVER (PARTITION BY #{columns.join(', ')} ORDER BY #{column}) AS alias_#{i}__"
43+
}
44+
(order_columns << super).join(", ")
45+
end
46+
2647
prepend(Module.new do
2748
# TODO: is this needed after
2849
# https://github.com/rsim/oracle-enhanced/commit/f76b6ef4edda72bddabab252177cb7f28d4418e2

0 commit comments

Comments
 (0)