From a61e2da7fe9f76614013481c27a557455f080752 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Wed, 7 Jul 2021 01:27:00 +0000 Subject: [PATCH] Add Table methods to access the creation and last modified dates git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1375 f203690c-595d-4dc9-a70b-905162fa7fd2 --- src/changes/changes.xml | 5 ++++ .../healthmarketscience/jackcess/Table.java | 16 +++++++++++++ .../jackcess/impl/ColumnImpl.java | 2 +- .../jackcess/impl/DatabaseImpl.java | 23 +++++++++++++++++++ .../jackcess/impl/TableImpl.java | 11 +++++++++ .../jackcess/DatabaseTest.java | 19 +++++++++++++++ 6 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 3714194..6698a2d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -4,6 +4,11 @@ Tim McCune + + + Add Table methods to access the creation and last modified dates. + + Ignore trailing spaces when creating text index entries. diff --git a/src/main/java/com/healthmarketscience/jackcess/Table.java b/src/main/java/com/healthmarketscience/jackcess/Table.java index 9eb2b15..5462e80 100644 --- a/src/main/java/com/healthmarketscience/jackcess/Table.java +++ b/src/main/java/com/healthmarketscience/jackcess/Table.java @@ -17,6 +17,7 @@ limitations under the License. package com.healthmarketscience.jackcess; import java.io.IOException; +import java.time.LocalDateTime; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -133,6 +134,21 @@ public interface Table extends Iterable */ public PropertyMap getProperties() throws IOException; + /** + * @return the created date for this table if available + * @usage _general_method_ + */ + public LocalDateTime getCreatedDate() throws IOException; + + /** + * Note: jackcess does not automatically update the modified date of a + * Table. + * + * @return the last updated date for this table if available + * @usage _general_method_ + */ + public LocalDateTime getUpdatedDate() throws IOException; + /** * @return All of the Indexes on this table (unmodifiable List) * @usage _intermediate_method_ diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/ColumnImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/ColumnImpl.java index 8387258..8eec0ea 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/ColumnImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/ColumnImpl.java @@ -1143,7 +1143,7 @@ public class ColumnImpl implements Column, Comparable, DateTimeConte return dtc.getDateTimeFactory().toDateDouble(value, dtc); } - private static LocalDateTime toLocalDateTime( + static LocalDateTime toLocalDateTime( Object value, DateTimeContext dtc) { if(value instanceof TemporalAccessor) { return temporalToLocalDateTime((TemporalAccessor)value, dtc); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java index b3f3768..80799ef 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java @@ -32,6 +32,7 @@ import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.text.SimpleDateFormat; +import java.time.LocalDateTime; import java.time.ZoneId; import java.util.ArrayList; import java.util.Arrays; @@ -257,6 +258,10 @@ public class DatabaseImpl implements Database, DateTimeContext /** the columns to read when getting object propertyes */ private static Collection SYSTEM_CATALOG_PROPS_COLUMNS = new HashSet(Arrays.asList(CAT_COL_ID, CAT_COL_PROPS)); + /** the columns to read when grabbing dates */ + private static Collection SYSTEM_CATALOG_DATE_COLUMNS = + new HashSet(Arrays.asList(CAT_COL_ID, + CAT_COL_DATE_CREATE, CAT_COL_DATE_UPDATE)); /** regex matching characters which are invalid in identifier names */ private static final Pattern INVALID_IDENTIFIER_CHARS = @@ -1535,6 +1540,24 @@ public class DatabaseImpl implements Database, DateTimeContext objectId, SYSTEM_CATALOG_PROPS_COLUMNS), owner); } + LocalDateTime getCreateDateForObject(int objectId) throws IOException { + return getDateForObject(objectId, CAT_COL_DATE_CREATE); + } + + LocalDateTime getUpdateDateForObject(int objectId) throws IOException { + return getDateForObject(objectId, CAT_COL_DATE_UPDATE); + } + + private LocalDateTime getDateForObject(int objectId, String dateCol) + throws IOException { + Row row = _tableFinder.getObjectRow(objectId, SYSTEM_CATALOG_DATE_COLUMNS); + if(row == null) { + return null; + } + Object date = row.get(dateCol); + return ((date != null) ? ColumnImpl.toLocalDateTime(date, this) : null); + } + private Integer getDbParentId() throws IOException { if(_dbParentId == null) { // need the parent id of the databases objects diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/TableImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/TableImpl.java index eafa376..d05f640 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/TableImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/TableImpl.java @@ -22,6 +22,7 @@ import java.io.StringWriter; import java.nio.BufferOverflowException; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.time.LocalDateTime; import java.util.AbstractMap; import java.util.ArrayList; import java.util.Arrays; @@ -476,6 +477,16 @@ public class TableImpl implements Table, PropertyMaps.Owner return _props; } + @Override + public LocalDateTime getCreatedDate() throws IOException { + return getDatabase().getCreateDateForObject(_tableDefPageNumber); + } + + @Override + public LocalDateTime getUpdatedDate() throws IOException { + return getDatabase().getUpdateDateForObject(_tableDefPageNumber); + } + /** * @return all PropertyMaps for this table (and columns) * @usage _advanced_method_ diff --git a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java index aa90a41..5f5a488 100644 --- a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -1090,6 +1090,25 @@ public class DatabaseTest extends TestCase } } + public void testTableDates() throws Exception { + for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { + Table table = open(testDB).getTable("Table1"); + String expectedCreateDate = null; + String expectedUpdateDate = null; + if(testDB.getExpectedFileFormat() == FileFormat.V1997) { + expectedCreateDate = "2010-03-05T14:48:26.420"; + expectedUpdateDate = "2010-03-05T14:48:26.607"; + } else { + expectedCreateDate = "2004-05-28T17:51:48.701"; + expectedUpdateDate = "2006-07-24T09:56:19.701"; + } + System.out.println("FOO " + testDB.getExpectedFileFormat() + " " + + table.getCreatedDate() + " " + + table.getUpdatedDate()); + assertEquals(expectedCreateDate, table.getCreatedDate().toString()); + assertEquals(expectedUpdateDate, table.getUpdatedDate().toString()); + } + } private static void checkRawValue(String expected, Object val) { if(expected != null) { -- 2.39.5