01: /*
02: * UseCommand.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.sql.commands;
13:
14: import java.sql.SQLException;
15:
16: import workbench.sql.formatter.SQLLexer;
17: import workbench.util.ExceptionUtil;
18: import workbench.resource.ResourceMgr;
19: import workbench.sql.SqlCommand;
20: import workbench.sql.StatementRunnerResult;
21: import workbench.sql.formatter.SQLToken;
22: import workbench.util.StringUtil;
23:
24: /**
25: * MS SQL Server's and MySQL's USE command.
26: *
27: * Actually this will be in effect if the JDBC driver reports
28: * that catalog's are supported
29: *
30: * This class will notify the connection used that the current database has changed
31: * so that the connection display in the main window can be updated.
32: * @author support@sql-workbench.net
33: */
34: public class UseCommand extends SqlCommand {
35: public static final String VERB = "USE";
36:
37: public UseCommand() {
38: }
39:
40: public StatementRunnerResult execute(String aSql)
41: throws SQLException {
42: StatementRunnerResult result = new StatementRunnerResult();
43: try {
44: SQLLexer lexer = new SQLLexer(aSql);
45:
46: // The first token should be the USE verb;
47: SQLToken t = lexer.getNextToken(false, false);
48:
49: // everything after the USE command is the catalog name
50: String catName = aSql.substring(t.getCharEnd()).trim();
51:
52: // DbMetadata.setCurrentCatalog() will fire the
53: // catalogChanged() event on the connection!
54: // no need to do this here
55: currentConnection.getMetadata().setCurrentCatalog(catName);
56:
57: String newCatalog = currentConnection.getMetadata()
58: .getCurrentCatalog();
59:
60: String msg = ResourceMgr.getString("MsgCatalogChanged");
61: String term = currentConnection.getMetadata()
62: .getCatalogTerm();
63:
64: msg = StringUtil.replace(msg, "%newcatalog%", newCatalog);
65: msg = StringUtil.replace(msg, "%catalogterm%", StringUtil
66: .capitalize(term));
67: result.addMessage(msg);
68: result.setSuccess();
69:
70: } catch (Exception e) {
71: result.clear();
72: result.addMessage(ResourceMgr.getString("MsgExecuteError"));
73: result.addMessage(ExceptionUtil.getAllExceptions(e));
74: result.setFailure();
75: } finally {
76: this .done();
77: }
78:
79: return result;
80: }
81:
82: public String getVerb() {
83: return VERB;
84: }
85:
86: }
|