@Test public void testWriteFormatFile() throws Exception { TestUtil util = getTestUtil(); final WbFile export = new WbFile(util.getBaseDir(), "export.txt"); DataExporter exporter = new DataExporter(null) { @Override public String getFullOutputFilename() { return export.getFullPath(); } @Override public boolean getExportHeaders() { return true; } @Override public String getTextDelimiter() { return "\t"; } @Override public String getTextQuoteChar() { return "\""; } }; ColumnIdentifier id = new ColumnIdentifier("id", Types.INTEGER, true); ColumnIdentifier firstname = new ColumnIdentifier("firstname", Types.VARCHAR); ColumnIdentifier lastname = new ColumnIdentifier("lastname", Types.VARCHAR); final TableIdentifier table = new TableIdentifier("person"); final ResultInfo info = new ResultInfo(new ColumnIdentifier[] {id, firstname, lastname}); info.setUpdateTable(table); RowDataConverter converter = new RowDataConverter() { @Override public ResultInfo getResultInfo() { return info; } @Override public StringBuilder convertRowData(RowData row, long rowIndex) { return new StringBuilder(); } @Override public StringBuilder getStart() { return null; } @Override public StringBuilder getEnd(long totalRows) { return null; } }; try { SqlServerFormatFileWriter writer = new SqlServerFormatFileWriter(); writer.writeFormatFile(exporter, converter); WbFile formatFile = new WbFile(util.getBaseDir(), "export.fmt"); assertTrue(formatFile.exists()); List<String> contents = StringUtil.readLines(formatFile); assertEquals("7.0", contents.get(0)); assertEquals("3", contents.get(1)); assertEquals("1 SQLCHAR 0 0 \"\\t\" 1 id", contents.get(2).trim()); assertEquals("2 SQLCHAR 0 0 \"\\t\" 2 firstname", contents.get(3).trim()); assertEquals("3 SQLCHAR 0 0 \"\\n\" 3 lastname", contents.get(4).trim()); } finally { util.emptyBaseDirectory(); } }
@Test public void testWriteFormatFile() throws Exception { TestUtil util = getTestUtil(); final WbFile export = new WbFile(util.getBaseDir(), "export.txt"); DataExporter exporter = new DataExporter(null) { @Override public String getFullOutputFilename() { return export.getFullPath(); } @Override public boolean getExportHeaders() { return true; } @Override public String getTextDelimiter() { return "\t"; } @Override public String getTextQuoteChar() { return "\""; } @Override public String getEncoding() { return "UTF-8"; } @Override public String getTableNameToUse() { return "person"; } }; ColumnIdentifier id = new ColumnIdentifier("id", Types.INTEGER, true); ColumnIdentifier firstname = new ColumnIdentifier("firstname", Types.VARCHAR); ColumnIdentifier lastname = new ColumnIdentifier("lastname", Types.VARCHAR); final TableIdentifier table = new TableIdentifier("person"); final ResultInfo info = new ResultInfo(new ColumnIdentifier[] {id, firstname, lastname}); info.setUpdateTable(table); RowDataConverter converter = new RowDataConverter() { @Override public ResultInfo getResultInfo() { return info; } @Override public StringBuilder convertRowData(RowData row, long rowIndex) { return new StringBuilder(); } @Override public StringBuilder getStart() { return null; } @Override public StringBuilder getEnd(long totalRows) { return null; } }; try { PostgresCopyStatementWriter writer = new PostgresCopyStatementWriter(); writer.writeFormatFile(exporter, converter); WbFile formatFile = new WbFile(util.getBaseDir(), "import_export.sql"); assertTrue(formatFile.exists()); List<String> contents = StringUtil.readLines(formatFile); assertNotNull(contents); assertEquals(3, contents.size()); assertEquals("copy person (id, firstname, lastname)", contents.get(0).trim()); assertEquals("from '" + export.getFullPath() + "'", contents.get(1).trim()); String expected = "with (format csv, header true, quote '\"', delimiter '\t', encoding 'UTF-8', null '');"; assertEquals(expected, contents.get(2).trim()); } finally { util.emptyBaseDirectory(); } }
@Override public StatementRunnerResult execute(String aSql) throws SQLException { StatementRunnerResult result = new StatementRunnerResult(); String parm = SqlUtil.stripVerb(aSql); cmdLine.parse(parm); WbFile inputFile = evaluateFileArgument(cmdLine.getValue(ARG_INPUT)); WbFile outputFile = evaluateFileArgument(cmdLine.getValue(ARG_OUTPUT)); WbFile xsltFile = evaluateFileArgument(cmdLine.getValue(ARG_STYLESHEET)); if (!cmdLine.hasArguments()) { result.addErrorMessageByKey("ErrXsltWrongParameter"); return result; } if (inputFile == null) { result.addErrorMessageByKey("ErrXsltMissingInputFile"); return result; } if (!inputFile.exists()) { result.addErrorMessageByKey("ErrFileNotFound", cmdLine.getValue(ARG_INPUT)); return result; } if (outputFile == null) { result.addErrorMessageByKey("ErrXsltMissingOutputFile"); return result; } if (xsltFile == null) { result.addErrorMessageByKey("ErrXsltMissingStylesheet"); return result; } if (!xsltFile.exists()) { result.addErrorMessageByKey("ErrFileNotFound", cmdLine.getValue(ARG_STYLESHEET)); return result; } Map<String, String> params = getParameters(cmdLine); XsltTransformer transformer = new XsltTransformer(); try { transformer.setSaveSystemOutMessages(true); transformer.setXsltBaseDir(getXsltBaseDir()); transformer.transform(inputFile, outputFile, xsltFile, params); String msg = transformer.getAllOutputs(); if (msg.length() != 0) { result.addMessage(msg); result.addMessage(""); // create newline } WbFile xsltUsed = new WbFile(transformer.getXsltUsed()); WbFile userXslt = new WbFile(xsltFile); if (xsltUsed != null && !userXslt.equals(xsltUsed)) { // If the xslt file has been "automatically" found, inform the user about this result.addMessage(ResourceMgr.getFormattedString("MsgXsltUsed", xsltUsed.getFullPath())); } result.addMessage(ResourceMgr.getFormattedString("MsgXsltSuccessful", outputFile)); result.setSuccess(); } catch (Exception e) { LogMgr.logError( "WbXslt.execute()", "Error when transforming '" + inputFile + "' to '" + outputFile + "' using " + xsltFile, e); String msg = transformer.getAllOutputs(e); LogMgr.logError("WbXslt.execute()", msg, null); result.addErrorMessage(msg); } return result; }