001: /*
002: * WbConnect.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2007, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.sql.wbcommands;
013:
014: import java.sql.SQLException;
015: import workbench.AppArguments;
016: import workbench.WbManager;
017: import workbench.db.ConnectionMgr;
018: import workbench.db.ConnectionProfile;
019: import workbench.db.WbConnection;
020: import workbench.gui.profiles.ProfileKey;
021: import workbench.log.LogMgr;
022: import workbench.resource.ResourceMgr;
023: import workbench.sql.BatchRunner;
024: import workbench.sql.SqlCommand;
025: import workbench.sql.StatementRunnerResult;
026: import workbench.util.ArgumentParser;
027: import workbench.util.ArgumentType;
028: import workbench.util.ExceptionUtil;
029: import workbench.util.StringUtil;
030:
031: /**
032: * @author support@sql-workbench.net
033: */
034: public class WbConnect extends SqlCommand {
035: private static int connectionId;
036:
037: public WbConnect() {
038: cmdLine = new ArgumentParser();
039: cmdLine.addArgument(AppArguments.ARG_PROFILE,
040: ArgumentType.ProfileArgument);
041: cmdLine.addArgument(AppArguments.ARG_PROFILE_GROUP);
042: cmdLine.addArgument(AppArguments.ARG_CONN_URL);
043: cmdLine.addArgument(AppArguments.ARG_CONN_DRIVER);
044: cmdLine.addArgument(AppArguments.ARG_CONN_JAR);
045: cmdLine.addArgument(AppArguments.ARG_CONN_USER);
046: cmdLine.addArgument(AppArguments.ARG_CONN_PWD);
047: cmdLine.addArgument(AppArguments.ARG_CONN_AUTOCOMMIT,
048: ArgumentType.BoolArgument);
049: cmdLine.addArgument(AppArguments.ARG_CONN_ROLLBACK,
050: ArgumentType.BoolArgument);
051: cmdLine.addArgument(AppArguments.ARG_CONN_TRIM_CHAR,
052: ArgumentType.BoolArgument);
053: }
054:
055: public String getVerb() {
056: return "WBCONNECT";
057: }
058:
059: @Override
060: public StatementRunnerResult execute(String aSql)
061: throws SQLException, Exception {
062: StatementRunnerResult result = new StatementRunnerResult();
063: result.setFailure();
064:
065: if (!WbManager.getInstance().isBatchMode()) {
066: result.addMessage(ResourceMgr.getString("ErrConnNoBatch"));
067: return result;
068: }
069:
070: String args = getCommandLine(aSql);
071: cmdLine.parse(args);
072:
073: ConnectionProfile profile = null;
074: String profName = cmdLine.getValue(AppArguments.ARG_PROFILE);
075: if (StringUtil.isEmptyString(profName)) {
076: profile = BatchRunner.createCmdLineProfile(cmdLine);
077: } else {
078: String group = cmdLine
079: .getValue(AppArguments.ARG_PROFILE_GROUP);
080: profile = ConnectionMgr.getInstance().getProfile(
081: new ProfileKey(profName, group));
082: }
083:
084: if (profile == null) {
085: result.addMessage(ResourceMgr.getString("ErrConnNoArgs"));
086: return result;
087: }
088:
089: WbConnection newConn = null;
090: try {
091: String id = null;
092:
093: if (runner.getConnectionClient() != null) {
094: id = runner.getConnectionClient().getConnectionId(
095: profile);
096: runner.getConnectionClient().connectBegin(profile);
097: } else {
098: connectionId++;
099: id = "batch-connect-" + connectionId;
100: }
101:
102: newConn = ConnectionMgr.getInstance().getConnection(
103: profile, id);
104: if (newConn != null && runner.getConnectionClient() == null) {
105: // Disconnect the old connection "manually" if no connectionClient
106: // is available, otherwise Connectable.connectBegin() takes care of that
107: WbConnection old = this .runner.getConnection();
108: if (old != null) {
109: LogMgr.logInfo("WbConnect.execute()",
110: "Closing old connection: "
111: + old.getDisplayString());
112: old.close();
113: }
114: }
115: LogMgr.logInfo("WbConnect.execute()", "Connected to: "
116: + newConn.getDisplayString());
117: this .runner.setConnection(newConn);
118:
119: if (runner.getConnectionClient() != null) {
120: runner.getConnectionClient().connected(newConn);
121: }
122:
123: this .setConnection(newConn);
124: result.addMessage(ResourceMgr.getFormattedString(
125: "MsgBatchConnectOk", newConn.getDisplayString()));
126: result.setSuccess();
127: } catch (Exception e) {
128: String err = ExceptionUtil.getDisplay(e);
129: if (runner.getConnectionClient() != null) {
130: runner.getConnectionClient().connectFailed(err);
131: }
132: result.addMessage(ResourceMgr
133: .getFormattedString("MsgBatchConnectError"));
134: result.addMessage(err);
135: result.setFailure();
136: } finally {
137: if (runner.getConnectionClient() != null) {
138: runner.getConnectionClient().connectEnded();
139: }
140: }
141:
142: return result;
143: }
144:
145: }
|