KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > ViewDef


1 /**
2  * com.mckoi.database.ViewDef 23 Aug 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.io.*;
28 import com.mckoi.database.global.BlobAccessor;
29 import com.mckoi.database.global.ByteLongObject;
30
31 /**
32  * A ViewDef object is a definition of a view stored in the database. It is
33  * an object that can be easily serialized and deserialized to/from the system
34  * view table. It contains the DataTableDef that describes the characteristics
35  * of the view result, and a QueryPlanNode that describes how the view can be
36  * constructed.
37  *
38  * @author Tobias Downer
39  */

40
41 public class ViewDef {
42
43   /**
44    * The DataTableDef object that describes the view column def.
45    */

46   private DataTableDef view_def;
47   
48   /**
49    * The QueryPlanNode that is used to evaluate the view.
50    */

51   private QueryPlanNode view_query_node;
52
53   /**
54    * Constructs the ViewDef object.
55    */

56   public ViewDef(DataTableDef view_def, QueryPlanNode query_node) {
57     this.view_def = view_def;
58     this.view_query_node = query_node;
59   }
60
61   /**
62    * Returns the DataTableDef for this view.
63    */

64   public DataTableDef getDataTableDef() {
65     return view_def;
66   }
67   
68   /**
69    * Returns the QueryPlanNode for this view.
70    */

71   public QueryPlanNode getQueryPlanNode() {
72     try {
73       return (QueryPlanNode) view_query_node.clone();
74     }
75     catch (CloneNotSupportedException JavaDoc e) {
76       throw new Error JavaDoc("Clone error: " + e.getMessage());
77     }
78   }
79
80   
81   
82   /**
83    * Forms this ViewDef object into a serialized ByteLongObject object that can
84    * be stored in a table.
85    */

86   ByteLongObject serializeToBlob() {
87     try {
88       ByteArrayOutputStream byte_out = new ByteArrayOutputStream();
89       ObjectOutputStream out = new ObjectOutputStream(byte_out);
90       // Write the version number
91
out.writeInt(1);
92       // Write the DataTableDef
93
getDataTableDef().write(out);
94       // Serialize the QueryPlanNode
95
out.writeObject(getQueryPlanNode());
96       
97       out.flush();
98       
99       return new ByteLongObject(byte_out.toByteArray());
100       
101     }
102     catch (IOException e) {
103       throw new Error JavaDoc("IO Error: " + e.getMessage());
104     }
105       
106   }
107   
108   /**
109    * Creates an instance of ViewDef from the serialized information stored in
110    * the blob.
111    */

112   static final ViewDef deserializeFromBlob(BlobAccessor blob) {
113     InputStream blob_in = blob.getInputStream();
114     try {
115       ObjectInputStream in = new ObjectInputStream(blob_in);
116       // Read the version
117
int version = in.readInt();
118       if (version == 1) {
119         DataTableDef view_def = DataTableDef.read(in);
120         view_def.setImmutable();
121         QueryPlanNode view_plan = (QueryPlanNode) in.readObject();
122         return new ViewDef(view_def, view_plan);
123       }
124       else {
125         throw new IOException(
126                            "Newer ViewDef version serialization: " + version);
127       }
128       
129     }
130     catch (IOException e) {
131       throw new Error JavaDoc("IO Error: " + e.getMessage());
132     }
133     catch (ClassNotFoundException JavaDoc e) {
134       throw new Error JavaDoc("Class not found: " + e.getMessage());
135     }
136   }
137
138 }
139
140
Popular Tags