00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.Externalizable;
00042
import java.io.IOException;
00043
import java.io.ObjectInput;
00044
import java.io.ObjectOutput;
00045
00046
import java.sql.SQLException;
00047
00048
import com.quadcap.sql.types.TypeBigInt;
00049
import com.quadcap.sql.types.Value;
00050
import com.quadcap.sql.types.ValueLong;
00051
00052
import com.quadcap.util.Debug;
00053
00054
00055
00056
00057
00058
00059 public class AutoNumberConstraint
00060
extends Constraint implements
Externalizable
00061 {
00062 transient AutoNumberStep step;
00063
00064 long current = -1;
00065
00066
00067
00068
00069 public AutoNumberConstraint() {}
00070
00071
00072
00073
00074 public AutoNumberConstraint(String name) {
00075 super(name);
00076 }
00077
00078
00079
00080
00081
00082 public void add(
Session session)
throws SQLException {
00083
if (!table.
isUnderConstruction()) {
00084
try {
00085
Column col =
getColumn();
00086 col.
isAutoIncr =
true;
00087 session.getDatabase().updateRelation(table);
00088 }
catch (IOException e) {
00089
throw DbException.wrapThrowable(e);
00090 }
00091 }
00092 }
00093
00094
00095
00096
00097 public void delete(
Session session)
throws SQLException, IOException {
00098
Column col =
getColumn();
00099 col.
isAutoIncr =
false;
00100 session.getDatabase().updateRelation(table);
00101 }
00102
00103
00104
00105
00106 public void applyInsert(
Session session,
Row row,
long rowId,
00107
Constraint activeIndex)
00108
throws SQLException, IOException
00109 {
00110 }
00111
00112
00113
00114
00115 public void checkDelete(
Session session,
Row row,
long rowId)
00116
throws SQLException, IOException
00117 {
00118 }
00119
00120
00121
00122
00123 public void applyDelete(
Session session,
Row row,
long rowId,
00124
Constraint activeIndex)
00125
throws SQLException, IOException
00126 {
00127 }
00128
00129 static Object
stepLock =
new Object();
00130
00131
00132
00133
00134
00135
00136 public void checkInsert(
Session session,
Row row)
00137
throws SQLException, IOException
00138 {
00139
Column c =
getColumn();
00140
int col = c.
getColumn();
00141
boolean incr =
false;
00142
Value v = row.item(col);
00143
Database db = session.getDatabase();
00144
00145
if (
Value.isNull(v)) {
00146 incr =
true;
00147 }
else {
00148
Value t = v.
convert(
TypeBigInt.typeBigInt);
00149
if (!(t instanceof
ValueLong)) {
00150 incr =
true;
00151 }
else {
00152
ValueLong l = (
ValueLong)t;
00153
current = db.
getTableIdentity(table.
getName());
00154
if (l.
longValue() >=
current) {
00155
current = l.
longValue() + 1;
00156
synchronized (
stepLock) {
00157
if (
step == null) {
00158
step =
new AutoNumberStep(session, table,
current);
00159 }
else {
00160
step.
setCurrentId(
current);
00161 }
00162 session.doStep(
step);
00163 }
00164 }
00165 }
00166 }
00167
00168
if (incr) {
00169
if (
true ||
current < 0) {
00170
00171
00172
00173
00174
current = db.
getTableIdentity(table.
getName());
00175 }
00176 session.setLastInsertId(
current);
00177 row.set(col,
new ValueLong(
current++));
00178
00179
00180
synchronized (
stepLock) {
00181
if (
step == null) {
00182
step =
new AutoNumberStep(session, table, current);
00183 }
else {
00184
step.
setCurrentId(current);
00185 }
00186 session.doStep(
step);
00187 }
00188 }
00189 }
00190
00191
00192
00193
00194 public void checkUpdate(
Session session, byte[] oldKey,
Row row,
00195
Row oldRow,
long rowId,
Constraint activeIndex)
00196
throws SQLException, IOException
00197 {
00198 }
00199
00200
00201
00202
00203 public void applyUpdate(
Session session, byte[] oldKey,
Row row,
00204
Row oldRow,
long rowId,
Constraint activeIndex)
00205
throws SQLException, IOException
00206 {
00207 }
00208
00209
00210
00211
00212 public void readExternal(ObjectInput in)
00213
throws IOException, ClassNotFoundException
00214 {
00215 super.readExternal(in);
00216
current = in.readLong();
00217 }
00218
00219
00220
00221
00222 public void writeExternal(ObjectOutput out)
throws IOException {
00223 super.writeExternal(out);
00224 out.writeLong(
current);
00225 }
00226
00227
00228
00229
00230 public int getPriority() {
return 0; }
00231 }