Showing
1 changed file
with
14 additions
and
10 deletions
@@ -45,11 +45,7 @@ public class SqlDbHelper { | @@ -45,11 +45,7 @@ public class SqlDbHelper { | ||
45 | public static Path dumpTableIfExists(Connection conn, String tableName, | 45 | public static Path dumpTableIfExists(Connection conn, String tableName, |
46 | String[] columns, String[] defaultValues, String dumpPrefix, boolean printHeader) throws Exception { | 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 | Path dumpFile = Files.createTempFile(dumpPrefix, null); | 49 | Path dumpFile = Files.createTempFile(dumpPrefix, null); |
54 | Files.deleteIfExists(dumpFile); | 50 | Files.deleteIfExists(dumpFile); |
55 | CSVFormat csvFormat = CSV_DUMP_FORMAT; | 51 | CSVFormat csvFormat = CSV_DUMP_FORMAT; |
@@ -61,9 +57,9 @@ public class SqlDbHelper { | @@ -61,9 +57,9 @@ public class SqlDbHelper { | ||
61 | try (ResultSet tableRes = stmt.executeQuery()) { | 57 | try (ResultSet tableRes = stmt.executeQuery()) { |
62 | ResultSetMetaData resMetaData = tableRes.getMetaData(); | 58 | ResultSetMetaData resMetaData = tableRes.getMetaData(); |
63 | Map<String, Integer> columnIndexMap = new HashMap<>(); | 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 | String columnName = resMetaData.getColumnName(i); | 61 | String columnName = resMetaData.getColumnName(i); |
66 | - columnIndexMap.put(columnName, i); | 62 | + columnIndexMap.put(columnName.toUpperCase(), i); |
67 | } | 63 | } |
68 | while(tableRes.next()) { | 64 | while(tableRes.next()) { |
69 | dumpRow(tableRes, columnIndexMap, columns, defaultValues, csvPrinter); | 65 | dumpRow(tableRes, columnIndexMap, columns, defaultValues, csvPrinter); |
@@ -77,6 +73,15 @@ public class SqlDbHelper { | @@ -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 | public static void loadTable(Connection conn, String tableName, String[] columns, Path sourceFile) throws Exception { | 85 | public static void loadTable(Connection conn, String tableName, String[] columns, Path sourceFile) throws Exception { |
81 | loadTable(conn, tableName, columns, sourceFile, false); | 86 | loadTable(conn, tableName, columns, sourceFile, false); |
82 | } | 87 | } |
@@ -89,7 +94,6 @@ public class SqlDbHelper { | @@ -89,7 +94,6 @@ public class SqlDbHelper { | ||
89 | csvFormat = CSV_DUMP_FORMAT.withHeader(columns); | 94 | csvFormat = CSV_DUMP_FORMAT.withHeader(columns); |
90 | } | 95 | } |
91 | try (PreparedStatement prepared = conn.prepareStatement(createInsertStatement(tableName, columns))) { | 96 | try (PreparedStatement prepared = conn.prepareStatement(createInsertStatement(tableName, columns))) { |
92 | - prepared.getParameterMetaData(); | ||
93 | try (CSVParser csvParser = new CSVParser(Files.newBufferedReader(sourceFile), csvFormat)) { | 97 | try (CSVParser csvParser = new CSVParser(Files.newBufferedReader(sourceFile), csvFormat)) { |
94 | csvParser.forEach(record -> { | 98 | csvParser.forEach(record -> { |
95 | try { | 99 | try { |
@@ -122,13 +126,13 @@ public class SqlDbHelper { | @@ -122,13 +126,13 @@ public class SqlDbHelper { | ||
122 | } | 126 | } |
123 | 127 | ||
124 | private static String getColumnValue(String column, String defaultValue, Map<String, Integer> columnIndexMap, ResultSet res) { | 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 | if (index > -1) { | 130 | if (index > -1) { |
127 | String str; | 131 | String str; |
128 | try { | 132 | try { |
129 | Object obj = res.getObject(index); | 133 | Object obj = res.getObject(index); |
130 | if (obj == null) { | 134 | if (obj == null) { |
131 | - str = ""; | 135 | + return null; |
132 | } else { | 136 | } else { |
133 | str = obj.toString(); | 137 | str = obj.toString(); |
134 | } | 138 | } |