diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2012-05-25 01:55:12 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2012-05-25 01:55:12 +0000 |
commit | 4fd4eb6ea2a312689de4b4dfaadc1e49ef090a05 (patch) | |
tree | 2b86d5171ad22875c8ed8e0cb1e42e26662dc270 /src | |
parent | 782e6175383597e0dc297c798bbccad582e2fd20 (diff) | |
download | jackcess-4fd4eb6ea2a312689de4b4dfaadc1e49ef090a05.tar.gz jackcess-4fd4eb6ea2a312689de4b4dfaadc1e49ef090a05.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/changes/changes.xml | 4 | ||||
-rw-r--r-- | src/java/com/healthmarketscience/jackcess/ImportUtil.java | 67 |
2 files changed, 43 insertions, 28 deletions
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 @@ <action dev="jahlborn" type="update" issue="3523179"> Add osgi header information to the manifest. </action> + <action dev="jahlborn" type="update" issue="3523181"> + Add ImportUtil.toColumns to enable more advanced Table creation + implementations. + </action> <action dev="jahlborn" type="fix" issue="3529534"> Fix NPE when running unit tests with db format MSISAM. </action> 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 @@ -62,6 +62,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<Column> toColumns(ResultSetMetaData md) + throws SQLException + { + List<Column> columns = new LinkedList<Column>(); + 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. * <p> * Equivalent to: @@ -129,34 +167,7 @@ public class ImportUtil name = Database.escapeIdentifier(name); Table table = null; if(!useExistingTable || ((table = db.getTable(name)) == null)) { - - - List<Column> columns = new LinkedList<Column>(); - 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<Column> columns = toColumns(md); table = createUniqueTable(db, name, columns, md, filter); } |