01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.ddl;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.constant.ErrorCode;
11: import org.h2.engine.Database;
12: import org.h2.engine.Session;
13: import org.h2.engine.User;
14: import org.h2.message.Message;
15: import org.h2.schema.Schema;
16:
17: /**
18: * This class represents the statement
19: * CREATE SCHEMA
20: */
21: public class CreateSchema extends DefineCommand {
22:
23: private String schemaName;
24: private String authorization;
25: private boolean ifNotExists;
26:
27: public CreateSchema(Session session) {
28: super (session);
29: }
30:
31: public void setIfNotExists(boolean ifNotExists) {
32: this .ifNotExists = ifNotExists;
33: }
34:
35: public int update() throws SQLException {
36: session.getUser().checkAdmin();
37: session.commit(true);
38: Database db = session.getDatabase();
39: User user = db.getUser(authorization);
40: user.checkAdmin();
41: if (db.findSchema(schemaName) != null) {
42: if (ifNotExists) {
43: return 0;
44: }
45: throw Message.getSQLException(
46: ErrorCode.SCHEMA_ALREADY_EXISTS_1, schemaName);
47: }
48: int id = getObjectId(true, true);
49: Schema schema = new Schema(db, id, schemaName, user, false);
50: db.addDatabaseObject(session, schema);
51: return 0;
52: }
53:
54: public void setSchemaName(String name) {
55: this .schemaName = name;
56: }
57:
58: public void setAuthorization(String userName) {
59: this.authorization = userName;
60: }
61:
62: }
|