From 0bca2b8a4d32eabc215c66c7d7507275675b29c5 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Tue, 25 Jul 2006 20:29:03 +0000 Subject: [PATCH] add write support for numeric/guid git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@69 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../healthmarketscience/jackcess/Column.java | 10 +- .../jackcess/Database.java | 9 +- .../jackcess/DatabaseTest.java | 100 +++++++++++++++++- 3 files changed, 112 insertions(+), 7 deletions(-) diff --git a/src/java/com/healthmarketscience/jackcess/Column.java b/src/java/com/healthmarketscience/jackcess/Column.java index 2e61b46..32b0e71 100644 --- a/src/java/com/healthmarketscience/jackcess/Column.java +++ b/src/java/com/healthmarketscience/jackcess/Column.java @@ -79,12 +79,12 @@ public class Column implements Comparable { */ private static final short LONG_VALUE_TYPE_OTHER_PAGES = (short) 0x0; - private static final Pattern GUID_PATTERN = Pattern.compile("\\s*[{]([\\p{XDigit}]{4})-([\\p{XDigit}]{2})-([\\p{XDigit}]{2})-([\\p{XDigit}]{2})-([\\p{XDigit}]{6})[}]\\s*"); + private static final Pattern GUID_PATTERN = Pattern.compile("\\s*[{]([\\p{XDigit}]{8})-([\\p{XDigit}]{4})-([\\p{XDigit}]{4})-([\\p{XDigit}]{4})-([\\p{XDigit}]{12})[}]\\s*"); /** default precision value for new numeric columns */ public static final byte DEFAULT_PRECISION = 18; /** default scale value for new numeric columns */ - public static final byte DEFAULT_SCALE = 18; + public static final byte DEFAULT_SCALE = 0; /** For text columns, whether or not they are compressed */ private boolean _compressedUnicode = false; @@ -812,8 +812,12 @@ public class Column implements Comparable { return BigDecimal.ZERO; } else if(value instanceof BigDecimal) { return (BigDecimal)value; - } else { + } else if(value instanceof BigInteger) { + return new BigDecimal((BigInteger)value); + } else if(value instanceof Number) { return new BigDecimal(((Number)value).doubleValue()); + } else { + return new BigDecimal(value.toString()); } } diff --git a/src/java/com/healthmarketscience/jackcess/Database.java b/src/java/com/healthmarketscience/jackcess/Database.java index 93e8a83..5635ed6 100644 --- a/src/java/com/healthmarketscience/jackcess/Database.java +++ b/src/java/com/healthmarketscience/jackcess/Database.java @@ -409,8 +409,13 @@ public class Database { buffer.putShort((short) 0); } buffer.putShort(columnNumber); //Column Number again - buffer.put((byte) 0x09); //Unknown - buffer.put((byte) 0x04); //Unknown + if(col.getType() == DataType.NUMERIC) { + buffer.put((byte) col.getPrecision()); // numeric precision + buffer.put((byte) col.getScale()); // numeric scale + } else { + buffer.put((byte) 0x00); //unused + buffer.put((byte) 0x00); //unused + } buffer.putShort((short) 0); //Unknown if (col.getType().isVariableLength()) { //Variable length buffer.put((byte) 0x2); diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java index ac42b08..adde151 100644 --- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -4,6 +4,7 @@ package com.healthmarketscience.jackcess; import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; @@ -242,7 +243,6 @@ public class DatabaseTest extends TestCase { Table table = open().getTable("Table1"); Map foundPKs = new HashMap(); for(Index index : table.getIndexes()) { - System.out.println(index); foundPKs.put(index.getColumns().iterator().next().getName(), index.isPrimaryKey()); } @@ -303,7 +303,7 @@ public class DatabaseTest extends TestCase { Table table = db.getTable("Test"); table.addRow(new BigDecimal("-2341234.03450")); table.addRow(37L); - table.addRow(new BigDecimal("10000.45")); + table.addRow("10000.45"); table.reset(); @@ -327,6 +327,102 @@ public class DatabaseTest extends TestCase { } } + public void testGUID() throws Exception + { + Database db = create(); + + List columns = new ArrayList(); + Column col = new Column(); + col.setName("A"); + col.setType(DataType.GUID); + columns.add(col); + db.createTable("test", columns); + + Table table = db.getTable("Test"); + table.addRow("{32A59F01-AA34-3E29-453F-4523453CD2E6}"); + table.addRow("{32a59f01-aa34-3e29-453f-4523453cd2e6}"); + table.addRow("{11111111-1111-1111-1111-111111111111}"); + table.addRow("{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF}"); + + table.reset(); + + List foundValues = new ArrayList(); + Map row = null; + while((row = table.getNextRow()) != null) { + foundValues.add(row.get("A")); + } + + assertEquals(Arrays.asList( + "{32A59F01-AA34-3E29-453F-4523453CD2E6}", + "{32A59F01-AA34-3E29-453F-4523453CD2E6}", + "{11111111-1111-1111-1111-111111111111}", + "{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF}"), + foundValues); + + try { + table.addRow("3245234"); + fail("IOException should have been thrown"); + } catch(IOException e) { + // ignored + } + } + + public void testNumeric() throws Exception + { + Database db = create(); + + List columns = new ArrayList(); + Column col = new Column(); + col.setName("A"); + col.setType(DataType.NUMERIC); + col.setScale((byte)4); + col.setPrecision((byte)8); + columns.add(col); + + col = new Column(); + col.setName("B"); + col.setType(DataType.NUMERIC); + col.setScale((byte)8); + col.setPrecision((byte)28); + columns.add(col); + db.createTable("test", columns); + + Table table = db.getTable("Test"); + table.addRow(new BigDecimal("-1234.03450"), + new BigDecimal("23923434453436.36234219")); + table.addRow(37L, 37L); + table.addRow("1000.45", "-3452345321000"); + + table.reset(); + + List foundSmallValues = new ArrayList(); + List foundBigValues = new ArrayList(); + Map row = null; + while((row = table.getNextRow()) != null) { + foundSmallValues.add(row.get("A")); + foundBigValues.add(row.get("B")); + } + + assertEquals(Arrays.asList( + new BigDecimal("-1234.0345"), + new BigDecimal("37.0000"), + new BigDecimal("1000.4500")), + foundSmallValues); + assertEquals(Arrays.asList( + new BigDecimal("23923434453436.36234219"), + new BigDecimal("37.00000000"), + new BigDecimal("-3452345321000.00000000")), + foundBigValues); + + try { + table.addRow(new BigDecimal("3245234.234"), + new BigDecimal("3245234.234")); + fail("IOException should have been thrown"); + } catch(IOException e) { + // ignored + } + } + private Object[] createTestRow() { return new Object[] {"Tim", "R", "McCune", 1234, (byte) 0xad, 555.66d, 777.88f, (short) 999, new Date()}; -- 2.39.5