org.drupal.project.computing
Class DDatabase

java.lang.Object
  extended by org.drupal.project.computing.DDatabase

public class DDatabase
extends java.lang.Object

Connect to Drupal through direct database access. It uses JDBC and Apache Commons DBUtils, so it's independent of DBMS implementations. Currently we support MySQL and PostgreSQL. Due to security reasons, it is not recommended to use this class directly to access Drupal database unless necessary. Typical reasons to use this class: 1) performance, 2) prototyping. Other than those 2 cases, you should consider to use {computing_record} to pass data in/out of Drupal. This can be used as standalone class.


Nested Class Summary
static class DDatabase.AsyncBatchRunner
          This is thread that upload data through JDBC concurrently.
static class DDatabase.DatabaseDriver
          Basic info about Drupal database driver's type.
static class DDatabase.UnitTest
           
 
Field Summary
protected  org.apache.commons.dbcp.BasicDataSource dataSource
          JDBC datasource for this Drupal connection.
protected  java.util.Properties dbProperties
           
protected  java.util.logging.Logger logger
           
 
Constructor Summary
DDatabase(java.util.Properties dbProperties)
          Initialize connection pooling and connect to the Drupal database.
 
Method Summary
 int[] batch(java.lang.String sql, java.lang.Object[][] params)
          Run batch database update.
 void close()
          Don't forget to close the connections after using it.
 java.lang.String d(java.lang.String sql)
          "Decorates" the SQL statement for Drupal.
 java.sql.Connection getConnection()
          Get one database JDBC connection.
 long insert(java.lang.String sql, java.lang.Object... params)
          Insert one record and get the auto-increment ID.
 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.
 int update(java.lang.String sql, java.lang.Object... params)
          Run Drupal database update queries (UPDATE, DELETE, INSERT) without using d().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected java.util.logging.Logger logger

dbProperties

protected final java.util.Properties dbProperties

dataSource

protected org.apache.commons.dbcp.BasicDataSource dataSource
JDBC datasource for this Drupal connection. we use BasicDataSource rather than the DataSource interface to check the closed property

Constructor Detail

DDatabase

public DDatabase(java.util.Properties dbProperties)
          throws DConnectionException
Initialize connection pooling and connect to the Drupal database. In the default database settings, we don't set AutoCommit/TransactionLevel etc pragmatically. Such settings should be set in the database connection string.

Throws:
DConnectionException
Method Detail

close

public void close()
           throws DConnectionException
Don't forget to close the connections after using it.

Throws:
DConnectionException

getConnection

public java.sql.Connection getConnection()
                                  throws DConnectionException
Get one database JDBC connection. The caller is responsible to close the connection.

Returns:
java.sql.Connection.
Throws:
DConnectionException

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 DConnectionException
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:
DConnectionException

queryValue

public java.lang.Object queryValue(java.lang.String sql,
                                   java.lang.Object... params)
                            throws DConnectionException
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:
DConnectionException

queryArray

public java.util.List<java.lang.Object[]> queryArray(java.lang.String sql,
                                                     java.lang.Object... params)
                                              throws DConnectionException
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:
DConnectionException

update

public int update(java.lang.String sql,
                  java.lang.Object... params)
           throws DConnectionException
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:
DConnectionException

insert

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

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

batch

public int[] batch(java.lang.String sql,
                   java.lang.Object[][] params)
            throws DConnectionException
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:
DConnectionException