Skip to content

Commit

Permalink
compiler: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dnicolodi committed Oct 7, 2023
1 parent c809450 commit 70bfd6b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
26 changes: 9 additions & 17 deletions beanquery/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ def __init__(self, context):
self.env = Environment()
self.default = context.tables.get('postings')

@property
def table(self):
return self.env.table

@table.setter
def table(self, value):
self.env.table = value

def compile(self, query, parameters=None):
"""Compile an AST into an executable statement."""
self.parameters = parameters
Expand Down Expand Up @@ -120,9 +112,9 @@ def _compile_journal(self, node: ast.Journal):

@_compile_statement.register
def _compile_print(self, node: ast.Print):
self.table = self.context.tables.get('entries')
self.env.table = self.context.tables.get('entries')
expr = self._compile_from(node.from_clause)
return EvalPrint(self.table, expr)
return EvalPrint(self.env.table, expr)

@_compile_statement.register
def _compile_select(self, node: ast.Select):
Expand Down Expand Up @@ -170,7 +162,7 @@ def _compile_select(self, node: ast.Select):
'all non-aggregates must be covered by GROUP-BY clause in aggregate query: '
'the following targets are missing: {}'.format(','.join(missing_names)))

query = EvalQuery(self.table,
query = EvalQuery(self.env.table,
c_targets,
c_where,
group_indexes,
Expand All @@ -193,13 +185,13 @@ def _compile_from(self, node):
# Subquery.
if isinstance(node, ast.Select):
query = self._compile_statement(node)
self.table = SubqueryTable(query)
self.env.table = SubqueryTable(query)
return None

# Table reference.
if isinstance(node, ast.Table):
self.table = self.context.tables.get(node.name)
if self.table is None:
self.env.table = self.context.tables.get(node.name)
if self.env.table is None:
raise CompilationError(f'table "{node.name}" does not exist', node)
return None

Expand All @@ -215,7 +207,7 @@ def _compile_from(self, node):
raise CompilationError('CLOSE date must follow OPEN date')

# Apply OPEN, CLOSE, and CLEAR clauses.
self.table = self.table.update(open=node.open, close=node.close, clear=node.clear)
self.env.table = self.env.table.update(open=node.open, close=node.close, clear=node.clear)

return c_expression

Expand All @@ -233,7 +225,7 @@ def _compile_targets(self, targets):
if isinstance(targets, ast.Asterisk):
# Insert the full list of available columns.
targets = [ast.Target(ast.Column(name), None)
for name in self.table.wildcard_columns]
for name in self.env.table.wildcard_columns]

# Compile targets.
c_targets = []
Expand Down Expand Up @@ -496,7 +488,7 @@ def _compile(self, node: Optional[ast.Node]):

@_compile.register
def _column(self, node: ast.Column):
column = self.table.columns.get(node.name)
column = self.env.table.columns.get(node.name)
if column is not None:
return column
var = self.env.get(node.name)
Expand Down
2 changes: 1 addition & 1 deletion beanquery/query_compile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TestCompileExpression(unittest.TestCase):
def setUpClass(cls):
context = Connection()
cls.compiler = compiler.Compiler(context)
cls.compiler.table = qe.PostingsEnvironment()
cls.compiler.env.table = qe.PostingsEnvironment()

def compile(self, expr):
return self.compiler._compile(expr)
Expand Down

0 comments on commit 70bfd6b

Please sign in to comment.