[tarantool-patches] [PATCH v2 1/4] sql: rework ALTER TABLE grammar

Nikita Pettik korablev at tarantool.org
Thu Mar 28 15:07:55 MSK 2019


Since ALTER TABLE ADD CONSTRAINT is going to be used to add various
constraint types (foreign key, unique etc), let's a bit refactor it to
make it more extendable.

Needed for #3097
---
 src/box/sql/parse.y          | 31 ++++++++++++++++++++++++-------
 test/sql-tap/alter2.test.lua |  2 +-
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
index 4eff61777..0c4603e83 100644
--- a/src/box/sql/parse.y
+++ b/src/box/sql/parse.y
@@ -1600,18 +1600,35 @@ cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
 }
 
 //////////////////////// ALTER TABLE table ... ////////////////////////////////
-cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
-  rename_entity_def_init(&pParse->rename_entity_def, X, &Z);
-  sql_alter_table_rename(pParse);
+%include {
+  struct alter_args {
+    struct SrcList *table_name;
+    /** Name of constraint OR new name of table in case of RENAME. */
+    struct Token name;
+  };
+}
+
+%type alter_table_start {struct SrcList *}
+alter_table_start(A) ::= ALTER TABLE fullname(T) . { A = T; }
+
+%type alter_add_constraint {struct alter_args}
+alter_add_constraint(A) ::= alter_table_start(T) ADD CONSTRAINT nm(N). {
+  A.table_name = T;
+  A.name = N;
 }
 
-cmd ::= ALTER TABLE fullname(X) ADD CONSTRAINT nm(Z) FOREIGN KEY
-        LP eidlist(FA) RP REFERENCES nm(T) eidlist_opt(TA) matcharg(M)
-        refargs(R) defer_subclause_opt(D). {
-  create_fk_def_init(&pParse->create_fk_def, X, &Z, FA, &T, TA, M, R, D);
+cmd ::= alter_add_constraint(N) FOREIGN KEY LP eidlist(FA) RP REFERENCES
+        nm(T) eidlist_opt(TA) matcharg(M) refargs(R) defer_subclause_opt(D). {
+  create_fk_def_init(&pParse->create_fk_def, N.table_name, &N.name, FA, &T, TA,
+                     M, R, D);
   sql_create_foreign_key(pParse);
 }
 
+cmd ::= alter_table_start(A) RENAME TO nm(N). {
+    rename_entity_def_init(&pParse->rename_entity_def, A, &N);
+    sql_alter_table_rename(pParse);
+}
+
 cmd ::= ALTER TABLE fullname(X) DROP CONSTRAINT nm(Z). {
   drop_fk_def_init(&pParse->drop_fk_def, X, &Z, false);
   sql_drop_foreign_key(pParse);
diff --git a/test/sql-tap/alter2.test.lua b/test/sql-tap/alter2.test.lua
index beb6f5d8a..cc1ff5088 100755
--- a/test/sql-tap/alter2.test.lua
+++ b/test/sql-tap/alter2.test.lua
@@ -223,7 +223,7 @@ test:do_catchsql_test(
         ALTER TABLE child ADD CONSTRAINT fk FOREIGN KEY REFERENCES child(id);
     ]], {
         -- <alter2-4.1>
-        1, "Syntax error near 'REFERENCES'"
+        1, "Keyword 'REFERENCES' is reserved. Please use double quotes if 'REFERENCES' is an identifier."
         -- </alter2-4.1>
     })
 
-- 
2.15.1





More information about the Tarantool-patches mailing list