diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2014-05-06 04:22:46 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2014-05-06 04:22:46 +0000 |
commit | 840ccbde0f0729e976fde4cec1a2fbfe55559b7b (patch) | |
tree | 9d85ec98f762e817de4cd40175e90898c1fe8a50 /src/main/java | |
parent | 7fdfea151021252e458426c8f99b73d55486e2f0 (diff) | |
download | jackcess-840ccbde0f0729e976fde4cec1a2fbfe55559b7b.tar.gz jackcess-840ccbde0f0729e976fde4cec1a2fbfe55559b7b.zip |
add convenience methods to Row for getting values cast to specific types
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@859 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src/main/java')
6 files changed, 168 insertions, 49 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/Row.java b/src/main/java/com/healthmarketscience/jackcess/Row.java index 2385e90..da3a4c6 100644 --- a/src/main/java/com/healthmarketscience/jackcess/Row.java +++ b/src/main/java/com/healthmarketscience/jackcess/Row.java @@ -19,7 +19,12 @@ USA package com.healthmarketscience.jackcess; +import java.util.Date; import java.util.Map; +import java.math.BigDecimal; + +import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey; + /** * A row of data as column name->value pairs. Values are strongly typed, and @@ -34,4 +39,70 @@ public interface Row extends Map<String,Object> * @return the id of this row */ public RowId getId(); + + /** + * Convenience method which gets the value for the row with the given name, + * casting it to a String. + */ + public String getString(String name); + + /** + * Convenience method which gets the value for the row with the given name, + * casting it to a Boolean. + */ + public Boolean getBoolean(String name); + + /** + * Convenience method which gets the value for the row with the given name, + * casting it to a Byte. + */ + public Byte getByte(String name); + + /** + * Convenience method which gets the value for the row with the given name, + * casting it to a Short. + */ + public Short getShort(String name); + + /** + * Convenience method which gets the value for the row with the given name, + * casting it to a Integer. + */ + public Integer getInt(String name); + + /** + * Convenience method which gets the value for the row with the given name, + * casting it to a BigDecimal. + */ + public BigDecimal getBigDecimal(String name); + + /** + * Convenience method which gets the value for the row with the given name, + * casting it to a Float. + */ + public Float getFloat(String name); + + /** + * Convenience method which gets the value for the row with the given name, + * casting it to a Double. + */ + public Double getDouble(String name); + + /** + * Convenience method which gets the value for the row with the given name, + * casting it to a Date. + */ + public Date getDate(String name); + + /** + * Convenience method which gets the value for the row with the given name, + * casting it to a byte[]. + */ + public byte[] getBytes(String name); + + /** + * Convenience method which gets the value for the row with the given name, + * casting it to a ComplexValueForeignKey. + */ + public ComplexValueForeignKey getForeignKey(String name); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/ComplexColumnSupport.java b/src/main/java/com/healthmarketscience/jackcess/impl/ComplexColumnSupport.java index 533e1ef..469a720 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/ComplexColumnSupport.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/ComplexColumnSupport.java @@ -79,14 +79,14 @@ public class ComplexColumnSupport complexTypeId); } Row cColRow = cursor.getCurrentRow(); - int tableId = (Integer)cColRow.get(COL_TABLE_ID); + int tableId = cColRow.getInt(COL_TABLE_ID); if(tableId != column.getTable().getTableDefPageNumber()) { throw new IOException( "Found complex column for table " + tableId + " but expected table " + column.getTable().getTableDefPageNumber()); } - int flatTableId = (Integer)cColRow.get(COL_FLAT_TABLE_ID); - int typeObjId = (Integer)cColRow.get(COL_COMPLEX_TYPE_OBJECT_ID); + int flatTableId = cColRow.getInt(COL_FLAT_TABLE_ID); + int typeObjId = cColRow.getInt(COL_COMPLEX_TYPE_OBJECT_ID); TableImpl typeObjTable = db.getTable(typeObjId); TableImpl flatTable = db.getTable(flatTableId); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java index 8ddc4a0..548833b 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java @@ -874,8 +874,8 @@ public class DatabaseImpl implements Database return null; } - String name = (String)objectRow.get(CAT_COL_NAME); - int flags = (Integer)objectRow.get(CAT_COL_FLAGS); + String name = objectRow.getString(CAT_COL_NAME); + int flags = objectRow.getInt(CAT_COL_FLAGS); return readTable(name, tableDefPageNumber, flags); } @@ -1093,10 +1093,10 @@ public class DatabaseImpl implements Database CursorImpl.createCursor(_systemCatalog).newIterable().setColumnNames( SYSTEM_CATALOG_COLUMNS)) { - String name = (String) row.get(CAT_COL_NAME); + String name = row.getString(CAT_COL_NAME); if (name != null && TYPE_QUERY.equals(row.get(CAT_COL_TYPE))) { queryInfo.add(row); - Integer id = (Integer)row.get(CAT_COL_ID); + Integer id = row.getInt(CAT_COL_ID); queryRowMap.put(id, new ArrayList<QueryImpl.Row>()); } } @@ -1116,9 +1116,9 @@ public class DatabaseImpl implements Database // lastly, generate all the queries List<Query> queries = new ArrayList<Query>(); for(Row row : queryInfo) { - String name = (String) row.get(CAT_COL_NAME); - Integer id = (Integer)row.get(CAT_COL_ID); - int flags = (Integer)row.get(CAT_COL_FLAGS); + String name = row.getString(CAT_COL_NAME); + Integer id = row.getInt(CAT_COL_ID); + int flags = row.getInt(CAT_COL_FLAGS); List<QueryImpl.Row> queryRows = queryRowMap.get(id); queries.add(QueryImpl.create(flags, name, queryRows, id)); } @@ -1164,7 +1164,7 @@ public class DatabaseImpl implements Database byte[] propsBytes = null; RowIdImpl rowId = null; if(objectRow != null) { - propsBytes = (byte[])objectRow.get(CAT_COL_PROPS); + propsBytes = objectRow.getBytes(CAT_COL_PROPS); rowId = (RowIdImpl)objectRow.getId(); } return readProperties(propsBytes, objectId, rowId); @@ -1191,8 +1191,8 @@ public class DatabaseImpl implements Database int objectId = -1; RowIdImpl rowId = null; if(objectRow != null) { - propsBytes = (byte[])objectRow.get(CAT_COL_PROPS); - objectId = (Integer)objectRow.get(CAT_COL_ID); + propsBytes = objectRow.getBytes(CAT_COL_PROPS); + objectId = objectRow.getInt(CAT_COL_ID); rowId = (RowIdImpl)objectRow.getId(); } return readProperties(propsBytes, objectId, rowId); @@ -1257,15 +1257,15 @@ public class DatabaseImpl implements Database String toTableName = ((toTable != null) ? toTable.getName() : null); for(Row row : cursor) { - String fromName = (String)row.get(REL_COL_FROM_TABLE); - String toName = (String)row.get(REL_COL_TO_TABLE); + String fromName = row.getString(REL_COL_FROM_TABLE); + String toName = row.getString(REL_COL_TO_TABLE); if(((fromTableName == null) || fromTableName.equalsIgnoreCase(fromName)) && ((toTableName == null) || toTableName.equalsIgnoreCase(toName))) { - String relName = (String)row.get(REL_COL_NAME); + String relName = row.getString(REL_COL_NAME); // found more info for a relationship. see if we already have some // info for this relationship @@ -1296,19 +1296,19 @@ public class DatabaseImpl implements Database if(rel == null) { // new relationship - int numCols = (Integer)row.get(REL_COL_COLUMN_COUNT); - int flags = (Integer)row.get(REL_COL_FLAGS); + int numCols = row.getInt(REL_COL_COLUMN_COUNT); + int flags = row.getInt(REL_COL_FLAGS); rel = new RelationshipImpl(relName, relFromTable, relToTable, flags, numCols); relationships.add(rel); } // add column info - int colIdx = (Integer)row.get(REL_COL_COLUMN_INDEX); + int colIdx = row.getInt(REL_COL_COLUMN_INDEX); ColumnImpl fromCol = relFromTable.getColumn( - (String)row.get(REL_COL_FROM_COLUMN)); + row.getString(REL_COL_FROM_COLUMN)); ColumnImpl toCol = relToTable.getColumn( - (String)row.get(REL_COL_TO_COLUMN)); + row.getString(REL_COL_TO_COLUMN)); rel.getFromColumns().set(colIdx, fromCol); rel.getToColumns().set(colIdx, toCol); @@ -1400,9 +1400,9 @@ public class DatabaseImpl implements Database getAccessControlEntries(), ACE_COL_OBJECT_ID, _tableParentId); for(Row row : cursor) { - Integer objId = (Integer)row.get(ACE_COL_OBJECT_ID); + Integer objId = row.getInt(ACE_COL_OBJECT_ID); if(_tableParentId.equals(objId)) { - _newTableSIDs.add((byte[])row.get(ACE_COL_SID)); + _newTableSIDs.add(row.getBytes(ACE_COL_SID)); } } @@ -1842,10 +1842,10 @@ public class DatabaseImpl implements Database for(Row row : getTableNamesCursor().newIterable().setColumnNames( SYSTEM_CATALOG_TABLE_NAME_COLUMNS)) { - String tableName = (String)row.get(CAT_COL_NAME); - int flags = (Integer)row.get(CAT_COL_FLAGS); - Short type = (Short)row.get(CAT_COL_TYPE); - int parentId = (Integer)row.get(CAT_COL_PARENT_ID); + String tableName = row.getString(CAT_COL_NAME); + int flags = row.getInt(CAT_COL_FLAGS); + Short type = row.getShort(CAT_COL_TYPE); + int parentId = row.getInt(CAT_COL_PARENT_ID); if((parentId == _tableParentId) && isTableType(type) && (isSystemObject(flags) == systemTables)) { @@ -1923,17 +1923,17 @@ public class DatabaseImpl implements Database Row row = _systemCatalogCursor.getCurrentRow( SYSTEM_CATALOG_COLUMNS); - Integer pageNumber = (Integer)row.get(CAT_COL_ID); - String realName = (String)row.get(CAT_COL_NAME); - int flags = (Integer)row.get(CAT_COL_FLAGS); - Short type = (Short)row.get(CAT_COL_TYPE); + Integer pageNumber = row.getInt(CAT_COL_ID); + String realName = row.getString(CAT_COL_NAME); + int flags = row.getInt(CAT_COL_FLAGS); + Short type = row.getShort(CAT_COL_TYPE); if(!isTableType(type)) { return null; } - String linkedDbName = (String)row.get(CAT_COL_DATABASE); - String linkedTableName = (String)row.get(CAT_COL_FOREIGN_NAME); + String linkedDbName = row.getString(CAT_COL_DATABASE); + String linkedTableName = row.getString(CAT_COL_FOREIGN_NAME); return createTableInfo(realName, pageNumber, flags, type, linkedDbName, linkedTableName); @@ -1999,25 +1999,25 @@ public class DatabaseImpl implements Database for(Row row : _systemCatalogCursor.newIterable().setColumnNames( SYSTEM_CATALOG_TABLE_NAME_COLUMNS)) { - Short type = (Short)row.get(CAT_COL_TYPE); + Short type = row.getShort(CAT_COL_TYPE); if(!isTableType(type)) { continue; } - int parentId = (Integer)row.get(CAT_COL_PARENT_ID); + int parentId = row.getInt(CAT_COL_PARENT_ID); if(parentId != _tableParentId) { continue; } - String realName = (String)row.get(CAT_COL_NAME); + String realName = row.getString(CAT_COL_NAME); if(!tableName.equalsIgnoreCase(realName)) { continue; } - Integer pageNumber = (Integer)row.get(CAT_COL_ID); - int flags = (Integer)row.get(CAT_COL_FLAGS); - String linkedDbName = (String)row.get(CAT_COL_DATABASE); - String linkedTableName = (String)row.get(CAT_COL_FOREIGN_NAME); + Integer pageNumber = row.getInt(CAT_COL_ID); + int flags = row.getInt(CAT_COL_FLAGS); + String linkedDbName = row.getString(CAT_COL_DATABASE); + String linkedTableName = row.getString(CAT_COL_FOREIGN_NAME); return createTableInfo(realName, pageNumber, flags, type, linkedDbName, linkedTableName); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/RowImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/RowImpl.java index 2191bf2..34071ad 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/RowImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/RowImpl.java @@ -20,8 +20,12 @@ USA package com.healthmarketscience.jackcess.impl; import java.util.LinkedHashMap; +import java.util.Date; +import java.math.BigDecimal; import com.healthmarketscience.jackcess.Row; +import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey; + /** * A row of data as column->value pairs. @@ -55,6 +59,50 @@ public class RowImpl extends LinkedHashMap<String,Object> implements Row return _id; } + public String getString(String name) { + return (String)get(name); + } + + public Boolean getBoolean(String name) { + return (Boolean)get(name); + } + + public Byte getByte(String name) { + return (Byte)get(name); + } + + public Short getShort(String name) { + return (Short)get(name); + } + + public Integer getInt(String name) { + return (Integer)get(name); + } + + public BigDecimal getBigDecimal(String name) { + return (BigDecimal)get(name); + } + + public Float getFloat(String name) { + return (Float)get(name); + } + + public Double getDouble(String name) { + return (Double)get(name); + } + + public Date getDate(String name) { + return (Date)get(name); + } + + public byte[] getBytes(String name) { + return (byte[])get(name); + } + + public ComplexValueForeignKey getForeignKey(String name) { + return (ComplexValueForeignKey)get(name); + } + @Override public String toString() { return CustomToStringStyle.valueBuilder("Row[" + _id + "]") diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/query/QueryImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/query/QueryImpl.java index 94c8e41..90032eb 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/query/QueryImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/query/QueryImpl.java @@ -582,14 +582,14 @@ public abstract class QueryImpl implements Query public Row(com.healthmarketscience.jackcess.Row tableRow) { this(tableRow.getId(), - (Byte)tableRow.get(COL_ATTRIBUTE), - (String)tableRow.get(COL_EXPRESSION), - (Short)tableRow.get(COL_FLAG), - (Integer)tableRow.get(COL_EXTRA), - (String)tableRow.get(COL_NAME1), - (String)tableRow.get(COL_NAME2), - (Integer)tableRow.get(COL_OBJECTID), - (byte[])tableRow.get(COL_ORDER)); + tableRow.getByte(COL_ATTRIBUTE), + tableRow.getString(COL_EXPRESSION), + tableRow.getShort(COL_FLAG), + tableRow.getInt(COL_EXTRA), + tableRow.getString(COL_NAME1), + tableRow.getString(COL_NAME2), + tableRow.getInt(COL_OBJECTID), + tableRow.getBytes(COL_ORDER)); } public Row(RowId id, Byte attribute, String expression, Short flag, diff --git a/src/main/java/com/healthmarketscience/jackcess/util/OleBlob.java b/src/main/java/com/healthmarketscience/jackcess/util/OleBlob.java index 893eac5..18ae5b7 100644 --- a/src/main/java/com/healthmarketscience/jackcess/util/OleBlob.java +++ b/src/main/java/com/healthmarketscience/jackcess/util/OleBlob.java @@ -56,7 +56,7 @@ import com.healthmarketscience.jackcess.impl.OleUtil; * <p/> * <b>Example for interpreting an existing OLE field:</b> * <pre> - * byte[] oleBytes = (byte[])row.get("MyOleColumn"); + * byte[] oleBytes = row.getBytes("MyOleColumn"); * OleBlob oleBlob = null; * try { * oleBlob = OleBlob.Builder.fromInternalData(oleBlob); |