def compile_InsertQuery( expr: qlast.InsertQuery, *, ctx: context.ContextLevel) -> irast.Set: with ctx.subquery() as ictx: ictx.implicit_id_in_shapes = False ictx.implicit_tid_in_shapes = False stmt = irast.InsertStmt() init_stmt(stmt, expr, ctx=ictx, parent_ctx=ctx) subject = dispatch.compile(expr.subject, ctx=ictx) assert isinstance(subject, irast.Set) # Self-references in INSERT are prohibited. pathctx.ban_path(subject.path_id, ctx=ictx) subject_stype = setgen.get_set_type(subject, ctx=ictx) if subject_stype.get_is_abstract(ctx.env.schema): raise errors.QueryError( f'cannot insert into abstract ' f'{subject_stype.get_verbosename(ctx.env.schema)}', context=expr.subject.context) if subject_stype.is_view(ctx.env.schema): raise errors.QueryError( f'cannot insert into view ' f'{subject_stype.get_shortname(ctx.env.schema)!r}', context=expr.subject.context) stmt.subject = compile_query_subject( subject, shape=expr.shape, view_rptr=ctx.view_rptr, compile_views=True, result_alias=expr.subject_alias, is_insert=True, ctx=ictx) stmt_subject_stype = setgen.get_set_type(subject, ctx=ictx) result = setgen.class_set( stmt_subject_stype.material_type(ctx.env.schema), path_id=stmt.subject.path_id, ctx=ctx) stmt.result = compile_query_subject( result, view_scls=ctx.view_scls, view_name=ctx.toplevel_result_view_name, compile_views=ictx.stmt is ictx.toplevel_stmt, ctx=ictx) result = fini_stmt(stmt, expr, ctx=ictx, parent_ctx=ctx) return result
def compile_InsertQuery( expr: qlast.InsertQuery, *, ctx: context.ContextLevel) -> irast.Set: if ctx.in_conditional is not None: raise errors.QueryError( 'INSERT statements cannot be used inside conditional expressions', context=expr.context, ) with ctx.subquery() as ictx: stmt = irast.InsertStmt() init_stmt(stmt, expr, ctx=ictx, parent_ctx=ctx) subject = dispatch.compile(expr.subject, ctx=ictx) assert isinstance(subject, irast.Set) subject_stype = setgen.get_set_type(subject, ctx=ictx) if subject_stype.get_is_abstract(ctx.env.schema): raise errors.QueryError( f'cannot insert into abstract ' f'{subject_stype.get_verbosename(ctx.env.schema)}', context=expr.subject.context) if subject_stype.is_view(ctx.env.schema): raise errors.QueryError( f'cannot insert into expression alias ' f'{subject_stype.get_shortname(ctx.env.schema)!r}', context=expr.subject.context) with ictx.new() as bodyctx: # Self-references in INSERT are prohibited. bodyctx.banned_paths = ictx.banned_paths.copy() pathctx.ban_path(subject.path_id, ctx=bodyctx) bodyctx.implicit_id_in_shapes = False bodyctx.implicit_tid_in_shapes = False bodyctx.implicit_limit = 0 stmt.subject = compile_query_subject( subject, shape=expr.shape, view_rptr=ctx.view_rptr, compile_views=True, result_alias=expr.subject_alias, is_insert=True, ctx=bodyctx) stmt_subject_stype = setgen.get_set_type(subject, ctx=ictx) result = setgen.class_set( schemactx.get_material_type(stmt_subject_stype, ctx=ctx), path_id=stmt.subject.path_id, ctx=ctx, ) with ictx.new() as resultctx: if ictx.stmt is ctx.toplevel_stmt: resultctx.expr_exposed = True stmt.result = compile_query_subject( result, view_scls=ctx.view_scls, view_name=ctx.toplevel_result_view_name, compile_views=ictx.stmt is ictx.toplevel_stmt, ctx=resultctx, ) result = fini_stmt(stmt, expr, ctx=ictx, parent_ctx=ctx) return result