-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathsqlserver_adapter.py
More file actions
74 lines (62 loc) · 3.06 KB
/
sqlserver_adapter.py
File metadata and controls
74 lines (62 loc) · 3.06 KB
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
from typing import Any, Optional
import dbt.exceptions
from dbt.adapters.base import ConstraintSupport, available
from dbt.adapters.fabric import FabricAdapter
from dbt.contracts.graph.nodes import ConstraintType
from dbt.adapters.sqlserver.relation_configs import SQLServerIndexConfig
from dbt.adapters.sqlserver.sqlserver_column import SQLServerColumn
from dbt.adapters.sqlserver.sqlserver_configs import SQLServerConfigs
from dbt.adapters.sqlserver.sqlserver_connections import SQLServerConnectionManager
from dbt.adapters.sqlserver.sqlserver_relation import SQLServerRelation
class SQLServerAdapter(FabricAdapter):
"""
Controls actual implmentation of adapter, and ability to override certain methods.
"""
ConnectionManager = SQLServerConnectionManager
Column = SQLServerColumn
Relation = SQLServerRelation
AdapterSpecificConfigs = SQLServerConfigs
CONSTRAINT_SUPPORT = {
ConstraintType.check: ConstraintSupport.ENFORCED,
ConstraintType.not_null: ConstraintSupport.ENFORCED,
ConstraintType.unique: ConstraintSupport.ENFORCED,
ConstraintType.primary_key: ConstraintSupport.ENFORCED,
ConstraintType.foreign_key: ConstraintSupport.ENFORCED,
}
@classmethod
def render_model_constraint(cls, constraint) -> Optional[str]:
constraint_prefix = "add constraint "
column_list = ", ".join(constraint.columns)
if constraint.name is None:
raise dbt.exceptions.DbtDatabaseError(
"Constraint name cannot be empty. Provide constraint name - column "
+ column_list
+ " and run the project again."
)
if constraint.type == ConstraintType.unique:
return constraint_prefix + f"{constraint.name} unique nonclustered({column_list})"
elif constraint.type == ConstraintType.primary_key:
return constraint_prefix + f"{constraint.name} primary key nonclustered({column_list})"
elif constraint.type == ConstraintType.foreign_key and constraint.expression:
return (
constraint_prefix
+ f"{constraint.name} foreign key({column_list}) references "
+ constraint.expression
)
elif constraint.type == ConstraintType.check and constraint.expression:
return f"{constraint_prefix} {constraint.name} check ({constraint.expression})"
elif constraint.type == ConstraintType.custom and constraint.expression:
return f"{constraint_prefix} {constraint.name} {constraint.expression}"
else:
return None
@classmethod
def date_function(cls):
return "getdate()"
@available
def parse_index(self, raw_index: Any) -> Optional[SQLServerIndexConfig]:
return SQLServerIndexConfig.parse(raw_index)
def valid_incremental_strategies(self):
"""The set of standard builtin strategies which this adapter supports out-of-the-box.
Not used to validate custom strategies defined by end users.
"""
return ["append", "delete+insert", "merge", "microbatch"]