Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: tarantool-patches@freelists.org
Cc: v.shpilevoy@tarantool.org, kostja@tarantool.org,
	Nikita Pettik <korablev@tarantool.org>
Subject: [tarantool-patches] [PATCH v2 2/5] sql: rework ALTER TABLE grammar
Date: Wed, 23 Jan 2019 20:56:15 +0300	[thread overview]
Message-ID: <3eef24dce00924c053835603f4811ded60739fae.1548265148.git.korablev@tarantool.org> (raw)
In-Reply-To: <cover.1548265148.git.korablev@tarantool.org>
In-Reply-To: <cover.1548265148.git.korablev@tarantool.org>

Since ALTER TABLE ADD CONSTRAINT is going to be used to add various
constraint types (foreign key, unique etc), we should rework its
grammar.  As a reference for it lets use one from ANSI.

Needed for #3097
---
 src/box/sql/parse.y | 77 ++++++++++++++++++++++++++++++-----------------------
 1 file changed, 43 insertions(+), 34 deletions(-)

diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
index b0327c27a..7044921c7 100644
--- a/src/box/sql/parse.y
+++ b/src/box/sql/parse.y
@@ -365,17 +365,8 @@ tcons ::= UNIQUE LP sortlist(X) RP. {
 }
 tcons ::= CHECK LP expr(E) RP onconf.
                                  {sql_add_check_constraint(pParse,&E);}
-tcons ::= FOREIGN KEY LP eidlist(FA) RP
-          REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
-  struct create_constraint_def *constr_def = pParse->alter_entity_def;
-  constr_def->is_deferred = D;
-  struct create_fk_def *fk_def =
-    create_fk_def_new(pParse, constr_def, FA, &T, TA, R);
-  if (fk_def == NULL)
-    break;
-  pParse->alter_entity_def = (void *) fk_def;
-  sql_create_foreign_key(pParse);
-}
+tcons ::= foreign_key_def.
+
 %type defer_subclause_opt {int}
 defer_subclause_opt(A) ::= .                    {A = 0;}
 defer_subclause_opt(A) ::= defer_subclause(A).
@@ -1551,18 +1542,33 @@ cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0);}
 cmd ::= ANALYZE nm(X).          {sqlite3Analyze(pParse, &X);}
 
 //////////////////////// ALTER TABLE table ... ////////////////////////////////
-cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
-  struct alter_entity_def *alter_def =
-    alter_entity_def_new(pParse, X);
-  /*
-   * After code preprocessing, this code snippet will get to
-   * one of "switch" cases. Hence, break is enough to gently
-   * terminate it. No clean-ups are required since all structs
-   * are allocated on region. OOM error is set inside def
-   * constructors.
-   */
+cmd ::= alter_table_start alter_table_action .
+
+/**
+ * We should get name of the table before processing
+ * any other rules. So, we've put this routine at
+ * the separate rule.
+ */
+alter_table_start ::= ALTER TABLE fullname(Z) . {
+  struct alter_entity_def *alter_def = alter_entity_def_new(pParse, Z);
+   /*
+    * After code preprocessing, this code snippet will get to
+    * one of "switch" cases. Hence, break is enough to gently
+    * terminate it. No clean-ups are required since all structs
+    * are allocated on region. OOM error is set inside def
+    * constructors.
+    */
   if (alter_def == NULL)
     break;
+  pParse->alter_entity_def = (void *) alter_def;
+}
+
+alter_table_action ::= add_constraint_def.
+alter_table_action ::= drop_constraint_def.
+alter_table_action ::= rename.
+
+rename ::= RENAME TO nm(Z). {
+  struct alter_entity_def *alter_def = pParse->alter_entity_def;
   struct rename_entity_def *rename_def =
     rename_entity_def_new(pParse, alter_def, Z);
   if (rename_def == NULL)
@@ -1571,21 +1577,27 @@ cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
   sql_alter_table_rename(pParse);
 }
 
-cmd ::= ALTER TABLE fullname(X) ADD CONSTRAINT nm(Z) FOREIGN KEY
-        LP eidlist(FA) RP REFERENCES nm(T) eidlist_opt(TA) refargs(R)
-        defer_subclause_opt(D). {
-  struct alter_entity_def *alter_def =
-    alter_entity_def_new(pParse, X);
-  if (alter_def == NULL)
-    break;
+add_constraint_def ::= add_constraint_start constraint_def.
+
+add_constraint_start ::= ADD CONSTRAINT nm(Z). {
+  struct alter_entity_def *alter_def = pParse->alter_entity_def;
   struct create_entity_def *create_def =
     create_entity_def_new(pParse, alter_def, Z, false);
   if (create_def == NULL)
     break;
   struct create_constraint_def *constraint_def =
-    create_constraint_def_new(pParse, create_def, D);
+    create_constraint_def_new(pParse, create_def, false);
   if (constraint_def == NULL)
     break;
+  pParse->alter_entity_def = (void *) constraint_def;
+}
+
+constraint_def ::= foreign_key_def.
+
+foreign_key_def ::= FOREIGN KEY LP eidlist(FA) RP REFERENCES nm(T)
+                    eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
+  struct create_constraint_def *constraint_def = pParse->alter_entity_def;
+  constraint_def->is_deferred = D;
   struct create_fk_def *fk_def =
     create_fk_def_new(pParse, constraint_def, FA, &T, TA, R);
   if (fk_def == NULL)
@@ -1594,11 +1606,8 @@ cmd ::= ALTER TABLE fullname(X) ADD CONSTRAINT nm(Z) FOREIGN KEY
   sql_create_foreign_key(pParse);
 }
 
-cmd ::= ALTER TABLE fullname(X) DROP CONSTRAINT nm(Z). {
-  struct alter_entity_def *alter_def =
-    alter_entity_def_new(pParse, X);
-  if (alter_def == NULL)
-    break;
+drop_constraint_def ::= DROP CONSTRAINT nm(Z). {
+  struct alter_entity_def *alter_def = pParse->alter_entity_def;
   struct drop_entity_def *drop_def =
     drop_entity_def_new(pParse, alter_def, Z, false);
   if (drop_def == NULL)
-- 
2.15.1

  parent reply	other threads:[~2019-01-23 17:56 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
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 ` Nikita Pettik [this message]
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=3eef24dce00924c053835603f4811ded60739fae.1548265148.git.korablev@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [tarantool-patches] [PATCH v2 2/5] sql: rework ALTER TABLE grammar' \
    /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