KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > interpret > Schema


1 /**
2  * com.mckoi.database.interpret.Schema 14 Sep 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.interpret;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import com.mckoi.database.*;
30
31 /**
32  * Statement container that handles the CREATE SCHEMA and DROP SCHEMA
33  * statements.
34  *
35  * @author Tobias Downer
36  */

37
38 public class Schema extends Statement {
39
40   /**
41    * The type (either 'create' or 'drop').
42    */

43   String JavaDoc type;
44
45   /**
46    * The name of the schema.
47    */

48   String JavaDoc schema_name;
49
50   // ---------- Implemented from Statement ----------
51

52   public void prepare() throws DatabaseException {
53     type = (String JavaDoc) cmd.getObject("type");
54     schema_name = (String JavaDoc) cmd.getObject("schema_name");
55   }
56
57   public Table evaluate() throws DatabaseException {
58
59     DatabaseQueryContext context = new DatabaseQueryContext(database);
60
61     String JavaDoc com = type.toLowerCase();
62
63     if (!database.getDatabase().canUserCreateAndDropSchema(
64                                                 context, user, schema_name)) {
65       throw new UserAccessException(
66                          "User not permitted to create or drop schema.");
67     }
68
69     // Is this a create schema command?
70
if (com.equals("create")) {
71       boolean ignore_case = database.isInCaseInsensitiveMode();
72       SchemaDef schema =
73                   database.resolveSchemaCase(schema_name, ignore_case);
74       if (schema == null) {
75         // Create the schema
76
database.createSchema(schema_name, "USER");
77         // Set the default grants for the schema
78
database.getGrantManager().addGrant(Privileges.SCHEMA_ALL_PRIVS,
79                     GrantManager.SCHEMA, schema_name, user.getUserName(),
80                     true, Database.INTERNAL_SECURE_USERNAME);
81       }
82       else {
83         throw new DatabaseException("Schema '" + schema_name +
84                                     "' already exists.");
85       }
86     }
87     // Is this a drop schema command?
88
else if (com.equals("drop")) {
89       boolean ignore_case = database.isInCaseInsensitiveMode();
90       SchemaDef schema =
91                   database.resolveSchemaCase(schema_name, ignore_case);
92       // Only allow user to drop USER typed schemas
93
if (schema == null) {
94         throw new DatabaseException(
95                  "Schema '" + schema_name + "' does not exist.");
96       }
97       else if (schema.getType().equals("USER")) {
98         // Check if the schema is empty.
99
TableName[] all_tables = database.getTableList();
100         String JavaDoc resolved_schema_name = schema.getName();
101         for (int i = 0; i < all_tables.length; ++i) {
102           if (all_tables[i].getSchema().equals(resolved_schema_name)) {
103             throw new DatabaseException(
104                           "Schema '" + schema_name + "' is not empty.");
105           }
106         }
107         // Drop the schema
108
database.dropSchema(schema.getName());
109         // Revoke all the grants for the schema
110
database.getGrantManager().revokeAllGrantsOnObject(
111                                        GrantManager.SCHEMA, schema.getName());
112         
113       }
114       else {
115         throw new DatabaseException(
116                                  "Can not drop schema '" + schema_name + "'");
117       }
118     }
119     else {
120       throw new DatabaseException("Unrecognised schema command.");
121     }
122
123     return FunctionTable.resultTable(context, 0);
124
125   }
126
127
128 }
129
Popular Tags