File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\CreateSequence.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.ddl;
8
9 import org.h2.command.CommandInterface;
10 import org.h2.constant.ErrorCode;
11 import org.h2.engine.Database;
12 import org.h2.engine.Session;
13 import org.h2.expression.Expression;
14 import org.h2.message.DbException;
15 import org.h2.schema.Schema;
16 import org.h2.schema.Sequence;
17
18 /**
19 * This class represents the statement
20 * CREATE SEQUENCE
21 */
22 public class CreateSequence extends SchemaCommand {
23
24 private String sequenceName;
25 private boolean ifNotExists;
26 private Expression start;
27 private Expression increment;
28 private Expression cacheSize;
29 private boolean belongsToTable;
30
31 public CreateSequence(Session session, Schema schema) {
32 super(session, schema);
33 }
34
35 public void setSequenceName(String sequenceName) {
36 this.sequenceName = sequenceName;
37 }
38
39 public void setIfNotExists(boolean ifNotExists) {
40 this.ifNotExists = ifNotExists;
41 }
42
43 public int update() {
44 session.commit(true);
45 Database db = session.getDatabase();
46 if (getSchema().findSequence(sequenceName) != null) {
47 if (ifNotExists) {
48 return 0;
49 }
50 throw DbException.get(ErrorCode.SEQUENCE_ALREADY_EXISTS_1, sequenceName);
51 }
52 int id = getObjectId();
53 Sequence sequence = new Sequence(getSchema(), id, sequenceName, belongsToTable);
54 sequence.setStartValue(getLong(start, 1));
55 sequence.setIncrement(getLong(increment, 1));
56 sequence.setCacheSize(getLong(cacheSize, Sequence.DEFAULT_CACHE_SIZE));
57 db.addSchemaObject(session, sequence);
58 return 0;
59 }
60
61 private long getLong(Expression expr, long defaultValue) {
62 if (expr == null) {
63 return defaultValue;
64 }
65 return expr.optimize(session).getValue(session).getLong();
66 }
67
68 public void setStartWith(Expression start) {
69 this.start = start;
70 }
71
72 public void setIncrement(Expression increment) {
73 this.increment = increment;
74 }
75
76 public void setBelongsToTable(boolean belongsToTable) {
77 this.belongsToTable = belongsToTable;
78 }
79
80 public void setCacheSize(Expression cacheSize) {
81 this.cacheSize = cacheSize;
82 }
83
84 public int getType() {
85 return CommandInterface.CREATE_SEQUENCE;
86 }
87
88 }
89