tuffy.db
Class RDB

java.lang.Object
  extended by tuffy.db.RDB

public class RDB
extends java.lang.Object

Interface with the RDBMS. Currently only supports PostgreSQL (8.4 or later).


Field Summary
(package private) static java.util.ArrayList<RDB> allRDBs
           
private  java.sql.Connection con
           
(package private) static int currentDBCounter
           
private  java.sql.Statement currentlyRunningQuery
           
 double estimatedCost
           
 double estimatedRows
           
private  int lastUpdateRowCount
           
private  boolean savedAutoCommit
           
 
Constructor Summary
RDB(java.lang.String url, java.lang.String user, java.lang.String password)
          Specifies a JDBC connection.
 
Method Summary
 void analyze(java.lang.String rel)
          Analyze a specific table.
 java.lang.Double callFunctionDouble(java.lang.String func, java.lang.String args)
          Call a function that returns a double.
 void callProcedure(java.lang.String proc)
          Call a stored procedure that doesn't have any parameters.
 void close()
          Close this connection.
 void commit()
          Commit the previous actions.
 void copyTable(java.lang.String src, java.lang.String dest)
          Copy the tuples of a table to another.
 long countTuples(java.lang.String table)
          Count the tuples in a table.
 void createConstantTable(java.util.HashMap<java.lang.String,java.lang.Integer> mapConstantID)
          Store the symbol-ID mapping into a symbol table.
 void createSetTable(java.lang.String rel, java.util.HashSet<java.lang.Integer> set)
          Create a table to store a set of integers
 void createTempTableIntList(java.lang.String rel, java.util.Collection<java.lang.Integer> vals)
           
 void disableAutoCommitForNow()
          Disable auto-commit so that JDBC won't fetch all query results at once.
 void dropIndex(java.lang.String idx)
          Drop an index if it exists.
 boolean dropSchema(java.lang.String sch)
          Try to drop a schema; remain silent if the specified schema doesn't exist.
 void dropSequence(java.lang.String seq)
          Try to drop a sequence; remain silent if the specified sequence doesn't exist.
private  boolean dropStuff(java.lang.String type, java.lang.String obj)
           
 void dropTable(java.lang.String rel)
          Try to drop a table; remain silent if the specified table doesn't exist.
 void dropView(java.lang.String view)
           
private  void dumpSQL(java.lang.String sql)
           
 void dumpTableToFile(Predicate p, java.lang.String fout)
          Dump a MAP world produced by MAP inference.
 java.lang.String estimateCost(java.lang.String sql)
           
 void estimateQuery(java.lang.String sql, boolean analyze)
           
 void execute(java.lang.String sql)
          Execute a SQL statement (query/update).
 void executeWhatever(java.lang.String sql)
           
 java.lang.String explain(java.lang.String sql)
          Explain a SQL query with an execution plan.
 java.sql.Connection getConnection()
          Return the database connection.
 int getLastUpdateRowCount()
          Return the number of affected tuples from last update.
 java.sql.PreparedStatement getPrepareStatement(java.lang.String sql)
          Return a prepared statement of the given SQL statement.
static RDB getRDBbyConfig()
          Attempt to establish the connection as specified in the (deault) configuration.
static RDB getRDBbyConfig(java.lang.String schema)
           
 int getSequenceCurValue(java.lang.String seq)
          Read the current value of a sequence.
 java.util.HashMap<java.lang.Integer,java.lang.String> loadIdSymbolMapFromTable()
          Load the symbol table into a hash table mapping symbol IDs to the original symbols.
 java.util.HashMap<java.lang.String,java.lang.Integer> loadSymbolIdMapFromTable()
          Load the symbol table into a hash table mapping symbols to their IDs.
 java.sql.ResultSet query(java.lang.String sql)
          Execute a SQL query.
private  void regExplainProc(java.lang.String pname)
          Register a stored procedure to explain SQL queries.
private  void registerDrivers()
          Register the JDBC driver.
 void resetSchema(java.lang.String schema)
          Reset the database schema that serves as Tuffy's workspace.
 void resetSequence(java.lang.String seq)
          Reset the value of a sequence to 1.
 void restoreAutoCommitState()
          Restore the auto-commit state saved by disableAutoCommitForNow().
 void setAutoCommit(boolean v)
          Set auto-commit state of this connection.
 int update(java.lang.String sql)
          Execute an update SQL statement.
 boolean updateBatch(java.util.ArrayList<java.lang.String> sqls)
          Execute a set of update SQL statements as a batch.
private  void updateRaw(java.lang.String sql)
           
 void vacuum(java.lang.String rel)
          Vacuum a specific table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lastUpdateRowCount

private int lastUpdateRowCount

savedAutoCommit

private boolean savedAutoCommit

allRDBs

static java.util.ArrayList<RDB> allRDBs

currentDBCounter

static int currentDBCounter

con

private java.sql.Connection con

currentlyRunningQuery

private java.sql.Statement currentlyRunningQuery

estimatedCost

public double estimatedCost

estimatedRows

public double estimatedRows
Constructor Detail

RDB

public RDB(java.lang.String url,
           java.lang.String user,
           java.lang.String password)
Specifies a JDBC connection.

Method Detail

disableAutoCommitForNow

public void disableAutoCommitForNow()
Disable auto-commit so that JDBC won't fetch all query results at once. Call this before retrieving data from a huge table. After the big query is done, call restoreAutoCommitState() to restore the initial auto-commit state.

See Also:
restoreAutoCommitState(), PostgreSQL's JDBC doc

regExplainProc

private void regExplainProc(java.lang.String pname)
Register a stored procedure to explain SQL queries.

Parameters:
pname - name of the stored procedure

estimateQuery

public void estimateQuery(java.lang.String sql,
                          boolean analyze)

estimateCost

public java.lang.String estimateCost(java.lang.String sql)

explain

public java.lang.String explain(java.lang.String sql)
Explain a SQL query with an execution plan.

Parameters:
sql -

createTempTableIntList

public void createTempTableIntList(java.lang.String rel,
                                   java.util.Collection<java.lang.Integer> vals)

restoreAutoCommitState

public void restoreAutoCommitState()
Restore the auto-commit state saved by disableAutoCommitForNow().

See Also:
disableAutoCommitForNow()

getLastUpdateRowCount

public int getLastUpdateRowCount()
Return the number of affected tuples from last update.


getConnection

public java.sql.Connection getConnection()
Return the database connection.


dumpTableToFile

public void dumpTableToFile(Predicate p,
                            java.lang.String fout)
Dump a MAP world produced by MAP inference.

Parameters:
fout - path of output file

getRDBbyConfig

public static RDB getRDBbyConfig()
Attempt to establish the connection as specified in the (deault) configuration.


getRDBbyConfig

public static RDB getRDBbyConfig(java.lang.String schema)

registerDrivers

private void registerDrivers()
Register the JDBC driver.


dumpSQL

private void dumpSQL(java.lang.String sql)

update

public int update(java.lang.String sql)
Execute an update SQL statement.

Returns:
the number of tuples affected

execute

public void execute(java.lang.String sql)
Execute a SQL statement (query/update).


executeWhatever

public void executeWhatever(java.lang.String sql)

updateRaw

private void updateRaw(java.lang.String sql)
                throws java.sql.SQLException
Throws:
java.sql.SQLException

updateBatch

public boolean updateBatch(java.util.ArrayList<java.lang.String> sqls)
Execute a set of update SQL statements as a batch.

Returns:
true on success

query

public java.sql.ResultSet query(java.lang.String sql)
Execute a SQL query.

Parameters:
sql - the SQL statement
Returns:
the result set. remembe to close it afterwards.

loadSymbolIdMapFromTable

public java.util.HashMap<java.lang.String,java.lang.Integer> loadSymbolIdMapFromTable()
Load the symbol table into a hash table mapping symbols to their IDs.

See Also:
Config.relConstants

loadIdSymbolMapFromTable

public java.util.HashMap<java.lang.Integer,java.lang.String> loadIdSymbolMapFromTable()
Load the symbol table into a hash table mapping symbol IDs to the original symbols.

See Also:
Config.relConstants

createConstantTable

public void createConstantTable(java.util.HashMap<java.lang.String,java.lang.Integer> mapConstantID)
Store the symbol-ID mapping into a symbol table.

Parameters:
mapConstantID - the symbol-ID mapping
See Also:
Config.relConstants

createSetTable

public void createSetTable(java.lang.String rel,
                           java.util.HashSet<java.lang.Integer> set)
Create a table to store a set of integers

Parameters:
rel - the name of the table
set - the set of integers

dropTable

public void dropTable(java.lang.String rel)
Try to drop a table; remain silent if the specified table doesn't exist.


dropSchema

public boolean dropSchema(java.lang.String sch)
Try to drop a schema; remain silent if the specified schema doesn't exist.


dropSequence

public void dropSequence(java.lang.String seq)
Try to drop a sequence; remain silent if the specified sequence doesn't exist.


dropView

public void dropView(java.lang.String view)

dropStuff

private boolean dropStuff(java.lang.String type,
                          java.lang.String obj)

getPrepareStatement

public java.sql.PreparedStatement getPrepareStatement(java.lang.String sql)
Return a prepared statement of the given SQL statement. A SQL statement with or without parameters can be pre-compiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.


resetSchema

public void resetSchema(java.lang.String schema)
Reset the database schema that serves as Tuffy's workspace.

See Also:
Config.db_schema

copyTable

public void copyTable(java.lang.String src,
                      java.lang.String dest)
Copy the tuples of a table to another. Can be used to check out the content of a temporary table.

Parameters:
src - name of the source table
dest - name the destination table; will be dropped if already exists

commit

public void commit()
Commit the previous actions. Useless when AutoCommit is on, which is so by default.


setAutoCommit

public void setAutoCommit(boolean v)
Set auto-commit state of this connection.


getSequenceCurValue

public int getSequenceCurValue(java.lang.String seq)
Read the current value of a sequence.

Parameters:
seq - the name of the sequence

countTuples

public long countTuples(java.lang.String table)
Count the tuples in a table.


close

public void close()
Close this connection.


analyze

public void analyze(java.lang.String rel)
Analyze a specific table.

Parameters:
rel - name of the table
See Also:
the PostgreSQL doc

vacuum

public void vacuum(java.lang.String rel)
Vacuum a specific table.

Parameters:
rel - name of the table
See Also:
the PostgreSQL doc

dropIndex

public void dropIndex(java.lang.String idx)
Drop an index if it exists.

Parameters:
idx - name of the index

resetSequence

public void resetSequence(java.lang.String seq)
Reset the value of a sequence to 1.

Parameters:
seq - name of the sequence

callProcedure

public void callProcedure(java.lang.String proc)
Call a stored procedure that doesn't have any parameters.

Parameters:
proc - name of the stored procedure

callFunctionDouble

public java.lang.Double callFunctionDouble(java.lang.String func,
                                           java.lang.String args)
Call a function that returns a double.

Parameters:
func - name of the function
args - arguments in the form of a string
Returns:
value returned by the function; null on error