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 | * TableView.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | * Created on 05.06.2004 |
33 | */ |
34 | package smallsql.database; |
35 | |
36 | import java.io.*; |
37 | import java.sql.*; |
38 | |
39 | |
40 | /** |
41 | * @author Volker Berlin |
42 | */ |
43 | abstract class TableView { |
44 | |
45 | static final int MAGIC_TABLE = 'S' << 24 | 'Q' << 16 | 'L' << 8 | 'T'; |
46 | static final int MAGIC_VIEW = 'S' << 24 | 'Q' << 16 | 'L' << 8 | 'V'; |
47 | static final int TABLE_VIEW_VERSION = 2; |
48 | static final int TABLE_VIEW_OLD_VERSION = 1; |
49 | |
50 | final String name; |
51 | final Columns columns; |
52 | |
53 | /** |
54 | * Mark the last change on the structur of the Table or View. |
55 | * If this value change then PreparedStatements need to recompile. |
56 | */ |
57 | private long timestamp = System.currentTimeMillis(); |
58 | |
59 | static final int LOCK_NONE = 0; // read on READ_COMMITED and READ_UNCOMMITED |
60 | static final int LOCK_INSERT = 1; // verhindert nur LOCK_TAB |
61 | static final int LOCK_READ = 2; // Tritt beim Lesen auf und verhindert ein Schreiben der Daten, es kann mehr als ein LOCK_READ pro page auftreten |
62 | static final int LOCK_WRITE = 3; // Tritt beim Schreiben auf und verhindert jeden weiteren Zugriff, es ist nur ein LOCK_WRITE pro page möglich |
63 | static final int LOCK_TAB = 4; // Sperre der gesamten Tabelle |
64 | |
65 | |
66 | TableView(String name, Columns columns){ |
67 | this.name = name; |
68 | this.columns = columns; |
69 | } |
70 | |
71 | /** |
72 | * Load a Table or View object. |
73 | */ |
74 | static TableView load(SSConnection con, Database database, String name) throws SQLException{ |
75 | RandomAccessFile raFile = null; |
76 | try{ |
77 | String fileName = Utils.createTableViewFileName( database, name ); |
78 | File file = new File( fileName ); |
79 | if(!file.exists()) |
80 | throw Utils.createSQLException("Table or View '" + name + "' does not exist"); |
81 | raFile = new RandomAccessFile( file, "rw" ); |
82 | int magic = raFile.readInt(); |
83 | int version = raFile.readInt(); |
84 | switch(magic){ |
85 | case MAGIC_TABLE: |
86 | case MAGIC_VIEW: |
87 | break; |
88 | default: |
89 | throw Utils.createSQLException("File '" + fileName + "' is not a valid Table or View store."); |
90 | } |
91 | if(version > TABLE_VIEW_VERSION) |
92 | throw Utils.createSQLException("File version (" + version + ") of file '" + fileName + "' is to new for this runtume."); |
93 | if(version < TABLE_VIEW_OLD_VERSION) |
94 | throw Utils.createSQLException("File version (" + version + ") of file '" + fileName + "' is to old for this runtume."); |
95 | if(magic == MAGIC_TABLE) |
96 | return new Table( con, name, raFile, raFile.getFilePointer(), version); |
97 | return new View ( con, name, raFile, raFile.getFilePointer()); |
98 | }catch(Throwable e){ |
99 | if(raFile != null) |
100 | try{ |
101 | raFile.close(); |
102 | }catch(Exception e2){ |
103 | DriverManager.println(e2.toString()); |
104 | } |
105 | throw Utils.createSQLException(e); |
106 | } |
107 | } |
108 | |
109 | |
110 | File getFile(Database database, String name) throws Exception{ |
111 | return new File( Utils.createTableViewFileName( database, name ) ); |
112 | } |
113 | |
114 | |
115 | RandomAccessFile createFile(Database database) throws Exception{ |
116 | File file = getFile( database, name ); |
117 | boolean ok = file.createNewFile(); |
118 | if(!ok) throw Utils.createSQLException("Table or View '" + name + "' allready exists."); |
119 | RandomAccessFile raFile = new RandomAccessFile( file, "rw" ); |
120 | writeMagic(raFile); |
121 | return raFile; |
122 | } |
123 | |
124 | abstract void writeMagic(RandomAccessFile raFile) throws Exception; |
125 | |
126 | |
127 | String getName(){ |
128 | return name; |
129 | } |
130 | |
131 | |
132 | long getTimestamp(){ |
133 | return timestamp; |
134 | } |
135 | |
136 | |
137 | /** |
138 | * Returns the index of a column name. The first column has the index 0. |
139 | */ |
140 | final int findColumnIdx(String name){ |
141 | // FIXME auf tree suche umstellen aus Performance Günden |
142 | for(int i=0; i<columns.size(); i++){ |
143 | if( columns.get(i).getName().equalsIgnoreCase(name) ) return i; |
144 | } |
145 | return -1; |
146 | } |
147 | |
148 | |
149 | /** |
150 | * Returns the Column of a column name. |
151 | */ |
152 | final Column findColumn(String name){ |
153 | for(int i=0; i<columns.size(); i++){ |
154 | Column column = columns.get(i); |
155 | if( column.getName().equalsIgnoreCase(name) ) return column; |
156 | } |
157 | return null; |
158 | } |
159 | |
160 | |
161 | |
162 | /** |
163 | * Close it and free all resources. |
164 | */ |
165 | void close() throws Exception{ |
166 | |
167 | } |
168 | |
169 | } |
170 | |
171 | |