001: /*
002: * $Id: RemountCommand.java,v 1.15 2005/12/20 18:32:28 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.commands;
042:
043: import java.io.File;
044:
045: import org.axiondb.AxionException;
046: import org.axiondb.Database;
047: import org.axiondb.ExternalTable;
048: import org.axiondb.Literal;
049: import org.axiondb.Selectable;
050: import org.axiondb.Table;
051: import org.axiondb.TableIdentifier;
052: import org.axiondb.engine.visitors.FindBindVariableVisitor;
053: import org.axiondb.jdbc.AxionResultSet;
054:
055: /**
056: * A <code>REMOUNT</code> command, which points the database at
057: * a new location or refresh the external table.
058: *
059: * @version $Revision: 1.15 $ $Date: 2005/12/20 18:32:28 $
060: * @author Rodney Waldhoff
061: * @author Ahimanikya Satapathy
062: */
063: public class RemountCommand extends BaseAxionCommand {
064: public RemountCommand() {
065: }
066:
067: public void setDirectory(String dir) {
068: _dir = dir;
069: }
070:
071: public void setDirectory(Literal dir) {
072: _dir = dir;
073: }
074:
075: public Object getDirectory() {
076: return _dir;
077: }
078:
079: public void setTable(TableIdentifier table) {
080: _table = table;
081: }
082:
083: public TableIdentifier getTable() {
084: return _table;
085: }
086:
087: public void setDataFilesOnly(boolean data) {
088: _dataFilesOnly = data;
089: }
090:
091: public boolean getDataFilesOnly() {
092: return _dataFilesOnly;
093: }
094:
095: public boolean execute(Database db) throws AxionException {
096: if (null == _table) {
097: remountDatabase(db);
098: } else {
099: remountTable(db);
100: }
101: return false;
102: }
103:
104: /** Unsupported */
105: public AxionResultSet executeQuery(Database database)
106: throws AxionException {
107: throw new UnsupportedOperationException("Use execute.");
108: }
109:
110: public int executeUpdate(Database database) throws AxionException {
111: execute(database);
112: return 0;
113: }
114:
115: public String toString() {
116: return "REMOUNT(" + _dir + ")";
117: }
118:
119: protected void buildBindVariables() {
120: setBindVariableVisitor(new FindBindVariableVisitor());
121: getBindVariableVisitor().visit((Selectable) _dir);
122: }
123:
124: private void remountDatabase(Database db) throws AxionException {
125: if (_dir == null) {
126: throw new AxionException(
127: "Usage: REMOUNT [table-name] [DATA] path");
128: }
129: db.remount(new File(getPath()));
130: }
131:
132: private void remountTable(Database db) throws AxionException {
133: Table table = db.getTable(_table);
134: if (null == table) {
135: throw new AxionException("Table " + _table + " not found.");
136: }
137: if (table instanceof ExternalTable) {
138: ((ExternalTable) table).remount();
139: } else {
140: File path = (_dir == null ? null : new File(getPath()));
141: table.remount(path, _dataFilesOnly);
142: }
143: }
144:
145: private String getPath() throws AxionException {
146: String path = null;
147: if (_dir instanceof Literal) {
148: path = (String) ((Literal) _dir).evaluate(null);
149: } else {
150: path = (String) _dir;
151: }
152: return path;
153: }
154:
155: private Object _dir; // a literal or String
156: private TableIdentifier _table;
157: private boolean _dataFilesOnly = false;
158: }
|