def visit_Set(self, node): if node.value is not None: self.write('SET ') self.write(common.quote_ident(node.name)) self.write(' = ') self.visit(node.value) else: self.write('RESET ') self.write(common.quote_ident(node.name))
def visit_AlterSystem(self, node): self.write('ALTER SYSTEM ') if node.value is not None: self.write('SET ') self.write(common.quote_ident(node.name)) self.write(' = ') self.visit(node.value) else: self.write('RESET ') self.write(common.quote_ident(node.name))
def get_id(self): raw_name = self.constraint.raw_constraint_name() name = common.edgedb_name_to_pg_name('{}#{}'.format( raw_name, self.index)) name = common.quote_ident(name) return '{} ON {} {}'.format(name, self.constraint.get_subject_type(), self.constraint.get_subject_name())
def visit_RangeVar(self, node): rel = node.relation if isinstance(rel, (pgast.Relation, pgast.NullRelation)): self.visit(rel) elif isinstance(rel, pgast.CommonTableExpr): self.write(common.quote_ident(rel.name)) else: raise SQLSourceGeneratorError( 'unexpected relation in RangeVar: {!r}'.format(rel)) if node.alias: self.write(' AS ') self.visit(node.alias)
def gen_ctes(self, ctes): self.write('WITH') count = len(ctes) for i, cte in enumerate(ctes): self.new_lines = 1 if getattr(cte, 'recursive', None): self.write('RECURSIVE ') self.write(common.quote_ident(cte.name)) self.write(' AS ') self.indentation += 1 self.new_lines = 1 self.write('(') self.visit(cte.query) self.write(')') if i != count - 1: self.write(',') self.indentation -= 1 self.new_lines = 1
def gen_ctes(self, ctes): self.write('WITH') count = len(ctes) for i, cte in enumerate(ctes): self.new_lines = 1 if getattr(cte, 'recursive', None): self.write('RECURSIVE ') self.write(common.quote_ident(cte.name)) self.write(' AS ') if cte.materialized is not None: if cte.materialized: self.write('MATERIALIZED ') else: self.write('NOT MATERIALIZED ') self.indentation += 1 self.new_lines = 1 self.write('(') self.visit(cte.query) self.write(')') if i != count - 1: self.write(',') self.indentation -= 1 self.new_lines = 1
def visit_NamedFuncArg(self, node): self.write(common.quote_ident(node.name), ' => ') self.visit(node.val)
def visit_NamedParamRef(self, node): self.write(common.quote_ident(node.name))
def visit_ColumnDef(self, node): self.write(common.quote_ident(node.name)) if node.typename: self.write(' ') self.visit(node.typename)
def visit_Alias(self, node): self.write(common.quote_ident(node.aliasname)) if node.colnames: self.write('(') self.write(', '.join(common.quote_ident(n) for n in node.colnames)) self.write(')')
def visit_UpdateTarget(self, node): self.write(common.quote_ident(node.name)) self.write(' = ') self.visit(node.val)
def visit_ResTarget(self, node): self.visit(node.val) if node.name: self.write(' AS ' + common.quote_ident(node.name))
def constraint_name(self, quote=True): name = self.raw_constraint_name() name = common.edgedb_name_to_pg_name(name) return common.quote_ident(name) if quote else name
def numbered_constraint_name(self, i, quote=True): raw_name = self.raw_constraint_name() name = common.edgedb_name_to_pg_name('{}#{}'.format(raw_name, i)) return common.quote_ident(name) if quote else name
def _compile_ql_transaction(self, ctx: CompileContext, ql: qlast.Transaction) -> dbstate.Query: cacheable = True single_unit = False modaliases = None if isinstance(ql, qlast.StartTransaction): ctx.state.start_tx() sql = 'START TRANSACTION' if ql.isolation is not None: sql += f' ISOLATION LEVEL {ql.isolation.value}' if ql.access is not None: sql += f' {ql.access.value}' if ql.deferrable is not None: sql += f' {ql.deferrable.value}' sql += ';' sql = (sql.encode(), ) action = dbstate.TxAction.START cacheable = False elif isinstance(ql, qlast.CommitTransaction): new_state: dbstate.TransactionState = ctx.state.commit_tx() modaliases = new_state.modaliases sql = (b'COMMIT', ) single_unit = True cacheable = False action = dbstate.TxAction.COMMIT elif isinstance(ql, qlast.RollbackTransaction): new_state: dbstate.TransactionState = ctx.state.rollback_tx() modaliases = new_state.modaliases sql = (b'ROLLBACK', ) single_unit = True cacheable = False action = dbstate.TxAction.ROLLBACK elif isinstance(ql, qlast.DeclareSavepoint): tx = ctx.state.current_tx() sp_id = tx.declare_savepoint(ql.name) if not self._bootstrap_mode: pgname = pg_common.quote_ident(ql.name) sql = (f''' INSERT INTO _edgecon_current_savepoint(sp_id) VALUES ( {pg_ql(sp_id)} ) ON CONFLICT (_sentinel) DO UPDATE SET sp_id = {pg_ql(sp_id)}; '''.encode(), f'SAVEPOINT {pgname};'.encode()) else: # pragma: no cover sql = (f'SAVEPOINT {pgname};'.encode(), ) cacheable = False action = dbstate.TxAction.DECLARE_SAVEPOINT elif isinstance(ql, qlast.ReleaseSavepoint): ctx.state.current_tx().release_savepoint(ql.name) pgname = pg_common.quote_ident(ql.name) sql = (f'RELEASE SAVEPOINT {pgname}'.encode(), ) action = dbstate.TxAction.RELEASE_SAVEPOINT elif isinstance(ql, qlast.RollbackToSavepoint): tx = ctx.state.current_tx() new_state: dbstate.TransactionState = tx.rollback_to_savepoint( ql.name) modaliases = new_state.modaliases pgname = pg_common.quote_ident(ql.name) sql = (f'ROLLBACK TO SAVEPOINT {pgname}'.encode(), ) single_unit = True cacheable = False action = dbstate.TxAction.ROLLBACK_TO_SAVEPOINT else: # pragma: no cover raise ValueError(f'expected a transaction AST node, got {ql!r}') return dbstate.TxControlQuery(sql=sql, action=action, cacheable=cacheable, single_unit=single_unit, modaliases=modaliases)