static sql_rel * create_trigger(mvc *sql, dlist *qname, int time, symbol *trigger_event, char *table_name, dlist *opt_ref, dlist *triggered_action) { char *tname = qname_table(qname); sql_schema *ss = cur_schema(sql); sql_table *t = NULL; int instantiate = (sql->emode == m_instantiate); int create = (!instantiate && sql->emode != m_deps); list *sq = NULL; sql_rel *r = NULL; dlist *columns = trigger_event->data.lval; char *old_name = NULL, *new_name = NULL; dlist *stmts = triggered_action->h->next->next->data.lval; if (opt_ref) { dnode *dl = opt_ref->h; for ( ; dl; dl = dl->next) { /* list (new(1)/old(0)), char */ char *n = dl->data.lval->h->next->data.sval; assert(dl->data.lval->h->type == type_int); if (!dl->data.lval->h->data.i_val) /*?l_val?*/ old_name = n; else new_name = n; } } if (create && !schema_privs(sql->role_id, ss)) return sql_error(sql, 02, "CREATE TRIGGER: access denied for %s to schema ;'%s'", stack_get_string(sql, "current_user"), ss->base.name); if (create && mvc_bind_trigger(sql, ss, tname) != NULL) return sql_error(sql, 02, "CREATE TRIGGER: name '%s' already in use", tname); if (create && !(t = mvc_bind_table(sql, ss, table_name))) return sql_error(sql, 02, "CREATE TRIGGER: unknown table '%s'", table_name); if (create && isView(t)) return sql_error(sql, 02, "CREATE TRIGGER: cannot create trigger on view '%s'", table_name); if (create) { int event = (trigger_event->token == SQL_INSERT)?0: (trigger_event->token == SQL_DELETE)?1:2; int orientation = triggered_action->h->data.i_val; char *condition = triggered_action->h->next->data.sval; char *q = QUERY(sql->scanner); assert(triggered_action->h->type == type_int); return rel_create_trigger(sql, t->s->base.name, t->base.name, tname, time, orientation, event, old_name, new_name, condition, q); } t = mvc_bind_table(sql, ss, table_name); stack_push_frame(sql, "OLD-NEW"); /* we need to add the old and new tables */ if (new_name) _stack_push_table(sql, new_name, t); if (old_name) _stack_push_table(sql, old_name, t); sq = sequential_block(sql, NULL, NULL, stmts, NULL, 1); r = rel_psm_block(sql->sa, sq); /* todo trigger_columns */ (void)columns; return r; }
static list * sequential_block (mvc *sql, sql_subtype *restype, list *restypelist, dlist *blk, char *opt_label, int is_func) { list *l=0; dnode *n; assert(!restype || !restypelist); if (THRhighwater()) return sql_error(sql, 10, "SELECT: too many nested operators"); if (blk->h) l = sa_list(sql->sa); stack_push_frame(sql, opt_label); for (n = blk->h; n; n = n->next ) { sql_exp *res = NULL; list *reslist = NULL; symbol *s = n->data.sym; switch (s->token) { case SQL_SET: res = psm_set_exp(sql, s->data.lval->h); break; case SQL_DECLARE: reslist = rel_psm_declare(sql, s->data.lval->h); break; case SQL_CREATE_TABLE: res = rel_psm_declare_table(sql, s->data.lval->h); break; case SQL_WHILE: res = rel_psm_while_do(sql, restype, s->data.lval->h, is_func); break; case SQL_IF: res = rel_psm_if_then_else(sql, restype, s->data.lval->h, is_func); break; case SQL_CASE: reslist = rel_psm_case(sql, restype, s->data.lval->h, is_func); break; case SQL_CALL: res = rel_psm_call(sql, s->data.sym); break; case SQL_RETURN: /*If it is not a function it cannot have a return statement*/ if (!is_func) res = sql_error(sql, 01, "Return statement in the procedure body"); else { /* should be last statement of a sequential_block */ if (n->next) { res = sql_error(sql, 01, "Statement after return"); } else { reslist = rel_psm_return(sql, restype, restypelist, s->data.sym); } } break; case SQL_SELECT: { /* row selections (into variables) */ exp_kind ek = {type_value, card_row, TRUE}; reslist = rel_select_into(sql, s, ek); } break; case SQL_COPYFROM: case SQL_BINCOPYFROM: case SQL_INSERT: case SQL_UPDATE: case SQL_DELETE: { sql_rel *r = rel_updates(sql, s); if (!r) return NULL; res = exp_rel(sql, r); } break; default: res = sql_error(sql, 01, "Statement '%s' is not a valid flow control statement", token2string(s->token)); } if (!res && !reslist) { l = NULL; break; } if (res) list_append(l, res); else list_merge(l, reslist, NULL); } stack_pop_frame(sql); return l; }
static sql_rel * create_trigger(mvc *sql, dlist *qname, int time, symbol *trigger_event, dlist *tqname, dlist *opt_ref, dlist *triggered_action, int replace) { const char *triggerschema = qname_schema(qname); const char *triggername = qname_table(qname); const char *sname = qname_schema(tqname); const char *tname = qname_table(tqname); sql_schema *ss = cur_schema(sql); sql_table *t = NULL; sql_trigger *st = NULL; int instantiate = (sql->emode == m_instantiate); int create = (!instantiate && sql->emode != m_deps), event, orientation; list *sq = NULL; sql_rel *r = NULL; char *q, *base = replace ? "CREATE OR REPLACE" : "CREATE"; dlist *columns = trigger_event->data.lval; const char *old_name = NULL, *new_name = NULL; dlist *stmts = triggered_action->h->next->next->data.lval; symbol *condition = triggered_action->h->next->data.sym; if (!sname) sname = ss->base.name; if (sname && !(ss = mvc_bind_schema(sql, sname))) return sql_error(sql, 02, SQLSTATE(3F000) "%s TRIGGER: no such schema '%s'", base, sname); if (opt_ref) { dnode *dl = opt_ref->h; for ( ; dl; dl = dl->next) { /* list (new(1)/old(0)), char */ char *n = dl->data.lval->h->next->data.sval; assert(dl->data.lval->h->type == type_int); if (!dl->data.lval->h->data.i_val) /*?l_val?*/ old_name = n; else new_name = n; } } if (create && !mvc_schema_privs(sql, ss)) return sql_error(sql, 02, SQLSTATE(42000) "%s TRIGGER: access denied for %s to schema ;'%s'", base, stack_get_string(sql, "current_user"), ss->base.name); if (create && !(t = mvc_bind_table(sql, ss, tname))) return sql_error(sql, 02, SQLSTATE(42000) "%s TRIGGER: unknown table '%s'", base, tname); if (create && isView(t)) return sql_error(sql, 02, SQLSTATE(42000) "%s TRIGGER: cannot create trigger on view '%s'", base, tname); if (triggerschema && strcmp(triggerschema, sname) != 0) return sql_error(sql, 02, SQLSTATE(42000) "%s TRIGGER: trigger and respective table must belong to the same schema", base); if (create && (st = mvc_bind_trigger(sql, ss, triggername)) != NULL) { if (replace) { if(mvc_drop_trigger(sql, ss, st)) return sql_error(sql, 02, SQLSTATE(HY001) "%s TRIGGER: %s", base, MAL_MALLOC_FAIL); } else { return sql_error(sql, 02, SQLSTATE(42000) "%s TRIGGER: name '%s' already in use", base, triggername); } } if (create) { switch (trigger_event->token) { case SQL_INSERT: event = 0; break; case SQL_DELETE: event = 1; break; case SQL_TRUNCATE: event = 3; break; default: event = 2; break; } orientation = triggered_action->h->data.i_val; q = query_cleaned(QUERY(sql->scanner)); assert(triggered_action->h->type == type_int); r = rel_create_trigger(sql, t->s->base.name, t->base.name, triggername, time, orientation, event, old_name, new_name, condition, q); GDKfree(q); return r; } if (!instantiate) { t = mvc_bind_table(sql, ss, tname); if(!stack_push_frame(sql, "OLD-NEW")) return sql_error(sql, 02, SQLSTATE(HY001) MAL_MALLOC_FAIL); /* we need to add the old and new tables */ if (!instantiate && new_name) { if(!_stack_push_table(sql, new_name, t)) return sql_error(sql, 02, SQLSTATE(HY001) MAL_MALLOC_FAIL); } if (!instantiate && old_name) { if(!_stack_push_table(sql, old_name, t)) return sql_error(sql, 02, SQLSTATE(HY001) MAL_MALLOC_FAIL); } } if (condition) { sql_rel *rel = NULL; if (new_name) /* in case of updates same relations is available via both names */ rel = stack_find_rel_view(sql, new_name); if (!rel && old_name) rel = stack_find_rel_view(sql, old_name); if (rel) rel = rel_logical_exp(sql, rel, condition, sql_where); if (!rel) return NULL; /* transition tables */ /* insert: rel_select(table [new], searchcondition) */ /* delete: rel_select(table [old], searchcondition) */ /* update: rel_select(table [old,new]), searchcondition) */ if (new_name) stack_update_rel_view(sql, new_name, rel); if (old_name) stack_update_rel_view(sql, old_name, new_name?rel_dup(rel):rel); } sq = sequential_block(sql, NULL, NULL, stmts, NULL, 1); r = rel_psm_block(sql->sa, sq); /* todo trigger_columns */ (void)columns; return r; }