*
* 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.]
*
* ---------------
* View.java
* ---------------
* Author: Volker Berlin
*
* Created on 31.05.2004
*/
package smallsql.database;
import java.io.*;
class View extends TableView{
final String sql;
final CommandSelect commandSelect;
View(SSConnection con, String name, RandomAccessFile raFile, long offset) throws Exception{
super( name, new Columns() );
StorePage storePage = new StorePage( null, -1, raFile, offset);
StoreImpl store = StoreImpl.createStore( null, storePage, SQLTokenizer.SELECT, offset);
sql = store.readString();
int type;
while((type = store.readInt()) != 0){
int offsetInPage = store.getCurrentOffsetInPage();
int size = store.readInt();
switch(type){
}
store.setCurrentOffsetInPage(offsetInPage + size);
}
raFile.close();
commandSelect = (CommandSelect)new SQLParser().parse(con, sql);
createColumns(con);
}
View(Database database, String name, String sql) throws Exception{
super( name, new Columns() );
this.sql = sql;
this.commandSelect = null;
write(database);
}
View(SSConnection con, CommandSelect commandSelect) throws Exception{
super("UNION", new Columns());
this.sql = null;
this.commandSelect = commandSelect;
createColumns(con);
}
private void createColumns(SSConnection con) throws Exception{
commandSelect.compile(con);
Expressions exprs = commandSelect.columnExpressions;
for(int c=0; c<exprs.size(); c++){
Expression expr = exprs.get(c);
if(expr instanceof ExpressionName){
Column column = ((ExpressionName)expr).getColumn().copy();
column.setName( expr.getAlias() );
columns.add( column );
}else{
columns.add( new ColumnExpression(expr));
}
}
}
static void drop(Database database, String name) throws Exception{
File file = new File( Utils.createTableViewFileName( database, name ) );
boolean ok = file.delete();
if(!ok) throw Utils.createSQLException("View '" + name + "' can't drop.");
}
private void write(Database database) throws Exception{
RandomAccessFile raFile = createFile( database );
StorePage storePage = new StorePage( null, -1, raFile, 8);
StoreImpl store = StoreImpl.createStore( null, storePage, SQLTokenizer.CREATE, 8);
store.writeString(sql);
store.writeInt( 0 );
store.writeFinsh(null);
raFile.close();
}
void writeMagic(RandomAccessFile raFile) throws Exception{
raFile.writeInt(MAGIC_VIEW);
raFile.writeInt(TABLE_VIEW_VERSION);
}
}