Package weka.experiment
Class DatabaseUtils
java.lang.Object
weka.experiment.DatabaseUtils
- All Implemented Interfaces:
Serializable
,RevisionHandler
- Direct Known Subclasses:
DatabaseConnection
,DatabaseResultListener
,DbUtils
,InstanceQuery
DatabaseUtils provides utility functions for accessing the experiment
database. The jdbc driver and database to be used default to "jdbc.idbDriver"
and "jdbc:idb=experiments.prp". These may be changed by creating a java
properties file called DatabaseUtils.props in user.home or the current
directory. eg:
jdbcDriver=jdbc.idbDriver
jdbcURL=jdbc:idb=experiments.prp
- Version:
- $Revision: 13476 $
- Author:
- Len Trigg (trigg@cs.waikato.ac.nz)
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Type mapping for BOOL used for reading experiment results.static final int
Type mapping for BYTE used for reading experiment results.static final int
Type mapping for DATE used for reading experiment results.static final int
Type mapping for DOUBLE used for reading experiment results.static final String
The name of the table containing the index to experiments.static final String
The name of the column containing the results table name.static final String
The prefix for result table names.static final String
The name of the column containing the experiment setup (parameters).static final String
The name of the column containing the experiment type (ResultProducer).static final int
Type mapping for FLOAT used for reading experiment results.static final int
Type mapping for INTEGER used for reading experiment results.static final int
Type mapping for LONG used for reading experiment results.static final String
The name of the properties file.static final int
Type mapping for SHORT used for reading experiment results.static final int
Type mapping for STRING used for reading experiment results.static final int
Type mapping for TEXT used for reading, e.g., text blobs.static final int
Type mapping for TIME used for reading TIME columns.static final int
Type mapping for TIMESTAMP used for reading java.sql.Timestamp columns -
Constructor Summary
ConstructorDescriptionReads properties and sets up the database drivers.DatabaseUtils
(File propsFile) Reads the properties from the specified file and sets up the database drivers.DatabaseUtils
(Properties props) Uses the specified properties to set up the database drivers. -
Method Summary
Modifier and TypeMethodDescriptionstatic String
arrayToString
(Object[] array) Converts an array of objects to a string by inserting a space between each element.attributeCaseFix
(String columnName) returns key column headings in their original case.void
close()
closes the m_PreparedStatement to avoid memory leaks.void
closes the ResultSet and the statement that generated the ResultSet to avoid memory leaks in JDBC drivers - in contrast to the JDBC specs, a lot of JDBC drives don't clean up correctly.void
Opens a connection to the database.void
Attempts to create the experiment index table.Attempts to insert a results entry for the table into the experiment index.createResultsTable
(ResultProducer rp, String tableName) Creates a results table for the supplied result producer.Returns the tip text for this property.Returns the tip text for this property.void
Closes the connection to the database.boolean
Executes a SQL query.boolean
Returns true if the experiment index exists.Get the value of DatabaseURL.boolean
getDebug()
Gets whether there should be printed some debugging output to stderr or not.Returns the currently stored keywords (as comma-separated list).Returns the currently set mask character.Get the database password.Object[]
getResultFromTable
(String tableName, ResultProducer rp, Object[] key) Executes a database query to extract a result for the supplied key from the database.Gets the results generated by a previous query.Gets the name of the experiment table that stores results from a particular ResultProducer.Returns the revision string.int
Returns the type of scrolling that the cursor supports, -1 if not supported or not connected.Get the database username.void
initialize
(File propsFile) Initializes the database connection.void
initialize
(Properties props) Initializes the database connection.boolean
Returns true if a database connection is active.boolean
Checks whether cursors are scrollable in general, false otherwise (also if not connected).boolean
Returns whether the cursors only support forward movement or are scroll sensitive (with ResultSet.CONCUR_READ_ONLY concurrency).boolean
Checks whether the given string is a reserved keyword.If the given string is a keyword, then the mask character will be appended and returned.Returns the tip text for this property.static String
processes the string in such a way that it can be stored in the database, i.e., it changes backslashes into slashes and doubles single quotes.void
putResultInTable
(String tableName, ResultProducer rp, Object[] key, Object[] result) Executes a database query to insert a result for the supplied key into the database.Executes a SQL SELECT query that returns a ResultSet.void
setDatabaseURL
(String newDatabaseURL) Set the value of DatabaseURL.void
setDebug
(boolean d) Sets whether there should be printed some debugging output to stderr or not.void
setKeywords
(String value) Sets the keywords (comma-separated list) to use.void
setKeywordsMaskChar
(String value) Sets the mask character to append to table or attribute names that are a reserved keyword.void
setPassword
(String password) Set the database password.void
setUsername
(String username) Set the database username.boolean
tableExists
(String tableName) Checks that a given table exists.int
translateDBColumnType
(String type) translates the column data type string to an integer value that indicates which data type / get()-Method to use in order to retrieve values from the database (see DatabaseUtils.Properties, InstanceQuery()).static String
typeName
(int type) Returns the name associated with a SQL type.int
Executes a SQL DDL query or an INSERT, DELETE or UPDATE.Returns the tip text for this property.
-
Field Details
-
EXP_INDEX_TABLE
The name of the table containing the index to experiments.- See Also:
-
EXP_TYPE_COL
The name of the column containing the experiment type (ResultProducer).- See Also:
-
EXP_SETUP_COL
The name of the column containing the experiment setup (parameters).- See Also:
-
EXP_RESULT_COL
The name of the column containing the results table name.- See Also:
-
EXP_RESULT_PREFIX
The prefix for result table names.- See Also:
-
PROPERTY_FILE
The name of the properties file.- See Also:
-
STRING
public static final int STRINGType mapping for STRING used for reading experiment results.- See Also:
-
BOOL
public static final int BOOLType mapping for BOOL used for reading experiment results.- See Also:
-
DOUBLE
public static final int DOUBLEType mapping for DOUBLE used for reading experiment results.- See Also:
-
BYTE
public static final int BYTEType mapping for BYTE used for reading experiment results.- See Also:
-
SHORT
public static final int SHORTType mapping for SHORT used for reading experiment results.- See Also:
-
INTEGER
public static final int INTEGERType mapping for INTEGER used for reading experiment results.- See Also:
-
LONG
public static final int LONGType mapping for LONG used for reading experiment results.- See Also:
-
FLOAT
public static final int FLOATType mapping for FLOAT used for reading experiment results.- See Also:
-
DATE
public static final int DATEType mapping for DATE used for reading experiment results.- See Also:
-
TEXT
public static final int TEXTType mapping for TEXT used for reading, e.g., text blobs.- See Also:
-
TIME
public static final int TIMEType mapping for TIME used for reading TIME columns.- See Also:
-
TIMESTAMP
public static final int TIMESTAMPType mapping for TIMESTAMP used for reading java.sql.Timestamp columns- See Also:
-
-
Constructor Details
-
DatabaseUtils
Reads properties and sets up the database drivers.- Throws:
Exception
- if an error occurs
-
DatabaseUtils
Reads the properties from the specified file and sets up the database drivers.- Parameters:
propsFile
- the props file to load, ignored if null or pointing to a directory- Throws:
Exception
- if an error occurs
-
DatabaseUtils
Uses the specified properties to set up the database drivers.- Parameters:
props
- the properties to use, ignored if null- Throws:
Exception
- if an error occurs
-
-
Method Details
-
initialize
Initializes the database connection.- Parameters:
propsFile
- the props file to load, ignored if null or pointing to a directory
-
initialize
Initializes the database connection.- Parameters:
props
- the properties to obtain the parameters from, ignored if null
-
attributeCaseFix
returns key column headings in their original case. Used for those databases that create uppercase column names.- Parameters:
columnName
- the column to retrieve the original case for- Returns:
- the original case
-
translateDBColumnType
translates the column data type string to an integer value that indicates which data type / get()-Method to use in order to retrieve values from the database (see DatabaseUtils.Properties, InstanceQuery()). Blanks in the type are replaced with underscores "_", since Java property names can't contain blanks.- Parameters:
type
- the column type as retrieved with java.sql.MetaData.getColumnTypeName(int)- Returns:
- an integer value that indicates which data type / get()-Method to use in order to retrieve values from the
-
arrayToString
Converts an array of objects to a string by inserting a space between each element. Null elements are printed as ?- Parameters:
array
- the array of objects- Returns:
- a value of type 'String'
-
typeName
Returns the name associated with a SQL type.- Parameters:
type
- the SQL type- Returns:
- the name of the type
-
databaseURLTipText
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the explorer/experimenter gui
-
getDatabaseURL
Get the value of DatabaseURL.- Returns:
- Value of DatabaseURL.
-
setDatabaseURL
Set the value of DatabaseURL.- Parameters:
newDatabaseURL
- Value to assign to DatabaseURL.
-
debugTipText
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the explorer/experimenter gui
-
setDebug
public void setDebug(boolean d) Sets whether there should be printed some debugging output to stderr or not.- Parameters:
d
- true if output should be printed
-
getDebug
public boolean getDebug()Gets whether there should be printed some debugging output to stderr or not.- Returns:
- true if output should be printed
-
usernameTipText
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the explorer/experimenter gui
-
setUsername
Set the database username.- Parameters:
username
- Username for Database.
-
getUsername
Get the database username.- Returns:
- Database username
-
passwordTipText
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the explorer/experimenter gui
-
setPassword
Set the database password.- Parameters:
password
- Password for Database.
-
getPassword
Get the database password.- Returns:
- Password for Database.
-
connectToDatabase
Opens a connection to the database.- Throws:
Exception
- if an error occurs
-
disconnectFromDatabase
Closes the connection to the database.- Throws:
Exception
- if an error occurs
-
isConnected
public boolean isConnected()Returns true if a database connection is active.- Returns:
- a value of type 'boolean'
-
isCursorScrollSensitive
public boolean isCursorScrollSensitive()Returns whether the cursors only support forward movement or are scroll sensitive (with ResultSet.CONCUR_READ_ONLY concurrency). Returns always false if not connected- Returns:
- true if connected and the cursor is scroll-sensitive
- See Also:
-
isCursorScrollable
public boolean isCursorScrollable()Checks whether cursors are scrollable in general, false otherwise (also if not connected).- Returns:
- true if scrollable and connected
- See Also:
-
getSupportedCursorScrollType
public int getSupportedCursorScrollType()Returns the type of scrolling that the cursor supports, -1 if not supported or not connected. Checks first for TYPE_SCROLL_SENSITIVE and then for TYPE_SCROLL_INSENSITIVE. In both cases CONCUR_READ_ONLY as concurrency is used.- Returns:
- the scroll type, or -1 if not connected or no scrolling supported
- See Also:
-
execute
Executes a SQL query. Caller must clean up manually withclose()
.- Parameters:
query
- the SQL query- Returns:
- true if the query generated results
- Throws:
SQLException
- if an error occurs- See Also:
-
getResultSet
Gets the results generated by a previous query. Caller must clean up manually withclose(ResultSet)
. Returns null if object has been deserialized.- Returns:
- the result set.
- Throws:
SQLException
- if an error occurs- See Also:
-
update
Executes a SQL DDL query or an INSERT, DELETE or UPDATE.- Parameters:
query
- the SQL DDL query- Returns:
- the number of affected rows
- Throws:
SQLException
- if an error occurs
-
select
Executes a SQL SELECT query that returns a ResultSet. Note: the ResultSet object must be closed by the caller.- Parameters:
query
- the SQL query- Returns:
- the generated ResultSet
- Throws:
SQLException
- if an error occurs
-
close
closes the ResultSet and the statement that generated the ResultSet to avoid memory leaks in JDBC drivers - in contrast to the JDBC specs, a lot of JDBC drives don't clean up correctly.- Parameters:
rs
- the ResultSet to clean up
-
close
public void close()closes the m_PreparedStatement to avoid memory leaks. -
tableExists
Checks that a given table exists.- Parameters:
tableName
- the name of the table to look for.- Returns:
- true if the table exists.
- Throws:
Exception
- if an error occurs.
-
processKeyString
processes the string in such a way that it can be stored in the database, i.e., it changes backslashes into slashes and doubles single quotes.- Parameters:
s
- the string to work on- Returns:
- the processed string
-
getResultFromTable
public Object[] getResultFromTable(String tableName, ResultProducer rp, Object[] key) throws Exception Executes a database query to extract a result for the supplied key from the database.- Parameters:
tableName
- the name of the table where the result is storedrp
- the ResultProducer who will generate the result if requiredkey
- the key for the result- Returns:
- true if the result with that key is in the database already
- Throws:
Exception
- if an error occurs
-
putResultInTable
public void putResultInTable(String tableName, ResultProducer rp, Object[] key, Object[] result) throws Exception Executes a database query to insert a result for the supplied key into the database.- Parameters:
tableName
- the name of the table where the result is storedrp
- the ResultProducer who will generate the result if requiredkey
- the key for the resultresult
- the result to store- Throws:
Exception
- if an error occurs
-
experimentIndexExists
Returns true if the experiment index exists.- Returns:
- true if the index exists
- Throws:
Exception
- if an error occurs
-
createExperimentIndex
Attempts to create the experiment index table.- Throws:
Exception
- if an error occurs.
-
createExperimentIndexEntry
Attempts to insert a results entry for the table into the experiment index.- Parameters:
rp
- the ResultProducer generating the results- Returns:
- the name of the created results table
- Throws:
Exception
- if an error occurs.
-
getResultsTableName
Gets the name of the experiment table that stores results from a particular ResultProducer.- Parameters:
rp
- the ResultProducer- Returns:
- the name of the table where the results for this ResultProducer are stored, or null if there is no table for this ResultProducer.
- Throws:
Exception
- if an error occurs
-
createResultsTable
Creates a results table for the supplied result producer.- Parameters:
rp
- the ResultProducer generating the resultstableName
- the name of the resultsTable- Returns:
- the name of the created results table
- Throws:
Exception
- if an error occurs.
-
setKeywords
Sets the keywords (comma-separated list) to use.- Parameters:
value
- the list of keywords
-
getKeywords
Returns the currently stored keywords (as comma-separated list).- Returns:
- the list of keywords
-
setKeywordsMaskChar
Sets the mask character to append to table or attribute names that are a reserved keyword.- Parameters:
value
- the new character
-
getKeywordsMaskChar
Returns the currently set mask character.- Returns:
- the character
-
isKeyword
Checks whether the given string is a reserved keyword.- Parameters:
s
- the string to check- Returns:
- true if the string is a keyword
- See Also:
-
m_Keywords
-
maskKeyword
If the given string is a keyword, then the mask character will be appended and returned. Otherwise, the same string will be returned unchanged.- Parameters:
s
- the string to check- Returns:
- the potentially masked string
- See Also:
-
m_KeywordsMaskChar
isKeyword(String)
-
getRevision
Returns the revision string.- Specified by:
getRevision
in interfaceRevisionHandler
- Returns:
- the revision
-