|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +import dbt.exceptions |
| 4 | +from dbt.adapters.base.impl import ConstraintSupport |
| 5 | +from dbt.adapters.fabric import FabricAdapter |
| 6 | +from dbt.contracts.graph.nodes import ConstraintType |
| 7 | + |
| 8 | +from dbt.adapters.sqlserver.sqlserver_column import SQLServerColumn |
| 9 | +from dbt.adapters.sqlserver.sqlserver_connections import SQLServerConnectionManager |
| 10 | +from dbt.adapters.sqlserver.sqlserver_relation import SQLServerRelation |
| 11 | + |
| 12 | + |
| 13 | +class SQLServerAdapter(FabricAdapter): |
| 14 | + """ |
| 15 | + Controls actual implmentation of adapter, and ability to override certain methods. |
| 16 | + """ |
| 17 | + |
| 18 | + ConnectionManager = SQLServerConnectionManager |
| 19 | + Column = SQLServerColumn |
| 20 | + Relation = SQLServerRelation |
| 21 | + |
| 22 | + CONSTRAINT_SUPPORT = { |
| 23 | + ConstraintType.check: ConstraintSupport.ENFORCED, |
| 24 | + ConstraintType.not_null: ConstraintSupport.ENFORCED, |
| 25 | + ConstraintType.unique: ConstraintSupport.ENFORCED, |
| 26 | + ConstraintType.primary_key: ConstraintSupport.ENFORCED, |
| 27 | + ConstraintType.foreign_key: ConstraintSupport.ENFORCED, |
| 28 | + } |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def render_model_constraint(cls, constraint) -> Optional[str]: |
| 32 | + constraint_prefix = "add constraint " |
| 33 | + column_list = ", ".join(constraint.columns) |
| 34 | + |
| 35 | + if constraint.name is None: |
| 36 | + raise dbt.exceptions.DbtDatabaseError( |
| 37 | + "Constraint name cannot be empty. Provide constraint name - column " |
| 38 | + + column_list |
| 39 | + + " and run the project again." |
| 40 | + ) |
| 41 | + |
| 42 | + if constraint.type == ConstraintType.unique: |
| 43 | + return constraint_prefix + f"{constraint.name} unique nonclustered({column_list})" |
| 44 | + elif constraint.type == ConstraintType.primary_key: |
| 45 | + return constraint_prefix + f"{constraint.name} primary key nonclustered({column_list})" |
| 46 | + elif constraint.type == ConstraintType.foreign_key and constraint.expression: |
| 47 | + return ( |
| 48 | + constraint_prefix |
| 49 | + + f"{constraint.name} foreign key({column_list}) references " |
| 50 | + + constraint.expression |
| 51 | + ) |
| 52 | + elif constraint.type == ConstraintType.check and constraint.expression: |
| 53 | + return f"{constraint_prefix} {constraint.name} check ({constraint.expression})" |
| 54 | + elif constraint.type == ConstraintType.custom and constraint.expression: |
| 55 | + return f"{constraint_prefix} {constraint.name} {constraint.expression}" |
| 56 | + else: |
| 57 | + return None |
| 58 | + |
| 59 | + @classmethod |
| 60 | + def date_function(cls): |
| 61 | + return "getdate()" |
0 commit comments