Debian Codegraph @

src/main/org/h2/command/ddl/CreateSchema.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.command.ddl;
7
8 import java.util.ArrayList;
9 import org.h2.api.ErrorCode;
10 import org.h2.command.CommandInterface;
11 import org.h2.engine.Database;
12 import org.h2.engine.RightOwner;
13 import org.h2.engine.SessionLocal;
14 import org.h2.message.DbException;
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 private ArrayList<String> tableEngineParams;
27
28 public CreateSchema(SessionLocal session) {
29 super(session);
30 }
31
32 public void setIfNotExists(boolean ifNotExists) {
33 this.ifNotExists = ifNotExists;
34 }
35
36 @Override
37 public long update() {
38 session.getUser().checkSchemaAdmin();
39 Database db = getDatabase();
40 RightOwner owner = db.findUserOrRole(authorization);
41 if (owner == null) {
42 throw DbException.get(ErrorCode.USER_OR_ROLE_NOT_FOUND_1, authorization);
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, owner, false);
52 schema.setTableEngineParams(tableEngineParams);
53 db.addDatabaseObject(session, schema);
54 return 0;
55 }
56
57 public void setSchemaName(String name) {
58 this.schemaName = name;
59 }
60
61 public void setAuthorization(String userName) {
62 this.authorization = userName;
63 }
64
65 public void setTableEngineParams(ArrayList<String> tableEngineParams) {
66 this.tableEngineParams = tableEngineParams;
67 }
68
69 @Override
70 public int getType() {
71 return CommandInterface.CREATE_SCHEMA;
72 }
73
74 }