001: /*
002: * WbImport.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 workbench.db.importer.ConstantColumnValues;
015: import java.io.File;
016: import java.sql.SQLException;
017: import java.util.ArrayList;
018: import java.util.List;
019: import workbench.WbManager;
020: import workbench.db.ColumnIdentifier;
021: import workbench.db.importer.ConstantColumnValues;
022: import workbench.db.importer.CycleErrorException;
023: import workbench.db.importer.DataImporter;
024: import workbench.db.importer.ParsingInterruptedException;
025: import workbench.db.importer.RowDataProducer;
026: import workbench.db.importer.TableStatements;
027: import workbench.db.importer.TextFileParser;
028: import workbench.db.importer.XmlDataFileParser;
029: import workbench.interfaces.ImportFileParser;
030: import workbench.util.ArgumentType;
031: import workbench.util.ExceptionUtil;
032: import workbench.log.LogMgr;
033: import workbench.resource.ResourceMgr;
034: import workbench.resource.Settings;
035: import workbench.sql.SqlCommand;
036: import workbench.sql.StatementRunnerResult;
037: import workbench.util.ArgumentParser;
038: import workbench.util.ConverterException;
039: import workbench.util.StringUtil;
040: import workbench.util.ValueConverter;
041: import workbench.util.WbFile;
042:
043: /**
044: *
045: * @author support@sql-workbench.net
046: */
047: public class WbImport extends SqlCommand {
048: public static final String VERB = "WBIMPORT";
049:
050: public static final String ARG_TYPE = "type";
051: public static final String ARG_FILE = "file";
052: public static final String ARG_TARGETTABLE = "table";
053: public static final String ARG_QUOTE = "quotechar";
054: public static final String ARG_CONTAINSHEADER = "header";
055: public static final String ARG_FILECOLUMNS = "fileColumns";
056: public static final String ARG_MODE = "mode";
057: public static final String ARG_KEYCOLUMNS = "keyColumns";
058: public static final String ARG_DELETE_TARGET = "deleteTarget";
059: public static final String ARG_EMPTY_STRING_IS_NULL = "emptyStringIsNull";
060: public static final String ARG_DECODE = "decode";
061: public static final String ARG_IMPORTCOLUMNS = "importColumns";
062: public static final String ARG_COL_FILTER = "columnFilter";
063: public static final String ARG_LINE_FILTER = "lineFilter";
064: public static final String ARG_DIRECTORY = "sourceDir";
065: public static final String ARG_TARGET_SCHEMA = "schema";
066: public static final String ARG_USE_TRUNCATE = "useTruncate";
067: public static final String ARG_TRIM_VALUES = "trimValues";
068: public static final String ARG_FILE_EXT = "extension";
069: public static final String ARG_UPDATE_WHERE = "updateWhere";
070: public static final String ARG_TRUNCATE_TABLE = "truncateTable";
071: public static final String ARG_CREATE_TABLE = "createTarget";
072: public static final String ARG_BLOB_ISFILENAME = "blobIsFilename";
073: public static final String ARG_CLOB_ISFILENAME = "clobIsFilename";
074: public static final String ARG_MULTI_LINE = "multiLine";
075: public static final String ARG_START_ROW = "startRow";
076: public static final String ARG_END_ROW = "endRow";
077: public static final String ARG_BADFILE = "badFile";
078: public static final String ARG_CONSTANTS = "constantValues";
079: public static final String ARG_COL_WIDTHS = "columnWidths";
080:
081: private DataImporter imp;
082:
083: public WbImport() {
084: this .isUpdatingCommand = true;
085: this .cmdLine = new ArgumentParser();
086: CommonArgs.addDelimiterParameter(cmdLine);
087: CommonArgs.addEncodingParameter(cmdLine);
088: CommonArgs.addProgressParameter(cmdLine);
089: CommonArgs.addCommitParameter(cmdLine);
090: CommonArgs.addContinueParameter(cmdLine);
091: CommonArgs.addCommitAndBatchParams(cmdLine);
092: CommonArgs.addQuoteEscaping(cmdLine);
093: CommonArgs.addConverterOptions(cmdLine, true);
094: CommonArgs.addCheckDepsParameter(cmdLine);
095: CommonArgs.addTableStatements(cmdLine);
096: CommonArgs.addTransactionControL(cmdLine);
097:
098: cmdLine.addArgument(ARG_TYPE, StringUtil
099: .stringToList("text,xml"));
100: cmdLine.addArgument(ARG_UPDATE_WHERE);
101: cmdLine.addArgument(ARG_FILE);
102: cmdLine
103: .addArgument(ARG_TARGETTABLE,
104: ArgumentType.TableArgument);
105: cmdLine.addArgument(ARG_QUOTE);
106: cmdLine.addArgument(ARG_CONTAINSHEADER,
107: ArgumentType.BoolArgument);
108: cmdLine.addArgument(ARG_FILECOLUMNS);
109: cmdLine.addArgument(ARG_MODE, StringUtil.stringToList(
110: "insert;update;insert,update;update,insert", ";"));
111: cmdLine.addArgument(ARG_KEYCOLUMNS);
112: cmdLine.addArgument(ARG_DELETE_TARGET,
113: ArgumentType.BoolArgument);
114: cmdLine.addArgument(ARG_EMPTY_STRING_IS_NULL,
115: ArgumentType.BoolArgument);
116: cmdLine.addArgument(ARG_DECODE, ArgumentType.BoolArgument);
117: cmdLine.addArgument(ARG_IMPORTCOLUMNS);
118: cmdLine.addArgument(ARG_COL_FILTER);
119: cmdLine.addArgument(ARG_LINE_FILTER);
120: cmdLine.addArgument(ARG_DIRECTORY);
121: cmdLine.addArgument(ARG_TARGET_SCHEMA);
122: cmdLine
123: .addArgument(ARG_USE_TRUNCATE,
124: ArgumentType.BoolArgument);
125: cmdLine.addArgument(ARG_TRIM_VALUES, ArgumentType.BoolArgument);
126: cmdLine.addArgument(ARG_FILE_EXT);
127: cmdLine.addArgument(ARG_TRUNCATE_TABLE,
128: ArgumentType.BoolArgument);
129: cmdLine
130: .addArgument(ARG_CREATE_TABLE,
131: ArgumentType.BoolArgument);
132: cmdLine.addArgument(ARG_BLOB_ISFILENAME,
133: ArgumentType.BoolArgument);
134: cmdLine.addArgument(ARG_CLOB_ISFILENAME,
135: ArgumentType.BoolArgument);
136: cmdLine.addArgument(ARG_MULTI_LINE, ArgumentType.BoolArgument);
137: cmdLine
138: .addArgument(ARG_START_ROW,
139: ArgumentType.IntegerArgument);
140: cmdLine.addArgument(ARG_END_ROW, ArgumentType.IntegerArgument);
141: cmdLine.addArgument(ARG_BADFILE);
142: cmdLine.addArgument(ARG_CONSTANTS);
143: cmdLine.addArgument(ARG_COL_WIDTHS);
144: ModifierArguments.addArguments(cmdLine);
145: }
146:
147: public String getVerb() {
148: return VERB;
149: }
150:
151: private void addWrongParamsMessage(StatementRunnerResult result) {
152: if (WbManager.getInstance().isBatchMode())
153: return;
154: String msg = getWrongParamsMessage();
155: result.addMessageNewLine();
156: result.addMessage(msg);
157: result.setFailure();
158: }
159:
160: private String getWrongParamsMessage() {
161: String result = ResourceMgr
162: .getString("ErrImportWrongParameters");
163: result = StringUtil.replace(result, "%continue_default%",
164: Boolean.toString(getContinueDefault()));
165: result = StringUtil.replace(result, "%multiline_default%",
166: Boolean.toString(getMultiDefault()));
167: result = StringUtil.replace(result, "%header_default%", Boolean
168: .toString(getHeaderDefault()));
169: result = StringUtil.replace(result, "%trim_default%", Boolean
170: .toString(getTrimDefault()));
171: result = StringUtil.replace(result, "%default_encoding%",
172: Settings.getInstance().getDefaultDataEncoding());
173: return result;
174: }
175:
176: private boolean getContinueDefault() {
177: return Settings.getInstance().getBoolProperty(
178: "workbench.import.default.continue", false);
179: }
180:
181: private boolean getHeaderDefault() {
182: return Settings.getInstance().getBoolProperty(
183: "workbench.import.default.header", true);
184: }
185:
186: private boolean getMultiDefault() {
187: return Settings.getInstance().getBoolProperty(
188: "workbench.import.default.multilinerecord", false);
189: }
190:
191: private boolean getTrimDefault() {
192: return Settings.getInstance().getBoolProperty(
193: "workbench.import.default.trimvalues", false);
194: }
195:
196: public StatementRunnerResult execute(final String sqlCommand)
197: throws SQLException {
198: imp = new DataImporter();
199: this .imp.setConnection(currentConnection);
200:
201: StatementRunnerResult result = new StatementRunnerResult(
202: sqlCommand);
203: String options = getCommandLine(sqlCommand);
204:
205: cmdLine.parse(options);
206:
207: if (cmdLine.hasUnknownArguments()) {
208: setUnknownMessage(result, cmdLine, getWrongParamsMessage());
209: return result;
210: }
211:
212: if (!cmdLine.hasArguments()) {
213: addWrongParamsMessage(result);
214: return result;
215: }
216:
217: WbFile inputFile = evaluateFileArgument(cmdLine
218: .getValue(ARG_FILE));
219: String type = cmdLine.getValue(ARG_TYPE);
220: String dir = cmdLine.getValue(ARG_DIRECTORY);
221:
222: if (inputFile == null && dir == null) {
223: result.addMessage(ResourceMgr
224: .getString("ErrImportFileMissing"));
225: addWrongParamsMessage(result);
226: return result;
227: }
228:
229: if (type == null) {
230: type = findTypeFromFilename(inputFile.getFullPath());
231: }
232:
233: if (type == null) {
234: result.addMessage(ResourceMgr
235: .getString("ErrImportTypeMissing"));
236: addWrongParamsMessage(result);
237: return result;
238: }
239:
240: if (!type.equalsIgnoreCase("text")
241: && !type.equalsIgnoreCase("txt")
242: && !type.equalsIgnoreCase("xml")) {
243: result.addMessage(ResourceMgr
244: .getString("ErrImportInvalidType"));
245: result.setFailure();
246: return result;
247: }
248:
249: String badFile = cmdLine.getValue(ARG_BADFILE);
250: if (badFile != null) {
251: boolean multiFileImport = (dir != null && inputFile == null);
252: File bf = new File(badFile);
253: if (multiFileImport && !bf.isDirectory()) {
254: result.addMessage(ResourceMgr
255: .getString("ErrImportBadFileNoDir"));
256: result.setFailure();
257: return result;
258: }
259: }
260: CommonArgs.setCommitAndBatchParams(imp, cmdLine);
261:
262: boolean continueOnError = cmdLine.getBoolean(
263: CommonArgs.ARG_CONTINUE, getContinueDefault());
264: imp.setContinueOnError(continueOnError);
265:
266: String table = cmdLine.getValue(ARG_TARGETTABLE);
267: String schema = cmdLine.getValue(ARG_TARGET_SCHEMA);
268:
269: if (inputFile != null) {
270: if (!inputFile.exists()) {
271: String msg = ResourceMgr
272: .getString("ErrImportFileNotFound");
273: msg = StringUtil.replace(msg, "%filename%", inputFile
274: .getFullPath());
275: LogMgr.logError("WbImport.execute()", msg, null);
276: result.addMessage(msg);
277: result.setFailure();
278: return result;
279: }
280: } else {
281: File d = new File(dir);
282: if (!d.exists()) {
283: String msg = ResourceMgr
284: .getString("ErrImportSourceDirNotFound");
285: msg = StringUtil.replace(msg, "%dir%", dir);
286: LogMgr.logError("WbImport.execute()", msg, null);
287: result.addMessage(msg);
288: result.setFailure();
289: return result;
290: }
291: if (!d.isDirectory()) {
292: String msg = ResourceMgr.getString("ErrImportNoDir");
293: msg = StringUtil.replace(msg, "%dir%", dir);
294: LogMgr.logError("WbImport.execute()", msg, null);
295: result.addMessage(msg);
296: result.setFailure();
297: return result;
298: }
299: }
300:
301: String encoding = cmdLine.getValue(CommonArgs.ARG_ENCODING);
302: ImportFileParser parser = null;
303:
304: if ("text".equalsIgnoreCase(type)
305: || "txt".equalsIgnoreCase(type)) {
306: if (table == null && dir == null) {
307: String msg = ResourceMgr
308: .getString("ErrTextImportRequiresTableName");
309: LogMgr.logError("WbImport.execute()", msg, null);
310: result.addMessage(msg);
311: result.setFailure();
312: return result;
313: }
314:
315: TextFileParser textParser = new TextFileParser();
316: parser = textParser;
317:
318: textParser.setTableName(table);
319: if (inputFile != null) {
320: textParser.setInputFile(inputFile);
321: } else {
322: textParser.setSourceDirectory(dir);
323: String ext = cmdLine.getValue(ARG_FILE_EXT);
324: if (ext != null)
325: textParser.setSourceExtension(ext);
326: }
327:
328: boolean multi = cmdLine.getBoolean(ARG_MULTI_LINE,
329: getMultiDefault());
330: textParser.setEnableMultilineRecords(multi);
331: textParser.setTargetSchema(schema);
332: textParser.setConnection(currentConnection);
333: textParser.setAbortOnError(!continueOnError);
334: textParser.setTreatClobAsFilenames(cmdLine.getBoolean(
335: ARG_CLOB_ISFILENAME, false));
336:
337: String delimiter = StringUtil.trimQuotes(cmdLine
338: .getValue(CommonArgs.ARG_DELIM));
339: if (delimiter != null)
340: textParser.setDelimiter(delimiter);
341:
342: String quote = cmdLine.getValue(ARG_QUOTE);
343: if (quote != null)
344: textParser.setQuoteChar(quote);
345:
346: textParser.setTrimValues(cmdLine.getBoolean(
347: ARG_TRIM_VALUES, getTrimDefault()));
348: textParser.setDecodeUnicode(cmdLine.getBoolean(ARG_DECODE,
349: false));
350:
351: if (encoding != null)
352: textParser.setEncoding(encoding);
353:
354: textParser.setEmptyStringIsNull(cmdLine.getBoolean(
355: ARG_EMPTY_STRING_IS_NULL, true));
356:
357: if (dir == null) {
358: boolean headerDefault = Settings
359: .getInstance()
360: .getBoolProperty(
361: "workbench.import.default.header", true);
362: boolean header = cmdLine.getBoolean(ARG_CONTAINSHEADER,
363: headerDefault);
364: textParser.setContainsHeader(header);
365:
366: String importcolumns = cmdLine
367: .getValue(ARG_IMPORTCOLUMNS);
368: if (importcolumns != null) {
369: try {
370: List<String> cols = StringUtil.stringToList(
371: importcolumns, ",", true, true);
372: textParser.setImportColumnNames(cols);
373: } catch (IllegalArgumentException e) {
374: result.addMessage(textParser.getMessages());
375: result.setFailure();
376: return result;
377: }
378: }
379:
380: String filecolumns = cmdLine.getValue(ARG_FILECOLUMNS);
381: if (filecolumns != null) {
382: List<String> cols = StringUtil.stringToList(
383: filecolumns, ",", true, true);
384: try {
385: List<ColumnIdentifier> colIds = new ArrayList<ColumnIdentifier>(
386: cols.size());
387: for (String colname : cols) {
388: ColumnIdentifier col = new ColumnIdentifier(
389: colname);
390: if (!colname
391: .equals(RowDataProducer.SKIP_INDICATOR)
392: && colIds.contains(col)) {
393: String msg = ResourceMgr
394: .getFormattedString(
395: "ErrImpDupColumn",
396: colname);
397: LogMgr.logError("WbImport.execute()",
398: msg, null);
399: result.addMessage(msg);
400: result.setFailure();
401: return result;
402: }
403: colIds.add(col);
404: }
405: textParser.setColumns(colIds, true);
406: } catch (Exception e) {
407: result.addMessage(textParser.getMessages());
408: result.setFailure();
409: return result;
410: }
411: }
412:
413: if (filecolumns == null) {
414: // read column definition from header line
415: // if no header was specified, the text parser
416: // will assume the columns in the text file
417: // map to the column in the target table
418: try {
419: textParser.setupFileColumns();
420: } catch (Exception e) {
421: result.setFailure();
422: result.addMessage(textParser.getMessages());
423: LogMgr.logError("WbImport.execute()",
424: ExceptionUtil.getDisplay(e), null);
425: return result;
426: }
427: }
428:
429: // The column filter has to bee applied after the
430: // columns are defined!
431: String filter = cmdLine.getValue(ARG_COL_FILTER);
432: if (filter != null) {
433: addColumnFilter(filter, textParser);
434: }
435:
436: } else {
437: // source directory specified --> Assume files contain headers
438: textParser.setContainsHeader(cmdLine.getBoolean(
439: ARG_CONTAINSHEADER, true));
440: }
441:
442: textParser.setTreatBlobsAsFilenames(cmdLine.getBoolean(
443: ARG_BLOB_ISFILENAME, true));
444:
445: String filter = cmdLine.getValue(ARG_LINE_FILTER);
446: if (filter != null) {
447: textParser.setLineFilter(StringUtil.trimQuotes(filter));
448: }
449: textParser.setQuoteEscaping(CommonArgs
450: .getQuoteEscaping(cmdLine));
451:
452: // when all columns are defined we can check for a fixed-width import
453: String width = cmdLine.getValue(ARG_COL_WIDTHS);
454: if (!StringUtil.isEmptyString(width)) {
455: try {
456: ColumnWidthDefinition def = new ColumnWidthDefinition(
457: width);
458: textParser.setColumnWidths(def.getColumnWidths());
459: } catch (MissingWidthDefinition e) {
460: result.addMessage(ResourceMgr.getFormattedString(
461: "ErrImpWrongWidth", e.getColumnName()));
462: result.setFailure();
463: return result;
464: }
465: }
466:
467: imp.setProducer(textParser);
468: } else if ("xml".equalsIgnoreCase(type)) {
469: XmlDataFileParser xmlParser = new XmlDataFileParser();
470: xmlParser.setConnection(currentConnection);
471: xmlParser.setAbortOnError(!continueOnError);
472: parser = xmlParser;
473:
474: // The encoding must be set as early as possible
475: // as the XmlDataFileParser might need it to read
476: // the table structure!
477: if (encoding != null)
478: xmlParser.setEncoding(encoding);
479:
480: if (dir != null) {
481: String ext = cmdLine.getValue(ARG_FILE_EXT);
482: if (ext != null)
483: xmlParser.setSourceExtension(ext);
484: xmlParser.setSourceDirectory(dir);
485: } else {
486: xmlParser.setSourceFile(inputFile);
487: if (table != null)
488: xmlParser.setTableName(table);
489: String cols = cmdLine.getValue(ARG_IMPORTCOLUMNS);
490: if (cols != null) {
491: try {
492: xmlParser.setColumns(cols);
493: } catch (Exception e) {
494: result.setFailure();
495: String col = xmlParser.getMissingColumn();
496: String msg = ResourceMgr.getString(
497: "ErrImportColumnNotFound").replaceAll(
498: "%name%", col);
499: result.addMessage(msg);
500: LogMgr
501: .logError("WbImport.execute()", msg,
502: null);
503: return result;
504: }
505: }
506: }
507:
508: imp.setCreateTarget(cmdLine.getBoolean(ARG_CREATE_TABLE,
509: false));
510: imp.setProducer(xmlParser);
511: }
512:
513: ValueConverter converter = null;
514: try {
515: converter = CommonArgs.getConverter(cmdLine);
516: } catch (Exception e) {
517: result.setFailure();
518: result.addMessage(e.getMessage());
519: return result;
520: }
521:
522: imp.setTransactionControl(cmdLine.getBoolean(
523: CommonArgs.ARG_TRANS_CONTROL, true));
524:
525: RowDataProducer prod = imp.getProducer();
526: if (prod != null) {
527: prod.setValueConverter(converter);
528: prod.setCheckDependencies(cmdLine
529: .getBoolean(CommonArgs.ARG_CHECK_FK_DEPS));
530: }
531:
532: try {
533: // Column Modifiers will only be evaluated for
534: // single file imports to avoid confusion of columns
535: if (dir == null) {
536: ModifierArguments args = new ModifierArguments(cmdLine);
537: parser.setValueModifier(args.getModifier());
538: }
539: } catch (NumberFormatException e) {
540: result.addMessage(ResourceMgr
541: .getString("ErrImportWrongLimit"));
542: result.setFailure();
543: return result;
544: }
545:
546: if (badFile != null)
547: imp.setBadfileName(badFile);
548:
549: String value = cmdLine.getValue(CommonArgs.ARG_PROGRESS);
550: if (value == null && inputFile != null) {
551: int interval = DataImporter
552: .estimateReportIntervalFromFileSize(inputFile);
553: imp.setReportInterval(interval);
554: } else if ("true".equalsIgnoreCase(value)) {
555: this .imp.setReportInterval(1);
556: } else if ("false".equalsIgnoreCase(value)) {
557: this .imp.setReportInterval(0);
558: } else if (value != null) {
559: int interval = StringUtil.getIntValue(value, 0);
560: this .imp.setReportInterval(interval);
561: } else {
562: this .imp.setReportInterval(10);
563: }
564:
565: String constants = cmdLine.getValue(ARG_CONSTANTS);
566: if (!StringUtil.isEmptyString(constants)) {
567: try {
568: ConstantColumnValues values = new ConstantColumnValues(
569: constants, this .currentConnection, table,
570: converter);
571: imp.setConstantColumnValues(values);
572: } catch (Exception e) {
573: LogMgr.logError("WbImport.execute()",
574: "Column constants could no be parsed", e);
575: result.setFailure();
576: result.addMessage(e.getMessage());
577: return result;
578: }
579: }
580:
581: String mode = cmdLine.getValue(ARG_MODE);
582: if (mode != null) {
583: if (!imp.setMode(mode)) {
584: result.addMessage(ResourceMgr.getString(
585: "ErrInvalidModeIgnored").replaceAll("%mode%",
586: mode));
587: }
588: }
589:
590: String where = cmdLine.getValue(ARG_UPDATE_WHERE);
591: imp.setWhereClauseForUpdate(where);
592:
593: if (schema != null) {
594: imp.setTargetSchema(schema);
595: }
596:
597: String keyColumns = cmdLine.getValue(ARG_KEYCOLUMNS);
598: imp.setKeyColumns(keyColumns);
599:
600: boolean delete = false;
601: boolean useTruncate = false;
602:
603: if (cmdLine.isArgPresent(ARG_TRUNCATE_TABLE)) {
604: delete = cmdLine.getBoolean(ARG_TRUNCATE_TABLE);
605: if (delete)
606: useTruncate = true;
607: } else {
608: delete = cmdLine.getBoolean(ARG_DELETE_TARGET);
609: useTruncate = cmdLine.getBoolean(ARG_USE_TRUNCATE, false);
610: }
611:
612: imp.setDeleteTarget(delete);
613: if (delete) {
614: imp.setUseTruncate(useTruncate);
615: }
616:
617: int startRow = cmdLine.getIntValue(ARG_START_ROW, -1);
618: if (startRow > 0)
619: imp.setStartRow(startRow);
620:
621: int endRow = cmdLine.getIntValue(ARG_END_ROW, -1);
622: if (endRow > 0)
623: imp.setEndRow(endRow);
624:
625: imp.setRowActionMonitor(this .rowMonitor);
626: imp.setPerTableStatements(new TableStatements(cmdLine));
627:
628: try {
629: imp.startImport();
630: if (imp.isSuccess()) {
631: result.setSuccess();
632: } else {
633: result.setFailure();
634: }
635: result.setWarning(imp.hasWarnings());
636: } catch (CycleErrorException e) {
637: // Logging already done.
638: result.setFailure();
639: } catch (SQLException e) {
640: LogMgr.logError("WbImport.execute()", "Error importing '"
641: + inputFile, e);
642: result.setFailure();
643: } catch (ConverterException e) {
644: LogMgr.logError("WbImport.execute()", "Error importing '"
645: + inputFile, e);
646: result.setFailure();
647: } catch (ParsingInterruptedException e) {
648: // Logging already done by DataImporter
649: result.setFailure();
650: } catch (Exception e) {
651: LogMgr.logError("WbImport.execute()", "Error importing '"
652: + inputFile + "': " + e.getMessage(), e);
653: result.setFailure();
654: addErrorInfo(result, sqlCommand, e);
655: }
656: result.addMessage(imp.getMessages());
657:
658: return result;
659: }
660:
661: private void addColumnFilter(String filters,
662: TextFileParser textParser) {
663: if (filters == null || filters.trim().length() == 0)
664: return;
665:
666: List<String> filterList = StringUtil.stringToList(filters, ",",
667: false);
668:
669: if (filterList.size() < 1)
670: return;
671:
672: for (String filterDef : filterList) {
673: List<String> l = StringUtil.stringToList(filterDef, "=",
674: true);
675: if (l.size() != 2)
676: continue;
677:
678: String col = l.get(0);
679: String regex = l.get(1);
680: textParser.addColumnFilter(col, StringUtil
681: .trimQuotes(regex));
682: }
683: }
684:
685: public void done() {
686: super .done();
687: this .imp = null;
688: }
689:
690: public void cancel() throws SQLException {
691: super .cancel();
692: if (this .imp != null) {
693: this .imp.cancelExecution();
694: }
695: }
696:
697: protected String findTypeFromFilename(String fname) {
698: if (fname == null)
699: return null;
700: if (fname.toLowerCase().endsWith(".txt"))
701: return "text";
702: if (fname.toLowerCase().endsWith(".xml"))
703: return "xml";
704: if (fname.toLowerCase().endsWith(".text"))
705: return "text";
706: if (fname.toLowerCase().endsWith(".csv"))
707: return "text";
708: return null;
709: }
710: }
|