Created
July 17, 2014 13:19
-
-
Save kaspersorensen/4096c418441eee125685 to your computer and use it in GitHub Desktop.
MetaModel fix for CSV file CREATE TABLE statements on configurations with no header lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/csv/src/main/java/org/apache/metamodel/csv/CsvCreateTableBuilder.java b/csv/src/main/java/org/apache/metamodel/csv/CsvCreateTableBuilder.java | |
| index c8eb569..cd0bb38 100644 | |
| --- a/csv/src/main/java/org/apache/metamodel/csv/CsvCreateTableBuilder.java | |
| +++ b/csv/src/main/java/org/apache/metamodel/csv/CsvCreateTableBuilder.java | |
| @@ -38,7 +38,12 @@ final class CsvCreateTableBuilder extends AbstractTableCreationBuilder<CsvUpdate | |
| MutableTable table = getTable(); | |
| String[] columnNames = table.getColumnNames(); | |
| - csvUpdateCallback.writeRow(columnNames, false); | |
| + | |
| + CsvDataContext csvDataContext = (CsvDataContext) csvUpdateCallback.getDataContext(); | |
| + | |
| + if (csvDataContext.getConfiguration().getColumnNameLineNumber() != CsvConfiguration.NO_COLUMN_NAME_LINE) { | |
| + csvUpdateCallback.writeRow(columnNames, false); | |
| + } | |
| CsvSchema schema = (CsvSchema) table.getSchema(); | |
| CsvTable csvTable = new CsvTable(schema, table.getName(), table.getColumnNames()); | |
| diff --git a/csv/src/test/java/org/apache/metamodel/csv/CsvDataContextTest.java b/csv/src/test/java/org/apache/metamodel/csv/CsvDataContextTest.java | |
| index 7a53723..e417cf6 100644 | |
| --- a/csv/src/test/java/org/apache/metamodel/csv/CsvDataContextTest.java | |
| +++ b/csv/src/test/java/org/apache/metamodel/csv/CsvDataContextTest.java | |
| @@ -56,15 +56,51 @@ public class CsvDataContextTest extends TestCase { | |
| private final CsvConfiguration semicolonConfiguration = new CsvConfiguration( | |
| CsvConfiguration.DEFAULT_COLUMN_NAME_LINE, "UTF-8", ';', '\'', CsvConfiguration.DEFAULT_ESCAPE_CHAR); | |
| + public void testEmptyFileNoColumnHeaderLine() throws Exception { | |
| + final File file = new File("target/testEmptyFileNoColumnHeaderLine.csv"); | |
| + FileHelper.copy(new File("src/test/resources/empty_file.csv"), file); | |
| + | |
| + CsvConfiguration csvConfiguration = new CsvConfiguration(CsvConfiguration.NO_COLUMN_NAME_LINE, | |
| + FileHelper.DEFAULT_ENCODING, CsvConfiguration.DEFAULT_SEPARATOR_CHAR, CsvConfiguration.NOT_A_CHAR, | |
| + CsvConfiguration.DEFAULT_ESCAPE_CHAR); | |
| + final CsvDataContext dc = new CsvDataContext(file, csvConfiguration); | |
| + assertEquals(1, dc.getDefaultSchema().getTableCount()); | |
| + | |
| + dc.executeUpdate(new UpdateScript() { | |
| + | |
| + @Override | |
| + public void run(UpdateCallback callback) { | |
| + callback.createTable(dc.getDefaultSchema(), "new_table").withColumn("COL_1").withColumn("COL_2") | |
| + .execute(); | |
| + callback.insertInto("new_table").value(0, "1").value(1, 2).execute(); | |
| + } | |
| + }); | |
| + | |
| + CsvDataContext dc1 = new CsvDataContext(file, csvConfiguration); | |
| + | |
| + Table[] tables = dc1.getDefaultSchema().getTables(); | |
| + assertEquals(1, tables.length); | |
| + | |
| + Table table = tables[0]; | |
| + assertEquals("testEmptyFileNoColumnHeaderLine.csv", table.getName()); | |
| + assertEquals(2, table.getColumnCount()); | |
| + | |
| + DataSet ds = dc1.query().from(table).selectAll().execute(); | |
| + assertTrue(ds.next()); | |
| + assertEquals("Row[values=[1, 2]]", ds.getRow().toString()); | |
| + assertFalse(ds.next()); | |
| + ds.close(); | |
| + } | |
| + | |
| public void testEmptyFileTableCreation() throws Exception { | |
| - final File file = new File("target/empty_file.csv"); | |
| + final File file = new File("target/testEmptyFileNoColumnHeaderLine.csv"); | |
| FileHelper.copy(new File("src/test/resources/empty_file.csv"), file); | |
| final CsvDataContext dc = new CsvDataContext(file); | |
| assertEquals(1, dc.getDefaultSchema().getTableCount()); | |
| final Table table1 = dc.getDefaultSchema().getTables()[0]; | |
| - assertEquals("empty_file.csv", table1.getName()); | |
| + assertEquals("testEmptyFileNoColumnHeaderLine.csv", table1.getName()); | |
| assertEquals(0, table1.getColumnCount()); | |
| dc.executeUpdate(new UpdateScript() { | |
| @@ -87,7 +123,7 @@ public class CsvDataContextTest extends TestCase { | |
| }); | |
| assertEquals("\"bar\",\"baz\"", FileHelper.readFileAsString(file)); | |
| - | |
| + | |
| // still the table count should only be 1 | |
| assertEquals(1, dc.getDefaultSchema().getTableCount()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment