Class ForeignTableVTI
- All Implemented Interfaces:
AutoCloseable, ResultSet, Wrapper, AwareVTI, RestrictedVTI
This class contains a table function which can be used to bulk-import data from a foreign database. Because the table function is a RestrictedVTI, it can also be used to periodically and efficiently integrate data streams from a foreign database.
If you need to siphon data out of the foreign database on an ongoing basis, you can restrict the data you SELECT. Note that the local views are backed by RestrictedVTIs. That means that the actual query sent to the foreign database will only involve the columns you SELECT. In addition, the query will include the WHERE clause, provided that it is simple enough (see the javadoc for RestrictedVTI):
The following script shows how to use this table function:
-- create a foreign database with a table in it
connect 'jdbc:derby:memory:db;create=true;user=test_dbo;password=test_dbopassword';
call syscs_util.syscs_create_user( 'test_dbo', 'test_dbopassword' );
create table employee
(
firstName varchar( 50 ),
lastName varchar( 50 ),
employeeID int primary key
);
insert into employee values ( 'Billy', 'Goatgruff', 1 );
insert into employee values ( 'Mary', 'Hadalittlelamb', 2 );
connect 'jdbc:derby:memory:db;shutdown=true';
-- now create the database where we will do our work
connect 'jdbc:derby:memory:db1;create=true';
-- register a table function with the shape of the foreign table
create function employeeFunction
(
schemaName varchar( 32672 ),
tableName varchar( 32672 ),
connectionURL varchar( 32672 )
)
returns table
(
firstName varchar( 50 ),
lastName varchar( 50 ),
employeeID int
)
language java parameter style derby_jdbc_result_set no sql
external name 'org.apache.derby.vti.ForeignTableVTI.readForeignTable'
;
-- create a convenience view to factor out the function parameters
create view foreignEmployee
as select firstName, lastName, employeeID
from table
(
employeeFunction
(
'TEST_DBO',
'EMPLOYEE',
'jdbc:derby:memory:db;user=test_dbo;password=test_dbopassword'
)
) s;
-- now select from the view as though it were a local table
select * from foreignEmployee;
select lastName from foreignEmployee where employeeID = 2;
-
Nested Class Summary
Nested classes/interfaces inherited from class VTITemplate
VTITemplate.ColumnDescriptor -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String[]private int[]private static HashMap<String, Connection> /////////////////////////////////////////////////////////////////////private Stringprivate Connectionprivate PreparedStatementprivate Stringprivate Stringprivate RestrictionFields inherited from interface ResultSet
CLOSE_CURSORS_AT_COMMIT, CONCUR_READ_ONLY, CONCUR_UPDATABLE, FETCH_FORWARD, FETCH_REVERSE, FETCH_UNKNOWN, HOLD_CURSORS_OVER_COMMIT, TYPE_FORWARD_ONLY, TYPE_SCROLL_INSENSITIVE, TYPE_SCROLL_SENSITIVE -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedForeignTableVTI(String foreignSchemaName, String foreignTableName, String connectionURL) ForeignTableVTI(String foreignSchemaName, String foreignTableName, Connection foreignConnection) Construct from the foreign schema and table name and a foreign connection. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()/////////////////////////////////////////////////////////////////////static intThis function is useful for verifying that the connection to the foreign database was dropped when the foreignViews tool was unloaded.private static StringdelimitedID(String text) static voiddropConnection(String connectionURL) Remove the cached connection to the foreign database.private static ConnectiongetForeignConnection(String connectionURL, Connection foreignConnection) /////////////////////////////////////////////////////////////////////voidinitScan(String[] columnNames, Restriction restriction) /////////////////////////////////////////////////////////////////////booleanisClosed()private StringBuild the query which will be sent to the foreign database.protected intmapColumnNumber(int derbyNumber) Map a 1-based Derby column number to a 1-based column number in the foreign query.booleannext()private static PreparedStatementprepareStatement(Connection conn, String text) static ForeignTableVTIreadForeignTable(String foreignSchemaName, String foreignTableName, String connectionURL) Table function to read a table in a foreign database.Methods inherited from class ForwardingVTI
getAsciiStream, getBigDecimal, getBigDecimal, getBinaryStream, getBlob, getBoolean, getByte, getBytes, getCharacterStream, getClob, getDate, getDate, getDouble, getFloat, getInt, getLong, getMetaData, getObject, getShort, getString, getTime, getTime, getTimestamp, getTimestamp, getWrappedResultSet, wasNull, wrapResultSetMethods inherited from class VTITemplate
absolute, afterLast, beforeFirst, cancelRowUpdates, clearWarnings, deleteRow, findColumn, first, getArray, getArray, getAsciiStream, getBigDecimal, getBigDecimal, getBinaryStream, getBlob, getBoolean, getByte, getBytes, getCharacterStream, getClob, getConcurrency, getContext, getCursorName, getDate, getDate, getDouble, getFetchDirection, getFetchSize, getFloat, getHoldability, getInt, getLong, getNCharacterStream, getNCharacterStream, getNClob, getNClob, getNString, getNString, getObject, getObject, getObject, getObject, getObject, getRef, getRef, getReturnTableSignature, getRow, getRowId, getRowId, getShort, getSQLXML, getSQLXML, getStatement, getString, getTime, getTime, getTimestamp, getTimestamp, getType, getUnicodeStream, getUnicodeStream, getURL, getURL, getWarnings, insertRow, isAfterLast, isBeforeFirst, isFirst, isLast, isWrapperFor, last, moveToCurrentRow, moveToInsertRow, notImplemented, previous, refreshRow, relative, rowDeleted, rowInserted, rowUpdated, setContext, setFetchDirection, setFetchSize, unwrap, updateArray, updateArray, updateAsciiStream, updateAsciiStream, updateAsciiStream, updateAsciiStream, updateAsciiStream, updateAsciiStream, updateBigDecimal, updateBigDecimal, updateBinaryStream, updateBinaryStream, updateBinaryStream, updateBinaryStream, updateBinaryStream, updateBinaryStream, updateBlob, updateBlob, updateBlob, updateBlob, updateBlob, updateBlob, updateBoolean, updateBoolean, updateByte, updateByte, updateBytes, updateBytes, updateCharacterStream, updateCharacterStream, updateCharacterStream, updateCharacterStream, updateCharacterStream, updateCharacterStream, updateClob, updateClob, updateClob, updateClob, updateClob, updateClob, updateDate, updateDate, updateDouble, updateDouble, updateFloat, updateFloat, updateInt, updateInt, updateLong, updateLong, updateNCharacterStream, updateNCharacterStream, updateNCharacterStream, updateNCharacterStream, updateNClob, updateNClob, updateNClob, updateNClob, updateNClob, updateNClob, updateNString, updateNString, updateNull, updateNull, updateObject, updateObject, updateObject, updateObject, updateRef, updateRef, updateRow, updateRowId, updateRowId, updateShort, updateShort, updateSQLXML, updateSQLXML, updateString, updateString, updateTime, updateTime, updateTimestamp, updateTimestampMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ResultSet
updateObject, updateObject, updateObject, updateObject
-
Field Details
-
_connections
///////////////////////////////////////////////////////////////////// -
_foreignSchemaName
-
_foreignTableName
-
_connectionURL
-
_foreignConnection
-
_columnNames
-
_restriction
-
_columnNumberMap
private int[] _columnNumberMap -
_foreignPreparedStatement
-
-
Constructor Details
-
ForeignTableVTI
public ForeignTableVTI(String foreignSchemaName, String foreignTableName, Connection foreignConnection) Construct from the foreign schema and table name and a foreign connection.
-
ForeignTableVTI
-
-
Method Details
-
readForeignTable
public static ForeignTableVTI readForeignTable(String foreignSchemaName, String foreignTableName, String connectionURL) Table function to read a table in a foreign database.
- Parameters:
foreignSchemaName- Case-sensitive name of foreign schemaforeignTableName- Case-sensitive name of foreign tableconnectionURL- URL for connecting to foreign database via DriverManager.getConnection()
-
dropConnection
Remove the cached connection to the foreign database. This method is called by ForeignDBViews.unloadTool().
-
countConnections
public static int countConnections()This function is useful for verifying that the connection to the foreign database was dropped when the foreignViews tool was unloaded.
-
close
/////////////////////////////////////////////////////////////////////- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceResultSet- Overrides:
closein classForwardingVTI- Throws:
SQLException
-
next
- Specified by:
nextin interfaceResultSet- Overrides:
nextin classForwardingVTI- Throws:
SQLException
-
isClosed
public boolean isClosed()- Specified by:
isClosedin interfaceResultSet- Overrides:
isClosedin classForwardingVTI
-
initScan
/////////////////////////////////////////////////////////////////////- Specified by:
initScanin interfaceRestrictedVTI- Throws:
SQLException
-
getForeignConnection
private static Connection getForeignConnection(String connectionURL, Connection foreignConnection) throws SQLException /////////////////////////////////////////////////////////////////////- Throws:
SQLException
-
makeQuery
Build the query which will be sent to the foreign database.
-
delimitedID
-
prepareStatement
- Throws:
SQLException
-
mapColumnNumber
protected int mapColumnNumber(int derbyNumber) Map a 1-based Derby column number to a 1-based column number in the foreign query.
- Overrides:
mapColumnNumberin classForwardingVTI
-