Showing
1 changed file
with
14 additions
and
10 deletions
... | ... | @@ -45,11 +45,7 @@ public class SqlDbHelper { |
45 | 45 | public static Path dumpTableIfExists(Connection conn, String tableName, |
46 | 46 | String[] columns, String[] defaultValues, String dumpPrefix, boolean printHeader) throws Exception { |
47 | 47 | |
48 | - DatabaseMetaData metaData = conn.getMetaData(); | |
49 | - ResultSet res = metaData.getTables(null, null, tableName, | |
50 | - new String[] {"TABLE"}); | |
51 | - if (res.next()) { | |
52 | - res.close(); | |
48 | + if (tableExists(conn, tableName)) { | |
53 | 49 | Path dumpFile = Files.createTempFile(dumpPrefix, null); |
54 | 50 | Files.deleteIfExists(dumpFile); |
55 | 51 | CSVFormat csvFormat = CSV_DUMP_FORMAT; |
... | ... | @@ -61,9 +57,9 @@ public class SqlDbHelper { |
61 | 57 | try (ResultSet tableRes = stmt.executeQuery()) { |
62 | 58 | ResultSetMetaData resMetaData = tableRes.getMetaData(); |
63 | 59 | Map<String, Integer> columnIndexMap = new HashMap<>(); |
64 | - for (int i = 0; i < resMetaData.getColumnCount(); i++) { | |
60 | + for (int i = 1; i <= resMetaData.getColumnCount(); i++) { | |
65 | 61 | String columnName = resMetaData.getColumnName(i); |
66 | - columnIndexMap.put(columnName, i); | |
62 | + columnIndexMap.put(columnName.toUpperCase(), i); | |
67 | 63 | } |
68 | 64 | while(tableRes.next()) { |
69 | 65 | dumpRow(tableRes, columnIndexMap, columns, defaultValues, csvPrinter); |
... | ... | @@ -77,6 +73,15 @@ public class SqlDbHelper { |
77 | 73 | } |
78 | 74 | } |
79 | 75 | |
76 | + private static boolean tableExists(Connection conn, String tableName) { | |
77 | + try (Statement stmt = conn.createStatement()) { | |
78 | + stmt.executeQuery("select * from " + tableName + " where 1=0"); | |
79 | + return true; | |
80 | + } catch (Exception e) { | |
81 | + return false; | |
82 | + } | |
83 | + } | |
84 | + | |
80 | 85 | public static void loadTable(Connection conn, String tableName, String[] columns, Path sourceFile) throws Exception { |
81 | 86 | loadTable(conn, tableName, columns, sourceFile, false); |
82 | 87 | } |
... | ... | @@ -89,7 +94,6 @@ public class SqlDbHelper { |
89 | 94 | csvFormat = CSV_DUMP_FORMAT.withHeader(columns); |
90 | 95 | } |
91 | 96 | try (PreparedStatement prepared = conn.prepareStatement(createInsertStatement(tableName, columns))) { |
92 | - prepared.getParameterMetaData(); | |
93 | 97 | try (CSVParser csvParser = new CSVParser(Files.newBufferedReader(sourceFile), csvFormat)) { |
94 | 98 | csvParser.forEach(record -> { |
95 | 99 | try { |
... | ... | @@ -122,13 +126,13 @@ public class SqlDbHelper { |
122 | 126 | } |
123 | 127 | |
124 | 128 | private static String getColumnValue(String column, String defaultValue, Map<String, Integer> columnIndexMap, ResultSet res) { |
125 | - int index = columnIndexMap.containsKey(column) ? columnIndexMap.get(column) : -1; | |
129 | + int index = columnIndexMap.containsKey(column.toUpperCase()) ? columnIndexMap.get(column.toUpperCase()) : -1; | |
126 | 130 | if (index > -1) { |
127 | 131 | String str; |
128 | 132 | try { |
129 | 133 | Object obj = res.getObject(index); |
130 | 134 | if (obj == null) { |
131 | - str = ""; | |
135 | + return null; | |
132 | 136 | } else { |
133 | 137 | str = obj.toString(); |
134 | 138 | } | ... | ... |