dbt-adapters 1.22.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dbt/adapters/__about__.py +1 -0
- dbt/adapters/__init__.py +8 -0
- dbt/adapters/base/README.md +13 -0
- dbt/adapters/base/__init__.py +16 -0
- dbt/adapters/base/column.py +173 -0
- dbt/adapters/base/connections.py +429 -0
- dbt/adapters/base/impl.py +2036 -0
- dbt/adapters/base/meta.py +150 -0
- dbt/adapters/base/plugin.py +32 -0
- dbt/adapters/base/query_headers.py +106 -0
- dbt/adapters/base/relation.py +648 -0
- dbt/adapters/cache.py +521 -0
- dbt/adapters/capability.py +63 -0
- dbt/adapters/catalogs/__init__.py +14 -0
- dbt/adapters/catalogs/_client.py +54 -0
- dbt/adapters/catalogs/_constants.py +1 -0
- dbt/adapters/catalogs/_exceptions.py +39 -0
- dbt/adapters/catalogs/_integration.py +113 -0
- dbt/adapters/clients/__init__.py +0 -0
- dbt/adapters/clients/jinja.py +24 -0
- dbt/adapters/contracts/__init__.py +0 -0
- dbt/adapters/contracts/connection.py +229 -0
- dbt/adapters/contracts/macros.py +11 -0
- dbt/adapters/contracts/relation.py +160 -0
- dbt/adapters/events/README.md +51 -0
- dbt/adapters/events/__init__.py +0 -0
- dbt/adapters/events/adapter_types_pb2.py +2 -0
- dbt/adapters/events/base_types.py +36 -0
- dbt/adapters/events/logging.py +83 -0
- dbt/adapters/events/types.py +436 -0
- dbt/adapters/exceptions/__init__.py +40 -0
- dbt/adapters/exceptions/alias.py +24 -0
- dbt/adapters/exceptions/cache.py +68 -0
- dbt/adapters/exceptions/compilation.py +269 -0
- dbt/adapters/exceptions/connection.py +16 -0
- dbt/adapters/exceptions/database.py +51 -0
- dbt/adapters/factory.py +264 -0
- dbt/adapters/protocol.py +150 -0
- dbt/adapters/py.typed +0 -0
- dbt/adapters/record/__init__.py +2 -0
- dbt/adapters/record/base.py +291 -0
- dbt/adapters/record/cursor/cursor.py +69 -0
- dbt/adapters/record/cursor/description.py +37 -0
- dbt/adapters/record/cursor/execute.py +39 -0
- dbt/adapters/record/cursor/fetchall.py +69 -0
- dbt/adapters/record/cursor/fetchmany.py +23 -0
- dbt/adapters/record/cursor/fetchone.py +23 -0
- dbt/adapters/record/cursor/rowcount.py +23 -0
- dbt/adapters/record/handle.py +55 -0
- dbt/adapters/record/serialization.py +115 -0
- dbt/adapters/reference_keys.py +39 -0
- dbt/adapters/relation_configs/README.md +25 -0
- dbt/adapters/relation_configs/__init__.py +12 -0
- dbt/adapters/relation_configs/config_base.py +46 -0
- dbt/adapters/relation_configs/config_change.py +26 -0
- dbt/adapters/relation_configs/config_validation.py +57 -0
- dbt/adapters/sql/__init__.py +2 -0
- dbt/adapters/sql/connections.py +263 -0
- dbt/adapters/sql/impl.py +286 -0
- dbt/adapters/utils.py +69 -0
- dbt/include/__init__.py +3 -0
- dbt/include/global_project/__init__.py +4 -0
- dbt/include/global_project/dbt_project.yml +7 -0
- dbt/include/global_project/docs/overview.md +43 -0
- dbt/include/global_project/macros/adapters/apply_grants.sql +167 -0
- dbt/include/global_project/macros/adapters/columns.sql +144 -0
- dbt/include/global_project/macros/adapters/freshness.sql +32 -0
- dbt/include/global_project/macros/adapters/indexes.sql +41 -0
- dbt/include/global_project/macros/adapters/metadata.sql +105 -0
- dbt/include/global_project/macros/adapters/persist_docs.sql +33 -0
- dbt/include/global_project/macros/adapters/relation.sql +84 -0
- dbt/include/global_project/macros/adapters/schema.sql +20 -0
- dbt/include/global_project/macros/adapters/show.sql +26 -0
- dbt/include/global_project/macros/adapters/timestamps.sql +52 -0
- dbt/include/global_project/macros/adapters/validate_sql.sql +10 -0
- dbt/include/global_project/macros/etc/datetime.sql +62 -0
- dbt/include/global_project/macros/etc/statement.sql +52 -0
- dbt/include/global_project/macros/generic_test_sql/accepted_values.sql +27 -0
- dbt/include/global_project/macros/generic_test_sql/not_null.sql +9 -0
- dbt/include/global_project/macros/generic_test_sql/relationships.sql +23 -0
- dbt/include/global_project/macros/generic_test_sql/unique.sql +12 -0
- dbt/include/global_project/macros/get_custom_name/get_custom_alias.sql +36 -0
- dbt/include/global_project/macros/get_custom_name/get_custom_database.sql +32 -0
- dbt/include/global_project/macros/get_custom_name/get_custom_schema.sql +60 -0
- dbt/include/global_project/macros/materializations/configs.sql +21 -0
- dbt/include/global_project/macros/materializations/functions/aggregate.sql +65 -0
- dbt/include/global_project/macros/materializations/functions/function.sql +20 -0
- dbt/include/global_project/macros/materializations/functions/helpers.sql +20 -0
- dbt/include/global_project/macros/materializations/functions/scalar.sql +69 -0
- dbt/include/global_project/macros/materializations/hooks.sql +35 -0
- dbt/include/global_project/macros/materializations/models/clone/can_clone_table.sql +7 -0
- dbt/include/global_project/macros/materializations/models/clone/clone.sql +67 -0
- dbt/include/global_project/macros/materializations/models/clone/create_or_replace_clone.sql +7 -0
- dbt/include/global_project/macros/materializations/models/incremental/column_helpers.sql +80 -0
- dbt/include/global_project/macros/materializations/models/incremental/incremental.sql +99 -0
- dbt/include/global_project/macros/materializations/models/incremental/is_incremental.sql +13 -0
- dbt/include/global_project/macros/materializations/models/incremental/merge.sql +120 -0
- dbt/include/global_project/macros/materializations/models/incremental/on_schema_change.sql +159 -0
- dbt/include/global_project/macros/materializations/models/incremental/strategies.sql +92 -0
- dbt/include/global_project/macros/materializations/models/materialized_view.sql +121 -0
- dbt/include/global_project/macros/materializations/models/table.sql +64 -0
- dbt/include/global_project/macros/materializations/models/view.sql +72 -0
- dbt/include/global_project/macros/materializations/seeds/helpers.sql +128 -0
- dbt/include/global_project/macros/materializations/seeds/seed.sql +60 -0
- dbt/include/global_project/macros/materializations/snapshots/helpers.sql +345 -0
- dbt/include/global_project/macros/materializations/snapshots/snapshot.sql +109 -0
- dbt/include/global_project/macros/materializations/snapshots/snapshot_merge.sql +34 -0
- dbt/include/global_project/macros/materializations/snapshots/strategies.sql +184 -0
- dbt/include/global_project/macros/materializations/tests/helpers.sql +44 -0
- dbt/include/global_project/macros/materializations/tests/test.sql +66 -0
- dbt/include/global_project/macros/materializations/tests/unit.sql +40 -0
- dbt/include/global_project/macros/materializations/tests/where_subquery.sql +15 -0
- dbt/include/global_project/macros/python_model/python.sql +114 -0
- dbt/include/global_project/macros/relations/column/columns_spec_ddl.sql +89 -0
- dbt/include/global_project/macros/relations/create.sql +23 -0
- dbt/include/global_project/macros/relations/create_backup.sql +17 -0
- dbt/include/global_project/macros/relations/create_intermediate.sql +17 -0
- dbt/include/global_project/macros/relations/drop.sql +41 -0
- dbt/include/global_project/macros/relations/drop_backup.sql +14 -0
- dbt/include/global_project/macros/relations/materialized_view/alter.sql +55 -0
- dbt/include/global_project/macros/relations/materialized_view/create.sql +10 -0
- dbt/include/global_project/macros/relations/materialized_view/drop.sql +14 -0
- dbt/include/global_project/macros/relations/materialized_view/refresh.sql +9 -0
- dbt/include/global_project/macros/relations/materialized_view/rename.sql +10 -0
- dbt/include/global_project/macros/relations/materialized_view/replace.sql +10 -0
- dbt/include/global_project/macros/relations/rename.sql +35 -0
- dbt/include/global_project/macros/relations/rename_intermediate.sql +14 -0
- dbt/include/global_project/macros/relations/replace.sql +50 -0
- dbt/include/global_project/macros/relations/schema.sql +8 -0
- dbt/include/global_project/macros/relations/table/create.sql +60 -0
- dbt/include/global_project/macros/relations/table/drop.sql +14 -0
- dbt/include/global_project/macros/relations/table/rename.sql +10 -0
- dbt/include/global_project/macros/relations/table/replace.sql +10 -0
- dbt/include/global_project/macros/relations/view/create.sql +27 -0
- dbt/include/global_project/macros/relations/view/drop.sql +14 -0
- dbt/include/global_project/macros/relations/view/rename.sql +10 -0
- dbt/include/global_project/macros/relations/view/replace.sql +66 -0
- dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql +107 -0
- dbt/include/global_project/macros/utils/any_value.sql +9 -0
- dbt/include/global_project/macros/utils/array_append.sql +8 -0
- dbt/include/global_project/macros/utils/array_concat.sql +7 -0
- dbt/include/global_project/macros/utils/array_construct.sql +12 -0
- dbt/include/global_project/macros/utils/bool_or.sql +9 -0
- dbt/include/global_project/macros/utils/cast.sql +7 -0
- dbt/include/global_project/macros/utils/cast_bool_to_text.sql +7 -0
- dbt/include/global_project/macros/utils/concat.sql +7 -0
- dbt/include/global_project/macros/utils/data_types.sql +129 -0
- dbt/include/global_project/macros/utils/date.sql +10 -0
- dbt/include/global_project/macros/utils/date_spine.sql +75 -0
- dbt/include/global_project/macros/utils/date_trunc.sql +7 -0
- dbt/include/global_project/macros/utils/dateadd.sql +14 -0
- dbt/include/global_project/macros/utils/datediff.sql +14 -0
- dbt/include/global_project/macros/utils/equals.sql +14 -0
- dbt/include/global_project/macros/utils/escape_single_quotes.sql +8 -0
- dbt/include/global_project/macros/utils/except.sql +9 -0
- dbt/include/global_project/macros/utils/generate_series.sql +53 -0
- dbt/include/global_project/macros/utils/hash.sql +7 -0
- dbt/include/global_project/macros/utils/intersect.sql +9 -0
- dbt/include/global_project/macros/utils/last_day.sql +15 -0
- dbt/include/global_project/macros/utils/length.sql +11 -0
- dbt/include/global_project/macros/utils/listagg.sql +30 -0
- dbt/include/global_project/macros/utils/literal.sql +7 -0
- dbt/include/global_project/macros/utils/position.sql +11 -0
- dbt/include/global_project/macros/utils/replace.sql +14 -0
- dbt/include/global_project/macros/utils/right.sql +12 -0
- dbt/include/global_project/macros/utils/safe_cast.sql +9 -0
- dbt/include/global_project/macros/utils/split_part.sql +26 -0
- dbt/include/global_project/tests/generic/builtin.sql +30 -0
- dbt/include/py.typed +0 -0
- dbt_adapters-1.22.2.dist-info/METADATA +124 -0
- dbt_adapters-1.22.2.dist-info/RECORD +173 -0
- dbt_adapters-1.22.2.dist-info/WHEEL +4 -0
- dbt_adapters-1.22.2.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import abc
|
|
2
|
+
from functools import wraps
|
|
3
|
+
from typing import Any, Callable, Dict, FrozenSet, Optional, Set
|
|
4
|
+
|
|
5
|
+
from dbt_common.events.functions import warn_or_error
|
|
6
|
+
|
|
7
|
+
from dbt.adapters.events.types import AdapterDeprecationWarning
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
Decorator = Callable[[Any], Callable]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class _Available:
|
|
14
|
+
def __call__(self, func: Callable) -> Callable:
|
|
15
|
+
func._is_available_ = True # type: ignore
|
|
16
|
+
return func
|
|
17
|
+
|
|
18
|
+
def parse(self, parse_replacement: Callable) -> Decorator:
|
|
19
|
+
"""A decorator factory to indicate that a method on the adapter will be
|
|
20
|
+
exposed to the database wrapper, and will be stubbed out at parse time
|
|
21
|
+
with the given function.
|
|
22
|
+
|
|
23
|
+
@available.parse()
|
|
24
|
+
def my_method(self, a, b):
|
|
25
|
+
if something:
|
|
26
|
+
return None
|
|
27
|
+
return big_expensive_db_query()
|
|
28
|
+
|
|
29
|
+
@available.parse(lambda *args, **args: {})
|
|
30
|
+
def my_other_method(self, a, b):
|
|
31
|
+
x = {}
|
|
32
|
+
x.update(big_expensive_db_query())
|
|
33
|
+
return x
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def inner(func):
|
|
37
|
+
func._parse_replacement_ = parse_replacement
|
|
38
|
+
return self(func)
|
|
39
|
+
|
|
40
|
+
return inner
|
|
41
|
+
|
|
42
|
+
def deprecated(
|
|
43
|
+
self, supported_name: str, parse_replacement: Optional[Callable] = None
|
|
44
|
+
) -> Decorator:
|
|
45
|
+
"""A decorator that marks a function as available, but also prints a
|
|
46
|
+
deprecation warning. Use like
|
|
47
|
+
|
|
48
|
+
@available.deprecated('my_new_method')
|
|
49
|
+
def my_old_method(self, arg):
|
|
50
|
+
args = compatability_shim(arg)
|
|
51
|
+
return self.my_new_method(*args)
|
|
52
|
+
|
|
53
|
+
@available.deprecated('my_new_slow_method', lambda *a, **k: (0, ''))
|
|
54
|
+
def my_old_slow_method(self, arg):
|
|
55
|
+
args = compatibility_shim(arg)
|
|
56
|
+
return self.my_new_slow_method(*args)
|
|
57
|
+
|
|
58
|
+
To make `adapter.my_old_method` available but also print out a warning
|
|
59
|
+
on use directing users to `my_new_method`.
|
|
60
|
+
|
|
61
|
+
The optional parse_replacement, if provided, will provide a parse-time
|
|
62
|
+
replacement for the actual method (see `available.parse`).
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
def wrapper(func):
|
|
66
|
+
func_name = func.__name__
|
|
67
|
+
|
|
68
|
+
@wraps(func)
|
|
69
|
+
def inner(*args, **kwargs):
|
|
70
|
+
warn_or_error(
|
|
71
|
+
AdapterDeprecationWarning(old_name=func_name, new_name=supported_name)
|
|
72
|
+
)
|
|
73
|
+
return func(*args, **kwargs)
|
|
74
|
+
|
|
75
|
+
if parse_replacement:
|
|
76
|
+
available_function = self.parse(parse_replacement)
|
|
77
|
+
else:
|
|
78
|
+
available_function = self
|
|
79
|
+
return available_function(inner)
|
|
80
|
+
|
|
81
|
+
return wrapper
|
|
82
|
+
|
|
83
|
+
def parse_none(self, func: Callable) -> Callable:
|
|
84
|
+
wrapper = self.parse(lambda *a, **k: None)
|
|
85
|
+
return wrapper(func)
|
|
86
|
+
|
|
87
|
+
def parse_list(self, func: Callable) -> Callable:
|
|
88
|
+
wrapper = self.parse(lambda *a, **k: [])
|
|
89
|
+
return wrapper(func)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
available = _Available()
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class available_property(property):
|
|
96
|
+
"""
|
|
97
|
+
This supports making dynamic properties (`@property`) available in the jinja context.
|
|
98
|
+
|
|
99
|
+
We use `@available` to make methods available in the jinja context, but this mechanism relies on the method being callable.
|
|
100
|
+
Intuitively, we should be able to use both `@available` and `@property` to create a dynamic property that's available in the jinja context.
|
|
101
|
+
|
|
102
|
+
Using the `@property` decorator as the inner decorator supplies `@available` with something that is not callable.
|
|
103
|
+
Instead of returning the method, `@property` returns the value itself, not the method that is called to create the value.
|
|
104
|
+
|
|
105
|
+
Using the `@available` decorator as the inner decorator adds `_is_available_ = True` to the function.
|
|
106
|
+
However, when the `@property` decorator executes, it returns a `property` object which does not have the `_is_available_` attribute.
|
|
107
|
+
|
|
108
|
+
This decorator solves this problem by simply adding `_is_available_ = True` as an attribute on the `property` built-in.
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
_is_available_ = True
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class AdapterMeta(abc.ABCMeta):
|
|
115
|
+
_available_: FrozenSet[str]
|
|
116
|
+
_parse_replacements_: Dict[str, Callable]
|
|
117
|
+
|
|
118
|
+
def __new__(mcls, name, bases, namespace, **kwargs) -> "AdapterMeta":
|
|
119
|
+
# mypy does not like the `**kwargs`. But `ABCMeta` itself takes
|
|
120
|
+
# `**kwargs` in its argspec here (and passes them to `type.__new__`.
|
|
121
|
+
# I'm not sure there is any benefit to it after poking around a bit,
|
|
122
|
+
# but having it doesn't hurt on the python side (and omitting it could
|
|
123
|
+
# hurt for obscure metaclass reasons, for all I know)
|
|
124
|
+
cls = abc.ABCMeta.__new__(mcls, name, bases, namespace, **kwargs)
|
|
125
|
+
|
|
126
|
+
# this is very much inspired by ABCMeta's own implementation
|
|
127
|
+
|
|
128
|
+
# dict mapping the method name to whether the model name should be
|
|
129
|
+
# injected into the arguments. All methods in here are exposed to the
|
|
130
|
+
# context.
|
|
131
|
+
available: Set[str] = set()
|
|
132
|
+
replacements: Dict[str, Any] = {}
|
|
133
|
+
|
|
134
|
+
# collect base class data first
|
|
135
|
+
for base in bases:
|
|
136
|
+
available.update(getattr(base, "_available_", set()))
|
|
137
|
+
replacements.update(getattr(base, "_parse_replacements_", set()))
|
|
138
|
+
|
|
139
|
+
# override with local data if it exists
|
|
140
|
+
for name, value in namespace.items():
|
|
141
|
+
if getattr(value, "_is_available_", False):
|
|
142
|
+
available.add(name)
|
|
143
|
+
parse_replacement = getattr(value, "_parse_replacement_", None)
|
|
144
|
+
if parse_replacement is not None:
|
|
145
|
+
replacements[name] = parse_replacement
|
|
146
|
+
|
|
147
|
+
cls._available_ = frozenset(available)
|
|
148
|
+
# should this be a namedtuple so it will be immutable like _available_?
|
|
149
|
+
cls._parse_replacements_ = replacements
|
|
150
|
+
return cls
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import List, Optional, Type
|
|
3
|
+
|
|
4
|
+
from dbt.adapters.contracts.connection import Credentials
|
|
5
|
+
from dbt.adapters.protocol import AdapterProtocol
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class AdapterPlugin:
|
|
9
|
+
"""Defines the basic requirements for a dbt adapter plugin.
|
|
10
|
+
|
|
11
|
+
:param include_path: The path to this adapter plugin's root
|
|
12
|
+
:param dependencies: A list of adapter names that this adapter depends
|
|
13
|
+
upon.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
adapter: Type[AdapterProtocol],
|
|
19
|
+
credentials: Type[Credentials],
|
|
20
|
+
include_path: str,
|
|
21
|
+
dependencies: Optional[List[str]] = None,
|
|
22
|
+
project_name: Optional[str] = None,
|
|
23
|
+
) -> None:
|
|
24
|
+
self.adapter: Type[AdapterProtocol] = adapter
|
|
25
|
+
self.credentials: Type[Credentials] = credentials
|
|
26
|
+
self.include_path: str = include_path
|
|
27
|
+
self.project_name: str = project_name or f"dbt_{Path(include_path).name}"
|
|
28
|
+
self.dependencies: List[str]
|
|
29
|
+
if dependencies is None:
|
|
30
|
+
self.dependencies = []
|
|
31
|
+
else:
|
|
32
|
+
self.dependencies = dependencies
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
from threading import local
|
|
2
|
+
from typing import Any, Callable, Dict, Optional
|
|
3
|
+
|
|
4
|
+
from dbt_common.exceptions import DbtRuntimeError
|
|
5
|
+
|
|
6
|
+
from dbt.adapters.clients.jinja import QueryStringGenerator
|
|
7
|
+
from dbt.adapters.contracts.connection import AdapterRequiredConfig, QueryComment
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class QueryHeaderContextWrapper:
|
|
11
|
+
def __init__(self, context) -> None:
|
|
12
|
+
self._inner_context = context
|
|
13
|
+
|
|
14
|
+
def __getattr__(self, name):
|
|
15
|
+
return getattr(self._inner_context, name, "")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class _QueryComment(local):
|
|
19
|
+
"""A thread-local class storing thread-specific state information for
|
|
20
|
+
connection management, namely:
|
|
21
|
+
- the current thread's query comment.
|
|
22
|
+
- a source_name indicating what set the current thread's query comment
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, initial) -> None:
|
|
26
|
+
self.query_comment: Optional[str] = initial
|
|
27
|
+
self.append: bool = False
|
|
28
|
+
|
|
29
|
+
def add(self, sql: str) -> str:
|
|
30
|
+
if not self.query_comment:
|
|
31
|
+
return sql
|
|
32
|
+
|
|
33
|
+
if self.append:
|
|
34
|
+
# replace last ';' with '<comment>;'
|
|
35
|
+
sql = sql.rstrip()
|
|
36
|
+
if sql[-1] == ";":
|
|
37
|
+
sql = sql[:-1]
|
|
38
|
+
return "{}\n/* {} */;".format(sql, self.query_comment.strip())
|
|
39
|
+
|
|
40
|
+
return "{}\n/* {} */".format(sql, self.query_comment.strip())
|
|
41
|
+
|
|
42
|
+
return "/* {} */\n{}".format(self.query_comment.strip(), sql)
|
|
43
|
+
|
|
44
|
+
def set(self, comment: Optional[str], append: bool):
|
|
45
|
+
if isinstance(comment, str) and "*/" in comment:
|
|
46
|
+
# tell the user "no" so they don't hurt themselves by writing
|
|
47
|
+
# garbage
|
|
48
|
+
raise DbtRuntimeError(f'query comment contains illegal value "*/": {comment}')
|
|
49
|
+
self.query_comment = comment
|
|
50
|
+
self.append = append
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
QueryStringFunc = Callable[[str, Optional[QueryHeaderContextWrapper]], str]
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class MacroQueryStringSetter:
|
|
57
|
+
DEFAULT_QUERY_COMMENT_APPEND = False
|
|
58
|
+
|
|
59
|
+
def __init__(
|
|
60
|
+
self, config: AdapterRequiredConfig, query_header_context: Dict[str, Any]
|
|
61
|
+
) -> None:
|
|
62
|
+
self.config = config
|
|
63
|
+
self._query_header_context = query_header_context
|
|
64
|
+
|
|
65
|
+
comment_macro = self._get_comment_macro()
|
|
66
|
+
self.generator: QueryStringFunc = lambda name, model: ""
|
|
67
|
+
# if the comment value was None or the empty string, just skip it
|
|
68
|
+
if comment_macro:
|
|
69
|
+
assert isinstance(comment_macro, str)
|
|
70
|
+
macro = "\n".join(
|
|
71
|
+
(
|
|
72
|
+
"{%- macro query_comment_macro(connection_name, node) -%}",
|
|
73
|
+
comment_macro,
|
|
74
|
+
"{% endmacro %}",
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
ctx = self._get_context()
|
|
78
|
+
self.generator = QueryStringGenerator(macro, ctx)
|
|
79
|
+
self.comment = _QueryComment(None)
|
|
80
|
+
self.reset()
|
|
81
|
+
|
|
82
|
+
def _get_comment_macro(self) -> Optional[str]:
|
|
83
|
+
return self.config.query_comment.comment
|
|
84
|
+
|
|
85
|
+
def _get_context(self) -> Dict[str, Any]:
|
|
86
|
+
return self._query_header_context
|
|
87
|
+
|
|
88
|
+
def add(self, sql: str) -> str:
|
|
89
|
+
return self.comment.add(sql)
|
|
90
|
+
|
|
91
|
+
def reset(self):
|
|
92
|
+
self.set("master", None)
|
|
93
|
+
|
|
94
|
+
def set(self, name: str, query_header_context: Any):
|
|
95
|
+
wrapped: Optional[QueryHeaderContextWrapper] = None
|
|
96
|
+
if query_header_context is not None:
|
|
97
|
+
wrapped = QueryHeaderContextWrapper(query_header_context)
|
|
98
|
+
comment_str = self.generator(name, wrapped)
|
|
99
|
+
|
|
100
|
+
append = self.DEFAULT_QUERY_COMMENT_APPEND
|
|
101
|
+
if (
|
|
102
|
+
isinstance(self.config.query_comment, QueryComment)
|
|
103
|
+
and self.config.query_comment.append is not None
|
|
104
|
+
):
|
|
105
|
+
append = self.config.query_comment.append
|
|
106
|
+
self.comment.set(comment_str, append)
|