com.healthmarketscience.jackcess.impl.DatabaseImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackcess Show documentation
Show all versions of jackcess Show documentation
A pure Java library for reading from and writing to MS Access databases.
/*
Copyright (c) 2005 Health Market Science, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.healthmarketscience.jackcess.impl;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeSet;
import java.util.regex.Pattern;
import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.Cursor;
import com.healthmarketscience.jackcess.CursorBuilder;
import com.healthmarketscience.jackcess.DataType;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.DateTimeType;
import com.healthmarketscience.jackcess.Index;
import com.healthmarketscience.jackcess.IndexBuilder;
import com.healthmarketscience.jackcess.IndexCursor;
import com.healthmarketscience.jackcess.PropertyMap;
import com.healthmarketscience.jackcess.Relationship;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.RuntimeIOException;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;
import com.healthmarketscience.jackcess.TableMetaData;
import com.healthmarketscience.jackcess.expr.EvalConfig;
import com.healthmarketscience.jackcess.impl.query.QueryImpl;
import com.healthmarketscience.jackcess.query.Query;
import com.healthmarketscience.jackcess.util.CaseInsensitiveColumnMatcher;
import com.healthmarketscience.jackcess.util.ColumnValidatorFactory;
import com.healthmarketscience.jackcess.util.ErrorHandler;
import com.healthmarketscience.jackcess.util.LinkResolver;
import com.healthmarketscience.jackcess.util.ReadOnlyFileChannel;
import com.healthmarketscience.jackcess.util.SimpleColumnValidatorFactory;
import com.healthmarketscience.jackcess.util.TableIterableBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* @author Tim McCune
* @usage _intermediate_class_
*/
public class DatabaseImpl implements Database, DateTimeContext
{
private static final Log LOG = LogFactory.getLog(DatabaseImpl.class);
/** this is the default "userId" used if we cannot find existing info. this
seems to be some standard "Admin" userId for access files */
private static final byte[] SYS_DEFAULT_SID = new byte[] {
(byte) 0xA6, (byte) 0x33};
/** the default value for the resource path used to load classpath
* resources.
*/
public static final String DEFAULT_RESOURCE_PATH =
"com/healthmarketscience/jackcess/";
/** the resource path to be used when loading classpath resources */
static final String RESOURCE_PATH =
System.getProperty(RESOURCE_PATH_PROPERTY, DEFAULT_RESOURCE_PATH);
/** whether or not this jvm has "broken" nio support */
static final boolean BROKEN_NIO = Boolean.TRUE.toString().equalsIgnoreCase(
System.getProperty(BROKEN_NIO_PROPERTY));
/** additional internal details about each FileFormat */
private static final Map FILE_FORMAT_DETAILS =
new EnumMap(Database.FileFormat.class);
static {
addFileFormatDetails(FileFormat.V1997, null, JetFormat.VERSION_3);
addFileFormatDetails(FileFormat.GENERIC_JET4, null, JetFormat.VERSION_4);
addFileFormatDetails(FileFormat.V2000, "empty", JetFormat.VERSION_4);
addFileFormatDetails(FileFormat.V2003, "empty2003", JetFormat.VERSION_4);
addFileFormatDetails(FileFormat.V2007, "empty2007", JetFormat.VERSION_12);
addFileFormatDetails(FileFormat.V2010, "empty2010", JetFormat.VERSION_14);
addFileFormatDetails(FileFormat.V2016, "empty2016", JetFormat.VERSION_16);
addFileFormatDetails(FileFormat.MSISAM, null, JetFormat.VERSION_MSISAM);
}
/** System catalog always lives on page 2 */
private static final int PAGE_SYSTEM_CATALOG = 2;
/** Name of the system catalog */
private static final String TABLE_SYSTEM_CATALOG = "MSysObjects";
/** this is the access control bit field for created tables. the value used
is equivalent to full access (Visual Basic DAO PermissionEnum constant:
dbSecFullAccess) */
private static final Integer SYS_FULL_ACCESS_ACM = 1048575;
/** ACE table column name of the actual access control entry */
private static final String ACE_COL_ACM = "ACM";
/** ACE table column name of the inheritable attributes flag */
private static final String ACE_COL_F_INHERITABLE = "FInheritable";
/** ACE table column name of the relevant objectId */
private static final String ACE_COL_OBJECT_ID = "ObjectId";
/** ACE table column name of the relevant userId */
private static final String ACE_COL_SID = "SID";
/** Relationship table column name of the column count */
private static final String REL_COL_COLUMN_COUNT = "ccolumn";
/** Relationship table column name of the flags */
private static final String REL_COL_FLAGS = "grbit";
/** Relationship table column name of the index of the columns */
private static final String REL_COL_COLUMN_INDEX = "icolumn";
/** Relationship table column name of the "to" column name */
private static final String REL_COL_TO_COLUMN = "szColumn";
/** Relationship table column name of the "to" table name */
private static final String REL_COL_TO_TABLE = "szObject";
/** Relationship table column name of the "from" column name */
private static final String REL_COL_FROM_COLUMN = "szReferencedColumn";
/** Relationship table column name of the "from" table name */
private static final String REL_COL_FROM_TABLE = "szReferencedObject";
/** Relationship table column name of the relationship */
private static final String REL_COL_NAME = "szRelationship";
/** System catalog column name of the page on which system object definitions
are stored */
private static final String CAT_COL_ID = "Id";
/** System catalog column name of the name of a system object */
private static final String CAT_COL_NAME = "Name";
private static final String CAT_COL_OWNER = "Owner";
/** System catalog column name of a system object's parent's id */
private static final String CAT_COL_PARENT_ID = "ParentId";
/** System catalog column name of the type of a system object */
private static final String CAT_COL_TYPE = "Type";
/** System catalog column name of the date a system object was created */
private static final String CAT_COL_DATE_CREATE = "DateCreate";
/** System catalog column name of the date a system object was updated */
private static final String CAT_COL_DATE_UPDATE = "DateUpdate";
/** System catalog column name of the flags column */
private static final String CAT_COL_FLAGS = "Flags";
/** System catalog column name of the properties column */
static final String CAT_COL_PROPS = "LvProp";
/** System catalog column name of the remote database */
private static final String CAT_COL_DATABASE = "Database";
/** System catalog column name of the remote table name */
private static final String CAT_COL_FOREIGN_NAME = "ForeignName";
/** top-level parentid for a database */
private static final int DB_PARENT_ID = 0xF000000;
/** the maximum size of any of the included "empty db" resources */
private static final long MAX_EMPTYDB_SIZE = 370000L;
/** this object is a "system" object */
static final int SYSTEM_OBJECT_FLAG = 0x80000000;
/** this object is another type of "system" object */
static final int ALT_SYSTEM_OBJECT_FLAG = 0x02;
/** this object is hidden */
public static final int HIDDEN_OBJECT_FLAG = 0x08;
/** all flags which seem to indicate some type of system object */
static final int SYSTEM_OBJECT_FLAGS =
SYSTEM_OBJECT_FLAG | ALT_SYSTEM_OBJECT_FLAG;
/** read-only channel access mode */
public static final OpenOption[] RO_CHANNEL_OPTS =
{StandardOpenOption.READ};
/** read/write channel access mode for existing files */
public static final OpenOption[] RW_CHANNEL_OPTS =
{StandardOpenOption.READ, StandardOpenOption.WRITE};
/** read/write/create channel access mode for new files */
public static final OpenOption[] RWC_CHANNEL_OPTS =
{StandardOpenOption.READ, StandardOpenOption.WRITE,
StandardOpenOption.CREATE};
/** Name of the system object that is the parent of all tables */
private static final String SYSTEM_OBJECT_NAME_TABLES = "Tables";
/** Name of the system object that is the parent of all databases */
private static final String SYSTEM_OBJECT_NAME_DATABASES = "Databases";
/** Name of the system object that is the parent of all relationships */
private static final String SYSTEM_OBJECT_NAME_RELATIONSHIPS = "Relationships";
/** Name of the table that contains system access control entries */
private static final String TABLE_SYSTEM_ACES = "MSysACEs";
/** Name of the table that contains table relationships */
private static final String TABLE_SYSTEM_RELATIONSHIPS = "MSysRelationships";
/** Name of the table that contains queries */
private static final String TABLE_SYSTEM_QUERIES = "MSysQueries";
/** Name of the table that contains complex type information */
private static final String TABLE_SYSTEM_COMPLEX_COLS = "MSysComplexColumns";
/** Name of the main database properties object */
private static final String OBJECT_NAME_DB_PROPS = "MSysDb";
/** Name of the summary properties object */
private static final String OBJECT_NAME_SUMMARY_PROPS = "SummaryInfo";
/** Name of the user-defined properties object */
private static final String OBJECT_NAME_USERDEF_PROPS = "UserDefined";
/** System object type for table definitions */
static final Short TYPE_TABLE = 1;
/** System object type for query definitions */
private static final Short TYPE_QUERY = 5;
/** System object type for linked table definitions */
private static final Short TYPE_LINKED_TABLE = 6;
/** System object type for relationships */
private static final Short TYPE_RELATIONSHIP = 8;
/** max number of table lookups to cache */
private static final int MAX_CACHED_LOOKUP_TABLES = 50;
/** the columns to read when reading system catalog normally */
private static Collection SYSTEM_CATALOG_COLUMNS =
new HashSet(Arrays.asList(CAT_COL_NAME, CAT_COL_TYPE, CAT_COL_ID,
CAT_COL_FLAGS, CAT_COL_PARENT_ID));
/** the columns to read when finding table details */
private static Collection SYSTEM_CATALOG_TABLE_DETAIL_COLUMNS =
new HashSet(Arrays.asList(CAT_COL_NAME, CAT_COL_TYPE, CAT_COL_ID,
CAT_COL_FLAGS, CAT_COL_PARENT_ID,
CAT_COL_DATABASE, CAT_COL_FOREIGN_NAME));
/** 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));
/** regex matching characters which are invalid in identifier names */
private static final Pattern INVALID_IDENTIFIER_CHARS =
Pattern.compile("[\\p{Cntrl}.!`\\]\\[]");
/** the File of the database */
private final Path _file;
/** the simple name of the database */
private final String _name;
/** whether or not this db is read-only */
private final boolean _readOnly;
/** Buffer to hold database pages */
private ByteBuffer _buffer;
/** ID of the Tables system object */
private Integer _tableParentId;
/** Format that the containing database is in */
private final JetFormat _format;
/**
* Cache map of UPPERCASE table names to page numbers containing their
* definition and their stored table name (max size
* MAX_CACHED_LOOKUP_TABLES).
*/
private final Map _tableLookup =
new SimpleCache(MAX_CACHED_LOOKUP_TABLES);
/** set of table names as stored in the mdb file, created on demand */
private Set _tableNames;
/** Reads and writes database pages */
private final PageChannel _pageChannel;
/** System catalog table */
private TableImpl _systemCatalog;
/** utility table finder */
private TableFinder _tableFinder;
/** System access control entries table (initialized on first use) */
private TableImpl _accessControlEntries;
/** ID of the Relationships system object */
private Integer _relParentId;
/** SIDs to use for the ACEs added for new relationships */
private final List _newRelSIDs = new ArrayList();
/** System relationships table (initialized on first use) */
private TableImpl _relationships;
/** System queries table (initialized on first use) */
private TableImpl _queries;
/** System complex columns table (initialized on first use) */
private TableImpl _complexCols;
/** SIDs to use for the ACEs added for new tables */
private final List _newTableSIDs = new ArrayList();
/** optional error handler to use when row errors are encountered */
private ErrorHandler _dbErrorHandler;
/** the file format of the database */
private FileFormat _fileFormat;
/** charset to use when handling text */
private Charset _charset;
/** timezone to use when handling dates */
private TimeZone _timeZone;
/** zoneId to use when handling dates */
private ZoneId _zoneId;
/** language sort order to be used for textual columns */
private ColumnImpl.SortOrder _defaultSortOrder;
/** default code page to be used for textual columns (in some dbs) */
private Short _defaultCodePage;
/** the ordering used for table columns */
private Table.ColumnOrder _columnOrder;
/** whether or not enforcement of foreign-keys is enabled */
private boolean _enforceForeignKeys;
/** whether or not auto numbers can be directly inserted by the user */
private boolean _allowAutoNumInsert;
/** whether or not to evaluate expressions */
private boolean _evaluateExpressions;
/** factory for ColumnValidators */
private ColumnValidatorFactory _validatorFactory = SimpleColumnValidatorFactory.INSTANCE;
/** cache of in-use tables */
private final TableCache _tableCache = new TableCache();
/** handler for reading/writing properteies */
private PropertyMaps.Handler _propsHandler;
/** ID of the Databases system object */
private Integer _dbParentId;
/** owner of objects we create */
private byte[] _newObjOwner;
/** core database properties */
private PropertyMaps _dbPropMaps;
/** summary properties */
private PropertyMaps _summaryPropMaps;
/** user-defined properties */
private PropertyMaps _userDefPropMaps;
/** linked table resolver */
private LinkResolver _linkResolver;
/** any linked databases which have been opened */
private Map _linkedDbs;
/** shared state used when enforcing foreign keys */
private final FKEnforcer.SharedState _fkEnforcerSharedState =
FKEnforcer.initSharedState();
/** shared context for evaluating expressions */
private DBEvalContext _evalCtx;
/** factory for the appropriate date/time type */
private ColumnImpl.DateTimeFactory _dtf;
/**
* Open an existing Database. If the existing file is not writeable or the
* readOnly flag is {@code true}, the file will be opened read-only.
* @param mdbFile File containing the database
* @param readOnly iff {@code true}, force opening file in read-only
* mode
* @param channel pre-opened FileChannel. if provided explicitly, it will
* not be closed by this Database instance
* @param autoSync whether or not to enable auto-syncing on write. if
* {@code true}, writes will be immediately flushed to disk.
* This leaves the database in a (fairly) consistent state
* on each write, but can be very inefficient for many
* updates. if {@code false}, flushing to disk happens at
* the jvm's leisure, which can be much faster, but may
* leave the database in an inconsistent state if failures
* are encountered during writing. Writes may be flushed at
* any time using {@link #flush}.
* @param charset Charset to use, if {@code null}, uses default
* @param timeZone TimeZone to use, if {@code null}, uses default
* @param provider CodecProvider for handling page encoding/decoding, may be
* {@code null} if no special encoding is necessary
* @usage _advanced_method_
*/
public static DatabaseImpl open(
Path mdbFile, boolean readOnly, FileChannel channel,
boolean autoSync, Charset charset, TimeZone timeZone,
CodecProvider provider)
throws IOException
{
boolean closeChannel = false;
if(channel == null) {
if(!Files.isReadable(mdbFile)) {
throw new FileNotFoundException("given file does not exist: " +
mdbFile);
}
// force read-only for non-writable files
readOnly |= !Files.isWritable(mdbFile);
// open file channel
channel = openChannel(mdbFile, readOnly, false);
closeChannel = true;
}
boolean success = false;
try {
if(!readOnly) {
// verify that format supports writing
JetFormat jetFormat = JetFormat.getFormat(channel);
if(jetFormat.READ_ONLY) {
// wrap the channel with a read-only version to enforce
// non-writability
channel = new ReadOnlyFileChannel(channel);
readOnly = true;
}
}
DatabaseImpl db = new DatabaseImpl(mdbFile, channel, closeChannel, autoSync,
null, charset, timeZone, provider,
readOnly);
success = true;
return db;
} finally {
if(!success && closeChannel) {
// something blew up, shutdown the channel (quietly)
ByteUtil.closeQuietly(channel);
}
}
}
/**
* Create a new Database for the given fileFormat
* @param fileFormat version of new database.
* @param mdbFile Location to write the new database to. If this file
* already exists, it will be overwritten.
* @param channel pre-opened FileChannel. if provided explicitly, it will
* not be closed by this Database instance
* @param autoSync whether or not to enable auto-syncing on write. if
* {@code true}, writes will be immediately flushed to disk.
* This leaves the database in a (fairly) consistent state
* on each write, but can be very inefficient for many
* updates. if {@code false}, flushing to disk happens at
* the jvm's leisure, which can be much faster, but may
* leave the database in an inconsistent state if failures
* are encountered during writing. Writes may be flushed at
* any time using {@link #flush}.
* @param charset Charset to use, if {@code null}, uses default
* @param timeZone TimeZone to use, if {@code null}, uses default
* @usage _advanced_method_
*/
public static DatabaseImpl create(FileFormat fileFormat, Path mdbFile,
FileChannel channel, boolean autoSync,
Charset charset, TimeZone timeZone)
throws IOException
{
FileFormatDetails details = getFileFormatDetails(fileFormat);
if (details.getFormat().READ_ONLY) {
throw new IOException("File format " + fileFormat +
" does not support writing for " + mdbFile);
}
if(details.getEmptyFilePath() == null) {
throw new IOException("File format " + fileFormat +
" does not support file creation for " + mdbFile);
}
boolean closeChannel = false;
if(channel == null) {
channel = openChannel(mdbFile, false, true);
closeChannel = true;
}
boolean success = false;
try {
channel.truncate(0);
transferDbFrom(channel, getResourceAsStream(details.getEmptyFilePath()));
channel.force(true);
DatabaseImpl db = new DatabaseImpl(mdbFile, channel, closeChannel, autoSync,
fileFormat, charset, timeZone, null,
false);
success = true;
return db;
} finally {
if(!success && closeChannel) {
// something blew up, shutdown the channel (quietly)
ByteUtil.closeQuietly(channel);
}
}
}
/**
* Package visible only to support unit tests via DatabaseTest.openChannel().
* @param mdbFile file to open
* @param readOnly true if read-only
* @return a FileChannel on the given file.
* @exception FileNotFoundException
* if the mode is "r" but the given file object does
* not denote an existing regular file, or if the mode begins
* with "rw" but the given file object does not denote
* an existing, writable regular file and a new regular file of
* that name cannot be created, or if some other error occurs
* while opening or creating the file
*/
static FileChannel openChannel(
Path mdbFile, boolean readOnly, boolean create)
throws IOException
{
OpenOption[] opts = (readOnly ? RO_CHANNEL_OPTS :
(create ? RWC_CHANNEL_OPTS : RW_CHANNEL_OPTS));
return FileChannel.open(mdbFile, opts);
}
/**
* Create a new database by reading it in from a FileChannel.
* @param file the File to which the channel is connected
* @param channel File channel of the database. This needs to be a
* FileChannel instead of a ReadableByteChannel because we need to
* randomly jump around to various points in the file.
* @param autoSync whether or not to enable auto-syncing on write. if
* {@code true}, writes will be immediately flushed to disk.
* This leaves the database in a (fairly) consistent state
* on each write, but can be very inefficient for many
* updates. if {@code false}, flushing to disk happens at
* the jvm's leisure, which can be much faster, but may
* leave the database in an inconsistent state if failures
* are encountered during writing. Writes may be flushed at
* any time using {@link #flush}.
* @param fileFormat version of new database (if known)
* @param charset Charset to use, if {@code null}, uses default
* @param timeZone TimeZone to use, if {@code null}, uses default
*/
protected DatabaseImpl(Path file, FileChannel channel, boolean closeChannel,
boolean autoSync, FileFormat fileFormat, Charset charset,
TimeZone timeZone, CodecProvider provider,
boolean readOnly)
throws IOException
{
_file = file;
_name = getName(file);
_readOnly = readOnly;
_format = JetFormat.getFormat(channel);
_charset = ((charset == null) ? getDefaultCharset(_format) : charset);
_columnOrder = getDefaultColumnOrder();
_enforceForeignKeys = getDefaultEnforceForeignKeys();
_allowAutoNumInsert = getDefaultAllowAutoNumberInsert();
_evaluateExpressions = getDefaultEvaluateExpressions();
_fileFormat = fileFormat;
setZoneInfo(timeZone, null);
_dtf = ColumnImpl.getDateTimeFactory(getDefaultDateTimeType());
_pageChannel = new PageChannel(channel, closeChannel, _format, autoSync);
if(provider == null) {
provider = DefaultCodecProvider.INSTANCE;
}
// note, it's slighly sketchy to pass ourselves along partially
// constructed, but only our _format and _pageChannel refs should be
// needed
_pageChannel.initialize(this, provider);
_buffer = _pageChannel.createPageBuffer();
readSystemCatalog();
}
@Override
public File getFile() {
return ((_file != null) ? _file.toFile() : null);
}
@Override
public Path getPath() {
return _file;
}
public String getName() {
return _name;
}
public boolean isReadOnly() {
return _readOnly;
}
/**
* @usage _advanced_method_
*/
public PageChannel getPageChannel() {
return _pageChannel;
}
/**
* @usage _advanced_method_
*/
public JetFormat getFormat() {
return _format;
}
/**
* @return The system catalog table
* @usage _advanced_method_
*/
public TableImpl getSystemCatalog() {
return _systemCatalog;
}
/**
* @return The system Access Control Entries table (loaded on demand)
* @usage _advanced_method_
*/
public TableImpl getAccessControlEntries() throws IOException {
if(_accessControlEntries == null) {
_accessControlEntries = getRequiredSystemTable(TABLE_SYSTEM_ACES);
}
return _accessControlEntries;
}
/**
* @return the complex column system table (loaded on demand)
* @usage _advanced_method_
*/
public TableImpl getSystemComplexColumns() throws IOException {
if(_complexCols == null) {
_complexCols = getRequiredSystemTable(TABLE_SYSTEM_COMPLEX_COLS);
}
return _complexCols;
}
@Override
public ErrorHandler getErrorHandler() {
return((_dbErrorHandler != null) ? _dbErrorHandler : ErrorHandler.DEFAULT);
}
@Override
public void setErrorHandler(ErrorHandler newErrorHandler) {
_dbErrorHandler = newErrorHandler;
}
@Override
public LinkResolver getLinkResolver() {
return((_linkResolver != null) ? _linkResolver : LinkResolver.DEFAULT);
}
@Override
public void setLinkResolver(LinkResolver newLinkResolver) {
_linkResolver = newLinkResolver;
}
@Override
public Map getLinkedDatabases() {
return ((_linkedDbs == null) ? Collections.emptyMap() :
Collections.unmodifiableMap(_linkedDbs));
}
@Override
public boolean isLinkedTable(Table table) throws IOException {
if((table == null) || (this == table.getDatabase())) {
// if the table is null or this db owns the table, not linked
return false;
}
// common case, local table name == remote table name
TableInfo tableInfo = lookupTable(table.getName());
if((tableInfo != null) && tableInfo.isLinked() &&
matchesLinkedTable(table, ((LinkedTableInfo)tableInfo).linkedTableName,
((LinkedTableInfo)tableInfo).linkedDbName)) {
return true;
}
// but, the local table name may not match the remote table name, so we
// need to do a search if the common case fails
return _tableFinder.isLinkedTable(table);
}
private boolean matchesLinkedTable(Table table, String linkedTableName,
String linkedDbName) {
return (table.getName().equalsIgnoreCase(linkedTableName) &&
(_linkedDbs != null) &&
(_linkedDbs.get(linkedDbName) == table.getDatabase()));
}
@Override
public TimeZone getTimeZone() {
return _timeZone;
}
@Override
public void setTimeZone(TimeZone newTimeZone) {
setZoneInfo(newTimeZone, null);
}
@Override
public ZoneId getZoneId() {
return _zoneId;
}
@Override
public void setZoneId(ZoneId newZoneId) {
setZoneInfo(null, newZoneId);
}
private void setZoneInfo(TimeZone newTimeZone, ZoneId newZoneId) {
if(newTimeZone != null) {
newZoneId = newTimeZone.toZoneId();
} else if(newZoneId != null) {
newTimeZone = TimeZone.getTimeZone(newZoneId);
} else {
newTimeZone = getDefaultTimeZone();
newZoneId = newTimeZone.toZoneId();
}
_timeZone = newTimeZone;
_zoneId = newZoneId;
}
@Override
public DateTimeType getDateTimeType() {
return _dtf.getType();
}
@Override
public void setDateTimeType(DateTimeType dateTimeType) {
_dtf = ColumnImpl.getDateTimeFactory(dateTimeType);
}
@Override
public ColumnImpl.DateTimeFactory getDateTimeFactory() {
return _dtf;
}
@Override
public Charset getCharset()
{
return _charset;
}
@Override
public void setCharset(Charset newCharset) {
if(newCharset == null) {
newCharset = getDefaultCharset(getFormat());
}
_charset = newCharset;
}
@Override
public Table.ColumnOrder getColumnOrder() {
return _columnOrder;
}
@Override
public void setColumnOrder(Table.ColumnOrder newColumnOrder) {
if(newColumnOrder == null) {
newColumnOrder = getDefaultColumnOrder();
}
_columnOrder = newColumnOrder;
}
@Override
public boolean isEnforceForeignKeys() {
return _enforceForeignKeys;
}
@Override
public void setEnforceForeignKeys(Boolean newEnforceForeignKeys) {
if(newEnforceForeignKeys == null) {
newEnforceForeignKeys = getDefaultEnforceForeignKeys();
}
_enforceForeignKeys = newEnforceForeignKeys;
}
@Override
public boolean isAllowAutoNumberInsert() {
return _allowAutoNumInsert;
}
@Override
public void setAllowAutoNumberInsert(Boolean allowAutoNumInsert) {
if(allowAutoNumInsert == null) {
allowAutoNumInsert = getDefaultAllowAutoNumberInsert();
}
_allowAutoNumInsert = allowAutoNumInsert;
}
@Override
public boolean isEvaluateExpressions() {
return _evaluateExpressions;
}
@Override
public void setEvaluateExpressions(Boolean evaluateExpressions) {
if(evaluateExpressions == null) {
evaluateExpressions = getDefaultEvaluateExpressions();
}
_evaluateExpressions = evaluateExpressions;
}
@Override
public ColumnValidatorFactory getColumnValidatorFactory() {
return _validatorFactory;
}
@Override
public void setColumnValidatorFactory(ColumnValidatorFactory newFactory) {
if(newFactory == null) {
newFactory = SimpleColumnValidatorFactory.INSTANCE;
}
_validatorFactory = newFactory;
}
/**
* @usage _advanced_method_
*/
FKEnforcer.SharedState getFKEnforcerSharedState() {
return _fkEnforcerSharedState;
}
@Override
public EvalConfig getEvalConfig() {
return getEvalContext();
}
/**
* @usage _advanced_method_
*/
DBEvalContext getEvalContext() {
if(_evalCtx == null) {
_evalCtx = new DBEvalContext(this);
}
return _evalCtx;
}
/**
* Returns a SimpleDateFormat for the given format string which is
* configured with a compatible Calendar instance (see
* {@link DatabaseBuilder#toCompatibleCalendar}) and this database's
* {@link TimeZone}.
*/
public SimpleDateFormat createDateFormat(String formatStr) {
SimpleDateFormat sdf = DatabaseBuilder.createDateFormat(formatStr);
sdf.setTimeZone(getTimeZone());
return sdf;
}
/**
* @returns the current handler for reading/writing properties, creating if
* necessary
*/
private PropertyMaps.Handler getPropsHandler() {
if(_propsHandler == null) {
_propsHandler = new PropertyMaps.Handler(this);
}
return _propsHandler;
}
@Override
public FileFormat getFileFormat() throws IOException {
if(_fileFormat == null) {
Map possibleFileFormats =
getFormat().getPossibleFileFormats();
if(possibleFileFormats.size() == 1) {
// single possible format (null key), easy enough
_fileFormat = possibleFileFormats.get(null);
} else {
// need to check the "AccessVersion" property
String accessVersion = (String)getDatabaseProperties().getValue(
PropertyMap.ACCESS_VERSION_PROP);
if(isBlank(accessVersion)) {
// no access version, fall back to "generic"
accessVersion = null;
}
_fileFormat = possibleFileFormats.get(accessVersion);
if(_fileFormat == null) {
throw new IllegalStateException(withErrorContext(
"Could not determine FileFormat"));
}
}
}
return _fileFormat;
}
/**
* @return a (possibly cached) page ByteBuffer for internal use. the
* returned buffer should be released using
* {@link #releaseSharedBuffer} when no longer in use
*/
private ByteBuffer takeSharedBuffer() {
// we try to re-use a single shared _buffer, but occassionally, it may be
// needed by multiple operations at the same time (e.g. loading a
// secondary table while loading a primary table). this method ensures
// that we don't corrupt the _buffer, but instead force the second caller
// to use a new buffer.
if(_buffer != null) {
ByteBuffer curBuffer = _buffer;
_buffer = null;
return curBuffer;
}
return _pageChannel.createPageBuffer();
}
/**
* Relinquishes use of a page ByteBuffer returned by
* {@link #takeSharedBuffer}.
*/
private void releaseSharedBuffer(ByteBuffer buffer) {
// we always stuff the returned buffer back into _buffer. it doesn't
// really matter if multiple values over-write, at the end of the day, we
// just need one shared buffer
_buffer = buffer;
}
/**
* @return the currently configured database default language sort order for
* textual columns
* @usage _intermediate_method_
*/
public ColumnImpl.SortOrder getDefaultSortOrder() throws IOException {
if(_defaultSortOrder == null) {
initRootPageInfo();
}
return _defaultSortOrder;
}
/**
* @return the currently configured database default code page for textual
* data (may not be relevant to all database versions)
* @usage _intermediate_method_
*/
public short getDefaultCodePage() throws IOException {
if(_defaultCodePage == null) {
initRootPageInfo();
}
return _defaultCodePage;
}
/**
* Reads various config info from the db page 0.
*/
private void initRootPageInfo() throws IOException {
ByteBuffer buffer = takeSharedBuffer();
try {
_pageChannel.readPage(buffer, 0);
_defaultSortOrder = ColumnImpl.readSortOrder(
buffer, _format.OFFSET_SORT_ORDER, _format);
_defaultCodePage = buffer.getShort(_format.OFFSET_CODE_PAGE);
} finally {
releaseSharedBuffer(buffer);
}
}
/**
* @return a PropertyMaps instance decoded from the given bytes (always
* returns non-{@code null} result).
* @usage _intermediate_method_
*/
public PropertyMaps readProperties(byte[] propsBytes, int objectId,
RowIdImpl rowId)
throws IOException
{
return getPropsHandler().read(propsBytes, objectId, rowId, null);
}
/**
* Read the system catalog
*/
private void readSystemCatalog() throws IOException {
_systemCatalog = readTable(TABLE_SYSTEM_CATALOG, PAGE_SYSTEM_CATALOG,
SYSTEM_OBJECT_FLAGS);
try {
_tableFinder = new DefaultTableFinder(
_systemCatalog.newCursor()
.setIndexByColumnNames(CAT_COL_PARENT_ID, CAT_COL_NAME)
.setColumnMatcher(CaseInsensitiveColumnMatcher.INSTANCE)
.toIndexCursor());
} catch(IllegalArgumentException e) {
if(LOG.isDebugEnabled()) {
LOG.debug(withErrorContext(
"Could not find expected index on table " +
_systemCatalog.getName()));
}
// use table scan instead
_tableFinder = new FallbackTableFinder(
_systemCatalog.newCursor()
.setColumnMatcher(CaseInsensitiveColumnMatcher.INSTANCE)
.toCursor());
}
_tableParentId = _tableFinder.findObjectId(DB_PARENT_ID,
SYSTEM_OBJECT_NAME_TABLES);
if(_tableParentId == null) {
throw new IOException(withErrorContext(
"Did not find required parent table id"));
}
if (LOG.isDebugEnabled()) {
LOG.debug(withErrorContext(
"Finished reading system catalog. Tables: " + getTableNames()));
}
}
@Override
public Set getTableNames() throws IOException {
if(_tableNames == null) {
_tableNames = getTableNames(true, false, true);
}
return _tableNames;
}
@Override
public Set getSystemTableNames() throws IOException {
return getTableNames(false, true, false);
}
private Set getTableNames(boolean normalTables, boolean systemTables,
boolean linkedTables)
throws IOException
{
Set tableNames = new TreeSet(String.CASE_INSENSITIVE_ORDER);
_tableFinder.getTableNames(tableNames, normalTables, systemTables,
linkedTables);
return tableNames;
}
@Override
public Iterator iterator() {
try {
return new TableIterator(getTableNames());
} catch(IOException e) {
throw new RuntimeIOException(e);
}
}
public Iterator iterator(TableIterableBuilder builder) {
try {
return new TableIterator(getTableNames(builder.isIncludeNormalTables(),
builder.isIncludeSystemTables(),
builder.isIncludeLinkedTables()));
} catch(IOException e) {
throw new RuntimeIOException(e);
}
}
@Override
public TableIterableBuilder newIterable() {
return new TableIterableBuilder(this);
}
@Override
public TableImpl getTable(String name) throws IOException {
return getTable(name, false);
}
@Override
public TableMetaData getTableMetaData(String name) throws IOException {
return getTableInfo(name, true);
}
/**
* @param tableDefPageNumber the page number of a table definition
* @return The table, or null if it doesn't exist
* @usage _advanced_method_
*/
public TableImpl getTable(int tableDefPageNumber) throws IOException {
// first, check for existing table
TableImpl table = _tableCache.get(tableDefPageNumber);
if(table != null) {
return table;
}
// lookup table info from system catalog
Row objectRow = _tableFinder.getObjectRow(
tableDefPageNumber, SYSTEM_CATALOG_COLUMNS);
if(objectRow == null) {
return null;
}
String name = objectRow.getString(CAT_COL_NAME);
int flags = objectRow.getInt(CAT_COL_FLAGS);
return readTable(name, tableDefPageNumber, flags);
}
/**
* @param name Table name
* @param includeSystemTables whether to consider returning a system table
* @return The table, or null if it doesn't exist
*/
protected TableImpl getTable(String name, boolean includeSystemTables)
throws IOException
{
TableInfo tableInfo = getTableInfo(name, includeSystemTables);
return ((tableInfo != null) ?
getTable(tableInfo, includeSystemTables) : null);
}
private TableInfo getTableInfo(String name, boolean includeSystemTables)
throws IOException
{
TableInfo tableInfo = lookupTable(name);
if ((tableInfo == null) || (tableInfo.pageNumber == null)) {
return null;
}
if(!includeSystemTables && tableInfo.isSystem()) {
return null;
}
return tableInfo;
}
private TableImpl getTable(TableInfo tableInfo, boolean includeSystemTables)
throws IOException
{
if(tableInfo.isLinked()) {
if(_linkedDbs == null) {
_linkedDbs = new HashMap();
}
String linkedDbName = ((LinkedTableInfo)tableInfo).linkedDbName;
String linkedTableName = ((LinkedTableInfo)tableInfo).linkedTableName;
Database linkedDb = _linkedDbs.get(linkedDbName);
if(linkedDb == null) {
linkedDb = getLinkResolver().resolveLinkedDatabase(this, linkedDbName);
_linkedDbs.put(linkedDbName, linkedDb);
}
return ((DatabaseImpl)linkedDb).getTable(linkedTableName,
includeSystemTables);
}
return readTable(tableInfo.tableName, tableInfo.pageNumber,
tableInfo.flags);
}
/**
* Create a new table in this database
* @param name Name of the table to create
* @param columns List of Columns in the table
* @deprecated use {@link TableBuilder} instead
*/
@Deprecated
public void createTable(String name, List columns)
throws IOException
{
createTable(name, columns, null);
}
/**
* Create a new table in this database
* @param name Name of the table to create
* @param columns List of Columns in the table
* @param indexes List of IndexBuilders describing indexes for the table
* @deprecated use {@link TableBuilder} instead
*/
@Deprecated
public void createTable(String name, List columns,
List indexes)
throws IOException
{
new TableBuilder(name)
.addColumns(columns)
.addIndexes(indexes)
.toTable(this);
}
@Override
public void createLinkedTable(String name, String linkedDbName,
String linkedTableName)
throws IOException
{
if(lookupTable(name) != null) {
throw new IllegalArgumentException(withErrorContext(
"Cannot create linked table with name of existing table '" + name +
"'"));
}
validateIdentifierName(name, getFormat().MAX_TABLE_NAME_LENGTH, "table");
validateName(linkedDbName, DataType.MEMO.getMaxSize(),
"linked database");
validateIdentifierName(linkedTableName, getFormat().MAX_TABLE_NAME_LENGTH,
"linked table");
getPageChannel().startWrite();
try {
int linkedTableId = _tableFinder.getNextFreeSyntheticId();
addNewTable(name, linkedTableId, TYPE_LINKED_TABLE, linkedDbName,
linkedTableName);
} finally {
getPageChannel().finishWrite();
}
}
/**
* Adds a newly created table to the relevant internal database structures.
*/
void addNewTable(String name, int tdefPageNumber, Short type,
String linkedDbName, String linkedTableName)
throws IOException
{
//Add this table to our internal list.
addTable(name, Integer.valueOf(tdefPageNumber), type, linkedDbName,
linkedTableName);
//Add this table to system tables
addToSystemCatalog(name, tdefPageNumber, type, linkedDbName,
linkedTableName, _tableParentId);
addToAccessControlEntries(tdefPageNumber, _tableParentId, _newTableSIDs);
}
@Override
public List getRelationships(Table table1, Table table2)
throws IOException
{
return getRelationships((TableImpl)table1, (TableImpl)table2);
}
public List getRelationships(
TableImpl table1, TableImpl table2)
throws IOException
{
int nameCmp = table1.getName().compareTo(table2.getName());
if(nameCmp == 0) {
throw new IllegalArgumentException(withErrorContext(
"Must provide two different tables"));
}
if(nameCmp > 0) {
// we "order" the two tables given so that we will return a collection
// of relationships in the same order regardless of whether we are given
// (TableFoo, TableBar) or (TableBar, TableFoo).
TableImpl tmp = table1;
table1 = table2;
table2 = tmp;
}
return getRelationshipsImpl(table1, table2, true);
}
@Override
public List getRelationships(Table table)
throws IOException
{
if(table == null) {
throw new IllegalArgumentException(withErrorContext("Must provide a table"));
}
// since we are getting relationships specific to certain table include
// all tables
return getRelationshipsImpl((TableImpl)table, null, true);
}
@Override
public List getRelationships()
throws IOException
{
return getRelationshipsImpl(null, null, false);
}
@Override
public List getSystemRelationships()
throws IOException
{
return getRelationshipsImpl(null, null, true);
}
private List getRelationshipsImpl(
TableImpl table1, TableImpl table2, boolean includeSystemTables)
throws IOException
{
initRelationships();
List relationships = new ArrayList();
if(table1 != null) {
Cursor cursor = createCursorWithOptionalIndex(
_relationships, REL_COL_FROM_TABLE, table1.getName());
collectRelationships(cursor, table1, table2, relationships,
includeSystemTables);
cursor = createCursorWithOptionalIndex(
_relationships, REL_COL_TO_TABLE, table1.getName());
collectRelationships(cursor, table2, table1, relationships,
includeSystemTables);
} else {
collectRelationships(new CursorBuilder(_relationships).toCursor(),
null, null, relationships, includeSystemTables);
}
return relationships;
}
RelationshipImpl writeRelationship(RelationshipCreator creator)
throws IOException
{
initRelationships();
String name = createRelationshipName(creator);
RelationshipImpl newRel = creator.createRelationshipImpl(name);
ColumnImpl ccol = _relationships.getColumn(REL_COL_COLUMN_COUNT);
ColumnImpl flagCol = _relationships.getColumn(REL_COL_FLAGS);
ColumnImpl icol = _relationships.getColumn(REL_COL_COLUMN_INDEX);
ColumnImpl nameCol = _relationships.getColumn(REL_COL_NAME);
ColumnImpl fromTableCol = _relationships.getColumn(REL_COL_FROM_TABLE);
ColumnImpl fromColCol = _relationships.getColumn(REL_COL_FROM_COLUMN);
ColumnImpl toTableCol = _relationships.getColumn(REL_COL_TO_TABLE);
ColumnImpl toColCol = _relationships.getColumn(REL_COL_TO_COLUMN);
int numCols = newRel.getFromColumns().size();
List