1 21 22 package org.apache.derby.impl.sql.execute; 23 import org.apache.derby.iapi.services.sanity.SanityManager; 24 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 25 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 26 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 27 import org.apache.derby.iapi.store.access.TransactionController; 28 import org.apache.derby.iapi.error.StandardException; 29 import org.apache.derby.catalog.UUID; 30 import org.apache.derby.iapi.reference.SQLState; 31 32 33 51 public class AutoincrementCounter 52 { 53 54 private Long start; 55 private long increment; 56 private String identity; 57 private long finalValue; 58 private String schemaName; 59 private String tableName; 60 private String columnName; 61 private long counter; 63 private int columnPosition; 64 private boolean initialized = false; 65 66 76 public AutoincrementCounter(Long start, long increment, long finalValue, 77 String s, String t, String c, int position) 78 { 79 this.increment = increment; 80 this.start = start; 81 this.initialized = false; 82 this.identity = makeIdentity(s,t,c); 83 this.finalValue = finalValue; 84 this.schemaName = s; 85 this.tableName = t; 86 this.columnName = c; 87 this.columnPosition = position; 88 } 90 91 94 public static String makeIdentity(String s, String t, String c) 95 { 96 return s + "." + t + "." + c; 97 } 98 99 102 public static String makeIdentity(TableDescriptor td, ColumnDescriptor cd) 103 { 104 return td.getSchemaName() + "." + td.getName() + 105 "." + cd.getColumnName(); 106 } 107 108 113 public void reset(boolean begin) 114 { 115 if (begin == true) 116 initialized = false; 117 else 118 { 119 counter = finalValue; 120 initialized = true; 121 } 122 124 } 125 126 131 public long update(long t) 132 { 133 counter = t; 134 initialized = true; 136 return counter; 137 } 138 139 145 public long update() throws StandardException 146 { 147 long counterVal; 148 149 if (initialized == false) 150 { 151 initialized = true; 154 155 if (start == null) 156 { 157 throw StandardException.newException( 158 SQLState.LANG_AI_COUNTER_ERROR); 159 } 160 counter = start.longValue(); 161 } 162 else 163 { 164 counter = counter + increment; 165 } 166 return counter; 168 } 169 170 174 public Long getCurrentValue() 175 { 176 if (initialized == false) 177 return null; 178 return new Long (counter); 179 } 180 181 184 public String getIdentity() 185 { 186 return identity; 187 } 188 189 199 public void flushToDisk(TransactionController tc, DataDictionary dd, 200 UUID tableUUID) 201 throws StandardException 202 { 203 dd.setAutoincrementValue(tc, tableUUID, columnName, counter, true); 204 } 205 206 211 public int getColumnPosition() 212 { 213 return columnPosition; 214 } 215 216 220 public Long getStartValue() 221 { 222 return start; 223 } 224 225 public String toString() 226 { 227 return "counter: " + identity + " current: " + counter 228 + " start: " + start + 229 " increment: " + increment + " final: " + finalValue; 230 } 231 } 232 233 234 | Popular Tags |