| 1 package com.quadcap.sql; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.sql.SQLException ; 47 48 import com.quadcap.sql.types.TypeBigInt; 49 import com.quadcap.sql.types.Value; 50 import com.quadcap.sql.types.ValueLong; 51 52 import com.quadcap.util.Debug; 53 54 59 public class AutoNumberConstraint 60 extends Constraint implements Externalizable  61 { 62 transient AutoNumberStep step; 63 64 long current = -1; 65 66 69 public AutoNumberConstraint() {} 70 71 74 public AutoNumberConstraint(String name) { 75 super(name); 76 } 77 78 82 public void add(Session session) throws SQLException { 83 if (!table.isUnderConstruction()) { 84 try { 85 Column col = getColumn(); 86 col.isAutoIncr = true; 87 session.getDatabase().updateRelation(table); 88 } catch (IOException e) { 89 throw DbException.wrapThrowable(e); 90 } 91 } 92 } 93 94 97 public void delete(Session session) throws SQLException , IOException { 98 Column col = getColumn(); 99 col.isAutoIncr = false; 100 session.getDatabase().updateRelation(table); 101 } 102 103 106 public void applyInsert(Session session, Row row, long rowId, 107 Constraint activeIndex) 108 throws SQLException , IOException  109 { 110 } 111 112 115 public void checkDelete(Session session, Row row, long rowId) 116 throws SQLException , IOException  117 { 118 } 119 120 123 public void applyDelete(Session session, Row row, long rowId, 124 Constraint activeIndex) 125 throws SQLException , IOException  126 { 127 } 128 129 static Object stepLock = new Object (); 130 131 136 public void checkInsert(Session session, Row row) 137 throws SQLException , IOException  138 { 139 Column c = getColumn(); 140 int col = c.getColumn(); 141 boolean incr = false; 142 Value v = row.item(col); 143 Database db = session.getDatabase(); 144 145 if (Value.isNull(v)) { 146 incr = true; 147 } else { 148 Value t = v.convert(TypeBigInt.typeBigInt); 149 if (!(t instanceof ValueLong)) { 150 incr = true; 151 } else { 152 ValueLong l = (ValueLong)t; 153 current = db.getTableIdentity(table.getName()); 154 if (l.longValue() >= current) { 155 current = l.longValue() + 1; 156 synchronized (stepLock) { 157 if (step == null) { 158 step = new AutoNumberStep(session, table, current); 159 } else { 160 step.setCurrentId(current); 161 } 162 session.doStep(step); } 164 } 165 } 166 } 167 168 if (incr) { 169 if (true || current < 0) { 170 current = db.getTableIdentity(table.getName()); 175 } 176 session.setLastInsertId(current); 177 row.set(col, new ValueLong(current++)); 178 synchronized (stepLock) { 181 if (step == null) { 182 step = new AutoNumberStep(session, table, current); 183 } else { 184 step.setCurrentId(current); 185 } 186 session.doStep(step); } 188 } 189 } 190 191 194 public void checkUpdate(Session session, byte[] oldKey, Row row, 195 Row oldRow, long rowId, Constraint activeIndex) 196 throws SQLException , IOException  197 { 198 } 199 200 203 public void applyUpdate(Session session, byte[] oldKey, Row row, 204 Row oldRow, long rowId, Constraint activeIndex) 205 throws SQLException , IOException  206 { 207 } 208 209 212 public void readExternal(ObjectInput in) 213 throws IOException , ClassNotFoundException  214 { 215 super.readExternal(in); 216 current = in.readLong(); 217 } 218 219 222 public void writeExternal(ObjectOutput out) throws IOException { 223 super.writeExternal(out); 224 out.writeLong(current); 225 } 226 227 230 public int getPriority() { return 0; } 231 } 232 | Popular Tags |