001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Jeff Mesnil
020: * Contributor(s): ______________________.
021: */package org.continuent.sequoia.console.text.commands.sqlconsole;
022:
023: import java.io.BufferedReader;
024: import java.io.FileReader;
025: import java.io.IOException;
026: import java.sql.Connection;
027:
028: import org.continuent.sequoia.common.i18n.ConsoleTranslate;
029: import org.continuent.sequoia.console.text.ConsoleException;
030: import org.continuent.sequoia.console.text.commands.ConsoleCommand;
031: import org.continuent.sequoia.console.text.module.VirtualDatabaseConsole;
032:
033: /**
034: * This class defines a "load" sql command
035: *
036: * @author <a href="mailto:jeff.mesnil@emicnetworks.com">Jeff Mesnil</a>
037: */
038: public class Load extends ConsoleCommand {
039:
040: /**
041: * Creates a new <code>Load</code> object
042: *
043: * @param module the command is attached to
044: */
045: public Load(VirtualDatabaseConsole module) {
046: super (module);
047: }
048:
049: /**
050: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
051: */
052: public void parse(String commandText) throws IOException,
053: ConsoleException {
054: String fileName = commandText.trim();
055: if ("".equals(fileName)) //$NON-NLS-1$
056: {
057: console.printError(getUsage());
058: return;
059: }
060: load(commandText.trim());
061: }
062:
063: /**
064: * Executes all the SQL requests contained in the specified file.
065: * sqlRequestCommand.parse(cmd);
066:
067: * @param fileName the file name to open
068: */
069: public void load(String fileName) {
070: Connection connection = ((VirtualDatabaseConsole) module)
071: .getConnection();
072:
073: BufferedReader file = null;
074: try {
075: file = new BufferedReader(new FileReader(fileName));
076: } catch (Exception e) {
077: console.printError(ConsoleTranslate.get(
078: "sql.command.load.file.error", e), e); //$NON-NLS-1$
079: return;
080: }
081:
082: console.println(ConsoleTranslate.get(
083: "sql.command.loading.file", fileName)); //$NON-NLS-1$
084: try {
085: String request;
086:
087: while ((request = file.readLine()) != null) {
088: request = request.trim();
089: console.println(request);
090:
091: if (request.equalsIgnoreCase("begin")) //$NON-NLS-1$
092: connection.setAutoCommit(false);
093: else if (request.equalsIgnoreCase("commit")) //$NON-NLS-1$
094: connection.commit();
095: else if (request.equalsIgnoreCase("rollback")) //$NON-NLS-1$
096: connection.rollback();
097: else { // Regular SQL request
098: ((VirtualDatabaseConsole) module).execSQL(request,
099: false);
100: }
101: }
102: } catch (Exception e) {
103: console.printError(ConsoleTranslate.get(
104: "sql.command.load.execute.error", //$NON-NLS-1$
105: e), e);
106: } finally {
107: try {
108: file.close();
109: } catch (IOException ignore) {
110: }
111: }
112: }
113:
114: /**
115: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
116: */
117: public String getCommandName() {
118: return "load"; //$NON-NLS-1$
119: }
120:
121: /**
122: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandParameters()
123: */
124: public String getCommandParameters() {
125: return ConsoleTranslate.get("sql.command.load.params"); //$NON-NLS-1$
126: }
127:
128: /**
129: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
130: */
131: public String getCommandDescription() {
132: return ConsoleTranslate.get("sql.command.load.description"); //$NON-NLS-1$
133: }
134: }
|