@@ -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