com.continuent.tungsten.replicator.database
Class OracleDatabase

java.lang.Object
  extended by com.continuent.tungsten.replicator.database.AbstractDatabase
      extended by com.continuent.tungsten.replicator.database.OracleDatabase
All Implemented Interfaces:
Database

public class OracleDatabase
extends AbstractDatabase

Defines an interface to the Oracle database

Author:
Scott Martin, Stephane Giron

Field Summary
 
Fields inherited from class com.continuent.tungsten.replicator.database.AbstractDatabase
autoCommit, connected, dbConn, dbDriver, dbms, dbPassword, dbUri, dbUser, defaultSchema, drivers, privileged
 
Fields inherited from interface com.continuent.tungsten.replicator.database.Database
MYSQL, ORACLE, POSTGRESQL, UNKNOWN
 
Constructor Summary
OracleDatabase()
           
 
Method Summary
protected  java.lang.String columnToTypeString(Column c, java.lang.String tableType)
          Return a properly constructed type specification for the column.
 void connect(boolean binlog)
          In Oracle, to support timestamp with local time zone replication we need to set the session level time zone to be the same as the database time zone.
 void createSchema(java.lang.String schema)
          Creates the named schema.
 void createTable(Table t, boolean replace)
          Creates a table using the supplied table definition.
 void createTable(Table table, boolean replace, java.lang.String tungstenSchema, java.lang.String tungstenTableType, java.lang.String serviceName)
          
 void dropSchema(java.lang.String schema)
          Drops the named schema.
 void dropTungstenCatalog(java.lang.String schemaName, java.lang.String tungstenTableType, java.lang.String serviceName)
          dropTungstenCatalog removes Tungsten catalog.
 Table findTable(int tableID)
          Return the Table with all it's 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.
 java.sql.ResultSet getColumnsResultSet(java.sql.DatabaseMetaData md, java.lang.String schemaName, java.lang.String tableName)
          This function should be implemented in concrete class.
 CsvWriter getCsvWriter(java.io.BufferedWriter writer)
          Returns a properly configured CsvWriter to generate CSV according to the preferred conventions of this DBMS type.
 java.lang.String getNowFunction()
          getNowFunction returns the database-specific way to get current date and time from the database.
 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  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.util.ArrayList<java.lang.String> getSchemas()
          Returns a list of schemas available on the server.
 SqlOperationMatcher getSqlNameMatcher()
          In Oracle, to support timestamp with local time zone replication we need to set the session level time zone to be the same as the database time zone.
protected  java.sql.ResultSet getTablesResultSet(java.sql.DatabaseMetaData md, java.lang.String schemaName, boolean baseTablesOnly)
          This function should be implemented in concrete class.
 java.lang.String getTimeDiff(java.lang.String string1, java.lang.String string2)
          getTimeDiff returns the database-specific way of subtracting two "dates" and return the result in seconds complete with space for the two bind variables.
 java.lang.String getUseSchemaQuery(java.lang.String schema)
          Returns a query that can be used to set the schema.
 int javaSQLTypeToNativeType(int javaSQLType)
          Opposite of the above function.
 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.
 boolean supportsCreateDropSchema()
          Returns false by default as only some database types allow schema to be created dynamically.
 boolean supportsUseDefaultSchema()
          Returns false by default as only some database types allow schema to change.
 void useDefaultSchema(java.lang.String schema)
          Changes the default schema to the named schema.
 
Methods inherited from class com.continuent.tungsten.replicator.database.AbstractDatabase
close, commit, connect, consistencyCheck, consistencyCheck, controlSessionLevelLogging, createStatement, createTable, createTable, createUser, delete, disconnect, dropTable, dropUser, execute, executeUpdate, findTable, getBlobAsBytes, getConnection, getControlTimestampQuery, getDatabaseMetaData, getDatabaseObjectName, getSessionVariable, getTables, getType, insert, isPrivileged, kill, listSessions, prepareOptionSetStatement, prepareStatement, replace, rollback, setAutoCommit, setPassword, setPrivileged, setSessionVariable, setUrl, setUser, supportsControlSessionLevelLogging, supportsControlTimestamp, supportsNativeSlaveSync, supportsReplace, supportsSessionVariables, supportsUserManagement, syncNativeSlave, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OracleDatabase

public OracleDatabase()
Method Detail

getSqlNameMatcher

public SqlOperationMatcher getSqlNameMatcher()
                                      throws ReplicatorException
In Oracle, to support timestamp with local time zone replication we need to set the session level time zone to be the same as the database time zone. Returns a SQL name matcher for this database type. You can get a matcher without calling connect() first.

Specified by:
getSqlNameMatcher in interface Database
Specified by:
getSqlNameMatcher in class AbstractDatabase
Throws:
ReplicatorException
See Also:
AbstractDatabase.getSqlNameMatcher()

connect

public void connect(boolean binlog)
             throws java.sql.SQLException
In Oracle, to support timestamp with local time zone replication we need to set the session level time zone to be the same as the database time zone. Connects to the database. You must set the url, user, and password then do this.

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

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
Overrides:
getPlaceHolder in class AbstractDatabase

nullsBoundDifferently

public 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. 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
Overrides:
nullsBoundDifferently in class AbstractDatabase

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
Overrides:
nullsEverBoundDifferently in class AbstractDatabase

columnToTypeString

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

Specified by:
columnToTypeString in class AbstractDatabase
Parameters:
c - Column for which specification is required
Returns:
String containing specification

createTable

public void createTable(Table t,
                        boolean replace)
                 throws java.sql.SQLException
Description copied from class: AbstractDatabase
Creates a table using the supplied table definition.

Specified by:
createTable in interface Database
Overrides:
createTable in class AbstractDatabase
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)

supportsUseDefaultSchema

public boolean supportsUseDefaultSchema()
Description copied from class: AbstractDatabase
Returns false by default as only some database types allow schema to change.

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

useDefaultSchema

public void useDefaultSchema(java.lang.String schema)
                      throws java.sql.SQLException
Description copied from class: AbstractDatabase
Changes the default schema to the named schema.

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

getUseSchemaQuery

public java.lang.String getUseSchemaQuery(java.lang.String schema)
Description copied from class: AbstractDatabase
Returns a query that can be used to set the schema.

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

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
Overrides:
nativeTypeToJavaSQLType in class AbstractDatabase
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
Overrides:
javaSQLTypeToNativeType in class AbstractDatabase
Throws:
java.sql.SQLException

findTable

public Table findTable(int tableID)
                throws java.sql.SQLException
Return the Table with all it's accompanying Columns that matches tableID. tableID is meant to be some sort of unique "object number" interpretted 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
Overrides:
findTable in class AbstractDatabase
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
Overrides:
findTable in class AbstractDatabase
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:
AbstractDatabase.findTable(int, java.lang.String)

supportsCreateDropSchema

public boolean supportsCreateDropSchema()
Description copied from class: AbstractDatabase
Returns false by default as only some database types allow schema to be created dynamically.

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

createSchema

public void createSchema(java.lang.String schema)
                  throws java.sql.SQLException
Description copied from class: AbstractDatabase
Creates the named schema.

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

dropSchema

public void dropSchema(java.lang.String schema)
                throws java.sql.SQLException
Description copied from class: AbstractDatabase
Drops the named schema.

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

getSchemas

public java.util.ArrayList<java.lang.String> getSchemas()
                                                 throws java.sql.SQLException
Description copied from interface: Database
Returns a list of schemas available on the server.

Returns:
list of available schemas
Throws:
java.sql.SQLException

getColumnsResultSet

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

Specified by:
getColumnsResultSet in interface Database
Specified by:
getColumnsResultSet in class AbstractDatabase
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 java.sql.ResultSet getPrimaryKeyResultSet(java.sql.DatabaseMetaData md,
                                                    java.lang.String schemaName,
                                                    java.lang.String tableName)
                                             throws java.sql.SQLException
Description copied from class: AbstractDatabase
This function should be implemented in concrete class.

Specified by:
getPrimaryKeyResultSet in class AbstractDatabase
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 java.sql.ResultSet getTablesResultSet(java.sql.DatabaseMetaData md,
                                                java.lang.String schemaName,
                                                boolean baseTablesOnly)
                                         throws java.sql.SQLException
Description copied from class: AbstractDatabase
This function should be implemented in concrete class.

Specified by:
getTablesResultSet in class AbstractDatabase
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

getNowFunction

public java.lang.String getNowFunction()
Description copied from interface: Database
getNowFunction returns the database-specific way to get current date and time from the database.

Returns:
the name of the function to be called at the database level to get the current date and time

getTimeDiff

public java.lang.String getTimeDiff(java.lang.String string1,
                                    java.lang.String string2)
getTimeDiff returns the database-specific way of subtracting two "dates" and return the result in seconds complete with space for the two bind variables. E.g. in MySQL it might be "time_to_sec(timediff(?, ?))". If either of the string variables are null, replace with the bind character (e.g. "?") else use the string given. For example getTimeDiff(null, "myTimeCol") -> time_to_sec(timediff(?, myTimeCol))


getCsvWriter

public CsvWriter getCsvWriter(java.io.BufferedWriter writer)
Returns a properly configured CsvWriter to generate CSV according to the preferred conventions of this DBMS type.

Parameters:
writer - A buffered writer to receive CSV output
Returns:
A property configured CsvWriter instance
See Also:
Database.getCsvWriter(java.io.BufferedWriter)

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
Overrides:
createTable in class AbstractDatabase
Throws:
java.sql.SQLException
See Also:
AbstractDatabase.createTable(com.continuent.tungsten.replicator.database.Table, boolean, 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
Overrides:
getReservedWords in class AbstractDatabase
Returns:
A list of reserved words.
See Also:
Oracle Docs

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
Overrides:
dropTungstenCatalog in class AbstractDatabase
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 - when an error occurs
See Also:
com.continuent.tungsten.replicator.database.AbstractDatabase#dropTungstenCatalog(java.lang.String)