KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > database > View


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  * View.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  * Created on 31.05.2004
33  */

34 package smallsql.database;
35
36 import java.io.*;
37
38
39 /**
40  * @author Volker Berlin
41  */

42 class View extends TableView{
43     final String JavaDoc sql;
44     final CommandSelect commandSelect;
45     
46     
47     /**
48      * Constructor for loading an existing view.
49      */

50     View(SSConnection con, String JavaDoc name, RandomAccessFile raFile, long offset) throws Exception JavaDoc{
51         super( name, new Columns() );
52         StorePage storePage = new StorePage( null, -1, raFile, offset);
53         StoreImpl store = StoreImpl.createStore( null, storePage, SQLTokenizer.SELECT, offset);
54         sql = store.readString();
55         
56         // read additional informations
57
int type;
58         while((type = store.readInt()) != 0){
59             int offsetInPage = store.getCurrentOffsetInPage();
60             int size = store.readInt();
61             switch(type){
62                 //currently there are no additinal informations, see write()
63
}
64             store.setCurrentOffsetInPage(offsetInPage + size);
65         }
66         
67         raFile.close();
68         commandSelect = (CommandSelect)new SQLParser().parse(con, sql);
69         createColumns(con);
70     }
71     
72     
73     /**
74      * Constructor for a new view. Is call on execute() of CREATE VIEW. This view is not init.
75      */

76     View(Database database, String JavaDoc name, String JavaDoc sql) throws Exception JavaDoc{
77         super( name, new Columns() );
78         this.sql = sql;
79         this.commandSelect = null;
80         write(database);
81     }
82
83     
84     /**
85      * Constructor for a UNION
86      */

87     View(SSConnection con, CommandSelect commandSelect) throws Exception JavaDoc{
88         super("UNION", new Columns());
89         this.sql = null;
90         this.commandSelect = commandSelect;
91         createColumns(con);
92     }
93     
94     
95     private void createColumns(SSConnection con) throws Exception JavaDoc{
96         commandSelect.compile(con);
97         Expressions exprs = commandSelect.columnExpressions;
98         for(int c=0; c<exprs.size(); c++){
99             Expression expr = exprs.get(c);
100             if(expr instanceof ExpressionName){
101                 Column column = ((ExpressionName)expr).getColumn().copy();
102                 column.setName( expr.getAlias() );
103                 columns.add( column );
104             }else{
105                 columns.add( new ColumnExpression(expr));
106             }
107         }
108     }
109     
110
111     /**
112      * Drop the View. This method is static that the file does not need to load and also corrupt files can be dropped.
113      */

114     static void drop(Database database, String JavaDoc name) throws Exception JavaDoc{
115         File file = new File( Utils.createTableViewFileName( database, name ) );
116         boolean ok = file.delete();
117         if(!ok) throw Utils.createSQLException("View '" + name + "' can't drop.");
118     }
119     
120
121     private void write(Database database) throws Exception JavaDoc{
122         RandomAccessFile raFile = createFile( database );
123         StorePage storePage = new StorePage( null, -1, raFile, 8);
124         StoreImpl store = StoreImpl.createStore( null, storePage, SQLTokenizer.CREATE, 8);
125         store.writeString(sql);
126         
127         // write additional informations
128
store.writeInt( 0 ); // no more additinal informations
129

130         store.writeFinsh(null);
131         raFile.close();
132     }
133
134     void writeMagic(RandomAccessFile raFile) throws Exception JavaDoc{
135         raFile.writeInt(MAGIC_VIEW);
136         raFile.writeInt(TABLE_VIEW_VERSION);
137     }
138     
139 }
140
Popular Tags