Debian Codegraph @

src/main/org/h2/command/ddl/CreateSequence.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 org.h2.api.ErrorCode;
9 import org.h2.command.CommandInterface;
10 import org.h2.engine.Database;
11 import org.h2.engine.SessionLocal;
12 import org.h2.message.DbException;
13 import org.h2.schema.Schema;
14 import org.h2.schema.Sequence;
15
16 /**
17 * This class represents the statement CREATE SEQUENCE.
18 */
19 public class CreateSequence extends SchemaOwnerCommand {
20
21 private String sequenceName;
22
23 private boolean ifNotExists;
24
25 private SequenceOptions options;
26
27 private boolean belongsToTable;
28
29 public CreateSequence(SessionLocal session, Schema schema) {
30 super(session, schema);
31 transactional = true;
32 }
33
34 public void setSequenceName(String sequenceName) {
35 this.sequenceName = sequenceName;
36 }
37
38 public void setIfNotExists(boolean ifNotExists) {
39 this.ifNotExists = ifNotExists;
40 }
41
42 public void setOptions(SequenceOptions options) {
43 this.options = options;
44 }
45
46 @Override
47 long update(Schema schema) {
48 Database db = getDatabase();
49 if (schema.findSequence(sequenceName) != null) {
50 if (ifNotExists) {
51 return 0;
52 }
53 throw DbException.get(ErrorCode.SEQUENCE_ALREADY_EXISTS_1, sequenceName);
54 }
55 int id = getObjectId();
56 Sequence sequence = new Sequence(session, schema, id, sequenceName, options, belongsToTable);
57 db.addSchemaObject(session, sequence);
58 return 0;
59 }
60
61 public void setBelongsToTable(boolean belongsToTable) {
62 this.belongsToTable = belongsToTable;
63 }
64
65 @Override
66 public int getType() {
67 return CommandInterface.CREATE_SEQUENCE;
68 }
69
70 }