Changeset: c6bce86a16bc for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=c6bce86a16bc
Modified Files:
        sql/backends/monet5/sql.c
        sql/include/sql_relation.h
        sql/server/rel_schema.c
        sql/server/sql_parser.y
Branch: default
Log Message:

CREATE SCHEMA IF EXISTS/DROP SCHEMA IF NOT EXISTS


diffs (272 lines):

diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -1257,6 +1257,7 @@ SQLcatalog(Client cntxt, MalBlkPtr mb, M
        str msg;
        int type = *getArgReference_int(stk, pci, 1);
        str sname = *getArgReference_str(stk, pci, 2);
+       int if_exists = 0;
 
        if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
                return msg;
@@ -1308,12 +1309,19 @@ SQLcatalog(Client cntxt, MalBlkPtr mb, M
                }
                break;
        }
+       case DDL_DROP_SCHEMA_IF_EXISTS:
+               if_exists = 1;
        case DDL_DROP_SCHEMA:{
                int action = *getArgReference_int(stk, pci, 4);
+
                sql_schema *s = mvc_bind_schema(sql, sname);
 
                if (!s) {
-                       msg = sql_message("3F000!DROP SCHEMA: name %s does not 
exist", sname);
+                       if (!if_exists) {
+                               msg = sql_message("3F000!DROP SCHEMA: name %s 
does not exist", sname);
+                       } else {
+                               break;
+                       }
                } else if (!mvc_schema_privs(sql, s)) {
                        msg = sql_message("42000!DROP SCHEMA: access denied for 
%s to schema ;'%s'", stack_get_string(sql, "current_user"), s->base.name);
                } else if (s == cur_schema(sql)) {
@@ -1336,6 +1344,8 @@ SQLcatalog(Client cntxt, MalBlkPtr mb, M
                msg = create_table_or_view(sql, sname, t, temp);
                break;
        }
+       case DDL_DROP_TABLE_IF_EXISTS:
+               if_exists = 1;
        case DDL_DROP_TABLE:{
                int action = *getArgReference_int(stk, pci, 4);
                str name = *getArgReference_str(stk, pci, 3);
@@ -1343,6 +1353,8 @@ SQLcatalog(Client cntxt, MalBlkPtr mb, M
                msg = drop_table(sql, sname, name, action);
                break;
        }
+       case DDL_DROP_VIEW_IF_EXISTS:
+               if_exists = 1;
        case DDL_DROP_VIEW:{
                int action = *getArgReference_int(stk, pci, 4);
                str name = *getArgReference_str(stk, pci, 3);
@@ -1547,6 +1559,8 @@ SQLcatalog(Client cntxt, MalBlkPtr mb, M
 
                return alter_table_set_access(sql, sname, tname, access);
        }
+       case DDL_EMPTY:
+               break;
        default:
                throw(SQL, "sql.catalog", "catalog unknown type");
        }
diff --git a/sql/include/sql_relation.h b/sql/include/sql_relation.h
--- a/sql/include/sql_relation.h
+++ b/sql/include/sql_relation.h
@@ -85,55 +85,60 @@ typedef struct expression {
 #define GET_PSM_LEVEL(level)   (level>>8)
 
 /* todo make enum */
-#define DDL_OUTPUT     1
-#define DDL_LIST       2       
-#define DDL_PSM                3       
+#define DDL_OUTPUT                    1
+#define DDL_LIST                      2        
+#define DDL_PSM                               3        
 
-#define DDL_CREATE_SEQ  5
-#define DDL_ALTER_SEQ   6
-#define DDL_DROP_SEQ    7
+#define DDL_CREATE_SEQ             5
+#define DDL_ALTER_SEQ              6
+#define DDL_DROP_SEQ               7
 
-#define DDL_RELEASE    11
-#define DDL_COMMIT     12
-#define DDL_ROLLBACK   13
-#define DDL_TRANS      14
+#define DDL_RELEASE                   11
+#define DDL_COMMIT                    12
+#define DDL_ROLLBACK              13
+#define DDL_TRANS                     14
 
-#define DDL_CREATE_SCHEMA 21
-#define DDL_DROP_SCHEMA   22
+#define DDL_CREATE_SCHEMA          21
+#define DDL_DROP_SCHEMA_IF_EXISTS  22
+#define DDL_DROP_SCHEMA            23
 
-#define DDL_CREATE_TABLE 24
-#define DDL_DROP_TABLE          25
-#define DDL_CREATE_VIEW  26
-#define DDL_DROP_VIEW    27
-#define DDL_DROP_CONSTRAINT    28
-#define DDL_ALTER_TABLE  29
+#define DDL_CREATE_TABLE           24
+#define DDL_DROP_TABLE_IF_EXISTS   25
+#define DDL_DROP_TABLE                    26
+#define DDL_CREATE_VIEW            27
+#define DDL_DROP_VIEW_IF_EXISTS    28
+#define DDL_DROP_VIEW              29
+#define DDL_DROP_CONSTRAINT        30
+#define DDL_ALTER_TABLE            31
 
-#define DDL_CREATE_TYPE 30 
-#define DDL_DROP_TYPE   31 
+#define DDL_CREATE_TYPE            32 
+#define DDL_DROP_TYPE              33 
 
-#define DDL_DROP_INDEX    33
+#define DDL_DROP_INDEX             34
 
-#define DDL_CREATE_FUNCTION 41 
-#define DDL_DROP_FUNCTION   42 
-#define DDL_CREATE_TRIGGER 43 
-#define DDL_DROP_TRIGGER   44 
+#define DDL_CREATE_FUNCTION        41 
+#define DDL_DROP_FUNCTION          42 
+#define DDL_CREATE_TRIGGER         43 
+#define DDL_DROP_TRIGGER           44 
 
-#define DDL_GRANT_ROLES 51
-#define DDL_REVOKE_ROLES 52
-#define DDL_GRANT      53
-#define DDL_REVOKE     54
-#define DDL_GRANT_FUNC         55
-#define DDL_REVOKE_FUNC 56
-#define DDL_CREATE_USER 57
-#define DDL_DROP_USER  58
-#define DDL_ALTER_USER         59
-#define DDL_RENAME_USER 60
-#define DDL_CREATE_ROLE 61
-#define DDL_DROP_ROLE  62
+#define DDL_GRANT_ROLES            51
+#define DDL_REVOKE_ROLES           52
+#define DDL_GRANT                     53
+#define DDL_REVOKE                    54
+#define DDL_GRANT_FUNC                    55
+#define DDL_REVOKE_FUNC            56
+#define DDL_CREATE_USER            57
+#define DDL_DROP_USER             58
+#define DDL_ALTER_USER                    59
+#define DDL_RENAME_USER            60
+#define DDL_CREATE_ROLE            61
+#define DDL_DROP_ROLE             62
 
 #define DDL_ALTER_TABLE_ADD_TABLE  63
 #define DDL_ALTER_TABLE_DEL_TABLE  64
-#define DDL_ALTER_TABLE_SET_ACCESS  65
+#define DDL_ALTER_TABLE_SET_ACCESS 65
+
+#define DDL_EMPTY 100
 
 #define MAXOPS 21
 
diff --git a/sql/server/rel_schema.c b/sql/server/rel_schema.c
--- a/sql/server/rel_schema.c
+++ b/sql/server/rel_schema.c
@@ -1163,6 +1163,7 @@ rel_schema(sql_allocator *sa, int cat_ty
 
        append(exps, exp_atom_int(sa, nr));
        append(exps, exp_atom_clob(sa, sname));
+
        if (auth)
                append(exps, exp_atom_clob(sa, auth));
        rel->l = NULL;
@@ -1176,7 +1177,7 @@ rel_schema(sql_allocator *sa, int cat_ty
 }
 
 static sql_rel *
-rel_create_schema(mvc *sql, dlist *auth_name, dlist *schema_elements)
+rel_create_schema(mvc *sql, dlist *auth_name, dlist *schema_elements, int 
ignore_in_use)
 {
        char *name = dlist_get_schema_name(auth_name);
        char *auth = schema_auth(auth_name);
@@ -1194,8 +1195,12 @@ rel_create_schema(mvc *sql, dlist *auth_
                name = auth;
        assert(name);
        if (mvc_bind_schema(sql, name)) {
-               sql_error(sql, 02, "3F000!CREATE SCHEMA: name '%s' already in 
use", name);
-               return NULL;
+               if (!ignore_in_use) {
+                       sql_error(sql, 02, "3F000!CREATE SCHEMA: name '%s' 
already in use", name);
+                       return NULL;
+               } else {
+                       return NULL;
+               }
        } else {
                sql_schema *os = sql->session->schema;
                dnode *n;
@@ -1935,7 +1940,7 @@ rel_schemas(mvc *sql, symbol *s)
                dlist *l = s->data.lval;
 
                ret = rel_create_schema(sql, l->h->data.lval,
-                               l->h->next->next->next->data.lval);
+                               l->h->next->next->next->data.lval, 
l->h->next->next->next->next->data.i_val);
        }       break;
        case SQL_DROP_SCHEMA:
        {
@@ -1943,7 +1948,8 @@ rel_schemas(mvc *sql, symbol *s)
                dlist *auth_name = l->h->data.lval;
 
                assert(l->h->next->type == type_int);
-               ret = rel_schema(sql->sa, DDL_DROP_SCHEMA, 
+               ret = rel_schema(sql->sa, 
+                          l->h->next->next->data.i_val ? 
DDL_DROP_SCHEMA_IF_EXISTS : DDL_DROP_SCHEMA, 
                           dlist_get_schema_name(auth_name),
                           NULL,
                           l->h->next->data.i_val);     /* drop_action */
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
@@ -495,6 +495,8 @@ int yydebug=1;
        opt_with_check_option
        create
        create_or_replace
+       if_exists
+       if_not_exists
 
        opt_with_grant
        opt_with_admin
@@ -700,6 +702,16 @@ create_or_replace:
        create
 |      CREATE OR REPLACE { $$ = TRUE; }
 
+
+if_exists:
+       /* empty */   { $$ = FALSE; }
+|      IF EXISTS     { $$ = TRUE; }
+
+if_not_exists:
+       /* empty */   { $$ = FALSE; }
+|      IF NOT EXISTS { $$ = TRUE; }
+
+
 drop:
     DROP               
 
@@ -805,18 +817,20 @@ set_statement:
   ;
 
 schema:
-       create SCHEMA schema_name_clause opt_schema_default_char_set
+       create SCHEMA if_not_exists schema_name_clause 
opt_schema_default_char_set
                        opt_path_specification  opt_schema_element_list
                { dlist *l = L();
-               append_list(l, $3);
-               append_symbol(l, $4);
+               append_list(l, $4);
                append_symbol(l, $5);
-               append_list(l, $6);
+               append_symbol(l, $6);
+               append_list(l, $7);
+               append_int(l, $3);
                $$ = _symbol_create_list( SQL_CREATE_SCHEMA, l); }
-  |    drop SCHEMA qname drop_action
+  |    drop SCHEMA if_exists qname drop_action
                { dlist *l = L();
-               append_list(l, $3);
-               append_int(l, $4);
+               append_list(l, $4);
+               append_int(l, $5);
+               append_int(l, $3);
                $$ = _symbol_create_list( SQL_DROP_SCHEMA, l); }
  ;
 
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to