list * exp_types(sql_allocator *sa, list *exps) { list *l = sa_list(sa); node *n; for ( n = exps->h; n; n = n->next) append(l, exp_subtype(n->data)); return l; }
static sql_exp* rel_psm_call(mvc * sql, symbol *se) { sql_subtype *t; sql_exp *res = NULL; exp_kind ek = {type_value, card_none, FALSE}; sql_rel *rel = NULL; res = rel_value_exp(sql, &rel, se, sql_sel, ek); if (!res || rel || ((t=exp_subtype(res)) && t->type)) /* only procedures */ return sql_error(sql, 01, "function calls are ignored"); return res; }
static list * rel_select_into( mvc *sql, symbol *sq, exp_kind ek) { SelectNode *sn = (SelectNode*)sq; dlist *into = sn->into; node *m; dnode *n; sql_rel *r; list *nl = NULL; /* SELECT ... INTO var_list */ sn->into = NULL; r = rel_subquery(sql, NULL, sq, ek, APPLY_JOIN); if (!r) return NULL; nl = sa_list(sql->sa); append(nl, exp_rel(sql, r)); for (m = r->exps->h, n = into->h; m && n; m = m->next, n = n->next) { sql_subtype *tpe = NULL; char *nme = n->data.sval; sql_exp *v = m->data; int level; if (!stack_find_var(sql, nme)) return sql_error(sql, 02, "SELECT INTO: variable '%s' unknown", nme); /* dynamic check for single values */ if (v->card > CARD_AGGR) { sql_subaggr *zero_or_one = sql_bind_aggr(sql->sa, sql->session->schema, "zero_or_one", exp_subtype(v)); assert(zero_or_one); v = exp_aggr1(sql->sa, v, zero_or_one, 0, 0, CARD_ATOM, 0); } tpe = stack_find_type(sql, nme); level = stack_find_frame(sql, nme); if (!v || !(v = rel_check_type(sql, tpe, v, type_equal))) return NULL; v = exp_set(sql->sa, nme, v, level); list_append(nl, v); } return nl; }
/* return val; */ static list * rel_psm_return( mvc *sql, sql_subtype *restype, list *restypelist, symbol *return_sym ) { exp_kind ek = {type_value, card_value, FALSE}; sql_exp *res; sql_rel *rel = NULL; int is_last = 0; list *l = sa_list(sql->sa); if (restypelist) ek.card = card_relation; res = rel_value_exp2(sql, &rel, return_sym, sql_sel, ek, &is_last); if (!res) return NULL; if (ek.card != card_relation && (!res || (res = rel_check_type(sql, restype, res, type_equal)) == NULL)) return NULL; else if (ek.card == card_relation && !rel) return NULL; if (rel && ek.card != card_relation) append(l, exp_rel(sql, rel)); else if (rel && !is_ddl(rel->op)) { list *exps = sa_list(sql->sa); node *n, *m; int isproject = (rel->op == op_project); list *oexps = rel->exps; sql_rel *l = rel->l; if (is_topn(rel->op)) oexps = l->exps; for (n = oexps->h, m = restypelist->h; n && m; n = n->next, m = m->next) { sql_exp *e = n->data; sql_arg *ce = m->data; char *cname = exp_name(e); char name[16]; if (!cname) cname = sa_strdup(sql->sa, number2name(name, 16, ++sql->label)); if (!isproject) e = exp_column(sql->sa, exp_relname(e), cname, exp_subtype(e), exp_card(e), has_nil(e), is_intern(e)); e = rel_check_type(sql, &ce->type, e, type_equal); if (!e) return NULL; append(exps, e); } if (isproject) rel -> exps = exps; else rel = rel_project(sql->sa, rel, exps); res = exp_rel(sql, rel); } else if (rel && restypelist){ /* handle return table-var */ list *exps = sa_list(sql->sa); sql_table *t = rel_ddl_table_get(rel); node *n, *m; char *tname = t->base.name; if (cs_size(&t->columns) != list_length(restypelist)) return sql_error(sql, 02, "RETURN: number of columns do not match"); for (n = t->columns.set->h, m = restypelist->h; n && m; n = n->next, m = m->next) { sql_column *c = n->data; sql_arg *ce = m->data; sql_exp *e = exp_alias(sql->sa, tname, c->base.name, tname, c->base.name, &c->type, CARD_MULTI, c->null, 0); e = rel_check_type(sql, &ce->type, e, type_equal); if (!e) return NULL; append(exps, e); } rel = rel_project(sql->sa, rel, exps); res = exp_rel(sql, rel); } append(l, exp_return(sql->sa, res, stack_nr_of_declared_tables(sql))); return l; }
static sql_rel * psm_analyze(mvc *sql, dlist *qname, dlist *columns, symbol *sample ) { exp_kind ek = {type_value, card_value, FALSE}; sql_exp *sample_exp = NULL, *call; char *sname = NULL, *tname = NULL; list *tl = sa_list(sql->sa); list *exps = sa_list(sql->sa), *analyze_calls = sa_list(sql->sa); sql_subfunc *f = NULL; if (sample) { sql_subtype *tpe = sql_bind_localtype("lng"); sample_exp = rel_value_exp( sql, NULL, sample, 0, ek); if (sample_exp) sample_exp = rel_check_type(sql, tpe, sample_exp, type_cast); } if (qname) { if (qname->h->next) sname = qname_schema(qname); else sname = qname_table(qname); if (!sname) sname = cur_schema(sql)->base.name; if (qname->h->next) tname = qname_table(qname); } /* call analyze( [schema, [ table ]], opt_sample_size ) */ if (sname) { sql_exp *sname_exp = exp_atom_clob(sql->sa, sname); append(exps, sname_exp); append(tl, exp_subtype(sname_exp)); } if (tname) { sql_exp *tname_exp = exp_atom_clob(sql->sa, tname); append(exps, tname_exp); append(tl, exp_subtype(tname_exp)); if (columns) append(tl, exp_subtype(tname_exp)); } if (!columns) { if (sample_exp) { append(exps, sample_exp); append(tl, exp_subtype(sample_exp)); } f = sql_bind_func_(sql->sa, mvc_bind_schema(sql, "sys"), "analyze", tl, F_PROC); if (!f) return sql_error(sql, 01, "Analyze procedure missing"); call = exp_op(sql->sa, exps, f); append(analyze_calls, call); } else { dnode *n; if (sample_exp) append(tl, exp_subtype(sample_exp)); f = sql_bind_func_(sql->sa, mvc_bind_schema(sql, "sys"), "analyze", tl, F_PROC); if (!f) return sql_error(sql, 01, "Analyze procedure missing"); for( n = columns->h; n; n = n->next) { char *cname = n->data.sval; list *nexps = list_dup(exps, NULL); sql_exp *cname_exp = exp_atom_clob(sql->sa, cname); append(nexps, cname_exp); if (sample_exp) append(nexps, sample_exp); /* call analyze( sname, tname, cname, opt_sample_size ) */ call = exp_op(sql->sa, nexps, f); append(analyze_calls, call); } } return rel_psm_block(sql->sa, analyze_calls); }
/* SET variable = value and set (variable1, .., variableN) = (query) */ static sql_exp * psm_set_exp(mvc *sql, dnode *n) { symbol *val = n->next->data.sym; sql_exp *e = NULL; int level = 0, is_last = 0; sql_subtype *tpe = NULL; sql_rel *rel = NULL; sql_exp *res = NULL; int single = (n->type == type_string); if (single) { exp_kind ek = {type_value, card_value, FALSE}; const char *name = n->data.sval; /* name can be 'parameter of the function' (ie in the param list) or a local or global variable, declared earlier */ /* check if variable is known from the stack */ if (!stack_find_var(sql, name)) { sql_arg *a = sql_bind_param(sql, name); if (!a) /* not parameter, ie local var ? */ return sql_error(sql, 01, SQLSTATE(42000) "Variable %s unknown", name); tpe = &a->type; } else { tpe = stack_find_type(sql, name); } e = rel_value_exp2(sql, &rel, val, sql_sel, ek, &is_last); if (!e || (rel && e->card > CARD_AGGR)) return NULL; level = stack_find_frame(sql, name); e = rel_check_type(sql, tpe, e, type_cast); if (!e) return NULL; if (rel) { sql_exp *er = exp_rel(sql, rel); list *b = sa_list(sql->sa); append(b, er); append(b, exp_set(sql->sa, name, e, level)); res = exp_rel(sql, rel_psm_block(sql->sa, b)); } else { res = exp_set(sql->sa, name, e, level); } } else { /* multi assignment */ exp_kind ek = {type_value, (single)?card_column:card_relation, FALSE}; sql_rel *rel_val = rel_subquery(sql, NULL, val, ek, APPLY_JOIN); dlist *vars = n->data.lval; dnode *m; node *n; list *b; if (!rel_val || !is_project(rel_val->op) || dlist_length(vars) != list_length(rel_val->exps)) { return sql_error(sql, 02, SQLSTATE(42000) "SET: Number of variables not equal to number of supplied values"); } b = sa_list(sql->sa); if (rel_val) { sql_exp *er = exp_rel(sql, rel_val); append(b, er); } for(m = vars->h, n = rel_val->exps->h; n && m; n = n->next, m = m->next) { char *vname = m->data.sval; sql_exp *v = n->data; if (!stack_find_var(sql, vname)) { sql_arg *a = sql_bind_param(sql, vname); if (!a) /* not parameter, ie local var ? */ return sql_error(sql, 01, SQLSTATE(42000) "Variable %s unknown", vname); tpe = &a->type; } else { tpe = stack_find_type(sql, vname); } if (!exp_name(v)) exp_label(sql->sa, v, ++sql->label); v = exp_column(sql->sa, exp_relname(v), exp_name(v), exp_subtype(v), v->card, has_nil(v), is_intern(v)); level = stack_find_frame(sql, vname); v = rel_check_type(sql, tpe, v, type_cast); if (!v) return NULL; if (v->card > CARD_AGGR) { sql_subaggr *zero_or_one = sql_bind_aggr(sql->sa, sql->session->schema, "zero_or_one", exp_subtype(v)); assert(zero_or_one); v = exp_aggr1(sql->sa, v, zero_or_one, 0, 0, CARD_ATOM, 0); } append(b, exp_set(sql->sa, vname, v, level)); } res = exp_rel(sql, rel_psm_block(sql->sa, b)); } return res; }
sql_exp * exp_copy( sql_allocator *sa, sql_exp * e) { sql_exp *l, *r, *r2, *ne = NULL; switch(e->type){ case e_column: ne = exp_column(sa, e->l, e->r, exp_subtype(e), e->card, has_nil(e), is_intern(e)); ne->flag = e->flag; break; case e_cmp: if (e->flag == cmp_or) { list *l = exps_copy(sa, e->l); list *r = exps_copy(sa, e->r); if (l && r) ne = exp_or(sa, l,r); } else if (e->flag == cmp_in || e->flag == cmp_notin || get_cmp(e) == cmp_filter) { sql_exp *l = exp_copy(sa, e->l); list *r = exps_copy(sa, e->r); if (l && r) { if (get_cmp(e) == cmp_filter) ne = exp_filter(sa, l, r, e->f, is_anti(e)); else ne = exp_in(sa, l, r, e->flag); } } else { l = exp_copy(sa, e->l); r = exp_copy(sa, e->r); if (e->f) { r2 = exp_copy(sa, e->f); if (l && r && r2) ne = exp_compare2(sa, l, r, r2, e->flag); } else if (l && r) { ne = exp_compare(sa, l, r, e->flag); } } break; case e_convert: l = exp_copy(sa, e->l); if (l) ne = exp_convert(sa, l, exp_fromtype(e), exp_totype(e)); break; case e_aggr: case e_func: { list *l = e->l, *nl = NULL; if (!l) { return e; } else { nl = exps_copy(sa, l); if (!nl) return NULL; } if (e->type == e_func) ne = exp_op(sa, nl, e->f); else ne = exp_aggr(sa, nl, e->f, need_distinct(e), need_no_nil(e), e->card, has_nil(e)); break; } case e_atom: if (e->l) ne = exp_atom(sa, e->l); else if (!e->r) ne = exp_atom_ref(sa, e->flag, &e->tpe); else ne = exp_param(sa, e->r, &e->tpe, e->flag); break; case e_psm: if (e->flag == PSM_SET) ne = exp_set(sa, e->name, exp_copy(sa, e->l), GET_PSM_LEVEL(e->flag)); break; } if (ne && e->p) ne->p = prop_copy(sa, e->p); if (e->name) exp_setname(sa, ne, exp_find_rel_name(e), exp_name(e)); return ne; }