1 24 25 package com.mckoi.database.interpret; 26 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import com.mckoi.database.*; 30 31 37 38 public class Schema extends Statement { 39 40 43 String type; 44 45 48 String schema_name; 49 50 52 public void prepare() throws DatabaseException { 53 type = (String ) cmd.getObject("type"); 54 schema_name = (String ) cmd.getObject("schema_name"); 55 } 56 57 public Table evaluate() throws DatabaseException { 58 59 DatabaseQueryContext context = new DatabaseQueryContext(database); 60 61 String 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 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 database.createSchema(schema_name, "USER"); 77 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 else if (com.equals("drop")) { 89 boolean ignore_case = database.isInCaseInsensitiveMode(); 90 SchemaDef schema = 91 database.resolveSchemaCase(schema_name, ignore_case); 92 if (schema == null) { 94 throw new DatabaseException( 95 "Schema '" + schema_name + "' does not exist."); 96 } 97 else if (schema.getType().equals("USER")) { 98 TableName[] all_tables = database.getTableList(); 100 String 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 database.dropSchema(schema.getName()); 109 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 |