com.continuent.tungsten.replicator.database
Class DrizzleDatabase

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

public class DrizzleDatabase
extends AbstractDatabase

Implements DBMS-specific operations for MySQL.

Author:
Scott Martin, Marcus Eriksson

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
DrizzleDatabase()
           
 
Method Summary
protected  java.lang.String columnToTypeString(Column c, java.lang.String tableType)
          Return a properly constructed type specification for the column.
 void connect()
          Connect to a MySQL database, which includes setting the wait_timeout to a very high value so we don't lose our connection.
 void connect(boolean binlog)
          Connect to a Drizzle database, which includes setting the wait_timeout to a very high value so we don't lose our connection.
 void controlSessionLevelLogging(boolean suppressed)
          Sets session-level logging of updates.
 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 dropSchema(java.lang.String schema)
          Drops the named schema.
 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.lang.String getControlTimestampQuery(java.lang.Long timestamp)
          MySQL supports the 'set timestamp' command, which is what we return.
 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.
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> getSchemas()
          Returns a list of schemas available on the server.
 java.lang.String getSessionVariable(java.lang.String name)
          Gets a variable on the current session.
 SqlOperationMatcher getSqlNameMatcher()
          Returns a SQL name matcher for this database type.
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.
 void setSessionVariable(java.lang.String name, java.lang.String value)
          Sets a variable on the current session using MySQL SET command.
 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 supportsReplace()
          Returns true if the implementation supports a SQL REPLACE command.
 boolean supportsSessionVariables()
          MySQL supports session variables.
 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, consistencyCheck, consistencyCheck, createStatement, createTable, createTable, createTable, createUser, delete, disconnect, dropTable, dropTungstenCatalog, dropUser, execute, executeUpdate, findTable, findTable, findTable, getBlobAsBytes, getConnection, getDatabaseMetaData, getDatabaseObjectName, getPlaceHolder, getReservedWords, getTables, getType, insert, isPrivileged, javaSQLTypeToNativeType, kill, listSessions, nativeTypeToJavaSQLType, nullsBoundDifferently, nullsEverBoundDifferently, prepareOptionSetStatement, prepareStatement, replace, rollback, setAutoCommit, setPassword, setPrivileged, setUrl, setUser, supportsNativeSlaveSync, supportsUserManagement, syncNativeSlave, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DrizzleDatabase

public DrizzleDatabase()
                throws java.sql.SQLException
Throws:
java.sql.SQLException
Method Detail

getSqlNameMatcher

public SqlOperationMatcher getSqlNameMatcher()
                                      throws ReplicatorException
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()

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

connect

public void connect()
             throws java.sql.SQLException
Connect to a MySQL database, which includes setting the wait_timeout to a very high value so we don't lose our connection. 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
Overrides:
connect in class AbstractDatabase
Throws:
java.sql.SQLException
See Also:
AbstractDatabase.connect()

connect

public void connect(boolean binlog)
             throws java.sql.SQLException
Connect to a Drizzle database, which includes setting the wait_timeout to a very high value so we don't lose our connection. 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)

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)

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

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)

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)

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
Overrides:
supportsControlSessionLevelLogging in class AbstractDatabase
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
Overrides:
controlSessionLevelLogging in class AbstractDatabase
Parameters:
suppressed - If true, updates are not logged; otherwise logging is turned on
Throws:
java.sql.SQLException
See Also:
Database.controlSessionLevelLogging(boolean)

supportsControlTimestamp

public boolean supportsControlTimestamp()
Description copied from class: AbstractDatabase
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
Overrides:
supportsControlTimestamp in class AbstractDatabase
See Also:
Database.supportsControlTimestamp()

getControlTimestampQuery

public java.lang.String getControlTimestampQuery(java.lang.Long timestamp)
MySQL supports the 'set timestamp' command, which is what we return.

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

supportsSessionVariables

public boolean supportsSessionVariables()
MySQL supports session variables.

Specified by:
supportsSessionVariables in interface Database
Overrides:
supportsSessionVariables in class AbstractDatabase
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 using MySQL SET command. 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
Overrides:
setSessionVariable in class AbstractDatabase
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(String, String)

getSessionVariable

public java.lang.String getSessionVariable(java.lang.String name)
                                    throws java.sql.SQLException
Gets a variable on the current session.

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