001: /**
002: * com.mckoi.database.interpret.Schema 14 Sep 2001
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database.interpret;
024:
025: import java.util.ArrayList;
026: import java.util.List;
027: import com.mckoi.database.*;
028:
029: /**
030: * Statement container that handles the CREATE SCHEMA and DROP SCHEMA
031: * statements.
032: *
033: * @author Tobias Downer
034: */
035:
036: public class Schema extends Statement {
037:
038: /**
039: * The type (either 'create' or 'drop').
040: */
041: String type;
042:
043: /**
044: * The name of the schema.
045: */
046: String schema_name;
047:
048: // ---------- Implemented from Statement ----------
049:
050: public void prepare() throws DatabaseException {
051: type = (String) cmd.getObject("type");
052: schema_name = (String) cmd.getObject("schema_name");
053: }
054:
055: public Table evaluate() throws DatabaseException {
056:
057: DatabaseQueryContext context = new DatabaseQueryContext(
058: database);
059:
060: String com = type.toLowerCase();
061:
062: if (!database.getDatabase().canUserCreateAndDropSchema(context,
063: user, schema_name)) {
064: throw new UserAccessException(
065: "User not permitted to create or drop schema.");
066: }
067:
068: // Is this a create schema command?
069: if (com.equals("create")) {
070: boolean ignore_case = database.isInCaseInsensitiveMode();
071: SchemaDef schema = database.resolveSchemaCase(schema_name,
072: ignore_case);
073: if (schema == null) {
074: // Create the schema
075: database.createSchema(schema_name, "USER");
076: // Set the default grants for the schema
077: database.getGrantManager().addGrant(
078: Privileges.SCHEMA_ALL_PRIVS,
079: GrantManager.SCHEMA, schema_name,
080: user.getUserName(), true,
081: Database.INTERNAL_SECURE_USERNAME);
082: } else {
083: throw new DatabaseException("Schema '" + schema_name
084: + "' already exists.");
085: }
086: }
087: // Is this a drop schema command?
088: else if (com.equals("drop")) {
089: boolean ignore_case = database.isInCaseInsensitiveMode();
090: SchemaDef schema = database.resolveSchemaCase(schema_name,
091: ignore_case);
092: // Only allow user to drop USER typed schemas
093: if (schema == null) {
094: throw new DatabaseException("Schema '" + schema_name
095: + "' does not exist.");
096: } else if (schema.getType().equals("USER")) {
097: // Check if the schema is empty.
098: TableName[] all_tables = database.getTableList();
099: String resolved_schema_name = schema.getName();
100: for (int i = 0; i < all_tables.length; ++i) {
101: if (all_tables[i].getSchema().equals(
102: resolved_schema_name)) {
103: throw new DatabaseException("Schema '"
104: + 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: } else {
114: throw new DatabaseException("Can not drop schema '"
115: + schema_name + "'");
116: }
117: } else {
118: throw new DatabaseException("Unrecognised schema command.");
119: }
120:
121: return FunctionTable.resultTable(context, 0);
122:
123: }
124:
125: }
|