From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 0EA36241A5 for ; Tue, 29 Jan 2019 14:03:48 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3QEw0YPXuSQq for ; Tue, 29 Jan 2019 14:03:47 -0500 (EST) Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 959892412A for ; Tue, 29 Jan 2019 14:03:47 -0500 (EST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.2 \(3445.102.3\)) Subject: [tarantool-patches] Re: [PATCH v2 1/5] sql: introduce structs assembling DDL arguments during parsing From: "n.pettik" In-Reply-To: <20190124123059.GB13097@chai> Date: Tue, 29 Jan 2019 22:03:44 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: <47B8C272-06BC-40B9-9F86-F9BF65B63A33@tarantool.org> References: <0fcc585532a1f1200a7dfd4a8e911ecf9f2c94aa.1548265148.git.korablev@tarantool.org> <20190124083630.GC3573@chai> <4029AB62-F67D-45F0-9370-2F3A37CB9D09@tarantool.org> <20190124123059.GB13097@chai> Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org Cc: Konstantin Osipov , Vladislav Shpilevoy > On 24 Jan 2019, at 15:30, Konstantin Osipov = wrote: >=20 > * n.pettik [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; >>>=20 >>> Please consider adding a type code to the base entity def, so that >>> you can use a base struct reference rather than void *.=20 >>=20 >> 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. >=20 > To assert the structure matches with its code. >=20 > --=20 > 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 =3D {}; struct create_constraint_def constr_def =3D {}; struct create_index_def idx_def =3D {}; + alter_def.entity_type =3D ENTITY_TYPE_INDEX; create_def.base =3D &alter_def; constr_def.base =3D &create_def; idx_def.base =3D &constr_def; @@ -1715,6 +1716,7 @@ sql_drop_table(struct Parse *parse_context, bool = is_view) { struct drop_entity_def *drop_def =3D = parse_context->alter_entity_def; struct alter_entity_def *alter_def =3D drop_def->base; + assert(alter_def->entity_type =3D=3D ENTITY_TYPE_TABLE); struct SrcList *table_name_list =3D alter_def->entity_name; struct Vdbe *v =3D sqlite3GetVdbe(parse_context); struct sqlite3 *db =3D parse_context->db; @@ -1821,6 +1823,7 @@ sql_create_foreign_key(struct Parse = *parse_context) struct create_constraint_def *create_constr_def =3D = create_fk_def->base; struct create_entity_def *create_def =3D = create_constr_def->base; struct alter_entity_def *alter_def =3D create_def->base; + assert(alter_def->entity_type =3D=3D ENTITY_TYPE_FK); /* * When this function is called second time during * statement (i.e. at VDBE runtime), @@ -2041,6 +2044,7 @@ sql_drop_foreign_key(struct Parse *parse_context) struct drop_entity_def *drop_def =3D (struct drop_entity_def *) = parse_context->alter_entity_def; struct alter_entity_def *alter_def =3D drop_def->base; + assert(alter_def->entity_type =3D=3D ENTITY_TYPE_FK); const char *table_name =3D alter_def->entity_name->a[0].zName; assert(table_name !=3D NULL); struct space *child =3D space_by_name(table_name); @@ -2257,6 +2261,7 @@ sql_create_index(struct Parse *parse) { struct create_constraint_def *create_constr_def =3D = create_idx_def->base; struct create_entity_def *create_entity_def =3D = create_constr_def->base; struct alter_entity_def *alter_entity_def =3D = create_entity_def->base; + assert(alter_entity_def->entity_type =3D=3D 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 =3D (struct drop_entity_def *) = parse_context->alter_entity_def; struct alter_entity_def *alter_def =3D drop_def->base; + assert(alter_def->entity_type =3D=3D ENTITY_TYPE_INDEX); struct Vdbe *v =3D sqlite3GetVdbe(parse_context); assert(v !=3D NULL); struct sqlite3 *db =3D 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 ::=3D ifexists(E) fullname(X) . { if (alter_def =3D=3D NULL) break; struct drop_entity_def *drop_def =3D - 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 =3D=3D NULL) break; pParse->alter_entity_def =3D (void *) drop_def; @@ -1284,6 +1285,7 @@ cmd ::=3D createkw uniqueflag(U) INDEX = ifnotexists(NE) nm(X) struct create_entity_def create_def =3D {}; struct create_constraint_def constr_def =3D {}; struct create_index_def idx_def =3D {}; + alter_def.entity_type =3D ENTITY_TYPE_INDEX; alter_def.entity_name =3D sqlite3SrcListAppend(pParse->db,0,&Y); create_def.base =3D &alter_def; create_def.name =3D X; @@ -1358,6 +1360,7 @@ collate(C) ::=3D COLLATE id. {C =3D 1;} cmd ::=3D DROP INDEX ifexists(E) nm(X) ON fullname(Y). { struct alter_entity_def alter_def =3D {}; struct drop_entity_def drop_def =3D {}; + alter_def.entity_type =3D ENTITY_TYPE_INDEX; alter_def.entity_name =3D Y; drop_def.base =3D &alter_def; drop_def.if_exist =3D E; @@ -1417,6 +1420,7 @@ trigger_decl(A) ::=3D TRIGGER ifnotexists(NOERR) = nm(B) struct alter_entity_def alter_def =3D {}; struct create_entity_def create_def =3D {}; struct create_trigger_def trigger_def =3D {}; + alter_def.entity_type =3D ENTITY_TYPE_TRIGGER; alter_def.entity_name =3D E; create_def.base =3D &alter_def; create_def.name =3D B; @@ -1539,6 +1543,7 @@ raisetype(A) ::=3D FAIL. {A =3D = ON_CONFLICT_ACTION_FAIL;} cmd ::=3D DROP TRIGGER ifexists(NOERR) fullname(X). { struct alter_entity_def alter_def =3D {}; struct drop_entity_def drop_def =3D {}; + alter_def.entity_type =3D ENTITY_TYPE_TRIGGER; alter_def.entity_name =3D X; drop_def.base =3D &alter_def; drop_def.if_exist =3D NOERR; @@ -1600,7 +1605,7 @@ cmd ::=3D ALTER TABLE fullname(X) DROP CONSTRAINT = nm(Z). { if (alter_def =3D=3D NULL) break; struct drop_entity_def *drop_def =3D - drop_entity_def_new(pParse, alter_def, Z, false); + drop_entity_def_new(pParse, alter_def, ENTITY_TYPE_FK, Z, false); if (drop_def =3D=3D NULL) break; pParse->alter_entity_def =3D (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 =3D ENTITY_TYPE_TABLE; alter_def->entity_name =3D name; return alter_def; } @@ -82,7 +83,8 @@ create_entity_def_new(struct Parse *parse, struct = alter_entity_def *base, =20 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 =3D sizeof(struct drop_entity_def); struct drop_entity_def *drop_def =3D = 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 =3D type; drop_def->base =3D base; drop_def->name =3D entity_name; drop_def->if_exist =3D 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 =3D ENTITY_TYPE_FK; fk_def->base =3D base; fk_def->child_cols =3D child_cols; fk_def->parent_name =3D 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 =3D ENTITY_TYPE_INDEX; idx_def->base =3D base; idx_def->cols =3D cols; idx_def->idx_type =3D 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 =3D 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, =20 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); =20 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 =3D = parse->alter_entity_def; struct create_entity_def *create_def =3D trigger_def->base; struct alter_entity_def *alter_def =3D create_def->base; + assert(alter_def->entity_type =3D=3D ENTITY_TYPE_TRIGGER); =20 char *trigger_name =3D NULL; if (alter_def->entity_name =3D=3D NULL || db->mallocFailed) @@ -426,6 +427,7 @@ sql_drop_trigger(struct Parse *parser) struct drop_entity_def *drop_def =3D (struct drop_entity_def *) parser->alter_entity_def; struct alter_entity_def *alter_def =3D drop_def->base; + assert(alter_def->entity_type =3D=3D ENTITY_TYPE_TRIGGER); struct SrcList *name =3D alter_def->entity_name; bool no_err =3D drop_def->if_exist; sqlite3 *db =3D parser->db;