KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > console > text > commands > sqlconsole > Load


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Jeff Mesnil
20  * Contributor(s): ______________________.
21  */

22
23 package org.continuent.sequoia.console.text.commands.sqlconsole;
24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.sql.Connection JavaDoc;
29
30 import org.continuent.sequoia.common.i18n.ConsoleTranslate;
31 import org.continuent.sequoia.console.text.ConsoleException;
32 import org.continuent.sequoia.console.text.commands.ConsoleCommand;
33 import org.continuent.sequoia.console.text.module.VirtualDatabaseConsole;
34
35 /**
36  * This class defines a "load" sql command
37  *
38  * @author <a HREF="mailto:jeff.mesnil@emicnetworks.com">Jeff Mesnil</a>
39  */

40 public class Load extends ConsoleCommand
41 {
42
43   /**
44    * Creates a new <code>Load</code> object
45    *
46    * @param module the command is attached to
47    */

48   public Load(VirtualDatabaseConsole module)
49   {
50     super(module);
51   }
52
53   /**
54    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
55    */

56   public void parse(String JavaDoc commandText) throws IOException JavaDoc, ConsoleException
57   {
58     String JavaDoc fileName = commandText.trim();
59     if ("".equals(fileName)) //$NON-NLS-1$
60
{
61       console.printError(getUsage());
62       return;
63     }
64     load(commandText.trim());
65   }
66
67   /**
68    * Executes all the SQL requests contained in the specified file.
69    * sqlRequestCommand.parse(cmd);
70       
71    * @param fileName the file name to open
72    */

73   public void load(String JavaDoc fileName)
74   {
75     Connection JavaDoc connection = ((VirtualDatabaseConsole)module).getConnection();
76     
77     BufferedReader JavaDoc file = null;
78     try
79     {
80       file = new BufferedReader JavaDoc(new FileReader JavaDoc(fileName));
81     }
82     catch (Exception JavaDoc e)
83     {
84       console.printError(
85           ConsoleTranslate.get("sql.command.load.file.error", e), e); //$NON-NLS-1$
86
return;
87     }
88
89     console.println(ConsoleTranslate.get("sql.command.loading.file", fileName)); //$NON-NLS-1$
90
try
91     {
92       String JavaDoc request;
93
94       while ((request = file.readLine()) != null)
95       {
96         request = request.trim();
97         console.println(request);
98
99         if (request.equalsIgnoreCase("begin")) //$NON-NLS-1$
100
connection.setAutoCommit(false);
101         else if (request.equalsIgnoreCase("commit")) //$NON-NLS-1$
102
connection.commit();
103         else if (request.equalsIgnoreCase("rollback")) //$NON-NLS-1$
104
connection.rollback();
105         else
106         { // Regular SQL request
107
((VirtualDatabaseConsole)module).execSQL(request, false);
108         }
109       }
110     }
111     catch (Exception JavaDoc e)
112     {
113       console.printError(ConsoleTranslate.get("sql.command.load.execute.error", //$NON-NLS-1$
114
e), e);
115     }
116     finally
117     {
118       try
119       {
120         file.close();
121       }
122       catch (IOException JavaDoc ignore)
123       {
124       }
125     }
126   }
127
128   /**
129    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
130    */

131   public String JavaDoc getCommandName()
132   {
133     return "load"; //$NON-NLS-1$
134
}
135
136   /**
137    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandParameters()
138    */

139   public String JavaDoc getCommandParameters()
140   {
141     return ConsoleTranslate.get("sql.command.load.params"); //$NON-NLS-1$
142
}
143
144   /**
145    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
146    */

147   public String JavaDoc getCommandDescription()
148   {
149     return ConsoleTranslate.get("sql.command.load.description"); //$NON-NLS-1$
150
}
151 }
Popular Tags