1 21 package oracle.toplink.essentials.tools.schemaframework; 23 24 import java.io.*; 25 import oracle.toplink.essentials.exceptions.*; 26 import oracle.toplink.essentials.sequencing.NativeSequence; 27 import oracle.toplink.essentials.internal.sessions.AbstractSession; 28 29 35 public class OracleSequenceDefinition extends SequenceDefinition { 36 37 38 protected int increment; 39 40 41 protected int start = 1; 42 43 public OracleSequenceDefinition(String name, int preallocationSize) { 44 super(name); 45 setIncrement(preallocationSize); 46 } 47 48 public OracleSequenceDefinition(String name, int preallocationSize, int start) { 49 super(name); 50 if(start <= 0) { 52 start = 1; 53 } 54 setIncrement(preallocationSize); 55 setStart(start); 56 } 57 58 public OracleSequenceDefinition(String name) { 59 this(name, 1); 60 } 61 62 public OracleSequenceDefinition(NativeSequence sequence) { 63 this(sequence.getName(), sequence.getPreallocationSize(), sequence.getInitialValue()); 64 } 65 66 70 public Writer buildCreationWriter(AbstractSession session, Writer writer) { 71 try { 72 writer.write("CREATE SEQUENCE "); 73 writer.write(getFullName()); 74 if (getIncrement() != 1) { 75 writer.write(" INCREMENT BY " + getIncrement()); 76 } 77 int startWith = getStart() + getIncrement() - 1; 81 writer.write(" START WITH " + startWith); 82 } catch (IOException ioException) { 83 throw ValidationException.fileError(ioException); 84 } 85 return writer; 86 } 87 88 92 public Writer buildDeletionWriter(AbstractSession session, Writer writer) { 93 try { 94 writer.write("DROP SEQUENCE "); 95 writer.write(getFullName()); 96 } catch (IOException ioException) { 97 throw ValidationException.fileError(ioException); 98 } 99 return writer; 100 } 101 102 106 public Writer buildAlterIncrementWriter(AbstractSession session, Writer writer) { 107 try { 108 writer.write("ALTER SEQUENCE "); 109 writer.write(getFullName()); 110 writer.write(" INCREMENT BY " + getIncrement()); 111 } catch (IOException ioException) { 112 throw ValidationException.fileError(ioException); 113 } 114 return writer; 115 } 116 117 121 public boolean checkIfExist(AbstractSession session) throws DatabaseException { 122 try { 123 session.executeSelectingCall(new oracle.toplink.essentials.queryframework.SQLCall("SELECT " + getName() + ".NEXTVAL FROM DUAL")); 124 } catch (DatabaseException exception) { 125 return false; 126 } 127 return true; 128 } 129 130 133 public int getIncrement() { 134 return increment; 135 } 136 137 140 public void setIncrement(int increment) { 141 this.increment = increment; 142 } 143 144 147 public int getStart() { 148 return start; 149 } 150 151 154 public void setStart(int start) { 155 this.start = start; 156 } 157 158 161 public void setStartAndIncrement(int value) { 162 setStart(1); 163 setIncrement(value); 164 } 165 166 170 public boolean isAlterSupported() { 171 return true; 172 } 173 174 179 public void alterOnDatabase(AbstractSession session) throws TopLinkException { 180 try { 181 session.executeNonSelectingCall(new oracle.toplink.essentials.queryframework.SQLCall(buildAlterIncrementWriter(session, new StringWriter()).toString())); 182 } catch (DatabaseException exception) { 183 createOnDatabase(session); 184 } 185 } 186 187 192 public void alterIncrement(AbstractSession session, Writer schemaWriter) throws ValidationException { 193 if (schemaWriter == null) { 194 this.alterOnDatabase(session); 195 } else { 196 this.buildAlterIncrementWriter(session, schemaWriter); 197 } 198 } 199 } 200 | Popular Tags |