001: /**
002: * com.mckoi.database.ViewDef 23 Aug 2002
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database;
024:
025: import java.io.*;
026: import com.mckoi.database.global.BlobAccessor;
027: import com.mckoi.database.global.ByteLongObject;
028:
029: /**
030: * A ViewDef object is a definition of a view stored in the database. It is
031: * an object that can be easily serialized and deserialized to/from the system
032: * view table. It contains the DataTableDef that describes the characteristics
033: * of the view result, and a QueryPlanNode that describes how the view can be
034: * constructed.
035: *
036: * @author Tobias Downer
037: */
038:
039: public class ViewDef {
040:
041: /**
042: * The DataTableDef object that describes the view column def.
043: */
044: private DataTableDef view_def;
045:
046: /**
047: * The QueryPlanNode that is used to evaluate the view.
048: */
049: private QueryPlanNode view_query_node;
050:
051: /**
052: * Constructs the ViewDef object.
053: */
054: public ViewDef(DataTableDef view_def, QueryPlanNode query_node) {
055: this .view_def = view_def;
056: this .view_query_node = query_node;
057: }
058:
059: /**
060: * Returns the DataTableDef for this view.
061: */
062: public DataTableDef getDataTableDef() {
063: return view_def;
064: }
065:
066: /**
067: * Returns the QueryPlanNode for this view.
068: */
069: public QueryPlanNode getQueryPlanNode() {
070: try {
071: return (QueryPlanNode) view_query_node.clone();
072: } catch (CloneNotSupportedException e) {
073: throw new Error("Clone error: " + e.getMessage());
074: }
075: }
076:
077: /**
078: * Forms this ViewDef object into a serialized ByteLongObject object that can
079: * be stored in a table.
080: */
081: ByteLongObject serializeToBlob() {
082: try {
083: ByteArrayOutputStream byte_out = new ByteArrayOutputStream();
084: ObjectOutputStream out = new ObjectOutputStream(byte_out);
085: // Write the version number
086: out.writeInt(1);
087: // Write the DataTableDef
088: getDataTableDef().write(out);
089: // Serialize the QueryPlanNode
090: out.writeObject(getQueryPlanNode());
091:
092: out.flush();
093:
094: return new ByteLongObject(byte_out.toByteArray());
095:
096: } catch (IOException e) {
097: throw new Error("IO Error: " + e.getMessage());
098: }
099:
100: }
101:
102: /**
103: * Creates an instance of ViewDef from the serialized information stored in
104: * the blob.
105: */
106: static final ViewDef deserializeFromBlob(BlobAccessor blob) {
107: InputStream blob_in = blob.getInputStream();
108: try {
109: ObjectInputStream in = new ObjectInputStream(blob_in);
110: // Read the version
111: int version = in.readInt();
112: if (version == 1) {
113: DataTableDef view_def = DataTableDef.read(in);
114: view_def.setImmutable();
115: QueryPlanNode view_plan = (QueryPlanNode) in
116: .readObject();
117: return new ViewDef(view_def, view_plan);
118: } else {
119: throw new IOException(
120: "Newer ViewDef version serialization: "
121: + version);
122: }
123:
124: } catch (IOException e) {
125: throw new Error("IO Error: " + e.getMessage());
126: } catch (ClassNotFoundException e) {
127: throw new Error("Class not found: " + e.getMessage());
128: }
129: }
130:
131: }
|