001: /*
002: * CommonArgs.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.util.List;
015: import workbench.interfaces.BatchCommitter;
016: import workbench.interfaces.Committer;
017: import workbench.interfaces.ProgressReporter;
018: import workbench.resource.ResourceMgr;
019: import workbench.resource.Settings;
020: import workbench.util.ArgumentParser;
021: import workbench.util.ArgumentType;
022: import workbench.util.QuoteEscapeType;
023: import workbench.util.StringUtil;
024: import workbench.util.ValueConverter;
025:
026: /**
027: * A class to manage common parameters for various WbCommands.
028: *
029: * When adding the parameters to the ArgumentParser, the necessary
030: * values for the code completion are also supplied.
031: * For parameters with no fixed set of values a sample list
032: * with popular (or so I think) values is added, e.g. the -encoding
033: * parameter.
034: *
035: * @author support@sql-workbench.net
036: */
037: public class CommonArgs {
038: public static final String ARG_PROGRESS = "showProgress";
039: public static final String ARG_ENCODING = "encoding";
040: public static final String ARG_COMMIT = "commitEvery";
041: public static final String ARG_DELIM = "delimiter";
042: public static final String ARG_VERBOSE_XML = "verboseXML";
043: public static final String ARG_IMPORT_MODE = "mode";
044: public static final String ARG_CONTINUE = "continueOnError";
045: public static final String ARG_BATCHSIZE = "batchSize";
046: public static final String ARG_COMMIT_BATCH = "commitBatch";
047: public static final String ARG_QUOTE_ESCAPE = "quoteCharEscaping";
048: public static final String ARG_AUTO_BOOLEAN = "booleanToNumber";
049: public static final String ARG_DATE_FORMAT = "dateFormat";
050: public static final String ARG_TIMESTAMP_FORMAT = "timestampFormat";
051: public static final String ARG_DECCHAR = "decimal";
052: public static final String ARG_FALSE_LITERALS = "literalsFalse";
053: public static final String ARG_TRUE_LITERALS = "literalsTrue";
054: public static final String ARG_CHECK_FK_DEPS = "checkDependencies";
055: public static final String ARG_PRE_TABLE_STMT = "preTableStatement";
056: public static final String ARG_POST_TABLE_STMT = "postTableStatement";
057: public static final String ARG_IGNORE_TABLE_STMT_ERRORS = "ignorePrePostErrors";
058: public static final String ARG_TRANS_CONTROL = "transactionControl";
059:
060: private static List<String> getDelimiterArguments() {
061: return StringUtil.stringToList("'\\t',';',\"','\",'|',<char>");
062: }
063:
064: public static void addTransactionControL(ArgumentParser cmdLine) {
065: cmdLine.addArgument(ARG_TRANS_CONTROL,
066: ArgumentType.BoolArgument);
067: }
068:
069: public static void addTableStatements(ArgumentParser cmdLine) {
070: cmdLine.addArgument(ARG_PRE_TABLE_STMT);
071: cmdLine.addArgument(ARG_POST_TABLE_STMT);
072: cmdLine.addArgument(ARG_IGNORE_TABLE_STMT_ERRORS,
073: ArgumentType.BoolArgument);
074: }
075:
076: public static void addCheckDepsParameter(ArgumentParser cmdLine) {
077: cmdLine.addArgument(ARG_CHECK_FK_DEPS,
078: ArgumentType.BoolArgument);
079: }
080:
081: public static void addContinueParameter(ArgumentParser cmdLine) {
082: cmdLine.addArgument(ARG_CONTINUE, ArgumentType.BoolArgument);
083: }
084:
085: public static void addImportModeParameter(ArgumentParser cmdLine) {
086: cmdLine
087: .addArgument(
088: ARG_IMPORT_MODE,
089: StringUtil
090: .stringToList("insert,update,\"update,insert\",\"insert,update\""));
091: }
092:
093: public static void addVerboseXmlParameter(ArgumentParser cmdLine) {
094: cmdLine.addArgument(ARG_VERBOSE_XML, ArgumentType.BoolArgument);
095: }
096:
097: public static void addCommitParameter(ArgumentParser cmdLine) {
098: cmdLine.addArgument(ARG_COMMIT, StringUtil
099: .stringToList("none,atEnd,<number>"));
100: }
101:
102: public static void addDelimiterParameter(ArgumentParser cmdLine) {
103: cmdLine.addArgument(ARG_DELIM, getDelimiterArguments());
104: }
105:
106: /**
107: * Adds the quoteCharEscaping argument. Valid values
108: * are none, duplicate, escape.
109: * @see workbench.util.QuoteEscapeType
110: */
111: public static void addQuoteEscaping(ArgumentParser cmdLine) {
112: cmdLine.addArgument(ARG_QUOTE_ESCAPE, StringUtil
113: .stringToList("none,duplicate,escape"));
114: }
115:
116: /**
117: * Adds the -encoding parameter to the ArgumentParser.
118: * The encodings that are added to the code completion list
119: * are retrieved from the Settings class.
120: * @param cmdLine the ArgumentParser to which the parameter should be added
121: * @see workbench.resource.Settings#getPopularEncodings()
122: */
123: public static void addEncodingParameter(ArgumentParser cmdLine) {
124: cmdLine.addArgument(ARG_ENCODING, StringUtil
125: .stringToList(Settings.getInstance()
126: .getPopularEncodings()));
127: }
128:
129: public static void addProgressParameter(ArgumentParser cmdLine) {
130: cmdLine.addArgument(ARG_PROGRESS, StringUtil
131: .stringToList("true,false,<number>"));
132: }
133:
134: public static void setProgressInterval(ProgressReporter reporter,
135: ArgumentParser cmdLine) {
136: String value = cmdLine.getValue(ARG_PROGRESS);
137:
138: if ("true".equalsIgnoreCase(value)) {
139: reporter.setReportInterval(1);
140: } else if ("false".equalsIgnoreCase(value)) {
141: reporter.setReportInterval(0);
142: } else if (value != null) {
143: int interval = StringUtil.getIntValue(value, 0);
144: reporter.setReportInterval(interval);
145: } else {
146: reporter
147: .setReportInterval(ProgressReporter.DEFAULT_PROGRESS_INTERVAL);
148: }
149: }
150:
151: public static void setCommitEvery(Committer committer,
152: ArgumentParser cmdLine) {
153: String commitParam = cmdLine.getValue("commitevery");
154: if (commitParam == null)
155: return;
156:
157: if ("none".equalsIgnoreCase(commitParam)
158: || "false".equalsIgnoreCase(commitParam)) {
159: committer.commitNothing();
160: } else if ("atEnd".equalsIgnoreCase(commitParam)) {
161: committer.setCommitEvery(0);
162: } else {
163: committer.setCommitEvery(StringUtil.getIntValue(
164: commitParam, 0));
165: }
166:
167: }
168:
169: public static void addCommitAndBatchParams(ArgumentParser cmdLine) {
170: cmdLine.addArgument(ARG_BATCHSIZE);
171: cmdLine
172: .addArgument(ARG_COMMIT_BATCH,
173: ArgumentType.BoolArgument);
174: }
175:
176: public static void setCommitAndBatchParams(
177: BatchCommitter committer, ArgumentParser cmdLine) {
178:
179: int queueSize = cmdLine.getIntValue(ARG_BATCHSIZE, -1);
180: String commitParam = cmdLine.getValue("commitevery");
181:
182: if (queueSize > 0) {
183: committer.setUseBatch(true);
184: committer.setBatchSize(queueSize);
185:
186: if (cmdLine.isArgPresent(ARG_COMMIT_BATCH)) {
187: committer.setCommitBatch(cmdLine.getBoolean(
188: ARG_COMMIT_BATCH, false));
189: } else if ("none".equalsIgnoreCase(commitParam)
190: || "false".equalsIgnoreCase(commitParam)) {
191: committer.commitNothing();
192: }
193: } else {
194: setCommitEvery(committer, cmdLine);
195: }
196: }
197:
198: public static QuoteEscapeType getQuoteEscaping(
199: ArgumentParser cmdLine) {
200: String esc = cmdLine.getValue(ARG_QUOTE_ESCAPE);
201: if (esc != null) {
202: try {
203: QuoteEscapeType escapeType = QuoteEscapeType
204: .valueOf(esc.trim().toLowerCase());
205: return escapeType;
206: } catch (Exception e) {
207: // ignore --> return none
208: }
209: }
210: return QuoteEscapeType.none;
211: }
212:
213: public static void addConverterOptions(ArgumentParser cmdLine,
214: boolean includeDateFormats) {
215: cmdLine
216: .addArgument(ARG_AUTO_BOOLEAN,
217: ArgumentType.BoolArgument);
218: cmdLine.addArgument(ARG_DECCHAR);
219: if (includeDateFormats) {
220: cmdLine.addArgument(ARG_DATE_FORMAT);
221: cmdLine.addArgument(ARG_TIMESTAMP_FORMAT);
222: }
223: cmdLine.addArgument(ARG_FALSE_LITERALS);
224: cmdLine.addArgument(ARG_TRUE_LITERALS);
225: }
226:
227: public static ValueConverter getConverter(ArgumentParser cmdLine)
228: throws IllegalArgumentException {
229: ValueConverter converter = new ValueConverter();
230: converter.setAutoConvertBooleanNumbers(cmdLine.getBoolean(
231: ARG_AUTO_BOOLEAN, true));
232:
233: String format = null;
234: try {
235: format = cmdLine.getValue(ARG_DATE_FORMAT);
236: if (format != null)
237: converter.setDefaultDateFormat(format);
238:
239: format = cmdLine.getValue(ARG_TIMESTAMP_FORMAT);
240: if (format != null)
241: converter.setDefaultTimestampFormat(format);
242: } catch (Exception e) {
243: String msg = ResourceMgr.getFormattedString(
244: "ErrIllegalDateTimeFormat", format);
245: throw new IllegalArgumentException(msg);
246: }
247:
248: String decimal = cmdLine.getValue(ARG_DECCHAR);
249: if (decimal != null)
250: converter.setDecimalCharacter(decimal.charAt(0));
251:
252: List<String> falseValues = StringUtil.stringToList(cmdLine
253: .getValue(ARG_FALSE_LITERALS), ",", true, true, false);
254: List<String> trueValues = StringUtil.stringToList(cmdLine
255: .getValue(ARG_TRUE_LITERALS), ",", true, true, false);
256: if (falseValues.size() > 0 && trueValues.size() > 0) {
257: converter.setBooleanLiterals(trueValues, falseValues);
258: }
259: return converter;
260: }
261:
262: }
|