Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Merge branch 'queries_params' into oct27
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Oct 28, 2022
2 parents f8d24ea + 515c827 commit 944920e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 12 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 @@ -38,6 +38,17 @@ def _expr_type(e: Expr) -> type:
return type(e)


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

name: str

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


@dataclass
class Alias(ExprNode):
expr: Expr
Expand Down
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

0 comments on commit 944920e

Please sign in to comment.