KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > text > commands > sqlconsole > Load


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Jeff Mesnil
22  * Contributor(s): ______________________.
23  */

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

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

50   public Load(VirtualDatabaseConsole module)
51   {
52     super(module);
53   }
54
55   /**
56    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#parse(java.lang.String)
57    */

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

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

133   public String JavaDoc getCommandName()
134   {
135     return "load";
136   }
137
138   /**
139    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandParameters()
140    */

141   public String JavaDoc getCommandParameters()
142   {
143     return ConsoleTranslate.get("sql.command.load.params");
144   }
145
146   /**
147    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandDescription()
148    */

149   public String JavaDoc getCommandDescription()
150   {
151     return ConsoleTranslate.get("sql.command.load.description");
152   }
153 }
Popular Tags