org.drupal.project.async_command
Class DrupalConnection

java.lang.Object
  extended by org.drupal.project.async_command.DrupalConnection

public class DrupalConnection
extends java.lang.Object

Handles database connection to a Drupal site. Uses DBCP for connection pooling and DbUtils for simplified JDBC calls.


Nested Class Summary
static class DrupalConnection.DatabaseType
           
 
Field Summary
protected  java.util.Properties config
          Deprecated. 
protected  org.apache.commons.dbcp.BasicDataSource dataSource
          JDBC datasource for this Drupal connection.
protected  DrupletConfig drupletConfig
          Configuration for Druplet, including config about the database.
protected static java.util.logging.Logger logger
          Default logger for the whole package.
 
Constructor Summary
DrupalConnection(DrupletConfig drupletConfig)
           
DrupalConnection(java.io.File configFile)
          Deprecated. 
DrupalConnection(java.util.Properties config)
          Deprecated. 
DrupalConnection(java.lang.String configString)
          Deprecated. 
 
Method Summary
 int[] batch(java.lang.String sql, java.lang.Object[][] params)
          Run batch database update.
 boolean checkConnection()
          Check if the DataSource connection is still valid or not.
 void close()
           
 void connect()
          Equivalent to this.connect(false)
 void connect(boolean reconnect)
          Initialize connection pooling and connect to the Drupal database.
static DrupalConnection create()
          Deprecated. 
 java.lang.String d(java.lang.String sql)
          "Decorates" the SQL statement for Drupal.
 java.sql.Connection getConnection()
          Get a Drupal database connection from the pool.
 DrupalConnection.DatabaseType getDatabaseType()
           
 javax.sql.DataSource getDataSource()
           
 java.lang.String getEncryptedFieldSecretKey()
           
 int getMaxBatchSize()
           
 long insertAutoIncrement(java.lang.String sql, java.lang.Object... params)
          Insert one record and get the auto-increment ID.
 long insertCommandRecord(java.util.Map<java.lang.String,java.lang.Object> fields)
          NOT SUPPORTED YET! Insert a command record into the database using fields.
 long issueAsyncCommand(java.lang.String app, java.lang.String command, java.lang.String description, java.util.Map<java.lang.String,java.lang.Object> options)
           
 java.util.List<java.util.Map<java.lang.String,java.lang.Object>> query(java.lang.String sql, java.lang.Object... params)
          Simply run Drupal database queries e.g., query("SELECT nid, title FROM {node} WHERE type=?", "forum");
 java.util.List<java.lang.Object[]> queryArray(java.lang.String sql, java.lang.Object... params)
          Simply run Drupal database queries, returning a list of arrays instead of maps.
 java.lang.Object queryValue(java.lang.String sql, java.lang.Object... params)
          Simply run Drupal database queries, returning one value.
 java.util.List<CommandRecord> retrieveAnyCommandRecord(java.lang.String sqlWhere)
          Retrieve any records in the {async_command} table given the "where" SQL clause.
 CommandRecord retrieveCommandRecord(long id)
          Load a record from database .
 java.util.List<CommandRecord> retrieveNewCommandRecord(java.lang.String appName)
          Retrieve a list of new and unhandled commands for the given app.
 void testConnection()
          Test if the Drupal connection is working or not.
 int update(java.lang.String sql, java.lang.Object... params)
          Run Drupal database update queries (UPDATE, DELETE, INSERT) without using d().
 java.lang.Object variableGet(java.lang.String varName)
          Java version of Drupal function variable_get().
 void variableSet(java.lang.String varName, java.lang.Object varValue)
          Java version of Drupal function variable_set().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

config

@Deprecated
protected java.util.Properties config
Deprecated. 
Database configuration.


drupletConfig

protected DrupletConfig drupletConfig
Configuration for Druplet, including config about the database.


logger

protected static java.util.logging.Logger logger
Default logger for the whole package.


dataSource

protected org.apache.commons.dbcp.BasicDataSource dataSource
JDBC datasource for this Drupal connection.

Constructor Detail

DrupalConnection

@Deprecated
public DrupalConnection(java.util.Properties config)
Deprecated. 

Initializes database connection properties, set default properties if not given. Deprecated. Use DrupletConfig instead.

Parameters:
config - Database configuration

DrupalConnection

public DrupalConnection(DrupletConfig drupletConfig)

DrupalConnection

@Deprecated
public DrupalConnection(java.lang.String configString)
Deprecated. 

Initializes database connection properties. Deprecated. Use DrupletConfig instead.

Parameters:
configString - Properties string.

DrupalConnection

@Deprecated
public DrupalConnection(java.io.File configFile)
Deprecated. 

Initializes database connection properties. Deprecated. Use DrupletConfig instead.

Parameters:
configFile - Properties file to config database connection.
Method Detail

getDataSource

public javax.sql.DataSource getDataSource()

getDatabaseType

public DrupalConnection.DatabaseType getDatabaseType()

create

@Deprecated
public static DrupalConnection create()
Deprecated. 

Factory method. Deprecated,

Returns:
A DrupalConnection using default config files.

connect

public void connect()
Equivalent to this.connect(false)


connect

public void connect(boolean reconnect)
Initialize connection pooling and connect to the Drupal database.

Parameters:
reconnect - Whether to force recreate dataSource.

testConnection

public void testConnection()
Test if the Drupal connection is working or not. Throws DrupalDatabaseException if error occurs. Otherwise do nothing.


close

public void close()

checkConnection

public boolean checkConnection()
Check if the DataSource connection is still valid or not.

Returns:
True if DataSource is still valid, otherwise false.

d

public java.lang.String d(java.lang.String sql)
"Decorates" the SQL statement for Drupal. replace {table} with prefix.

Parameters:
sql - the original SQL statement to be decorated.
Returns:
the "decorated" SQL statement

query

public java.util.List<java.util.Map<java.lang.String,java.lang.Object>> query(java.lang.String sql,
                                                                              java.lang.Object... params)
                                                                       throws java.sql.SQLException
Simply run Drupal database queries e.g., query("SELECT nid, title FROM {node} WHERE type=?", "forum");

Parameters:
sql - SQL query to be executed, use {} for table names
params - Parameters to complete the SQL query
Returns:
A list of rows as Map (column => value)
Throws:
java.sql.SQLException

queryValue

public java.lang.Object queryValue(java.lang.String sql,
                                   java.lang.Object... params)
                            throws java.sql.SQLException
Simply run Drupal database queries, returning one value. e.g. query("SELECT title FROM {node} WHERE nid=?", 1);

Parameters:
sql - SQL query to be executed, use {} for table names
params - Parameters to complete the SQL query
Returns:
One value object
Throws:
java.sql.SQLException

queryArray

public java.util.List<java.lang.Object[]> queryArray(java.lang.String sql,
                                                     java.lang.Object... params)
                                              throws java.sql.SQLException
Simply run Drupal database queries, returning a list of arrays instead of maps.

Parameters:
sql - SQL query to be executed, use {} for table names
params - Parameters to complete the SQL query
Returns:
A list of arrays
Throws:
java.sql.SQLException

update

public int update(java.lang.String sql,
                  java.lang.Object... params)
           throws java.sql.SQLException
Run Drupal database update queries (UPDATE, DELETE, INSERT) without using d(). e.g., query("UPDATE {node} SET sticky=1 WHERE type=?", "forum");

Parameters:
sql - SQL update query to be executed, use {} for table names.
params - parameters to complete the SQL query
Returns:
number of rows affected
Throws:
java.sql.SQLException

insertAutoIncrement

public long insertAutoIncrement(java.lang.String sql,
                                java.lang.Object... params)
                         throws java.sql.SQLException
Insert one record and get the auto-increment ID.

Parameters:
sql - SQL INSERT statement.
params - Parameters to complete INSERT
Returns:
auto-increment ID.
Throws:
java.sql.SQLException

batch

public int[] batch(java.lang.String sql,
                   java.lang.Object[][] params)
            throws java.sql.SQLException
Run batch database update.

Parameters:
sql - SQL update query to be executed, use {} for table names.
params - params parameters to complete the SQL query
Returns:
number of rows affected in each batch.
Throws:
java.sql.SQLException

variableGet

public java.lang.Object variableGet(java.lang.String varName)
Java version of Drupal function variable_get(). You need to know the type of the variable in order to change the type of the returned Object

Parameters:
varName - Drupal variable name
Returns:
the value of the variable, could be float, integer, string, or PhpObject as PHP array, or null
See Also:
SerializedPhpParser

variableSet

public void variableSet(java.lang.String varName,
                        java.lang.Object varValue)
Java version of Drupal function variable_set(). Doesn't guarantee to work. Use with Caution.

Parameters:
varName - the variable name.
varValue - the variable value in PHP code string. MessageFormat

retrieveAnyCommandRecord

public java.util.List<CommandRecord> retrieveAnyCommandRecord(java.lang.String sqlWhere)
Retrieve any records in the {async_command} table given the "where" SQL clause.

Parameters:
sqlWhere - Starts with "WHERE". If null, then returns all records.
Returns:
A list of records in the {async_command} table that satisfy the WHERE SQL clause.

retrieveNewCommandRecord

public java.util.List<CommandRecord> retrieveNewCommandRecord(java.lang.String appName)
Retrieve a list of new and unhandled commands for the given app.

Parameters:
appName -
Returns:

insertCommandRecord

public long insertCommandRecord(java.util.Map<java.lang.String,java.lang.Object> fields)
NOT SUPPORTED YET! Insert a command record into the database using fields.

Parameters:
fields - fields data eg field=>value.
Returns:
ID of the newly created record.

issueAsyncCommand

public long issueAsyncCommand(java.lang.String app,
                              java.lang.String command,
                              java.lang.String description,
                              java.util.Map<java.lang.String,java.lang.Object> options)

retrieveCommandRecord

public CommandRecord retrieveCommandRecord(long id)
Load a record from database .

Parameters:
id - Record id from the database.
Returns:
The record object.

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Get a Drupal database connection from the pool. Caller is responsible to close it (Note: not sure if it'll be managed by ConnectionPooling)

Returns:
Throws:
java.sql.SQLException

getMaxBatchSize

public int getMaxBatchSize()

getEncryptedFieldSecretKey

public java.lang.String getEncryptedFieldSecretKey()