diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2019-01-29 02:04:49 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2019-01-29 02:04:49 +0000 |
commit | a06385c7c985f31561ad52a4643419ab57222e37 (patch) | |
tree | a35f2791a97155bbd655a4dbe56724a30fdc454a | |
parent | cb9ed9bd0bee33de31c4c3323dc46cac53be2745 (diff) | |
download | jackcess-a06385c7c985f31561ad52a4643419ab57222e37.tar.gz jackcess-a06385c7c985f31561ad52a4643419ab57222e37.zip |
Use column label from ResultSetMetaData when importing. Fixes #152
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1268 f203690c-595d-4dc9-a70b-905162fa7fd2
-rw-r--r-- | src/changes/changes.xml | 3 | ||||
-rw-r--r-- | src/main/java/com/healthmarketscience/jackcess/util/ImportUtil.java | 88 | ||||
-rw-r--r-- | src/test/java/com/healthmarketscience/jackcess/util/ImportTest.java | 15 |
3 files changed, 55 insertions, 51 deletions
diff --git a/src/changes/changes.xml b/src/changes/changes.xml index caa9288..4d5fea7 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -8,6 +8,9 @@ <action dev="jahlborn" type="fix"> Fix parsing of escaped double quotes in expressions. </action> + <action dev="jahlborn" type="fix" system="SourceForge2" issue="152"> + Use column label from ResultSetMetaData when importing. + </action> </release> <release version="2.2.2" date="2019-01-05"> <action dev="jahlborn" type="fix" system="SourceForge2" issue="151"> diff --git a/src/main/java/com/healthmarketscience/jackcess/util/ImportUtil.java b/src/main/java/com/healthmarketscience/jackcess/util/ImportUtil.java index 61e91b9..8cfa9f9 100644 --- a/src/main/java/com/healthmarketscience/jackcess/util/ImportUtil.java +++ b/src/main/java/com/healthmarketscience/jackcess/util/ImportUtil.java @@ -47,14 +47,14 @@ import com.healthmarketscience.jackcess.impl.DatabaseImpl; * @author James Ahlborn * @usage _general_class_ */ -public class ImportUtil +public class ImportUtil { /** Batch commit size for copying other result sets into this database */ private static final int COPY_TABLE_BATCH_SIZE = 200; /** the platform line separator */ static final String LINE_SEPARATOR = System.getProperty("line.separator"); - + private ImportUtil() {} /** @@ -69,7 +69,7 @@ public class ImportUtil { List<ColumnBuilder> columns = new LinkedList<ColumnBuilder>(); for (int i = 1; i <= md.getColumnCount(); i++) { - ColumnBuilder column = new ColumnBuilder(md.getColumnName(i)) + ColumnBuilder column = new ColumnBuilder(md.getColumnLabel(i)) .escapeName(); int lengthInUnits = md.getColumnDisplaySize(i); column.setSQLType(md.getColumnType(i), lengthInUnits); @@ -100,7 +100,7 @@ public class ImportUtil * <p> * Equivalent to: * {@code importResultSet(source, db, name, SimpleImportFilter.INSTANCE);} - * + * * @param name Name of the new table to create * @param source ResultSet to copy from * @@ -115,13 +115,13 @@ public class ImportUtil { return importResultSet(source, db, name, SimpleImportFilter.INSTANCE); } - + /** * Copy an existing JDBC ResultSet into a new table in this database. * <p> * Equivalent to: * {@code importResultSet(source, db, name, filter, false);} - * + * * @param name Name of the new table to create * @param source ResultSet to copy from * @param filter valid import filter @@ -137,11 +137,11 @@ public class ImportUtil { return importResultSet(source, db, name, filter, false); } - + /** * Copy an existing JDBC ResultSet into a new (or optionally existing) table * in this database. - * + * * @param name Name of the new table to create * @param source ResultSet to copy from * @param filter valid import filter @@ -150,7 +150,7 @@ public class ImportUtil * name * * @return the name of the imported table - * + * * @see Builder */ public static String importResultSet(ResultSet source, Database db, @@ -191,13 +191,13 @@ public class ImportUtil return table.getName(); } - + /** * Copy a delimited text file into a new table in this database. * <p> * Equivalent to: * {@code importFile(f, name, db, delim, SimpleImportFilter.INSTANCE);} - * + * * @param name Name of the new table to create * @param f Source file to import * @param delim Regular expression representing the delimiter string. @@ -219,7 +219,7 @@ public class ImportUtil * <p> * Equivalent to: * {@code importFile(f, name, db, delim, "'", filter, false);} - * + * * @param name Name of the new table to create * @param f Source file to import * @param delim Regular expression representing the delimiter string. @@ -243,7 +243,7 @@ public class ImportUtil * <p> * Equivalent to: * {@code importReader(new BufferedReader(new FileReader(f)), db, name, delim, "'", filter, useExistingTable, true);} - * + * * @param name Name of the new table to create * @param f Source file to import * @param delim Regular expression representing the delimiter string. @@ -258,8 +258,8 @@ public class ImportUtil * @see #importReader(BufferedReader,Database,String,String,ImportFilter,boolean) * @see Builder */ - public static String importFile(File f, Database db, String name, - String delim, char quote, + public static String importFile(File f, Database db, String name, + String delim, char quote, ImportFilter filter, boolean useExistingTable) throws IOException @@ -272,7 +272,7 @@ public class ImportUtil * <p> * Equivalent to: * {@code importReader(new BufferedReader(new FileReader(f)), db, name, delim, "'", filter, useExistingTable, header);} - * + * * @param name Name of the new table to create * @param f Source file to import * @param delim Regular expression representing the delimiter string. @@ -288,8 +288,8 @@ public class ImportUtil * @see #importReader(BufferedReader,Database,String,String,char,ImportFilter,boolean,boolean) * @see Builder */ - public static String importFile(File f, Database db, String name, - String delim, char quote, + public static String importFile(File f, Database db, String name, + String delim, char quote, ImportFilter filter, boolean useExistingTable, boolean header) @@ -298,7 +298,7 @@ public class ImportUtil BufferedReader in = null; try { in = new BufferedReader(new FileReader(f)); - return importReader(in, db, name, delim, quote, filter, + return importReader(in, db, name, delim, quote, filter, useExistingTable, header); } finally { ByteUtil.closeQuietly(in); @@ -310,7 +310,7 @@ public class ImportUtil * <p> * Equivalent to: * {@code importReader(in, db, name, delim, SimpleImportFilter.INSTANCE);} - * + * * @param name Name of the new table to create * @param in Source reader to import * @param delim Regular expression representing the delimiter string. @@ -320,19 +320,19 @@ public class ImportUtil * @see #importReader(BufferedReader,Database,String,String,ImportFilter) * @see Builder */ - public static String importReader(BufferedReader in, Database db, + public static String importReader(BufferedReader in, Database db, String name, String delim) throws IOException { return importReader(in, db, name, delim, SimpleImportFilter.INSTANCE); } - + /** * Copy a delimited text file into a new table in this database. * <p> * Equivalent to: * {@code importReader(in, db, name, delim, filter, false);} - * + * * @param name Name of the new table to create * @param in Source reader to import * @param delim Regular expression representing the delimiter string. @@ -343,7 +343,7 @@ public class ImportUtil * @see #importReader(BufferedReader,Database,String,String,ImportFilter,boolean) * @see Builder */ - public static String importReader(BufferedReader in, Database db, + public static String importReader(BufferedReader in, Database db, String name, String delim, ImportFilter filter) throws IOException @@ -357,7 +357,7 @@ public class ImportUtil * <p> * Equivalent to: * {@code importReader(in, db, name, delim, '"', filter, false);} - * + * * @param name Name of the new table to create * @param in Source reader to import * @param delim Regular expression representing the delimiter string. @@ -367,12 +367,12 @@ public class ImportUtil * name * * @return the name of the imported table - * + * * @see Builder */ - public static String importReader(BufferedReader in, Database db, + public static String importReader(BufferedReader in, Database db, String name, String delim, - ImportFilter filter, + ImportFilter filter, boolean useExistingTable) throws IOException { @@ -386,7 +386,7 @@ public class ImportUtil * <p> * Equivalent to: * {@code importReader(in, db, name, delim, '"', filter, useExistingTable, true);} - * + * * @param name Name of the new table to create * @param in Source reader to import * @param delim Regular expression representing the delimiter string. @@ -397,23 +397,23 @@ public class ImportUtil * name * * @return the name of the imported table - * + * * @see Builder */ - public static String importReader(BufferedReader in, Database db, + public static String importReader(BufferedReader in, Database db, String name, String delim, char quote, ImportFilter filter, boolean useExistingTable) throws IOException { - return importReader(in, db, name, delim, quote, filter, useExistingTable, + return importReader(in, db, name, delim, quote, filter, useExistingTable, true); } /** * Copy a delimited text file into a new (or optionally exixsting) table in * this database. - * + * * @param name Name of the new table to create * @param in Source reader to import * @param delim Regular expression representing the delimiter string. @@ -426,10 +426,10 @@ public class ImportUtil * valid if useExistingTable is {@code true} * * @return the name of the imported table - * + * * @see Builder */ - public static String importReader(BufferedReader in, Database db, + public static String importReader(BufferedReader in, Database db, String name, String delim, char quote, ImportFilter filter, boolean useExistingTable, boolean header) @@ -449,7 +449,7 @@ public class ImportUtil List<ColumnBuilder> columns = new LinkedList<ColumnBuilder>(); Object[] columnNames = splitLine(line, delimPat, quote, in, 0); - + for (int i = 0; i < columnNames.length; i++) { columns.add(new ColumnBuilder((String)columnNames[i], DataType.TEXT) .escapeName() @@ -458,21 +458,21 @@ public class ImportUtil } table = createUniqueTable(db, name, columns, null, filter); - + // the first row was a header row header = true; } List<Object[]> rows = new ArrayList<Object[]>(COPY_TABLE_BATCH_SIZE); int numColumns = table.getColumnCount(); - + if(!header) { // first line is _not_ a header line Object[] data = splitLine(line, delimPat, quote, in, numColumns); data = filter.filterRow(data); if(data != null) { rows.add(data); - } + } } while ((line = in.readLine()) != null) @@ -535,7 +535,7 @@ public class ImportUtil idx = endIdx + 1; } else { - + // done idx = endIdx; break; @@ -582,7 +582,7 @@ public class ImportUtil */ private static Table createUniqueTable(Database db, String name, List<ColumnBuilder> columns, - ResultSetMetaData md, + ResultSetMetaData md, ImportFilter filter) throws IOException, SQLException { @@ -592,7 +592,7 @@ public class ImportUtil while(db.getTable(name) != null) { name = baseName + (counter++); } - + return new TableBuilder(name) .addColumns(filter.filterColumns(columns, md)) .toTable(db); @@ -661,7 +661,7 @@ public class ImportUtil public String importResultSet(ResultSet source) throws SQLException, IOException { - return ImportUtil.importResultSet(source, _db, _tableName, _filter, + return ImportUtil.importResultSet(source, _db, _tableName, _filter, _useExistingTable); } @@ -677,7 +677,7 @@ public class ImportUtil * @see ImportUtil#importReader(BufferedReader,Database,String,String,char,ImportFilter,boolean,boolean) */ public String importReader(BufferedReader reader) throws IOException { - return ImportUtil.importReader(reader, _db, _tableName, _delim, _quote, + return ImportUtil.importReader(reader, _db, _tableName, _delim, _quote, _filter, _useExistingTable, _header); } } diff --git a/src/test/java/com/healthmarketscience/jackcess/util/ImportTest.java b/src/test/java/com/healthmarketscience/jackcess/util/ImportTest.java index 97c0faf..5e0b036 100644 --- a/src/test/java/com/healthmarketscience/jackcess/util/ImportTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/util/ImportTest.java @@ -39,9 +39,9 @@ import com.healthmarketscience.jackcess.impl.JetFormatTest; import junit.framework.TestCase; import static com.healthmarketscience.jackcess.TestUtil.*; -/** +/** * @author Rob Di Marco - */ + */ public class ImportTest extends TestCase { @@ -194,7 +194,7 @@ public class ImportTest extends TestCase assertEquals(Arrays.asList( "RESULT_PHYS_ID", "FIRST", "MIDDLE", "LAST", "OUTLIER", "RANK", "CLAIM_COUNT", "PROCEDURE_COUNT", - "WEIGHTED_CLAIM_COUNT", "WEIGHTED_PROCEDURE_COUNT"), + "WEIGHTED_CLAIM_COUNT", "WEIGHTED_PROCEDURE_COUNT"), colNames); db.close(); @@ -271,7 +271,7 @@ public class ImportTest extends TestCase private List<Integer> _displaySizes = new ArrayList<Integer>(); private List<Integer> _scales = new ArrayList<Integer>(); private List<Integer> _precisions = new ArrayList<Integer>(); - + public Object invoke(Object proxy, Method method, Object[] args) { String methodName = method.getName(); @@ -284,7 +284,8 @@ public class ImportTest extends TestCase return Boolean.FALSE; } else if(methodName.equals("getColumnCount")) { return _types.size(); - } else if(methodName.equals("getColumnName")) { + } else if(methodName.equals("getColumnName") || + methodName.equals("getColumnLabel")) { return getValue(_names, args[0]); } else if(methodName.equals("getColumnDisplaySize")) { return getValue(_displaySizes, args[0]); @@ -318,5 +319,5 @@ public class ImportTest extends TestCase return values.get((Integer)index - 1); } } - -} + +} |