1 5 package org.h2.command.ddl; 6 7 import java.sql.SQLException ; 8 9 import org.h2.engine.Database; 10 import org.h2.engine.Session; 11 import org.h2.message.Message; 12 import org.h2.schema.Schema; 13 import org.h2.schema.Sequence; 14 15 public class CreateSequence extends SchemaCommand { 16 17 private String sequenceName; 18 private boolean ifNotExists; 19 private long start = 1; 20 private long increment = 1; 21 private boolean belongsToTable; 22 23 public CreateSequence(Session session, Schema schema) { 24 super(session, schema); 25 } 26 27 public void setSequenceName(String sequenceName) { 28 this.sequenceName = sequenceName; 29 } 30 31 public void setIfNotExists(boolean ifNotExists) { 32 this.ifNotExists = ifNotExists; 33 } 34 35 public int update() throws SQLException { 36 session.commit(); 37 Database db = session.getDatabase(); 38 if(getSchema().findSequence(sequenceName)!=null) { 39 if (ifNotExists) { 40 return 0; 41 } 42 throw Message.getSQLException(Message.SEQUENCE_ALREADY_EXISTS_1, sequenceName); 43 } 44 int id = getObjectId(false, true); 45 Sequence sequence = new Sequence(getSchema(), id, sequenceName, belongsToTable); 46 sequence.setStartValue(start); 47 sequence.setIncrement(increment); 48 db.addSchemaObject(session, sequence); 49 return 0; 50 } 51 52 public void setStartWith(long start) { 53 this.start = start; 54 } 55 56 public void setIncrement(long increment) { 57 this.increment = increment; 58 } 59 60 public boolean getBelongsToTable() { 61 return belongsToTable; 62 } 63 64 public void setBelongsToTable(boolean belongsToTable) { 65 this.belongsToTable = belongsToTable; 66 } 67 68 } 69 | Popular Tags |