001: /*
002: * WbInclude.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, 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.sql.DelimiterDefinition;
017: import workbench.util.ArgumentType;
018: import workbench.util.ExceptionUtil;
019: import workbench.resource.ResourceMgr;
020: import workbench.sql.BatchRunner;
021: import workbench.sql.SqlCommand;
022: import workbench.sql.StatementRunnerResult;
023: import workbench.util.ArgumentParser;
024: import workbench.util.SqlUtil;
025: import workbench.util.StringUtil;
026: import workbench.resource.Settings;
027: import workbench.util.WbFile;
028:
029: /**
030: * @author support@sql-workbench.net
031: */
032: public class WbInclude extends SqlCommand {
033: public static final String VERB = "WBINCLUDE";
034: public static final WbInclude INCLUDE_LONG = new WbInclude(VERB);
035: public static final WbInclude INCLUDE_SHORT = new WbInclude("@");
036: public static final WbInclude INCLUDE_FB = new WbInclude("INPUT");
037:
038: private final String verb;
039: private BatchRunner batchRunner;
040:
041: private WbInclude(String aVerb) {
042: this .verb = aVerb;
043: cmdLine = new ArgumentParser();
044: cmdLine.addArgument("file");
045: cmdLine.addArgument("continueOnError",
046: ArgumentType.BoolArgument);
047: cmdLine.addArgument("checkEscapedQuotes",
048: ArgumentType.BoolArgument);
049: cmdLine.addArgument("delimiter", StringUtil
050: .stringToList("';','/',<char>"));
051: cmdLine.addArgument("verbose", ArgumentType.BoolArgument);
052: cmdLine.addArgument(AppArguments.ARG_IGNORE_DROP,
053: ArgumentType.BoolArgument);
054: CommonArgs.addEncodingParameter(cmdLine);
055: this .isUpdatingCommand = true;
056: }
057:
058: public String getVerb() {
059: return verb;
060: }
061:
062: protected boolean isConnectionRequired() {
063: return false;
064: }
065:
066: public StatementRunnerResult execute(String aSql)
067: throws SQLException {
068: StatementRunnerResult result = new StatementRunnerResult();
069: result.setSuccess();
070:
071: String clean = SqlUtil.makeCleanSql(aSql, false, '"');
072: boolean isShortInclude = false;
073:
074: if (clean.charAt(0) == '@') {
075: clean = clean.substring(1);
076: isShortInclude = true;
077: } else {
078: clean = SqlUtil.stripVerb(clean);
079: }
080:
081: WbFile file = null;
082:
083: if (isShortInclude) {
084: file = evaluateFileArgument(clean);
085: } else {
086: cmdLine.parse(clean);
087: file = evaluateFileArgument(cmdLine.getValue("file"));
088: }
089:
090: if (file == null || file.length() == 0) {
091: String msg = ResourceMgr
092: .getString("ErrIncludeWrongParameter");
093: result.addMessage(msg);
094: result.setFailure();
095: return result;
096: }
097:
098: if (!file.exists()) {
099: result.setFailure();
100: String msg = ResourceMgr
101: .getString("ErrIncludeFileNotFound");
102: msg = StringUtil.replace(msg, "%filename%", file
103: .getFullPath());
104: result.addMessage(msg);
105: return result;
106: }
107:
108: boolean continueOnError = cmdLine.getBoolean("continueonerror",
109: false);
110: boolean checkEscape = cmdLine.getBoolean("checkescapedquotes",
111: Settings.getInstance().getCheckEscapedQuotes());
112: boolean verbose = cmdLine.getBoolean("verbose", false);
113: boolean defaultIgnore = currentConnection.getProfile()
114: .getIgnoreDropErrors();
115: boolean ignoreDrop = cmdLine.getBoolean(
116: AppArguments.ARG_IGNORE_DROP, defaultIgnore);
117: String encoding = cmdLine.getValue("encoding");
118:
119: String delim = cmdLine.getValue("delimiter");
120: try {
121: batchRunner = new BatchRunner(file.getCanonicalPath());
122: String dir = file.getCanonicalFile().getParent();
123: batchRunner.setBaseDir(dir);
124: batchRunner.setConnection(currentConnection);
125: if (delim != null)
126: batchRunner.setDelimiter(DelimiterDefinition
127: .parseCmdLineArgument(delim));
128: batchRunner.setResultLogger(this .resultLogger);
129: batchRunner.setVerboseLogging(verbose);
130: batchRunner.setRowMonitor(this .rowMonitor);
131: batchRunner.setAbortOnError(!continueOnError);
132: batchRunner.setCheckEscapedQuotes(checkEscape);
133: batchRunner.setShowTiming(false);
134: batchRunner.setEncoding(encoding);
135: batchRunner.setParameterPrompter(this .prompter);
136: batchRunner.setIgnoreDropErrors(ignoreDrop);
137: batchRunner.execute();
138: if (batchRunner.isSuccess()) {
139: result.setSuccess();
140: } else {
141: result.setFailure();
142: }
143: } catch (Exception th) {
144: result.setFailure();
145: result.addMessage(ExceptionUtil.getDisplay(th));
146: }
147: return result;
148: }
149:
150: public void done() {
151: // nothing to do
152: }
153:
154: public void cancel() throws SQLException {
155: super.cancel();
156: if (batchRunner != null) {
157: batchRunner.cancel();
158: }
159: }
160:
161: }
|