Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion data_diff/queries/ast_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from data_diff.utils import ArithString, join_iter

from .compiler import Compilable, Compiler
from .compiler import Compilable, Compiler, cv_params
from .base import SKIP, CompileError, DbPath, Schema, args_as_tuple


Expand Down Expand Up @@ -691,3 +691,18 @@ def compile(self, c: Compiler) -> str:
class Commit(Statement):
def compile(self, c: Compiler) -> str:
return "COMMIT" if not c.database.is_autocommit else SKIP

@dataclass
class Param(ExprNode, ITable):
"""A value placeholder, to be specified at compilation time using the `cv_params` context variable."""

name: str

@property
def source_table(self):
return self

def compile(self, c: Compiler) -> str:
params = cv_params.get()
return c._compile(params[self.name])

10 changes: 9 additions & 1 deletion data_diff/queries/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
from data_diff.utils import ArithString
from data_diff.databases.database_types import AbstractDialect, DbPath

import contextvars

cv_params = contextvars.ContextVar("params")


@dataclass
class Compiler:
database: AbstractDialect
params: dict = {}
in_select: bool = False # Compilation runtime flag
in_join: bool = False # Compilation runtime flag

Expand All @@ -21,7 +26,10 @@ class Compiler:

_counter: List = [0]

def compile(self, elem) -> str:
def compile(self, elem, params=None) -> str:
if params:
cv_params.set(params)

res = self._compile(elem)
if self.root and self._subqueries:
subq = ", ".join(f"\n {k} AS ({v})" for k, v in self._subqueries.items())
Expand Down
1 change: 1 addition & 0 deletions tests/test_database_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def init_conns():
"numeric",
"real",
"double precision",
"Number(5, 2)",
],
"uuid": [
"CHAR(100)",
Expand Down