JDocCoverage Report - 21.04.2006 22:02:51

Namemethod, %comment, %TODO@see
smallsql.database.TableView1017,9%   (661/3027)00

/* =============================================================
 * SmallSQL : a free Java DBMS library for the Java(tm) platform
 * =============================================================
 *
 * (C) Copyright 2004-2006, by Volker Berlin.
 *
 * Project Info:  http://www.smallsql.de/
 *
 * 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.*;


/**
 * @author Volker Berlin
 */
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;

	/** 
	 * Mark the last change on the structur of the Table or View.
	 * If this value change then PreparedStatements need to recompile.
	 */
	private long timestamp = System.currentTimeMillis();
	
	static final int LOCK_NONE   = 0; // read on READ_COMMITED and READ_UNCOMMITED
	static final int LOCK_INSERT = 1; // verhindert nur LOCK_TAB
	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
	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
	static final int LOCK_TAB    = 4; // Sperre der gesamten Tabelle


	TableView(String name, Columns columns){
		this.name = name;
		this.columns = columns;
	}
	
	/**
	 * Load a Table or View object. 
	 */
	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;
	}
	

	/**
	 * Returns the index of a column name. The first column has the index 0.
	 */
	final int findColumnIdx(String name){
		// FIXME auf tree suche umstellen aus Performance Günden
		for(int i=0; i<columns.size(); i++){
			if( columns.get(i).getName().equalsIgnoreCase(name) ) return i;
		}
		return -1;
	}


	/**
	 * Returns the Column of a column name.
	 */
	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;
	}


	
	/**
	 * Close it and free all resources.
	 */
	void close() throws Exception{
		
	}

}