|
14 | 14 | See the License for the specific language governing permissions and |
15 | 15 | limitations under the License. |
16 | 16 | #} |
17 | | -{% macro oracle__get_catalog(information_schema, schemas) -%} |
18 | | - |
19 | | - {%- call statement('catalog', fetch_result=True) -%} |
20 | | - {# |
21 | | - If the user has multiple databases set and the first one is wrong, this will fail. |
22 | | - But we won't fail in the case where there are multiple quoting-difference-only dbs, which is better. |
23 | | - #} |
24 | | - {% set database = information_schema.database %} |
25 | | - {% if database == 'None' or database is undefined or database is none %} |
26 | | - {% set database = get_database_name() %} |
27 | | - {% endif %} |
28 | | - {{ adapter.verify_database(database) }} |
29 | 17 |
|
30 | | - with columns as ( |
31 | | - select |
32 | | - SYS_CONTEXT('userenv', 'DB_NAME') table_catalog, |
33 | | - owner table_schema, |
34 | | - table_name, |
35 | | - column_name, |
36 | | - data_type, |
37 | | - data_type_mod, |
38 | | - decode(data_type_owner, null, TO_CHAR(null), SYS_CONTEXT('userenv', 'DB_NAME')) domain_catalog, |
39 | | - data_type_owner domain_schema, |
40 | | - data_length character_maximum_length, |
41 | | - data_length character_octet_length, |
42 | | - data_length, |
43 | | - data_precision numeric_precision, |
44 | | - data_scale numeric_scale, |
45 | | - nullable is_nullable, |
46 | | - coalesce(column_id, 0) ordinal_position, |
47 | | - default_length, |
48 | | - data_default column_default, |
49 | | - num_distinct, |
50 | | - low_value, |
51 | | - high_value, |
52 | | - density, |
53 | | - num_nulls, |
54 | | - num_buckets, |
55 | | - last_analyzed, |
56 | | - sample_size, |
57 | | - SYS_CONTEXT('userenv', 'DB_NAME') character_set_catalog, |
58 | | - 'SYS' character_set_schema, |
59 | | - SYS_CONTEXT('userenv', 'DB_NAME') collation_catalog, |
60 | | - 'SYS' collation_schema, |
61 | | - character_set_name, |
62 | | - char_col_decl_length, |
63 | | - global_stats, |
64 | | - user_stats, |
65 | | - avg_col_len, |
66 | | - char_length, |
67 | | - char_used, |
68 | | - v80_fmt_image, |
69 | | - data_upgraded, |
70 | | - histogram |
71 | | - from sys.all_tab_columns |
72 | | - ), |
73 | | - tables as |
74 | | - (select SYS_CONTEXT('userenv', 'DB_NAME') table_catalog, |
| 18 | +{% macro oracle__get_catalog_tables_sql(information_schema) -%} |
| 19 | + select SYS_CONTEXT('userenv', 'DB_NAME') table_catalog, |
75 | 20 | owner table_schema, |
76 | 21 | table_name, |
77 | 22 | case |
|
82 | 27 | else 'BASE TABLE' |
83 | 28 | end table_type |
84 | 29 | from sys.all_tables |
85 | | - where upper(table_name) not in (select upper(mview_name) from sys.all_mviews) |
| 30 | + where upper(table_name) not in (select upper(mview_name) from sys.all_mviews) |
86 | 31 | union all |
87 | 32 | select SYS_CONTEXT('userenv', 'DB_NAME'), |
88 | 33 | owner, |
|
95 | 40 | mview_name, |
96 | 41 | 'MATERIALIZED VIEW' |
97 | 42 | from sys.all_mviews |
98 | | - ) |
99 | | - select |
100 | | - tables.table_catalog as "table_database", |
101 | | - tables.table_schema as "table_schema", |
102 | | - tables.table_name as "table_name", |
103 | | - tables.table_type as "table_type", |
104 | | - all_tab_comments.comments as "table_comment", |
105 | | - columns.column_name as "column_name", |
106 | | - ordinal_position as "column_index", |
107 | | - case |
108 | | - when data_type like '%CHAR%' |
109 | | - then data_type || '(' || cast(char_length as varchar(10)) || ')' |
110 | | - else data_type |
111 | | - end as "column_type", |
112 | | - all_col_comments.comments as "column_comment", |
113 | | - tables.table_schema as "table_owner" |
114 | | - from tables |
115 | | - inner join columns on upper(columns.table_catalog) = upper(tables.table_catalog) |
116 | | - and upper(columns.table_schema) = upper(tables.table_schema) |
117 | | - and upper(columns.table_name) = upper(tables.table_name) |
118 | | - left join all_tab_comments |
119 | | - on upper(all_tab_comments.owner) = upper(tables.table_schema) |
120 | | - and upper(all_tab_comments.table_name) = upper(tables.table_name) |
121 | | - left join all_col_comments |
122 | | - on upper(all_col_comments.owner) = upper(columns.table_schema) |
123 | | - and upper(all_col_comments.table_name) = upper(columns.table_name) |
124 | | - and upper(all_col_comments.column_name) = upper(columns.column_name) |
125 | | - where ( |
126 | | - {%- for schema in schemas -%} |
127 | | - upper(tables.table_schema) = upper('{{ schema }}'){%- if not loop.last %} or {% endif -%} |
128 | | - {%- endfor -%} |
129 | | - ) |
130 | | - order by |
131 | | - tables.table_schema, |
132 | | - tables.table_name, |
133 | | - ordinal_position |
134 | | - {%- endcall -%} |
| 43 | +{%- endmacro %} |
| 44 | + |
| 45 | +{% macro oracle__get_catalog_columns_sql(information_schema) -%} |
| 46 | + select |
| 47 | + SYS_CONTEXT('userenv', 'DB_NAME') table_catalog, |
| 48 | + owner table_schema, |
| 49 | + table_name, |
| 50 | + column_name, |
| 51 | + data_type, |
| 52 | + data_type_mod, |
| 53 | + decode(data_type_owner, null, TO_CHAR(null), SYS_CONTEXT('userenv', 'DB_NAME')) domain_catalog, |
| 54 | + data_type_owner domain_schema, |
| 55 | + data_length character_maximum_length, |
| 56 | + data_length character_octet_length, |
| 57 | + data_length, |
| 58 | + data_precision numeric_precision, |
| 59 | + data_scale numeric_scale, |
| 60 | + nullable is_nullable, |
| 61 | + coalesce(column_id, 0) ordinal_position, |
| 62 | + default_length, |
| 63 | + data_default column_default, |
| 64 | + num_distinct, |
| 65 | + low_value, |
| 66 | + high_value, |
| 67 | + density, |
| 68 | + num_nulls, |
| 69 | + num_buckets, |
| 70 | + last_analyzed, |
| 71 | + sample_size, |
| 72 | + SYS_CONTEXT('userenv', 'DB_NAME') character_set_catalog, |
| 73 | + 'SYS' character_set_schema, |
| 74 | + SYS_CONTEXT('userenv', 'DB_NAME') collation_catalog, |
| 75 | + 'SYS' collation_schema, |
| 76 | + character_set_name, |
| 77 | + char_col_decl_length, |
| 78 | + global_stats, |
| 79 | + user_stats, |
| 80 | + avg_col_len, |
| 81 | + char_length, |
| 82 | + char_used, |
| 83 | + v80_fmt_image, |
| 84 | + data_upgraded, |
| 85 | + histogram |
| 86 | + from sys.all_tab_columns |
| 87 | +{%- endmacro %} |
| 88 | + |
| 89 | +{% macro oracle__get_catalog_results_sql() -%} |
| 90 | + select |
| 91 | + tables.table_catalog as "table_database", |
| 92 | + tables.table_schema as "table_schema", |
| 93 | + tables.table_name as "table_name", |
| 94 | + tables.table_type as "table_type", |
| 95 | + all_tab_comments.comments as "table_comment", |
| 96 | + columns.column_name as "column_name", |
| 97 | + ordinal_position as "column_index", |
| 98 | + case |
| 99 | + when data_type like '%CHAR%' |
| 100 | + then data_type || '(' || cast(char_length as varchar(10)) || ')' |
| 101 | + else data_type |
| 102 | + end as "column_type", |
| 103 | + all_col_comments.comments as "column_comment", |
| 104 | + tables.table_schema as "table_owner" |
| 105 | + from tables |
| 106 | + inner join columns on upper(columns.table_catalog) = upper(tables.table_catalog) |
| 107 | + and upper(columns.table_schema) = upper(tables.table_schema) |
| 108 | + and upper(columns.table_name) = upper(tables.table_name) |
| 109 | + left join all_tab_comments |
| 110 | + on upper(all_tab_comments.owner) = upper(tables.table_schema) |
| 111 | + and upper(all_tab_comments.table_name) = upper(tables.table_name) |
| 112 | + left join all_col_comments |
| 113 | + on upper(all_col_comments.owner) = upper(columns.table_schema) |
| 114 | + and upper(all_col_comments.table_name) = upper(columns.table_name) |
| 115 | + and upper(all_col_comments.column_name) = upper(columns.column_name) |
| 116 | +{%- endmacro %} |
| 117 | + |
| 118 | +{% macro oracle__get_catalog_schemas_where_clause_sql(schemas) -%} |
| 119 | + where ( |
| 120 | + {%- for schema in schemas -%} |
| 121 | + upper(tables.table_schema) = upper('{{ schema }}'){%- if not loop.last %} or {% endif -%} |
| 122 | + {%- endfor -%} |
| 123 | + ) |
| 124 | +{%- endmacro %} |
| 125 | + |
| 126 | +{% macro oracle__get_catalog_relations_where_clause_sql(relations) -%} |
| 127 | + where ( |
| 128 | + {%- for relation in relations -%} |
| 129 | + {% if relation.schema and relation.identifier %} |
| 130 | + ( |
| 131 | + upper(tables.table_schema) = upper('{{ relation.schema }}') |
| 132 | + and upper(tables.table_name) = upper('{{ relation.identifier }}') |
| 133 | + ) |
| 134 | + {% elif relation.schema %} |
| 135 | + ( |
| 136 | + upper(tables.table_schema) = upper('{{ relation.schema }}') |
| 137 | + ) |
| 138 | + {% else %} |
| 139 | + {% do exceptions.raise_compiler_error( |
| 140 | + '`get_catalog_relations` requires a list of relations, each with a schema' |
| 141 | + ) %} |
| 142 | + {% endif %} |
| 143 | + |
| 144 | + {%- if not loop.last %} or {% endif -%} |
| 145 | + {%- endfor -%} |
| 146 | + ) |
| 147 | +{%- endmacro %} |
| 148 | + |
| 149 | +{% macro oracle__get_catalog(information_schema, schemas) -%} |
| 150 | + {% set query %} |
| 151 | + with tables as ( |
| 152 | + {{ oracle__get_catalog_tables_sql(information_schema) }} |
| 153 | + ), |
| 154 | + columns as ( |
| 155 | + {{ oracle__get_catalog_columns_sql(information_schema) }} |
| 156 | + ) |
| 157 | + {{ oracle__get_catalog_results_sql() }} |
| 158 | + {{ oracle__get_catalog_schemas_where_clause_sql(schemas) }} |
| 159 | + order by |
| 160 | + tables.table_schema, |
| 161 | + tables.table_name, |
| 162 | + ordinal_position |
| 163 | + {%- endset -%} |
| 164 | + {{ return(run_query(query)) }} |
| 165 | +{%- endmacro %} |
| 166 | + |
| 167 | +{% macro oracle__get_catalog_relations(information_schema, relations) -%} |
| 168 | + {% set query %} |
| 169 | + with tables as ( |
| 170 | + {{ oracle__get_catalog_tables_sql(information_schema) }} |
| 171 | + ), |
| 172 | + columns as ( |
| 173 | + {{ oracle__get_catalog_columns_sql(information_schema) }} |
| 174 | + ) |
| 175 | + {{ oracle__get_catalog_results_sql() }} |
| 176 | + {{ oracle__get_catalog_relations_where_clause_sql(relations) }} |
| 177 | + order by |
| 178 | + tables.table_schema, |
| 179 | + tables.table_name, |
| 180 | + ordinal_position |
| 181 | + {%- endset -%} |
135 | 182 |
|
136 | | - {{ return(load_result('catalog').table) }} |
| 183 | + {{ return(run_query(query)) }} |
137 | 184 |
|
138 | 185 | {%- endmacro %} |
0 commit comments