From: James Ahlborn Date: Fri, 25 May 2012 01:55:12 +0000 (+0000) Subject: Add ImportUtil.toColumns to enable more advanced Table creation implementations ... X-Git-Tag: jackcess-1.2.8~4 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4fd4eb6ea2a312689de4b4dfaadc1e49ef090a05;p=jackcess.git Add ImportUtil.toColumns to enable more advanced Table creation implementations (fixes issue 3523181) git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@627 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9e81c4a..389314b 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -8,6 +8,10 @@ Add osgi header information to the manifest. + + Add ImportUtil.toColumns to enable more advanced Table creation + implementations. + Fix NPE when running unit tests with db format MSISAM. diff --git a/src/java/com/healthmarketscience/jackcess/ImportUtil.java b/src/java/com/healthmarketscience/jackcess/ImportUtil.java index 28aa9ea..0fc1802 100644 --- a/src/java/com/healthmarketscience/jackcess/ImportUtil.java +++ b/src/java/com/healthmarketscience/jackcess/ImportUtil.java @@ -61,6 +61,44 @@ public class ImportUtil private ImportUtil() {} + /** + * Returns a List of Column instances converted from the given + * ResultSetMetaData (this is the same method used by the various {@code + * importResultSet()} methods). + * + * @return a List of Columns + */ + public static List toColumns(ResultSetMetaData md) + throws SQLException + { + List columns = new LinkedList(); + for (int i = 1; i <= md.getColumnCount(); i++) { + Column column = new Column(); + column.setName(Database.escapeIdentifier(md.getColumnName(i))); + int lengthInUnits = md.getColumnDisplaySize(i); + column.setSQLType(md.getColumnType(i), lengthInUnits); + DataType type = column.getType(); + // we check for isTrueVariableLength here to avoid setting the length + // for a NUMERIC column, which pretends to be var-len, even though it + // isn't + if(type.isTrueVariableLength() && !type.isLongValue()) { + column.setLengthInUnits((short)lengthInUnits); + } + if(type.getHasScalePrecision()) { + int scale = md.getScale(i); + int precision = md.getPrecision(i); + if(type.isValidScale(scale)) { + column.setScale((byte)scale); + } + if(type.isValidPrecision(precision)) { + column.setPrecision((byte)precision); + } + } + columns.add(column); + } + return columns; + } + /** * Copy an existing JDBC ResultSet into a new table in this database. *

@@ -129,34 +167,7 @@ public class ImportUtil name = Database.escapeIdentifier(name); Table table = null; if(!useExistingTable || ((table = db.getTable(name)) == null)) { - - - List columns = new LinkedList(); - for (int i = 1; i <= md.getColumnCount(); i++) { - Column column = new Column(); - column.setName(Database.escapeIdentifier(md.getColumnName(i))); - int lengthInUnits = md.getColumnDisplaySize(i); - column.setSQLType(md.getColumnType(i), lengthInUnits); - DataType type = column.getType(); - // we check for isTrueVariableLength here to avoid setting the length - // for a NUMERIC column, which pretends to be var-len, even though it - // isn't - if(type.isTrueVariableLength() && !type.isLongValue()) { - column.setLengthInUnits((short)lengthInUnits); - } - if(type.getHasScalePrecision()) { - int scale = md.getScale(i); - int precision = md.getPrecision(i); - if(type.isValidScale(scale)) { - column.setScale((byte)scale); - } - if(type.isValidPrecision(precision)) { - column.setPrecision((byte)precision); - } - } - columns.add(column); - } - + List columns = toColumns(md); table = createUniqueTable(db, name, columns, md, filter); }