*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* TableView.java
* ---------------
* Author: Volker Berlin
*
* Created on 05.06.2004
*/
package smallsql.database;
import java.io.*;
import java.sql.*;
abstract class TableView {
static final int MAGIC_TABLE = 'S' << 24 | 'Q' << 16 | 'L' << 8 | 'T';
static final int MAGIC_VIEW = 'S' << 24 | 'Q' << 16 | 'L' << 8 | 'V';
static final int TABLE_VIEW_VERSION = 2;
static final int TABLE_VIEW_OLD_VERSION = 1;
final String name;
final Columns columns;
private long timestamp = System.currentTimeMillis();
static final int LOCK_NONE = 0; static final int LOCK_INSERT = 1; static final int LOCK_READ = 2; static final int LOCK_WRITE = 3; static final int LOCK_TAB = 4;
TableView(String name, Columns columns){
this.name = name;
this.columns = columns;
}
static TableView load(SSConnection con, Database database, String name) throws SQLException{
RandomAccessFile raFile = null;
try{
String fileName = Utils.createTableViewFileName( database, name );
File file = new File( fileName );
if(!file.exists())
throw Utils.createSQLException("Table or View '" + name + "' does not exist");
raFile = new RandomAccessFile( file, "rw" );
int magic = raFile.readInt();
int version = raFile.readInt();
switch(magic){
case MAGIC_TABLE:
case MAGIC_VIEW:
break;
default:
throw Utils.createSQLException("File '" + fileName + "' is not a valid Table or View store.");
}
if(version > TABLE_VIEW_VERSION)
throw Utils.createSQLException("File version (" + version + ") of file '" + fileName + "' is to new for this runtume.");
if(version < TABLE_VIEW_OLD_VERSION)
throw Utils.createSQLException("File version (" + version + ") of file '" + fileName + "' is to old for this runtume.");
if(magic == MAGIC_TABLE)
return new Table( con, name, raFile, raFile.getFilePointer(), version);
return new View ( con, name, raFile, raFile.getFilePointer());
}catch(Throwable e){
if(raFile != null)
try{
raFile.close();
}catch(Exception e2){
DriverManager.println(e2.toString());
}
throw Utils.createSQLException(e);
}
}
File getFile(Database database, String name) throws Exception{
return new File( Utils.createTableViewFileName( database, name ) );
}
RandomAccessFile createFile(Database database) throws Exception{
File file = getFile( database, name );
boolean ok = file.createNewFile();
if(!ok) throw Utils.createSQLException("Table or View '" + name + "' allready exists.");
RandomAccessFile raFile = new RandomAccessFile( file, "rw" );
writeMagic(raFile);
return raFile;
}
abstract void writeMagic(RandomAccessFile raFile) throws Exception;
String getName(){
return name;
}
long getTimestamp(){
return timestamp;
}
final int findColumnIdx(String name){
for(int i=0; i<columns.size(); i++){
if( columns.get(i).getName().equalsIgnoreCase(name) ) return i;
}
return -1;
}
final Column findColumn(String name){
for(int i=0; i<columns.size(); i++){
Column column = columns.get(i);
if( column.getName().equalsIgnoreCase(name) ) return column;
}
return null;
}
void close() throws Exception{
}
}