Tarantool development patches archive
 help / color / mirror / Atom feed
From: "n.pettik" <korablev@tarantool.org>
To: tarantool-patches@freelists.org
Cc: Konstantin Osipov <kostja@tarantool.org>,
	Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Subject: [tarantool-patches] Re: [PATCH v2 1/5] sql: introduce structs assembling DDL arguments during parsing
Date: Tue, 29 Jan 2019 22:03:44 +0300	[thread overview]
Message-ID: <47B8C272-06BC-40B9-9F86-F9BF65B63A33@tarantool.org> (raw)
In-Reply-To: <20190124123059.GB13097@chai>



> On 24 Jan 2019, at 15:30, Konstantin Osipov <kostja@tarantool.org> wrote:
> 
> * n.pettik <korablev@tarantool.org> [19/01/24 13:51]:
>>>> +	 * One of parse_def structures which are used to
>>>> +	 * assemble and carry arguments of DDL routines
>>>> +	 * from parse.y
>>>> +	 */
>>>> +	void *alter_entity_def;
>>> 
>>> Please consider adding a type code to the base entity def, so that
>>> you can use a base struct reference rather than void *. 
>> 
>> Please, look at code and explain why do we need type
>> code? At first I added it, but it turned out that such code
>> was completely useless. The only real usage was to
>> assert that type of structure matches with its code.
> 
> To assert the structure matches with its code.
> 
> -- 
> Konstantin Osipov, Moscow, Russia, +7 903 626 22 32
> http://tarantool.io - www.twitter.com/kostja_osipov

diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index d0e19407a..dfaa6b505 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -717,6 +717,7 @@ sqlite3AddPrimaryKey(Parse * pParse,        /* Parsing context */
        struct create_entity_def create_def = {};
        struct create_constraint_def constr_def = {};
        struct create_index_def idx_def = {};
+       alter_def.entity_type = ENTITY_TYPE_INDEX;
        create_def.base = &alter_def;
        constr_def.base = &create_def;
        idx_def.base = &constr_def;
@@ -1715,6 +1716,7 @@ sql_drop_table(struct Parse *parse_context, bool is_view)
 {
        struct drop_entity_def *drop_def = parse_context->alter_entity_def;
        struct alter_entity_def *alter_def = drop_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_TABLE);
        struct SrcList *table_name_list = alter_def->entity_name;
        struct Vdbe *v = sqlite3GetVdbe(parse_context);
        struct sqlite3 *db = parse_context->db;
@@ -1821,6 +1823,7 @@ sql_create_foreign_key(struct Parse *parse_context)
        struct create_constraint_def *create_constr_def = create_fk_def->base;
        struct create_entity_def *create_def = create_constr_def->base;
        struct alter_entity_def *alter_def = create_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_FK);
        /*
         * When this function is called second time during
         * <CREATE TABLE ...> statement (i.e. at VDBE runtime),
@@ -2041,6 +2044,7 @@ sql_drop_foreign_key(struct Parse *parse_context)
        struct drop_entity_def *drop_def =
                (struct drop_entity_def *) parse_context->alter_entity_def;
        struct alter_entity_def *alter_def = drop_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_FK);
        const char *table_name = alter_def->entity_name->a[0].zName;
        assert(table_name != NULL);
        struct space *child = space_by_name(table_name);
@@ -2257,6 +2261,7 @@ sql_create_index(struct Parse *parse) {
        struct create_constraint_def *create_constr_def = create_idx_def->base;
        struct create_entity_def *create_entity_def = create_constr_def->base;
        struct alter_entity_def *alter_entity_def = create_entity_def->base;
+       assert(alter_entity_def->entity_type == ENTITY_TYPE_INDEX);
        /*
         * Get list of columns to be indexed. It will be NULL if
         * this is a primary key or unique-constraint on the most
@@ -2580,6 +2585,7 @@ sql_drop_index(struct Parse *parse_context)
        struct drop_entity_def *drop_def =
                (struct drop_entity_def *) parse_context->alter_entity_def;
        struct alter_entity_def *alter_def = drop_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_INDEX);
        struct Vdbe *v = sqlite3GetVdbe(parse_context);
        assert(v != NULL);
        struct sqlite3 *db = parse_context->db;
diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
index b0327c27a..1d7f13122 100644
--- a/src/box/sql/parse.y
+++ b/src/box/sql/parse.y
@@ -408,7 +408,8 @@ drop_table ::= ifexists(E) fullname(X) . {
   if (alter_def == NULL)
     break;
   struct drop_entity_def *drop_def =
-    drop_entity_def_new(pParse, alter_def, (struct Token){}, E);
+    drop_entity_def_new(pParse, alter_def, ENTITY_TYPE_TABLE,
+                        (struct Token){}, E);
   if (drop_def == NULL)
     break;
   pParse->alter_entity_def = (void *) drop_def;
@@ -1284,6 +1285,7 @@ cmd ::= createkw uniqueflag(U) INDEX ifnotexists(NE) nm(X)
   struct create_entity_def create_def = {};
   struct create_constraint_def constr_def = {};
   struct create_index_def idx_def = {};
+  alter_def.entity_type = ENTITY_TYPE_INDEX;
   alter_def.entity_name = sqlite3SrcListAppend(pParse->db,0,&Y);
   create_def.base = &alter_def;
   create_def.name = X;
@@ -1358,6 +1360,7 @@ collate(C) ::= COLLATE id.   {C = 1;}
 cmd ::= DROP INDEX ifexists(E) nm(X) ON fullname(Y).   {
   struct alter_entity_def alter_def = {};
   struct drop_entity_def drop_def = {};
+  alter_def.entity_type = ENTITY_TYPE_INDEX;
   alter_def.entity_name = Y;
   drop_def.base = &alter_def;
   drop_def.if_exist = E;
@@ -1417,6 +1420,7 @@ trigger_decl(A) ::= TRIGGER ifnotexists(NOERR) nm(B)
   struct alter_entity_def alter_def = {};
   struct create_entity_def create_def = {};
   struct create_trigger_def trigger_def = {};
+  alter_def.entity_type = ENTITY_TYPE_TRIGGER;
   alter_def.entity_name = E;
   create_def.base = &alter_def;
   create_def.name = B;
@@ -1539,6 +1543,7 @@ raisetype(A) ::= FAIL.      {A = ON_CONFLICT_ACTION_FAIL;}
 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
   struct alter_entity_def alter_def = {};
   struct drop_entity_def drop_def = {};
+  alter_def.entity_type = ENTITY_TYPE_TRIGGER;
   alter_def.entity_name = X;
   drop_def.base = &alter_def;
   drop_def.if_exist = NOERR;
@@ -1600,7 +1605,7 @@ cmd ::= ALTER TABLE fullname(X) DROP CONSTRAINT nm(Z). {
   if (alter_def == NULL)
     break;
   struct drop_entity_def *drop_def =
-    drop_entity_def_new(pParse, alter_def, Z, false);
+    drop_entity_def_new(pParse, alter_def, ENTITY_TYPE_FK, Z, false);
   if (drop_def == NULL)
     break;
   pParse->alter_entity_def = (void *) drop_def;
diff --git a/src/box/sql/parse_def.c b/src/box/sql/parse_def.c
index 7f241636a..d3581e4bb 100644
--- a/src/box/sql/parse_def.c
+++ b/src/box/sql/parse_def.c
@@ -41,6 +41,7 @@ alter_entity_def_new(struct Parse *parse, struct SrcList *name)
                parse->nErr++;
                return NULL;
        }
+       alter_def->entity_type = ENTITY_TYPE_TABLE;
        alter_def->entity_name = name;
        return alter_def;
 }
@@ -82,7 +83,8 @@ create_entity_def_new(struct Parse *parse, struct alter_entity_def *base,
 
 struct drop_entity_def *
 drop_entity_def_new(struct Parse *parse, struct alter_entity_def *base,
-                   struct Token entity_name, bool if_exist)
+                   enum entity_type type, struct Token entity_name,
+                   bool if_exist)
 {
        size_t sz = sizeof(struct drop_entity_def);
        struct drop_entity_def *drop_def = region_alloc(&parse->region, sz);
@@ -92,6 +94,7 @@ drop_entity_def_new(struct Parse *parse, struct alter_entity_def *base,
                parse->nErr++;
                return NULL;
        }
+       base->entity_type = type;
        drop_def->base = base;
        drop_def->name = entity_name;
        drop_def->if_exist = if_exist;
@@ -129,6 +132,7 @@ create_fk_def_new(struct Parse *parse, struct create_constraint_def *base,
                parse->nErr++;
                return NULL;
        }
+       base->base->base->entity_type = ENTITY_TYPE_FK;
        fk_def->base = base;
        fk_def->child_cols = child_cols;
        fk_def->parent_name = parent_name;
@@ -150,6 +154,7 @@ create_index_def_new(struct Parse *parse, struct create_constraint_def *base,
                parse->nErr++;
                return NULL;
        }
+       base->base->base->entity_type = ENTITY_TYPE_INDEX;
        idx_def->base = base;
        idx_def->cols = cols;
        idx_def->idx_type = idx_type;
diff --git a/src/box/sql/parse_def.h b/src/box/sql/parse_def.h
index 2f6d3d047..4ed7c977a 100644
--- a/src/box/sql/parse_def.h
+++ b/src/box/sql/parse_def.h
@@ -66,7 +66,18 @@
  * CREATE TRIGGER tr1 ...
  * ALTER *TABLE* -> CREATE ENTITY -> CREATE TRIGGER
  */
+
+enum entity_type {
+       ENTITY_TYPE_TABLE = 0,
+       ENTITY_TYPE_INDEX,
+       ENTITY_TYPE_TRIGGER,
+       ENTITY_TYPE_FK,
+       entity_type_MAX
+};
+
 struct alter_entity_def {
+       /** Type of topmost entity. */
+       enum entity_type entity_type;
        /** As a rule it is a name of table to be altered. */
        struct SrcList *entity_name;
 };
@@ -153,7 +164,7 @@ create_entity_def_new(struct Parse *parse, struct alter_entity_def *base,
 
 struct drop_entity_def *
 drop_entity_def_new(struct Parse *parse, struct alter_entity_def *base,
-                   struct Token name, bool if_exist);
+                   enum entity_type type, struct Token name, bool if_exist);
 
 struct create_constraint_def *
 create_constraint_def_new(struct Parse *parse, struct create_entity_def *base,
diff --git a/src/box/sql/trigger.c b/src/box/sql/trigger.c
index 685343ce9..c6c9c0393 100644
--- a/src/box/sql/trigger.c
+++ b/src/box/sql/trigger.c
@@ -71,6 +71,7 @@ sql_trigger_begin(struct Parse *parse)
        struct create_trigger_def *trigger_def = parse->alter_entity_def;
        struct create_entity_def *create_def = trigger_def->base;
        struct alter_entity_def *alter_def = create_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_TRIGGER);
 
        char *trigger_name = NULL;
        if (alter_def->entity_name == NULL || db->mallocFailed)
@@ -426,6 +427,7 @@ sql_drop_trigger(struct Parse *parser)
        struct drop_entity_def *drop_def =
                (struct drop_entity_def *) parser->alter_entity_def;
        struct alter_entity_def *alter_def = drop_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_TRIGGER);
        struct SrcList *name = alter_def->entity_name;
        bool no_err = drop_def->if_exist;
        sqlite3 *db = parser->db;

  reply	other threads:[~2019-01-29 19:03 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-23 17:56 [tarantool-patches] [PATCH v2 0/5] Introduce ALTER TABLE ADD CONSTRAINT UNIQUE/PK Nikita Pettik
2019-01-23 17:56 ` [tarantool-patches] [PATCH v2 1/5] sql: introduce structs assembling DDL arguments during parsing Nikita Pettik
2019-01-24  8:36   ` [tarantool-patches] " Konstantin Osipov
2019-01-24 10:47     ` n.pettik
2019-01-24 12:30       ` Konstantin Osipov
2019-01-29 19:03         ` n.pettik [this message]
2019-01-29 19:29   ` Vladislav Shpilevoy
2019-01-29 20:04     ` n.pettik
2019-01-29 20:20       ` Vladislav Shpilevoy
2019-01-29 21:25         ` n.pettik
2019-01-31 19:32     ` n.pettik
2019-02-04 15:25       ` Vladislav Shpilevoy
2019-02-08 14:25         ` n.pettik
2019-02-15 20:13           ` Vladislav Shpilevoy
2019-02-27 22:56             ` n.pettik
2019-03-12 12:50               ` Vladislav Shpilevoy
2019-03-14 18:13                 ` n.pettik
2019-03-25 11:25                   ` Vladislav Shpilevoy
2019-03-26 18:01                     ` n.pettik
2019-03-26 18:06                       ` Vladislav Shpilevoy
2019-03-27 13:00                         ` n.pettik
2019-03-27 13:29                           ` Vladislav Shpilevoy
2019-03-27 13:44                             ` n.pettik
2019-03-27 14:03                               ` Vladislav Shpilevoy
2019-03-27 14:11                                 ` n.pettik
2019-01-23 17:56 ` [tarantool-patches] [PATCH v2 2/5] sql: rework ALTER TABLE grammar Nikita Pettik
2019-01-23 17:56 ` [tarantool-patches] [PATCH v2 3/5] sql: refactor getNewIid() function Nikita Pettik
2019-01-23 17:56 ` [tarantool-patches] [PATCH v2 4/5] sql: fix error message for improperly created index Nikita Pettik
2019-02-08 17:14   ` [tarantool-patches] " Konstantin Osipov
2019-01-23 17:56 ` [tarantool-patches] [PATCH v2 5/5] sql: introduce ALTER TABLE ADD CONSTRAINT UNIQUE/PRIMARY KEY Nikita Pettik
2019-01-24  8:31   ` [tarantool-patches] " Konstantin Osipov
2019-01-29 19:29   ` Vladislav Shpilevoy
2019-02-08 17:16   ` Konstantin Osipov
2019-02-08 17:36     ` n.pettik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47B8C272-06BC-40B9-9F86-F9BF65B63A33@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='[tarantool-patches] Re: [PATCH v2 1/5] sql: introduce structs assembling DDL arguments during parsing' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox