@@ -756,32 +756,39 @@ def resolve_data_source_name(name)
756756 end
757757
758758 binds = [
759- bind_string ( "table_owner" , owner ) ,
760- bind_string ( "table_name" , identifier ) ,
761- bind_string ( "table_owner" , owner ) ,
762- bind_string ( "table_name" , identifier ) ,
763759 bind_string ( "table_owner" , owner ) ,
764760 bind_string ( "table_name" , identifier ) ,
765761 bind_string ( "real_name" , real_name ) ,
766762 ]
763+ # Single-pass lookup against all_objects, ordered so the first row
764+ # is the one the legacy 4-way UNION ALL (all_tables, all_views,
765+ # owner synonym, public synonym) would have returned: prefer a
766+ # match in the caller's schema over PUBLIC, and within a schema
767+ # prefer TABLE over VIEW over SYNONYM.
767768 result = select_one ( <<~SQL . squish , "SCHEMA" , binds )
768- SELECT owner, table_name, 'TABLE' name_type
769- FROM all_tables WHERE owner = :table_owner AND table_name = :table_name
770- UNION ALL
771- SELECT owner, view_name table_name, 'VIEW' name_type
772- FROM all_views WHERE owner = :table_owner AND view_name = :table_name
773- UNION ALL
774- SELECT table_owner, table_name, 'SYNONYM' name_type
775- FROM all_synonyms WHERE owner = :table_owner AND synonym_name = :table_name
776- UNION ALL
777- SELECT table_owner, table_name, 'SYNONYM' name_type
778- FROM all_synonyms WHERE owner = 'PUBLIC' AND synonym_name = :real_name
769+ SELECT owner, object_name table_name, object_type name_type
770+ FROM all_objects
771+ WHERE ((owner = :table_owner AND object_name = :table_name)
772+ OR (owner = 'PUBLIC' AND object_name = :real_name))
773+ AND object_type IN ('TABLE', 'VIEW', 'SYNONYM')
774+ ORDER BY DECODE(owner, 'PUBLIC', 2, 1),
775+ DECODE(object_type, 'TABLE', 1, 'VIEW', 2, 'SYNONYM', 3)
779776 SQL
780777
781778 raise OracleEnhanced ::ConnectionException , %Q{"DESC #{ name } " failed; does it exist?} unless result
782779
783780 if result [ "name_type" ] == "SYNONYM"
784- name = "#{ result [ 'owner' ] && "#{ result [ 'owner' ] } ." } #{ result [ 'table_name' ] } "
781+ synonym_binds = [
782+ bind_string ( "owner" , result [ "owner" ] ) ,
783+ bind_string ( "synonym_name" , result [ "table_name" ] ) ,
784+ ]
785+ syn = select_one ( <<~SQL . squish , "SCHEMA" , synonym_binds )
786+ SELECT table_owner, table_name
787+ FROM all_synonyms
788+ WHERE owner = :owner AND synonym_name = :synonym_name
789+ SQL
790+ raise OracleEnhanced ::ConnectionException , %Q{"DESC #{ name } " failed; does it exist?} unless syn
791+ name = "#{ syn [ 'table_owner' ] && "#{ syn [ 'table_owner' ] } ." } #{ syn [ 'table_name' ] } "
785792 else
786793 return [ result [ "owner" ] , result [ "table_name" ] ]
787794 end
0 commit comments