001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.BufferedInputStream;
042: import java.io.BufferedOutputStream;
043: import java.io.Externalizable;
044: import java.io.IOException;
045: import java.io.ObjectInput;
046: import java.io.ObjectOutput;
047:
048: import java.sql.SQLException;
049:
050: import com.quadcap.sql.file.BlockFile;
051: import com.quadcap.sql.file.PageManager;
052: import com.quadcap.sql.file.RandomAccess;
053: import com.quadcap.sql.file.RandomAccessInputStream;
054: import com.quadcap.sql.file.RandomAccessOutputStream;
055:
056: import com.quadcap.sql.io.ObjectInputStream;
057: import com.quadcap.sql.io.ObjectOutputStream;
058: import com.quadcap.sql.io.Extern;
059:
060: import com.quadcap.sql.types.Value;
061: import com.quadcap.sql.types.ValueNull;
062:
063: import com.quadcap.io.CountedInputStream;
064:
065: import com.quadcap.util.Debug;
066:
067: /**
068: * Log step to record allocations of auto numbers...
069: *
070: * @author Stan Bailes
071: */
072: public class AutoNumberStep extends LogStep implements Externalizable {
073: String tableName = null;
074: long num = 0;
075:
076: /**
077: * Default constructor for serialization
078: */
079: public AutoNumberStep() {
080: }
081:
082: /**
083: * Explicit constructor
084: */
085: public AutoNumberStep(Session session, Table table, long num) {
086: super (session);
087: this .tableName = table.getName();
088: this .num = num;
089: }
090:
091: /**
092: * We don't bother to undo this -- we don't mind gaps.
093: */
094: public void undo(Session session) throws IOException, SQLException {
095: }
096:
097: /**
098: * "Redo" keeps the constraint number in sync. We don't care about
099: * the rows for which this was generated, we just need the max number
100: * that we ever gave out....
101: */
102: public void redo(Session session) throws IOException, SQLException {
103: Database db = session.getDatabase();
104: db.updateTableIdentity(tableName, num);
105: }
106:
107: public void prepare(Session session) throws IOException,
108: SQLException {
109: }
110:
111: public void readExternal(ObjectInput in) throws IOException,
112: ClassNotFoundException {
113: super .readExternal(in);
114: this .tableName = (String) in.readObject();
115: this .num = in.readLong();
116: }
117:
118: public void writeExternal(ObjectOutput out) throws IOException {
119: super .writeExternal(out);
120: out.writeObject(tableName);
121: out.writeLong(num);
122: }
123:
124: //#ifdef DEBUG
125: public String toString() {
126: StringBuffer sb = new StringBuffer(super .toString());
127: sb.append(" AutoNumberStep(");
128: sb.append(tableName);
129: sb.append(',');
130: sb.append(num);
131: return sb.toString();
132: }
133:
134: //#endif
135:
136: /**
137: * Exposed so a careful client can avoid an allocation
138: */
139: public void setCurrentId(long x) {
140: this .num = x;
141: }
142:
143: static Extern extern;
144:
145: public void setExtern(Extern extern) {
146: AutoNumberStep.extern = extern;
147: }
148:
149: public Extern getExtern() {
150: return extern;
151: }
152: }
|