1 | /* ============================================================= |
2 | * SmallSQL : a free Java DBMS library for the Java(tm) platform |
3 | * ============================================================= |
4 | * |
5 | * (C) Copyright 2004-2006, by Volker Berlin. |
6 | * |
7 | * Project Info: http://www.smallsql.de/ |
8 | * |
9 | * This library is free software; you can redistribute it and/or modify it |
10 | * under the terms of the GNU Lesser General Public License as published by |
11 | * the Free Software Foundation; either version 2.1 of the License, or |
12 | * (at your option) any later version. |
13 | * |
14 | * This library is distributed in the hope that it will be useful, but |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 | * License for more details. |
18 | * |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
22 | * USA. |
23 | * |
24 | * [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
25 | * in the United States and other countries.] |
26 | * |
27 | * --------------- |
28 | * Identity.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | * Created on 16.04.2004 |
33 | */ |
34 | package smallsql.database; |
35 | |
36 | import java.io.*; |
37 | import java.sql.SQLException; |
38 | |
39 | |
40 | /** |
41 | * @author Volker Berlin |
42 | */ |
43 | public class Identity extends Number { |
44 | |
45 | final private long filePos; |
46 | final private RandomAccessFile raFile; |
47 | final private byte[] page = new byte[8]; |
48 | final private Table table; |
49 | private long value; |
50 | |
51 | public Identity(Table table, RandomAccessFile raFile, long filePos) throws IOException{ |
52 | synchronized(raFile){ |
53 | raFile.seek(filePos); |
54 | raFile.read(page); |
55 | } |
56 | value = ((long)(page[ 0 ]) << 56) | |
57 | ((long)(page[ 1 ] & 0xFF) << 48) | |
58 | ((long)(page[ 2 ] & 0xFF) << 40) | |
59 | ((long)(page[ 3 ] & 0xFF) << 32) | |
60 | ((long)(page[ 4 ] & 0xFF) << 24) | |
61 | ((page[ 5 ] & 0xFF) << 16) | |
62 | ((page[ 6 ] & 0xFF) << 8) | |
63 | ((page[ 7 ] & 0xFF)); |
64 | this.table = table; |
65 | this.raFile = raFile; |
66 | this.filePos = filePos; |
67 | } |
68 | |
69 | private StorePage createStorePage(){ |
70 | page[ 0 ] = (byte)(value >> 56); |
71 | page[ 1 ] = (byte)(value >> 48); |
72 | page[ 2 ] = (byte)(value >> 40); |
73 | page[ 3 ] = (byte)(value >> 32); |
74 | page[ 4 ] = (byte)(value >> 24); |
75 | page[ 5 ] = (byte)(value >> 16); |
76 | page[ 6 ] = (byte)(value >> 8); |
77 | page[ 7 ] = (byte)(value); |
78 | return new StorePage( page, 8, raFile, filePos); |
79 | } |
80 | |
81 | void createNextValue(SSConnection con) throws SQLException{ |
82 | value++; |
83 | con.add( createStorePage() ); |
84 | } |
85 | |
86 | |
87 | void setNextValue(Expression expr) throws Exception{ |
88 | if(expr.isNull()) return; |
89 | long newValue = expr.getLong(); |
90 | if(newValue > value){ |
91 | value = newValue; |
92 | createStorePage().commit(); |
93 | } |
94 | } |
95 | |
96 | public float floatValue() { |
97 | return value; |
98 | } |
99 | |
100 | public double doubleValue() { |
101 | return value; |
102 | } |
103 | |
104 | public int intValue() { |
105 | return (int)value; |
106 | } |
107 | |
108 | public long longValue() { |
109 | return value; |
110 | } |
111 | |
112 | } |