Kaynağa Gözat

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
tags/rel_1_1_5
James Ahlborn 18 yıl önce
ebeveyn
işleme
0bca2b8a4d

+ 7
- 3
src/java/com/healthmarketscience/jackcess/Column.java Dosyayı Görüntüle

*/ */
private static final short LONG_VALUE_TYPE_OTHER_PAGES = (short) 0x0; 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 */ /** default precision value for new numeric columns */
public static final byte DEFAULT_PRECISION = 18; public static final byte DEFAULT_PRECISION = 18;
/** default scale value for new numeric columns */ /** 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 */ /** For text columns, whether or not they are compressed */
private boolean _compressedUnicode = false; private boolean _compressedUnicode = false;
return BigDecimal.ZERO; return BigDecimal.ZERO;
} else if(value instanceof BigDecimal) { } else if(value instanceof BigDecimal) {
return (BigDecimal)value; 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()); return new BigDecimal(((Number)value).doubleValue());
} else {
return new BigDecimal(value.toString());
} }
} }



+ 7
- 2
src/java/com/healthmarketscience/jackcess/Database.java Dosyayı Görüntüle

buffer.putShort((short) 0); buffer.putShort((short) 0);
} }
buffer.putShort(columnNumber); //Column Number again 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 buffer.putShort((short) 0); //Unknown
if (col.getType().isVariableLength()) { //Variable length if (col.getType().isVariableLength()) { //Variable length
buffer.put((byte) 0x2); buffer.put((byte) 0x2);

+ 98
- 2
test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java Dosyayı Görüntüle



import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
Table table = open().getTable("Table1"); Table table = open().getTable("Table1");
Map<String, Boolean> foundPKs = new HashMap<String, Boolean>(); Map<String, Boolean> foundPKs = new HashMap<String, Boolean>();
for(Index index : table.getIndexes()) { for(Index index : table.getIndexes()) {
System.out.println(index);
foundPKs.put(index.getColumns().iterator().next().getName(), foundPKs.put(index.getColumns().iterator().next().getName(),
index.isPrimaryKey()); index.isPrimaryKey());
} }
Table table = db.getTable("Test"); Table table = db.getTable("Test");
table.addRow(new BigDecimal("-2341234.03450")); table.addRow(new BigDecimal("-2341234.03450"));
table.addRow(37L); table.addRow(37L);
table.addRow(new BigDecimal("10000.45"));
table.addRow("10000.45");


table.reset(); table.reset();


} }
} }


public void testGUID() throws Exception
{
Database db = create();

List<Column> columns = new ArrayList<Column>();
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<Object> foundValues = new ArrayList<Object>();
Map<String, Object> 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<Column> columns = new ArrayList<Column>();
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<Object> foundSmallValues = new ArrayList<Object>();
List<Object> foundBigValues = new ArrayList<Object>();
Map<String, Object> 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() { private Object[] createTestRow() {
return new Object[] {"Tim", "R", "McCune", 1234, (byte) 0xad, 555.66d, return new Object[] {"Tim", "R", "McCune", 1234, (byte) 0xad, 555.66d,
777.88f, (short) 999, new Date()}; 777.88f, (short) 999, new Date()};

Loading…
İptal
Kaydet