-
Notifications
You must be signed in to change notification settings - Fork 53
/
adapters.sql
140 lines (122 loc) · 4.5 KB
/
adapters.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{% macro mysql__list_schemas(database) %}
{% call statement('list_schemas', fetch_result=True, auto_begin=False) -%}
select distinct schema_name
from information_schema.schemata
{%- endcall %}
{{ return(load_result('list_schemas').table) }}
{% endmacro %}
{% macro mysql__create_schema(relation) -%}
{%- call statement('create_schema') -%}
create schema if not exists {{ relation.without_identifier() }}
{%- endcall -%}
{% endmacro %}
{% macro mysql__drop_schema(relation) -%}
{%- call statement('drop_schema') -%}
drop schema if exists {{ relation.without_identifier() }}
{% endcall %}
{% endmacro %}
{% macro mysql__drop_relation(relation) -%}
{% call statement('drop_relation', auto_begin=False) -%}
drop {{ relation.type }} if exists {{ relation }}
{%- endcall %}
{% endmacro %}
{% macro mysql__truncate_relation(relation) -%}
{% call statement('truncate_relation') -%}
truncate table {{ relation }}
{%- endcall %}
{% endmacro %}
{% macro mysql__create_table_as(temporary, relation, sql) -%}
{%- set sql_header = config.get('sql_header', none) -%}
{{ sql_header if sql_header is not none }}
create {% if temporary: -%}temporary{%- endif %} table
{{ relation.include(database=False) }}
{% set contract_config = config.get('contract') %}
{% if contract_config.enforced %}
{{ get_assert_columns_equivalent(sql) }}
{{ get_table_columns_and_constraints() }}
{%- set sql = get_select_subquery(sql) %}
{% else %}
as
{% endif %}
(
{{ sql }}
)
{% endmacro %}
{% macro mysql__current_timestamp() -%}
current_timestamp()
{%- endmacro %}
{% macro mysql__rename_relation(from_relation, to_relation) -%}
{#
MySQL rename fails when the relation already exists, so a 2-step process is needed:
1. Drop the existing relation
2. Rename the new relation to existing relation
#}
{% call statement('drop_relation') %}
drop {{ to_relation.type }} if exists {{ to_relation }} cascade
{% endcall %}
{% call statement('rename_relation') %}
rename table {{ from_relation }} to {{ to_relation }}
{% endcall %}
{% endmacro %}
{% macro mysql__check_schema_exists(database, schema) -%}
{# no-op #}
{# see MySQLAdapter.check_schema_exists() #}
{% endmacro %}
{% macro mysql__get_columns_in_relation(relation) -%}
{% call statement('get_columns_in_relation', fetch_result=True) %}
show columns from {{ relation.schema }}.{{ relation.identifier }}
{% endcall %}
{% set table = load_result('get_columns_in_relation').table %}
{{ return(sql_convert_columns_in_relation(table)) }}
{% endmacro %}
{% macro mysql__list_relations_without_caching(schema_relation) %}
{% call statement('list_relations_without_caching', fetch_result=True) -%}
select
null as "database",
table_name as name,
table_schema as "schema",
case when table_type = 'BASE TABLE' then 'table'
when table_type = 'VIEW' then 'view'
else table_type
end as table_type
from information_schema.tables
where table_schema = '{{ schema_relation.schema }}'
{% endcall %}
{{ return(load_result('list_relations_without_caching').table) }}
{% endmacro %}
{% macro mysql__generate_database_name(custom_database_name=none, node=none) -%}
{% do return(None) %}
{%- endmacro %}
{% macro mysql__get_phony_data_for_type(data_type) %}
{#
The types that MySQL supports in CAST statements are NOT the same as the
types that are supported in table definitions. This is a bit of a hack to
work around the known mismatches.
#}
{%- if data_type.lower() == 'integer' -%}
0
{%- elif data_type.lower() == 'text' -%}
''
{%- elif data_type.lower() == 'integer unsigned' -%}
cast(null as unsigned)
{%- elif data_type.lower() == 'integer signed' -%}
cast(null as signed)
{%- else -%}
cast(null as {{ data_type }})
{%- endif -%}
{% endmacro %}
{% macro mysql__get_empty_schema_sql(columns) %}
{%- set col_err = [] -%}
select
{% for i in columns %}
{%- set col = columns[i] -%}
{%- if col['data_type'] is not defined -%}
{{ col_err.append(col['name']) }}
{%- endif -%}
{% set col_name = adapter.quote(col['name']) if col.get('quote') else col['name'] %}
{{ mysql__get_phony_data_for_type(col['data_type']) }} as {{ col_name }}{{ ", " if not loop.last }}
{%- endfor -%}
{%- if (col_err | length) > 0 -%}
{{ exceptions.column_type_missing(column_names=col_err) }}
{%- endif -%}
{% endmacro %}