Changeset: 40165328087a for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=40165328087a
Modified Files:
        sql/server/rel_sequence.c
        sql/server/sql_parser.h
        sql/server/sql_parser.y
Branch: default
Log Message:

made sequence number options more flexible (ie no need to give them in a
fixed order)


diffs (truncated from 578 to 300 lines):

diff --git a/sql/server/rel_sequence.c b/sql/server/rel_sequence.c
--- a/sql/server/rel_sequence.c
+++ b/sql/server/rel_sequence.c
@@ -115,6 +115,81 @@ rel_create_seq(
        return res;
 }
 
+#define SEQ_TYPE       0
+#define SEQ_START      1
+#define SEQ_INC                2
+#define SEQ_MIN                3
+#define SEQ_MAX                4
+#define SEQ_CYCLE      5
+#define SEQ_CACHE      6
+
+static sql_rel *
+list_create_seq(
+       mvc *sql,
+       sql_schema *ss,
+       dlist *qname,
+       dlist *options,
+       int bedropped)
+{
+       dnode *n;
+       sql_subtype* t = NULL;
+       lng start = 1, inc = 1, min = 0, max = 0, cache = 1;
+       int used = 0, cycle = 0;
+
+       /* check if no option is given twice */
+       for (n = options->h; n; n = n->next) {
+               symbol *s = n->data.sym;
+
+               switch(s->token) {
+               case SQL_TYPE:
+                       if ((used&(1<<SEQ_TYPE))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: AS type found should be used as most once");
+                       used |= (1<<SEQ_TYPE);
+                       t = &s->data.lval->h->data.typeval;
+                       break;
+               case SQL_START:
+                       if ((used&(1<<SEQ_START))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: START value should be passed as most once");
+                       used |= (1<<SEQ_START);
+                       start = s->data.l_val;
+                       break;
+               case SQL_INC:
+                       if ((used&(1<<SEQ_INC))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: INCREMENT value should be passed as most once");
+                       used |= (1<<SEQ_INC);
+                       inc = s->data.l_val;
+                       break;
+               case SQL_MINVALUE:
+                       if ((used&(1<<SEQ_MIN))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: MINVALUE or NO MINVALUE should be passed as most once");
+                       used |= (1<<SEQ_MIN);
+                       min = s->data.l_val;
+                       break;
+               case SQL_MAXVALUE:
+                       if ((used&(1<<SEQ_MAX))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: MAXVALUE or NO MAXVALUE should be passed as most once");
+                       used |= (1<<SEQ_MAX);
+                       max = s->data.l_val;
+                       break;
+               case SQL_CYCLE:
+                       if ((used&(1<<SEQ_CYCLE))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: CYCLE or NO CYCLE should be passed as most once");
+                       used |= (1<<SEQ_CYCLE);
+                       cycle = s->data.i_val;
+                       break;
+               case SQL_CACHE:
+                       if ((used&(1<<SEQ_CACHE))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: CACHE value should be passed as most once");
+                       used |= (1<<SEQ_CACHE);
+                       cache = s->data.l_val;
+                       break;
+               default:
+                       assert(0);
+               }
+       }
+       return rel_create_seq(sql, ss, qname, t, start, inc, min, max, cache, 
cycle, bedropped);
+}
+
 static sql_rel *
 rel_alter_seq(
                mvc *sql,
@@ -172,18 +247,77 @@ rel_alter_seq(
                if (!val || !(val = rel_check_type(sql, lng_t, val, 
type_equal)))
                        return NULL;
        } else if (start_type == 2) {
-               switch (start_list->h->next->type) {
-                       case type_int:
-                               val = exp_atom_lng(sql->sa, 
(lng)start_list->h->next->data.i_val);
-                               break;
-                       case type_lng:
-                               val = exp_atom_lng(sql->sa, 
start_list->h->next->data.l_val);
-                               break;
-                       default:
-                               assert(0);
+               assert (start_list->h->next->type == type_lng); 
+               val = exp_atom_lng(sql->sa, start_list->h->next->data.l_val);
+       }
+       return rel_seq(sql->sa, DDL_ALTER_SEQ, s->base.name, seq, r, val);
+}
+
+static sql_rel *
+list_alter_seq(
+       mvc *sql,
+       sql_schema *ss,
+       dlist *qname,
+       dlist *options)
+{
+       dnode *n;
+       sql_subtype* t = NULL;
+       lng inc = -1, min = -1, max = -1, cache = -1;
+       dlist *start = NULL;
+       int used = 0, cycle = 0;
+
+       /* check if no option is given twice */
+       for (n = options->h; n; n = n->next) {
+               symbol *s = n->data.sym;
+
+               switch(s->token) {
+               case SQL_TYPE:
+                       if ((used&(1<<SEQ_TYPE))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: AS type found should be used as most once");
+                       used |= (1<<SEQ_TYPE);
+                       t = &s->data.lval->h->data.typeval;
+                       break;
+               case SQL_START:
+                       if ((used&(1<<SEQ_START))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: START value should be passed as most once");
+                       used |= (1<<SEQ_START);
+                       start = s->data.lval;
+                       break;
+               case SQL_INC:
+                       if ((used&(1<<SEQ_INC))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: INCREMENT value should be passed as most once");
+                       used |= (1<<SEQ_INC);
+                       inc = s->data.l_val;
+                       break;
+               case SQL_MINVALUE:
+                       if ((used&(1<<SEQ_MIN))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: MINVALUE or NO MINVALUE should be passed as most once");
+                       used |= (1<<SEQ_MIN);
+                       min = s->data.l_val;
+                       break;
+               case SQL_MAXVALUE:
+                       if ((used&(1<<SEQ_MAX))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: MAXVALUE or NO MAXVALUE should be passed as most once");
+                       used |= (1<<SEQ_MAX);
+                       max = s->data.l_val;
+                       break;
+               case SQL_CYCLE:
+                       if ((used&(1<<SEQ_CYCLE))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: CYCLE or NO CYCLE should be passed as most once");
+                       used |= (1<<SEQ_CYCLE);
+                       cycle = s->data.i_val;
+                       break;
+               case SQL_CACHE:
+                       if ((used&(1<<SEQ_CACHE))) 
+                               return sql_error(sql, 02, "3F000!CREATE 
SEQUENCE: CACHE value should be passed as most once");
+                       used |= (1<<SEQ_CACHE);
+                       cache = s->data.l_val;
+                       break;
+               default:
+                       assert(0);
                }
        }
-       return rel_seq(sql->sa, DDL_ALTER_SEQ, s->base.name, seq, r, val);
+       return rel_alter_seq(sql, ss, qname, t, start, inc, min, max, cache, 
cycle);
 }
 
 sql_rel *
@@ -195,50 +329,24 @@ rel_sequences(mvc *sql, symbol *s)
                case SQL_CREATE_SEQ:
                {
                        dlist *l = s->data.lval;
-                       dlist *h = l->h->next->next->data.lval;
 
-                       assert(h->h->type == type_lng);
-                       assert(h->h->next->type == type_lng);
-                       assert(h->h->next->next->type == type_lng);
-                       assert(h->h->next->next->next->type == type_lng);
-                       assert(h->h->next->next->next->next->type == type_lng);
-                       assert(h->h->next->next->next->next->next->type == 
type_int);
-                       assert(h->h->next->next->next->next->next->next->type 
== type_int);
-                       res = rel_create_seq(
+                       res = list_create_seq(
 /* mvc* sql */         sql,
 /* sql_schema* s */    cur_schema(sql), 
 /* dlist* qname */     l->h->data.lval, 
-/* sql_subtype* t*/    &l->h->next->data.typeval, 
-/* lng start */                h->h->data.l_val, 
-/* lng inc */          h->h->next->data.l_val, 
-/* lng min */          h->h->next->next->data.l_val, 
-/* lng max */          h->h->next->next->next->data.l_val,
-/* lng cache */                h->h->next->next->next->next->data.l_val,
-/* int cycle */                h->h->next->next->next->next->next->data.i_val,
-/* int bedropped */    h->h->next->next->next->next->next->next->data.i_val);
+/* dlist* options */   l->h->next->data.lval, 
+/* int bedropped */    l->h->next->next->data.i_val); 
                }
                break;
                case SQL_ALTER_SEQ:
                {
                        dlist* l = s->data.lval;
-                       dlist *h = l->h->next->next->data.lval;
                        
-                       assert(h->h->next->type == type_lng);
-                       assert(h->h->next->next->type == type_lng);
-                       assert(h->h->next->next->next->type == type_lng);
-                       assert(h->h->next->next->next->next->type == type_lng);
-                       assert(h->h->next->next->next->next->next->type == 
type_int);
-                       res = rel_alter_seq(
+                       res = list_alter_seq(
 /* mvc* sql */         sql,
 /* sql_schema* s */    cur_schema(sql), 
 /* dlist* qname */     l->h->data.lval, 
-/* sql_subtype* t*/    &l->h->next->data.typeval, 
-/* dlist start */      h->h->data.lval, 
-/* lng inc */          h->h->next->data.l_val, 
-/* lng min */          h->h->next->next->data.l_val, 
-/* lng max */          h->h->next->next->next->data.l_val,
-/* lng cache */                h->h->next->next->next->next->data.l_val,
-/* int cycle */                h->h->next->next->next->next->next->data.i_val);
+/* dlist* options */   l->h->next->data.lval);
                }
                break;
                case SQL_DROP_SEQ:
diff --git a/sql/server/sql_parser.h b/sql/server/sql_parser.h
--- a/sql/server/sql_parser.h
+++ b/sql/server/sql_parser.h
@@ -153,6 +153,12 @@ typedef enum tokens {
        SQL_NEXT,
        SQL_MULSTMT,
        SQL_WITH,
+       SQL_START,
+       SQL_INC,
+       SQL_MINVALUE,
+       SQL_MAXVALUE,
+       SQL_CACHE,
+       SQL_CYCLE,
        SQL_XMLCOMMENT,
        SQL_XMLCONCAT,
        SQL_XMLDOCUMENT,
diff --git a/sql/server/sql_parser.y b/sql/server/sql_parser.y
--- a/sql/server/sql_parser.y
+++ b/sql/server/sql_parser.y
@@ -115,6 +115,9 @@ int yydebug=1;
        func_def
        index_def
        seq_def
+       opt_seq_param
+       opt_alt_seq_param
+       opt_seq_common_param
        all_or_any_predicate
        like_exp
        between_predicate
@@ -356,7 +359,6 @@ int yydebug=1;
        opt_seq_params
        opt_alt_seq_params
        serial_opt_params
-       opt_restart
        triggered_action
        opt_referencing_list
        old_or_new_values_alias_list
@@ -397,7 +399,6 @@ int yydebug=1;
        nonzero
        opt_bounds
        opt_column
-       opt_cycle
        opt_encrypted
        opt_for_each
        opt_from_grantor
@@ -437,14 +438,9 @@ int yydebug=1;
        nonzerowrd
 
 %type <l_val>
-       opt_start
        lngval
        poslng
        nonzerolng
-       opt_increment
-       opt_min
-       opt_max
-       opt_cache
 
 %type <bval>
        opt_brackets
@@ -1106,23 +1102,22 @@ create_statement:
 /*=== BEGIN SEQUENCES ===*/
 seq_def:
 /*
- * CREATE SEQUENCE name AS datatype
+ * CREATE SEQUENCE name 
+ *      [ AS datatype ]
  *     [ START WITH start ] 
  *     [ INCREMENT BY increment ]
  *     [ MINVALUE minvalue | NO MINVALUE ]
  *     [ MAXVALUE maxvalue | NO MAXVALUE ]
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to