com.continuent.tungsten.replicator.database
Class AbstractDatabase

java.lang.Object
  extended by com.continuent.tungsten.replicator.database.AbstractDatabase
All Implemented Interfaces:
Database
Direct Known Subclasses:
DerbyDatabase, DrizzleDatabase, GreenplumDatabase, MySQLDatabase, OracleDatabase, PostgreSQLDatabase

public abstract class AbstractDatabase
extends java.lang.Object
implements Database

Provides a generic implementation for Database interface. Subclasses must supply at least the implementation for method columnToTypeString(), which converts values from java.sql.Types to DBMS-specific column specifications.

Version:
1.0
Author:
Scott Martin

Field Summary
protected  boolean autoCommit
           
protected  boolean connected
           
protected  java.sql.Connection dbConn
           
protected  java.lang.String dbDriver
           
protected  DBMS dbms
           
protected  java.lang.String dbPassword
           
protected  java.lang.String dbUri
           
protected  java.lang.String dbUser
           
protected  java.lang.String defaultSchema
           
protected static java.util.Map<java.lang.String,java.lang.Class<?>> drivers
           
protected  boolean privileged
           
 
Fields inherited from interface com.continuent.tungsten.replicator.database.Database
MYSQL, ORACLE, POSTGRESQL, UNKNOWN
 
Constructor Summary
AbstractDatabase()
          Create a new database instance.
 
Method Summary
 void close()
          Closes the instance and frees all resources.
protected abstract  java.lang.String columnToTypeString(Column c, java.lang.String tableType)
          Return a properly constructed type specification for the column.
 void commit()
          Commit the current transaction.
 void connect()
          Connects to the database.
 void connect(boolean binlog)
          Connects to the database.
 void consistencyCheck(Table ct, ConsistencyCheck cc)
          Runs consistency check transaction
 void consistencyCheck(Table ct, ConsistencyCheck cc, java.lang.String masterCrc, int masterCnt)
          Runs consistency check transaction:
a.) If masterCrc==null - normal (master side) consistency check is executed, which is expected to be replicated to the slaves by replication.
b.) If masterCrc!=null, then masterCrc and masterCnt are put into corresponding columns, while executed check's results are put into this_crc and this_cnt columns.
 void controlSessionLevelLogging(boolean suppressed)
          Sets session-level logging of updates.
 void createSchema(java.lang.String schema)
          Creates the named schema.
 java.sql.Statement createStatement()
          Generate a JDBC statement.
 void createTable(Table t, boolean replace)
          Creates a table using the supplied table definition.
 void createTable(Table table, boolean replace, java.lang.String tungstenTableType)
          Stub routine that ignores table type.
 void createTable(Table table, boolean replace, java.lang.String tungstenSchema, java.lang.String tungstenTableType)
          
 void createTable(Table table, boolean replace, java.lang.String tungstenSchema, java.lang.String tungstenTableType, java.lang.String serviceName)
          
 void createUser(User user)
          Creates a user.
 int delete(Table table, boolean allRows)
          Deletes a row in a table.
 void disconnect()
          Disconnects from the database.
 void dropSchema(java.lang.String schema)
          Drops the named schema.
 void dropTable(Table table)
          Drops an existing table.
 void dropTungstenCatalog(java.lang.String schemaName, java.lang.String tungstenTableType, java.lang.String serviceName)
          dropTungstenCatalog removes Tungsten catalog.
 void dropUser(User user, boolean ignore)
          Deletes a user.
 void execute(java.lang.String SQL)
          Executes a SQL request.
 void executeUpdate(java.lang.String SQL)
          Executes a SQL request containing an update.
 Table findTable(int tableID)
          Return the Table with all its current accompanying Columns that matches tableID.
 Table findTable(int tableID, java.lang.String scn)
          Return the Table with all its accompanying Columns at the provided scn that matches tableID.
 Table findTable(java.lang.String schemaName, java.lang.String tableName)
          Return the Table with all its accompanying Columns.
 byte[] getBlobAsBytes(java.sql.ResultSet resultSet, int column)
          Fetches the given column as a byte[] array.
abstract  java.sql.ResultSet getColumnsResultSet(java.sql.DatabaseMetaData md, java.lang.String schemaName, java.lang.String tableName)
          This function should be implemented in concrete class.
 java.sql.Connection getConnection()
          Returns the database connection
 java.lang.String getControlTimestampQuery(java.lang.Long timestamp)
          Returns a query that can be used to set the timestamp.
 java.sql.DatabaseMetaData getDatabaseMetaData()
          Returns JDBC DatabaseMetadata for the current connection.
 java.lang.String getDatabaseObjectName(java.lang.String name)
          Returns the eventually quoted database object name.
 java.lang.String getPlaceHolder(OneRowChange.ColumnSpec col, java.lang.Object colValue, java.lang.String typeDesc)
          Return a place holder in a prepared statement for a column of type ColumnSpec.
protected abstract  java.sql.ResultSet getPrimaryKeyResultSet(java.sql.DatabaseMetaData md, java.lang.String schemaName, java.lang.String tableName)
          This function should be implemented in concrete class.
 java.util.ArrayList<java.lang.String> getReservedWords()
          Returns a list of reserved words used by the DBMS, which cannot be used as table and column names.
Words are expected to be upper case and applications checking with something similar to getReservedWords().contains(table.getName().toUpperCase()).
 java.lang.String getSessionVariable(java.lang.String name)
          Gets a variable on the current session.
abstract  SqlOperationMatcher getSqlNameMatcher()
          Returns a SQL name matcher for this database type.
 java.util.ArrayList<Table> getTables(java.lang.String schemaName, boolean baseTablesOnly)
          Implement ability to fetch tables.
protected abstract  java.sql.ResultSet getTablesResultSet(java.sql.DatabaseMetaData md, java.lang.String schemaName, boolean baseTablesOnly)
          This function should be implemented in concrete class.
 DBMS getType()
          Returns the type of DBMS behind the interface
 java.lang.String getUseSchemaQuery(java.lang.String schema)
          Returns a query that can be used to set the schema.
 int insert(Table table)
          Inserts a row into a table.
 boolean isPrivileged()
          Returns true if this account is a super user.
 int javaSQLTypeToNativeType(int javaSQLType)
          Opposite of the above function.
 void kill(Session session)
          Kills a particular session.
 java.util.List<Session> listSessions()
          Lists user sessions in the DBMS server.
 int nativeTypeToJavaSQLType(int nativeType)
          Databases have various column types usually identified by some vendor defined integer value.
 boolean nullsBoundDifferently(OneRowChange.ColumnSpec col)
          Return TRUE IFF NULL values are bound differently in SQL statement from non null values for the given column type.
 boolean nullsEverBoundDifferently()
          return true IFF nulls are sometimes treated differently in nullsBoundDifferently() as non nulls.
 java.lang.String prepareOptionSetStatement(java.lang.String optionName, java.lang.String optionValue)
          prepareOptionSetStatement generates the sql statement that is to be used to set an option (or a session variable) at the database connection level.
 java.sql.PreparedStatement prepareStatement(java.lang.String statement)
          Generate a JDBC prepared statement.
 void replace(Table table)
          Replaces a row in the table using the data supplied by the Table specification.
 void rollback()
          Rollback the current transaction.
 void setAutoCommit(boolean autoCommit)
          Toggles autocommit by calling Connection.setAutocommit().
 void setPassword(java.lang.String dbPassword)
          Sets the database password.
 void setPrivileged(boolean privileged)
          Determines whether this connection has superuser privileges (e.g., SYSDBA or SUPER depending on the DBMS type).
 void setSessionVariable(java.lang.String name, java.lang.String value)
          Sets a variable on the current session.
 void setUrl(java.lang.String dbUri)
          Sets the JDBC URL used for database connections.
 void setUser(java.lang.String dbUser)
          Sets the database user.
 boolean supportsControlSessionLevelLogging()
          Returns true if this implementation allow clients to turn logging of SQL updates on and off at the session level.
 boolean supportsControlTimestamp()
          By default we do not support controlling the timestamp.
 boolean supportsCreateDropSchema()
          Returns false by default as only some database types allow schema to be created dynamically.
 boolean supportsNativeSlaveSync()
          Returns true if this implementation supports synchronization of native slave replication with Tungsten position.
 boolean supportsReplace()
          Returns true if the implementation supports a SQL REPLACE command.
 boolean supportsSessionVariables()
          By default we do not support setting session variables.
 boolean supportsUseDefaultSchema()
          Returns false by default as only some database types allow schema to change.
 boolean supportsUserManagement()
          Returns true if this implementation supports user management commands.
 void syncNativeSlave(java.lang.String eventId)
          Synchronize the native slave position with Tungsten so that native replication can start properly after Tungsten goes offline.
 int update(Table table, java.util.ArrayList<Column> whereClause, java.util.ArrayList<Column> values)
          Updates on or more rows in a table.
 void useDefaultSchema(java.lang.String schema)
          Changes the default schema to the named schema.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.continuent.tungsten.replicator.database.Database
getCsvWriter, getNowFunction, getSchemas, getTimeDiff
 

Field Detail

dbms

protected DBMS dbms

dbDriver

protected java.lang.String dbDriver

dbUri

protected java.lang.String dbUri

dbUser

protected java.lang.String dbUser

dbPassword

protected java.lang.String dbPassword

privileged

protected boolean privileged

dbConn

protected java.sql.Connection dbConn

autoCommit

protected boolean autoCommit

defaultSchema

protected java.lang.String defaultSchema

drivers

protected static java.util.Map<java.lang.String,java.lang.Class<?>> drivers

connected

protected boolean connected
Constructor Detail

AbstractDatabase

public AbstractDatabase()
Create a new database instance. To use the database instance you must at minimum set the url, host, and password properties.

Method Detail

getConnection

public java.sql.Connection getConnection()
Description copied from interface: Database
Returns the database connection

Specified by:
getConnection in interface Database

getType

public DBMS getType()
Description copied from interface: Database
Returns the type of DBMS behind the interface

Specified by:
getType in interface Database

getSqlNameMatcher

public abstract SqlOperationMatcher getSqlNameMatcher()
                                               throws ReplicatorException
Description copied from interface: Database
Returns a SQL name matcher for this database type. You can get a matcher without calling connect() first.

Specified by:
getSqlNameMatcher in interface Database
Throws:
ReplicatorException

setUrl

public void setUrl(java.lang.String dbUri)
Description copied from interface: Database
Sets the JDBC URL used for database connections.

Specified by:
setUrl in interface Database

setUser

public void setUser(java.lang.String dbUser)
Description copied from interface: Database
Sets the database user.

Specified by:
setUser in interface Database

setPassword

public void setPassword(java.lang.String dbPassword)
Description copied from interface: Database
Sets the database password.

Specified by:
setPassword in interface Database

isPrivileged

public boolean isPrivileged()
Description copied from interface: Database
Returns true if this account is a super user.

Specified by:
isPrivileged in interface Database

setPrivileged

public void setPrivileged(boolean privileged)
Description copied from interface: Database
Determines whether this connection has superuser privileges (e.g., SYSDBA or SUPER depending on the DBMS type). Superusers can create logins, kill sessions, and manipulate replication.

Specified by:
setPrivileged in interface Database
Parameters:
privileged - If true this account is a superuser

getPlaceHolder

public java.lang.String getPlaceHolder(OneRowChange.ColumnSpec col,
                                       java.lang.Object colValue,
                                       java.lang.String typeDesc)
Description copied from interface: Database
Return a place holder in a prepared statement for a column of type ColumnSpec. Typically "?" as is INSERT INTO FOO VALUES(?)

Specified by:
getPlaceHolder in interface Database

nullsBoundDifferently

public boolean nullsBoundDifferently(OneRowChange.ColumnSpec col)
Description copied from interface: Database
Return TRUE IFF NULL values are bound differently in SQL statement from non null values for the given column type. For example, in Oracle, the datatype XML must look like "XMLTYPE(?)" in most SQL statements, but in the case of a NULL value, it would look simply like "?".

Specified by:
nullsBoundDifferently in interface Database

nullsEverBoundDifferently

public boolean nullsEverBoundDifferently()
Description copied from interface: Database
return true IFF nulls are sometimes treated differently in nullsBoundDifferently() as non nulls.

Specified by:
nullsEverBoundDifferently in interface Database

columnToTypeString

protected abstract java.lang.String columnToTypeString(Column c,
                                                       java.lang.String tableType)
Return a properly constructed type specification for the column. Concrete Database subclasses must implement at least this method if no others.

Parameters:
c - Column for which specification is required
Returns:
String containing specification

connect

public void connect()
             throws java.sql.SQLException
Connects to the database. You must set the url, user, and password then do this. Connection does not log queries by default.

Specified by:
connect in interface Database
Throws:
java.sql.SQLException
See Also:
Database.connect()

connect

public void connect(boolean binlog)
             throws java.sql.SQLException
Connects to the database. You must set the url, user, and password then do this.

Specified by:
connect in interface Database
Parameters:
binlog - log connection updates.
Throws:
java.sql.SQLException
See Also:
Database.connect(boolean)

disconnect

public void disconnect()
Disconnects from the database. This is also accomplished by close().

Specified by:
disconnect in interface Database
See Also:
Database.disconnect()

getDatabaseMetaData

public java.sql.DatabaseMetaData getDatabaseMetaData()
                                              throws java.sql.SQLException
Description copied from interface: Database
Returns JDBC DatabaseMetadata for the current connection.

Specified by:
getDatabaseMetaData in interface Database
Throws:
java.sql.SQLException

supportsCreateDropSchema

public boolean supportsCreateDropSchema()
Returns false by default as only some database types allow schema to be created dynamically.

Specified by:
supportsCreateDropSchema in interface Database
See Also:
Database.supportsCreateDropSchema()

createSchema

public void createSchema(java.lang.String schema)
                  throws java.sql.SQLException
Creates the named schema.

Specified by:
createSchema in interface Database
Throws:
java.sql.SQLException
See Also:
Database.createSchema(java.lang.String)

dropSchema

public void dropSchema(java.lang.String schema)
                throws java.sql.SQLException
Drops the named schema.

Specified by:
dropSchema in interface Database
Throws:
java.sql.SQLException
See Also:
Database.dropSchema(java.lang.String)

supportsUseDefaultSchema

public boolean supportsUseDefaultSchema()
Returns false by default as only some database types allow schema to change.

Specified by:
supportsUseDefaultSchema in interface Database
See Also:
Database.supportsUseDefaultSchema()

useDefaultSchema

public void useDefaultSchema(java.lang.String schema)
                      throws java.sql.SQLException
Changes the default schema to the named schema.

Specified by:
useDefaultSchema in interface Database
Throws:
java.sql.SQLException
See Also:
Database.useDefaultSchema(java.lang.String)

getUseSchemaQuery

public java.lang.String getUseSchemaQuery(java.lang.String schema)
Returns a query that can be used to set the schema.

Specified by:
getUseSchemaQuery in interface Database
See Also:
Database.getUseSchemaQuery(java.lang.String)

supportsControlSessionLevelLogging

public boolean supportsControlSessionLevelLogging()
Returns true if this implementation allow clients to turn logging of SQL updates on and off at the session level. (Currently only MySQL supports this feature.)

Specified by:
supportsControlSessionLevelLogging in interface Database
See Also:
Database.supportsControlSessionLevelLogging()

controlSessionLevelLogging

public void controlSessionLevelLogging(boolean suppressed)
                                throws java.sql.SQLException
Sets session-level logging of updates.

Specified by:
controlSessionLevelLogging in interface Database
Parameters:
suppressed - If true, updates are not logged; otherwise logging is turned on
Throws:
java.sql.SQLException
See Also:
Database.controlSessionLevelLogging(boolean)

supportsNativeSlaveSync

public boolean supportsNativeSlaveSync()
Returns true if this implementation supports synchronization of native slave replication with Tungsten position. (Currently only MySQL supports this feature.)

Specified by:
supportsNativeSlaveSync in interface Database
See Also:
Database.supportsNativeSlaveSync()

syncNativeSlave

public void syncNativeSlave(java.lang.String eventId)
                     throws java.sql.SQLException
Synchronize the native slave position with Tungsten so that native replication can start properly after Tungsten goes offline.

Specified by:
syncNativeSlave in interface Database
Parameters:
eventId - Tungsten event ID containing native log coordinates
Throws:
java.sql.SQLException
See Also:
Database.syncNativeSlave(java.lang.String)

supportsControlTimestamp

public boolean supportsControlTimestamp()
By default we do not support controlling the timestamp. Returns true if this implementation supports changing the timestamp value used by functions that return the current time.

Specified by:
supportsControlTimestamp in interface Database
See Also:
Database.supportsControlTimestamp()

getControlTimestampQuery

public java.lang.String getControlTimestampQuery(java.lang.Long timestamp)
Returns a query that can be used to set the timestamp.

Specified by:
getControlTimestampQuery in interface Database
Parameters:
timestamp - Time in milliseconds according to Java standard
See Also:
supportsControlTimestamp()

supportsSessionVariables

public boolean supportsSessionVariables()
By default we do not support setting session variables. Returns true if this implementation supports setting session variables.

Specified by:
supportsSessionVariables in interface Database
See Also:
Database.supportsSessionVariables()

setSessionVariable

public void setSessionVariable(java.lang.String name,
                               java.lang.String value)
                        throws java.sql.SQLException
Sets a variable on the current session. Sets a session variable. This works only if the database type supports session variables.

Specified by:
setSessionVariable in interface Database
Parameters:
name - Name of the variable to set
value - Value to set
Throws:
java.sql.SQLException - Thrown if setting variable is unsuccessful
See Also:
Database.setSessionVariable(java.lang.String, java.lang.String)

getSessionVariable

public java.lang.String getSessionVariable(java.lang.String name)
                                    throws java.sql.SQLException
Gets a variable on the current session. Get session variable. This works only if the database type supports session variables.

Specified by:
getSessionVariable in interface Database
Parameters:
name - Name of the variable to get
Returns:
Value of variable or null if unset
Throws:
java.sql.SQLException - Thrown if getting variable is unsuccessful
See Also:
Database.getSessionVariable(java.lang.String)

supportsUserManagement

public boolean supportsUserManagement()
Returns true if this implementation supports user management commands.

Specified by:
supportsUserManagement in interface Database
See Also:
Database.supportsUserManagement()

createUser

public void createUser(User user)
                throws java.sql.SQLException
Creates a user. Works only if the database type supports user management. This call is intended for testing.

Specified by:
createUser in interface Database
Parameters:
user - User definition
Throws:
java.sql.SQLException
See Also:
Database.createUser(com.continuent.tungsten.replicator.database.User)

dropUser

public void dropUser(User user,
                     boolean ignore)
              throws java.sql.SQLException
Deletes a user. Works only if the database type supports user management. This call is intended for testing.

Specified by:
dropUser in interface Database
Parameters:
user - User definition
ignore - If true, ignore error
Throws:
java.sql.SQLException
See Also:
Database.dropUser(com.continuent.tungsten.replicator.database.User, boolean)

listSessions

public java.util.List<Session> listSessions()
                                     throws java.sql.SQLException
Lists user sessions in the DBMS server.

Specified by:
listSessions in interface Database
Throws:
java.sql.SQLException
See Also:
Database.listSessions()

kill

public void kill(Session session)
          throws java.sql.SQLException,
                 ReplicatorException
Kills a particular session.

Specified by:
kill in interface Database
Throws:
java.sql.SQLException
ReplicatorException
See Also:
Database.kill(com.continuent.tungsten.replicator.database.Session)

execute

public void execute(java.lang.String SQL)
             throws java.sql.SQLException
Executes a SQL request.

Specified by:
execute in interface Database
Throws:
java.sql.SQLException
See Also:
Database.execute(java.lang.String)

executeUpdate

public void executeUpdate(java.lang.String SQL)
                   throws java.sql.SQLException
Executes a SQL request containing an update.

Specified by:
executeUpdate in interface Database
Throws:
java.sql.SQLException
See Also:
Database.executeUpdate(java.lang.String)

insert

public int insert(Table table)
           throws java.sql.SQLException
Inserts a row into a table. Values are taken from the current Column instances stored in the table definition.

Specified by:
insert in interface Database
Parameters:
table - Table instance containing column data
Returns:
the number of inserted rows
Throws:
java.sql.SQLException
See Also:
Database.insert(com.continuent.tungsten.replicator.database.Table)

update

public int update(Table table,
                  java.util.ArrayList<Column> whereClause,
                  java.util.ArrayList<Column> values)
           throws java.sql.SQLException
Updates on or more rows in a table.

Specified by:
update in interface Database
Parameters:
table - Table instance to update
whereClause - List of columns containing where clause values, which are ANDed
values - List of columns containing values to set for matching rows
Returns:
the number of updated rows
Throws:
java.sql.SQLException
See Also:
Database.update(com.continuent.tungsten.replicator.database.Table, java.util.ArrayList, java.util.ArrayList)

supportsReplace

public boolean supportsReplace()
Description copied from interface: Database
Returns true if the implementation supports a SQL REPLACE command.

Specified by:
supportsReplace in interface Database

replace

public void replace(Table table)
             throws java.sql.SQLException
Replaces a row in the table using the data supplied by the Table specification. The Table instance's primary key is used to locate the matching row. This uses a REPLACE command if it is available.

Specified by:
replace in interface Database
Parameters:
table - Table definition with column data and primary key
Throws:
java.sql.SQLException
See Also:
Database.replace(com.continuent.tungsten.replicator.database.Table)

delete

public int delete(Table table,
                  boolean allRows)
           throws java.sql.SQLException
Deletes a row in a table.

Specified by:
delete in interface Database
Parameters:
table - Table specification with primary key; columns for the key must be defined.
allRows - flag indicating that all rows from the underlying table should be deleted
Returns:
the number of deleted rows
Throws:
java.sql.SQLException
See Also:
Database.delete(com.continuent.tungsten.replicator.database.Table, boolean)

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String statement)
                                            throws java.sql.SQLException
Generate a JDBC prepared statement.

Specified by:
prepareStatement in interface Database
Parameters:
statement - SQL statement to prepare
Throws:
java.sql.SQLException
See Also:
Database.prepareStatement(java.lang.String)

createStatement

public java.sql.Statement createStatement()
                                   throws java.sql.SQLException
Generate a JDBC statement.

Specified by:
createStatement in interface Database
Throws:
java.sql.SQLException
See Also:
Database.createStatement()

commit

public void commit()
            throws java.sql.SQLException
Commit the current transaction.

Specified by:
commit in interface Database
Throws:
java.sql.SQLException
See Also:
Database.commit()

rollback

public void rollback()
              throws java.sql.SQLException
Rollback the current transaction.

Specified by:
rollback in interface Database
Throws:
java.sql.SQLException
See Also:
Database.rollback()

setAutoCommit

public void setAutoCommit(boolean autoCommit)
                   throws java.sql.SQLException
Toggles autocommit by calling Connection.setAutocommit().

Specified by:
setAutoCommit in interface Database
Throws:
java.sql.SQLException
See Also:
Database.setAutoCommit(boolean)

createTable

public void createTable(Table t,
                        boolean replace)
                 throws java.sql.SQLException
Creates a table using the supplied table definition.

Specified by:
createTable in interface Database
Parameters:
t - Table specification
replace - If true, replace an existing table
Throws:
java.sql.SQLException
See Also:
Database.createTable(com.continuent.tungsten.replicator.database.Table, boolean)

dropTable

public void dropTable(Table table)
Drops an existing table.

Specified by:
dropTable in interface Database
See Also:
Database.dropTable(com.continuent.tungsten.replicator.database.Table)

close

public void close()
Closes the instance and frees all resources.

Specified by:
close in interface Database
See Also:
Database.close()

nativeTypeToJavaSQLType

public int nativeTypeToJavaSQLType(int nativeType)
                            throws java.sql.SQLException
Description copied from interface: Database
Databases have various column types usually identified by some vendor defined integer value. e.g. 2 = Oracle number, 12 = Oracle date. This function converts the vendor specific type numbers into the java.sql.Type numbers we shall use internally.

Specified by:
nativeTypeToJavaSQLType in interface Database
Throws:
java.sql.SQLException

javaSQLTypeToNativeType

public int javaSQLTypeToNativeType(int javaSQLType)
                            throws java.sql.SQLException
Description copied from interface: Database
Opposite of the above function. I cannot image why any of the portable code should ever need to call this function. But it seems like a fine place to create a place holder for such a function declaration even if only called from vendor specific code.

Specified by:
javaSQLTypeToNativeType in interface Database
Throws:
java.sql.SQLException

findTable

public Table findTable(int tableID)
                throws java.sql.SQLException
Description copied from interface: Database
Return the Table with all its current accompanying Columns that matches tableID. tableID is meant to be some sort of unique "object number" interpreted within the current connection. The exact nature of of what this tableID will likely vary from rdbms to rdbms but in Oracle parlance it is an object number. Returns null if no such table exists.

Specified by:
findTable in interface Database
Throws:
java.sql.SQLException

findTable

public Table findTable(int tableID,
                       java.lang.String scn)
                throws java.sql.SQLException
Return the Table with all its accompanying Columns at the provided scn that matches tableID. tableID is meant to be some sort of unique "object number" interpreted within the current connection. The exact nature of of what this tableID will likely vary from rdbms to rdbms but in Oracle parlance it is an object number. Returns null if no such table exists.

Specified by:
findTable in interface Database
Parameters:
tableID - the object which is looked for
scn - when the object is search for
Returns:
a Table if matching was found
Throws:
java.sql.SQLException - if an error occurs
See Also:
Database.findTable(int, java.lang.String)

getColumnsResultSet

public abstract java.sql.ResultSet getColumnsResultSet(java.sql.DatabaseMetaData md,
                                                       java.lang.String schemaName,
                                                       java.lang.String tableName)
                                                throws java.sql.SQLException
This function should be implemented in concrete class.

Specified by:
getColumnsResultSet in interface Database
Parameters:
md - DatabaseMetaData object
schemaName - schema name
tableName - table name
Returns:
ResultSet as produced by DatabaseMetaData.getColumns() for a given schema and table
Throws:
java.sql.SQLException

getPrimaryKeyResultSet

protected abstract java.sql.ResultSet getPrimaryKeyResultSet(java.sql.DatabaseMetaData md,
                                                             java.lang.String schemaName,
                                                             java.lang.String tableName)
                                                      throws java.sql.SQLException
This function should be implemented in concrete class.

Parameters:
md - DatabaseMetaData object
schemaName - schema name
tableName - table name
Returns:
ResultSet as produced by DatabaseMetaData.getPrimaryKeys() for a given schema and table
Throws:
java.sql.SQLException

getTablesResultSet

protected abstract java.sql.ResultSet getTablesResultSet(java.sql.DatabaseMetaData md,
                                                         java.lang.String schemaName,
                                                         boolean baseTablesOnly)
                                                  throws java.sql.SQLException
This function should be implemented in concrete class.

Parameters:
md - DatabaseMetaData object
schemaName - schema name
baseTablesOnly - If true, return only base tables, not catalogs or views
Returns:
ResultSet as produced by DatabaseMetaData.getTables() for a given schema
Throws:
java.sql.SQLException

findTable

public Table findTable(java.lang.String schemaName,
                       java.lang.String tableName)
                throws java.sql.SQLException
Return the Table with all its accompanying Columns.

Specified by:
findTable in interface Database
Parameters:
schemaName - name of schema containing the table
tableName - name of the table
Returns:
a Table if matching was found
Throws:
java.sql.SQLException - if an error occurs
See Also:
Database.findTable(java.lang.String, java.lang.String)

getTables

public java.util.ArrayList<Table> getTables(java.lang.String schemaName,
                                            boolean baseTablesOnly)
                                     throws java.sql.SQLException
Implement ability to fetch tables. Returns a list of tables available in the schema

Specified by:
getTables in interface Database
Parameters:
schemaName - Name of the schema
baseTablesOnly - If true, only return real tables and not catalogs or views
Returns:
list of tables in the schema
Throws:
java.sql.SQLException
See Also:
Database.getTables(java.lang.String, boolean)

consistencyCheck

public void consistencyCheck(Table ct,
                             ConsistencyCheck cc)
                      throws java.sql.SQLException,
                             ConsistencyException
Runs consistency check transaction

Specified by:
consistencyCheck in interface Database
Parameters:
ct - consitency table
cc - ConsistencyCheck specification
Throws:
java.sql.SQLException
ConsistencyException
See Also:
Database.consistencyCheck(com.continuent.tungsten.replicator.database.Table, com.continuent.tungsten.replicator.consistency.ConsistencyCheck)

consistencyCheck

public void consistencyCheck(Table ct,
                             ConsistencyCheck cc,
                             java.lang.String masterCrc,
                             int masterCnt)
                      throws java.sql.SQLException,
                             ConsistencyException
Runs consistency check transaction:
a.) If masterCrc==null - normal (master side) consistency check is executed, which is expected to be replicated to the slaves by replication.
b.) If masterCrc!=null, then masterCrc and masterCnt are put into corresponding columns, while executed check's results are put into this_crc and this_cnt columns. The idea behind this is that if there's no replication running, that would usually copy over the consistency check from master to slave, it inserts consistency check results of the master to this slave, runs consistency check on this slave and updates the slave part (this_crc and this_cnt) in the created consistency row accordingly.

Specified by:
consistencyCheck in interface Database
Parameters:
ct - Consistency table.
cc - ConsistencyCheck specification.
masterCrc - CRC value of this check on the master.
masterCnt - Count of rows of this check on the master.
Throws:
java.sql.SQLException
ConsistencyException
See Also:
Database.consistencyCheck(com.continuent.tungsten.replicator.database.Table, com.continuent.tungsten.replicator.consistency.ConsistencyCheck, String, int)

createTable

public void createTable(Table table,
                        boolean replace,
                        java.lang.String tungstenTableType)
                 throws java.sql.SQLException
Stub routine that ignores table type. MySQL databases must override this.

Specified by:
createTable in interface Database
Parameters:
table - Table specification
replace - If true, replace an existing table
tungstenTableType - table type to be used for tungsten catalog tables
Throws:
java.sql.SQLException
See Also:
Database.createTable(com.continuent.tungsten.replicator.database.Table, boolean, java.lang.String)

createTable

public void createTable(Table table,
                        boolean replace,
                        java.lang.String tungstenSchema,
                        java.lang.String tungstenTableType)
                 throws java.sql.SQLException

Specified by:
createTable in interface Database
Throws:
java.sql.SQLException
See Also:
Database.createTable(com.continuent.tungsten.replicator.database.Table, boolean, java.lang.String, java.lang.String)

createTable

public void createTable(Table table,
                        boolean replace,
                        java.lang.String tungstenSchema,
                        java.lang.String tungstenTableType,
                        java.lang.String serviceName)
                 throws java.sql.SQLException

Specified by:
createTable in interface Database
Throws:
java.sql.SQLException
See Also:
Database.createTable(com.continuent.tungsten.replicator.database.Table, boolean, java.lang.String, java.lang.String, java.lang.String)

prepareOptionSetStatement

public java.lang.String prepareOptionSetStatement(java.lang.String optionName,
                                                  java.lang.String optionValue)
prepareOptionSetStatement generates the sql statement that is to be used to set an option (or a session variable) at the database connection level.

Specified by:
prepareOptionSetStatement in interface Database
Parameters:
optionName - the option to be set
optionValue - the value to be used
Returns:
a string that contains the statement that should be executed, or null if this does not exist for a database
See Also:
Database.prepareOptionSetStatement(java.lang.String, java.lang.String)

getBlobAsBytes

public byte[] getBlobAsBytes(java.sql.ResultSet resultSet,
                             int column)
                      throws java.sql.SQLException
Fetches the given column as a byte[] array. For MySQL and Oracle this means converting BLOB field into byte[] array, while for PostgreSQL - returning the bytea type field's value.

Specified by:
getBlobAsBytes in interface Database
Parameters:
resultSet - ResultSet which has this blob column.
column - Index of the column to fetch value from.
Returns:
byte[] array containing underlying BLOB or bytea field value.
Throws:
java.sql.SQLException - As this method is operating on ResultSet, it might throw a SQLException.
See Also:
Database.getBlobAsBytes(ResultSet, int)

getDatabaseObjectName

public java.lang.String getDatabaseObjectName(java.lang.String name)
Returns the eventually quoted database object name. For example, with mysql, database object names should be backticked (`example`).

Specified by:
getDatabaseObjectName in interface Database
Parameters:
name - unquoted database object name
Returns:
eventually quoted database object name
See Also:
Database.getDatabaseObjectName(java.lang.String)

getReservedWords

public java.util.ArrayList<java.lang.String> getReservedWords()
Returns a list of reserved words used by the DBMS, which cannot be used as table and column names.
Words are expected to be upper case and applications checking with something similar to getReservedWords().contains(table.getName().toUpperCase()).

Specified by:
getReservedWords in interface Database
Returns:
A list of reserved words.

dropTungstenCatalog

public void dropTungstenCatalog(java.lang.String schemaName,
                                java.lang.String tungstenTableType,
                                java.lang.String serviceName)
                         throws java.sql.SQLException
dropTungstenCatalog removes Tungsten catalog.

Specified by:
dropTungstenCatalog in interface Database
Parameters:
schemaName - The schema name where Tungsten catalog is stored
tungstenTableType - The type of table used to store Tungsten metadata
serviceName - The service name for which the catalog has to be dropped
Throws:
java.sql.SQLException
See Also:
com.continuent.tungsten.replicator.database.Database#dropTungstenCatalog(java.lang.String)