001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.Vector;
042:
043: import java.io.Externalizable;
044: import java.io.IOException;
045: import java.io.ObjectInput;
046: import java.io.ObjectOutput;
047:
048: import java.sql.ResultSet;
049: import java.sql.SQLException;
050:
051: import com.quadcap.sql.io.Extern;
052: import com.quadcap.util.Debug;
053:
054: /**
055: * Implementation of the SQL <b>CREATE VIEW</b> statement.
056: *
057: * @author Stan Bailes
058: */
059:
060: public class StmtCreateView extends LogStep implements Stmt,
061: Externalizable {
062: View view;
063:
064: /**
065: * Default constructor
066: */
067: public StmtCreateView() {
068: }
069:
070: /**
071: * Explicit constructor from View (parser)
072: */
073: public StmtCreateView(Session session, View view) {
074: super (session);
075: this .view = view;
076: }
077:
078: /**
079: * LogStep.undo(): Remove the view
080: */
081: public void undo(Session session) throws IOException, SQLException {
082: Database db = session.getDatabase();
083: db.removeRelation(view.getName());
084: }
085:
086: /**
087: * LogStep.redo(): Add the view and record the view dependency relations
088: */
089: public void redo(Session session) throws IOException, SQLException {
090: Database db = session.getDatabase();
091: Vector v = view.getBaseTables();
092:
093: for (int i = 0; i < v.size(); i++) {
094: String base = (String) v.elementAt(i);
095: db.checkViewDependency(base, view.getName());
096: }
097:
098: view.addColumns(session);
099: db.addRelation(view);
100: for (int i = 0; i < v.size(); i++) {
101: String base = (String) v.elementAt(i);
102: db.addViewDependency(base, view.getName());
103: }
104: }
105:
106: public void execute(Session session) throws IOException,
107: SQLException {
108: session.getTableWriteLock("#Schema");
109: view.addColumns(session);
110: session.doStep(this );
111: }
112:
113: public void prepare(Session session) throws IOException,
114: SQLException {
115: }
116:
117: public void readExternal(ObjectInput in) throws IOException,
118: ClassNotFoundException {
119: super .readExternal(in);
120: view = (View) in.readObject();
121: }
122:
123: public void writeExternal(ObjectOutput out) throws IOException {
124: super .writeExternal(out);
125: out.writeObject(view);
126: }
127:
128: //#ifdef DEBUG
129: /**
130: * Return a displayable representation for debugging
131: */
132: public String toString() {
133: StringBuffer sb = new StringBuffer(super .toString());
134: sb.append(" CreateView(");
135: sb.append(view.toString());
136: sb.append(')');
137: return sb.toString();
138: }
139:
140: //#endif
141:
142: static Extern extern;
143:
144: public void setExtern(Extern extern) {
145: StmtCreateView.extern = extern;
146: }
147:
148: public Extern getExtern() {
149: return extern;
150: }
151: }
|