DropColumn.java :  » Database-DBMS » Quadcap-Embeddable-Database » com » quadcap » sql » Java Open Source

Java Open Source » Database DBMS » Quadcap Embeddable Database 
Quadcap Embeddable Database » com » quadcap » sql » DropColumn.java
package com.quadcap.sql;

/* Copyright 1999 - 2003 Quadcap Software.  All rights reserved.
 *
 * This software is distributed under the Quadcap Free Software License.
 * This software may be used or modified for any purpose, personal or
 * commercial.  Open Source redistributions are permitted.  Commercial
 * redistribution of larger works derived from, or works which bundle
 * this software requires a "Commercial Redistribution License"; see
 * http://www.quadcap.com/purchase.
 *
 * Redistributions qualify as "Open Source" under  one of the following terms:
 *   
 *    Redistributions are made at no charge beyond the reasonable cost of
 *    materials and delivery.
 *
 *    Redistributions are accompanied by a copy of the Source Code or by an
 *    irrevocable offer to provide a copy of the Source Code for up to three
 *    years at the cost of materials and delivery.  Such redistributions
 *    must allow further use, modification, and redistribution of the Source
 *    Code under substantially the same terms as this license.
 *
 * Redistributions of source code must retain the copyright notices as they
 * appear in each source code file, these license terms, and the
 * disclaimer/limitation of liability set forth as paragraph 6 below.
 *
 * Redistributions in binary form must reproduce this Copyright Notice,
 * these license terms, and the disclaimer/limitation of liability set
 * forth as paragraph 6 below, in the documentation and/or other materials
 * provided with the distribution.
 *
 * The Software is provided on an "AS IS" basis.  No warranty is
 * provided that the Software is free of defects, or fit for a
 * particular purpose.  
 *
 * Limitation of Liability. Quadcap Software shall not be liable
 * for any damages suffered by the Licensee or any third party resulting
 * from use of the Software.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import java.sql.SQLException;

import com.quadcap.sql.file.BlockFile;
import com.quadcap.sql.file.ByteUtil;
import com.quadcap.sql.file.PageManager;
import com.quadcap.sql.file.RandomAccess;
import com.quadcap.sql.file.RandomAccessInputStream;
import com.quadcap.sql.file.RandomAccessOutputStream;

import com.quadcap.sql.io.ObjectInputStream;
import com.quadcap.sql.io.ObjectOutputStream;
import com.quadcap.sql.io.Extern;

import com.quadcap.sql.index.Btree;

import com.quadcap.sql.types.Value;
import com.quadcap.sql.types.ValueNull;

import com.quadcap.io.CountedInputStream;

import com.quadcap.util.Debug;

/**
 * Log step to add a column to a table, supplying default values as necessary.
 *
 * @author Stan Bailes
 */
public class DropColumn extends LogStep implements Externalizable {
    transient Table      table;

    Column        column;
    String        tableName     = null;

    /**
     * Default constructor
     */
    public DropColumn() {}

    /**
     * Explicit constructor from table and column
     */
    public DropColumn(Session session, Table table, Column column) {
        super(session);
  this.table = table;
  this.tableName = table.getName();
  this.column = column;
    }

    /**
     * Lazy table accessor
     */
    Table getTable(Database db) throws IOException {
  if (table == null) {
      table = (Table)db.getRelation(tableName);
  }
  return table;
    }

    /**
     * LogStep.redo(), our job is to remove the column definition from the
     * schema.
     */
    public void redo(Session session) throws IOException, SQLException {
  Database db = session.getDatabase();
  getTable(db);
  Column c = table.getColumn(column.getName());
  table.deleteColumn(c.getColumn());
  db.updateRelation(table);
    }

    /**
     * LogStep.undo(), our job is to update the table's definition to
     * include the new column
     */
    public void undo(Session session) throws IOException, SQLException {
  Database db = session.getDatabase();
  getTable(db);
  table.addColumn(column, column.getColumn());
  db.updateRelation(table);
    }

    /**
     * I'm ready
     */
    public void prepare(Session session) throws IOException, SQLException {
    }

    /**
     * Read me from a stream
     */
    public void readExternal(ObjectInput in)
  throws IOException, ClassNotFoundException
    {
  super.readExternal(in);
  this.column = (Column)in.readObject();
  this.tableName = (String)in.readObject();
    }

    /**
     * Write me to a stream
     */
    public void writeExternal(ObjectOutput out) throws IOException {
  super.writeExternal(out);
  out.writeObject(column);
  out.writeObject(tableName);
    }

    /**
     * Return a string representation for display/debugging purposes
     */
    //#ifdef DEBUG
    public String toString() {
  StringBuffer sb = new StringBuffer(super.toString());
        sb.append(" DropColumn(");
  sb.append(tableName);
  sb.append(',');
  sb.append(column.toString());
  return sb.toString();
    }
    //#endif

    /**
     * My class's Extern object (see com.quadcap.sql.io.Extern)
     */
    static Extern extern;
    public void setExtern(Extern extern) { DropColumn.extern = extern; }
    public Extern getExtern() { return extern; }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.