org.dftproject.genesis.core.data
Class DataConnection

java.lang.Object
  extended by org.dftproject.genesis.core.data.DataConnection

public class DataConnection
extends java.lang.Object

DataConnection wraps a JDBC connection to the underlying database.


Constructor Summary
DataConnection(java.sql.Connection conn, java.lang.String store)
          Creates a wrapper for the JDBC connection which can manipulate the given store.
 
Method Summary
 void close()
          Closes the underlying JDBC connection.
 void createStore()
          Creates the current store.
 void delete(INode graph, INode subject, INode predicate, INode object)
          Deletes the given statement from the current store.
protected  void deleteStatement(java.lang.String graph, java.lang.String subject, java.lang.String predicate, java.lang.String object)
          Internal method which deletes the raw values of a statement from the current store.
 void deleteStore()
          Drops all tables, indexes, procedures, and triggers related to the current store from the database.
protected  java.lang.String escape(java.lang.String str)
          Escapes the given string for use in database queries.
protected  void executeNoErrorHandling(java.lang.String sql)
          Executes the given SQL statement.
 java.sql.ResultSet executeQuery(java.lang.String sql)
          Executes the given SQL query.
 int executeUpdate(java.lang.String sql)
          Executes the given SQL update statement.
protected  long getLongLiteralID(java.lang.String value, boolean add)
          Returns the ID of the given long literal.
 java.lang.String getLongLiteralTableName()
          Returns the name of the long literal table for the current store.
protected  java.lang.String getLongLiteralValue(long id)
          Returns the raw long literal value associated with the ID.
 java.lang.String getStatementTableName()
          Returns the name of the statement table for the current store.
protected  java.lang.String getWhereClause(java.lang.String graph, java.lang.String subject, java.lang.String predicate, java.lang.String object)
          Returns a SQL WHERE clause that matches the given raw statement values (non-null parameters).
static void handleDelete(java.lang.String graph, java.lang.String subject, java.lang.String predicate, java.lang.String object)
          Called by the statement table deletion trigger to notify listeners.
static void handleInsert(java.lang.String graph, java.lang.String subject, java.lang.String predicate, java.lang.String object)
          Called by the statement table insertion trigger to notify listeners.
protected  void initEscape()
          Initializes the regular expression pattern and replacement string used for escaping database strings.
 void insert(INode graph, INode subject, INode predicate, INode object)
          Inserts the given statement into the current store.
protected  void insertStatement(java.lang.String graph, java.lang.String subject, java.lang.String predicate, java.lang.String object)
          Internal method which inserts the raw values of a statement into the current store.
 java.lang.String nodeToString(INode node, boolean addIfLong)
          Returns the raw database representation of the given node.
 boolean storeExists()
          Returns whether or not the current store exists.
 INode stringToNode(java.lang.String value, DataView view)
          Instantiates the node given by the raw database representation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataConnection

public DataConnection(java.sql.Connection conn,
                      java.lang.String store)
Creates a wrapper for the JDBC connection which can manipulate the given store.

Parameters:
conn - an open JDBC connection
store - the store that this connection manipulates
Method Detail

close

public void close()
Closes the underlying JDBC connection.


initEscape

protected void initEscape()
Initializes the regular expression pattern and replacement string used for escaping database strings.


escape

protected java.lang.String escape(java.lang.String str)
Escapes the given string for use in database queries.

Parameters:
str - the string to escape
Returns:
the escaped string

getStatementTableName

public java.lang.String getStatementTableName()
Returns the name of the statement table for the current store.

Returns:
the name of the statement table

getLongLiteralTableName

public java.lang.String getLongLiteralTableName()
Returns the name of the long literal table for the current store.

The long literal table is used for storing literals whose raw database representation is too large to fit in the statement table.

Returns:
the name of the long literal table

storeExists

public boolean storeExists()
                    throws java.sql.SQLException
Returns whether or not the current store exists.

Returns:
whether or not the current store exists
Throws:
java.sql.SQLException

createStore

public void createStore()
                 throws java.sql.SQLException
Creates the current store.

The name of the store will be used as a table prefix.

Throws:
java.sql.SQLException

deleteStore

public void deleteStore()
                 throws java.sql.SQLException
Drops all tables, indexes, procedures, and triggers related to the current store from the database.

Throws:
java.sql.SQLException

insert

public void insert(INode graph,
                   INode subject,
                   INode predicate,
                   INode object)
Inserts the given statement into the current store.

Parameters:
graph - the graph
subject - the subject
predicate - the predicate
object - the object

insertStatement

protected void insertStatement(java.lang.String graph,
                               java.lang.String subject,
                               java.lang.String predicate,
                               java.lang.String object)
Internal method which inserts the raw values of a statement into the current store.

Parameters:
graph - the graph
subject - the subject
predicate - the predicate
object - the object

delete

public void delete(INode graph,
                   INode subject,
                   INode predicate,
                   INode object)
            throws java.sql.SQLException
Deletes the given statement from the current store.

Parameters:
graph - the graph
subject - the subject
predicate - the predicate
object - the object
Throws:
java.sql.SQLException

deleteStatement

protected void deleteStatement(java.lang.String graph,
                               java.lang.String subject,
                               java.lang.String predicate,
                               java.lang.String object)
                        throws java.sql.SQLException
Internal method which deletes the raw values of a statement from the current store.

Parameters:
graph - the graph
subject - the subject
predicate - the predicate
object - the object
Throws:
java.sql.SQLException

getLongLiteralValue

protected java.lang.String getLongLiteralValue(long id)
Returns the raw long literal value associated with the ID.

Parameters:
id - the id of the long literal to look up
Returns:
the value of the long literal, or null if it does not exist

getLongLiteralID

protected long getLongLiteralID(java.lang.String value,
                                boolean add)
Returns the ID of the given long literal. Optionally adds the long literal and returns the new ID if it does not already exist.

Parameters:
value - the long literal
add - whether or not to add the long literal if it does not already exist
Returns:
the ID of the long literal

getWhereClause

protected java.lang.String getWhereClause(java.lang.String graph,
                                          java.lang.String subject,
                                          java.lang.String predicate,
                                          java.lang.String object)
Returns a SQL WHERE clause that matches the given raw statement values (non-null parameters).

Parameters:
graph - the graph, or null for any graph
subject - the subject, or null for any subject
predicate - the predicate, or null for any predicate
object - the object, or null for any object
Returns:
the corresponding SQL WHERE clause

executeNoErrorHandling

protected void executeNoErrorHandling(java.lang.String sql)
                               throws java.sql.SQLException
Executes the given SQL statement.

Parameters:
sql - the SQL statement
Throws:
java.sql.SQLException

executeQuery

public java.sql.ResultSet executeQuery(java.lang.String sql)
                                throws java.sql.SQLException
Executes the given SQL query.

Parameters:
sql - the SQL query
Returns:
the results
Throws:
java.sql.SQLException

executeUpdate

public int executeUpdate(java.lang.String sql)
                  throws java.sql.SQLException
Executes the given SQL update statement.

Parameters:
sql - the SQL query
Returns:
the number of rows affected
Throws:
java.sql.SQLException

handleInsert

public static void handleInsert(java.lang.String graph,
                                java.lang.String subject,
                                java.lang.String predicate,
                                java.lang.String object)
Called by the statement table insertion trigger to notify listeners.

Parameters:
graph - the graph
subject - the subject
predicate - the predicate
object - the object

handleDelete

public static void handleDelete(java.lang.String graph,
                                java.lang.String subject,
                                java.lang.String predicate,
                                java.lang.String object)
Called by the statement table deletion trigger to notify listeners.

Parameters:
graph - the graph
subject - the subject
predicate - the predicate
object - the object

nodeToString

public java.lang.String nodeToString(INode node,
                                     boolean addIfLong)
Returns the raw database representation of the given node.

If the node is a long literal, this method can optionally add it to the long literals table of the current store. This is neccessary when the result of this method will be used to add the node to the current store.

Parameters:
node - the node
addIfLong - whether or not to add long literals to the current store
Returns:
the raw database representation of the node

stringToNode

public INode stringToNode(java.lang.String value,
                          DataView view)
Instantiates the node given by the raw database representation. If the node is a resource, this method uses the given view to return a cached copy when available.

Parameters:
value - the raw database representation
view - the view to use as a resource cache
Returns:
the node