1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.command.ddl; |
7 | |
8 | import org.h2.api.ErrorCode; |
9 | import org.h2.command.CommandInterface; |
10 | import org.h2.engine.Database; |
11 | import org.h2.engine.Session; |
12 | import org.h2.engine.User; |
13 | import org.h2.message.DbException; |
14 | import org.h2.schema.Schema; |
15 | |
16 | /** |
17 | * This class represents the statement |
18 | * CREATE SCHEMA |
19 | */ |
20 | public class CreateSchema extends DefineCommand { |
21 | |
22 | private String schemaName; |
23 | private String authorization; |
24 | private boolean ifNotExists; |
25 | |
26 | public CreateSchema(Session session) { |
27 | super(session); |
28 | } |
29 | |
30 | public void setIfNotExists(boolean ifNotExists) { |
31 | this.ifNotExists = ifNotExists; |
32 | } |
33 | |
34 | @Override |
35 | public int update() { |
36 | session.getUser().checkSchemaAdmin(); |
37 | session.commit(true); |
38 | Database db = session.getDatabase(); |
39 | User user = db.getUser(authorization); |
40 | // during DB startup, the Right/Role records have not yet been loaded |
41 | if (!db.isStarting()) { |
42 | user.checkSchemaAdmin(); |
43 | } |
44 | if (db.findSchema(schemaName) != null) { |
45 | if (ifNotExists) { |
46 | return 0; |
47 | } |
48 | throw DbException.get(ErrorCode.SCHEMA_ALREADY_EXISTS_1, schemaName); |
49 | } |
50 | int id = getObjectId(); |
51 | Schema schema = new Schema(db, id, schemaName, user, false); |
52 | db.addDatabaseObject(session, schema); |
53 | return 0; |
54 | } |
55 | |
56 | public void setSchemaName(String name) { |
57 | this.schemaName = name; |
58 | } |
59 | |
60 | public void setAuthorization(String userName) { |
61 | this.authorization = userName; |
62 | } |
63 | |
64 | @Override |
65 | public int getType() { |
66 | return CommandInterface.CREATE_SCHEMA; |
67 | } |
68 | |
69 | } |