![JAR search and dependency download from the Maven repository](/logo.png)
atg.adapter.gsa.SQLProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of atgdust Show documentation
Show all versions of atgdust Show documentation
ATG DUST is a framework for building JUnit tests for
applications built on the ATG Dynamo platform. This framework allows
one to quickly write test code that depends up Nucleus or ATG
Repositories. By using this framework one can drastically cut down
on development time. It takes only a few seconds to start up a test
with a repository, but it may take multiple minutes to start up an
application server. To get started with DUST, take a look at
http://atgdust.sourceforge.net/first-test.html. This page will walk
you through the process of running a basic test which starts
Nucleus. After that, read the other getting started guides to
describe how to create standalone Junit tests which can startup
repositories and use the DynamoHttpServletResponse classes.
For only ATG10 and tested.
The newest version!
/**
* Copyright 2007 ATG DUST Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package atg.adapter.gsa ;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.sql.DataSource;
import javax.transaction.TransactionManager;
import org.apache.log4j.Logger;
import atg.dtm.TransactionDemarcation;
import atg.dtm.TransactionDemarcationException;
import atg.nucleus.GenericService;
import atg.service.jdbc.BasicDataSource;
/** A generic class to execute SQL actions against a database.
*
* Parts copied from atg.service.idgen.?? by mgk
*
* @author mf
* @version 1.0
**/
public
class SQLProcessor
{
// =============== MEMBER VARIABLES =================
private static Logger log = Logger.getLogger(SQLProcessor.class);
DataSource mDataSource;
/** sets the DataSource from which to get DB connections
**/
public void setDataSource(DataSource pDataSource) {
mDataSource = pDataSource; }
/** returns the DataSource from which db connections are obtained */
public DataSource getDataSource() {
return mDataSource;}
TransactionManager mTxManager;
/** sets the TransactionManager that should be used to monitor transactions */
public void setTransactionManager( TransactionManager pManager ) {
mTxManager = pManager; }
/** returns the TransactionManager that should be used to monitor transaction */
public TransactionManager getTransactionManager() {
return mTxManager; }
String mDetermineTableExistsSQL = "SELECT * from ";
/** sets String executed to determine whether a table exists. The table
* name is appended to the end of the string before execution occurs.
*/
public void setDetermineTableExistsSQL( String pStr ) {
mDetermineTableExistsSQL = pStr; }
/** returns String executed to determine whether a table exists. The table
* name is appended to the end of the string before execution occurs.
*/
public String getDetermineTableExistsSQL() {
return mDetermineTableExistsSQL; }
String mDropTableSQL = "DROP TABLE ";
/** sets String executed to drop a table. The table name is appended to the
* end of the string before execution
*/
public void setDropTableSQL( String pStr ) {
mDropTableSQL = pStr; }
/** returns String executed to drop a table. The table name is appended to the
* end of the string before execution
*/
public String getDropTableSQL() {
return mDropTableSQL; }
/** String delimiter used to separate a large String passed to
* createTables() into an array of individual Create Table statements
* default value is "CREATE TABLE"
* This delimiter _will_ be included in the final create statements
*/
String mCreateTableBeginDelimiter = "CREATE TABLE ";
public void setCreateTableBeginDelimiter( String pStr ) {
mCreateTableBeginDelimiter = pStr; }
public String getCreateTableBeginDelimiter() {
return mCreateTableBeginDelimiter; }
/** String delimiter used to separate a large String passed to
* createTables() into an array of individual Create Table statements
* default value is ";"
* This delimiter _will not_ be included in the final create statements
*/
String mCreateTableEndDelimiter = ";";
public void setCreateTableEndDelimiter( String pStr ) {
mCreateTableEndDelimiter = pStr; }
public String getCreateTableEndDelimiter() {
return mCreateTableEndDelimiter; }
/** an optional GenericService component whose logging services should be used by
* this component.
*/
private GenericService mLogger;
public void setLoggingManager( GenericService pLogger ) {
mLogger = pLogger; }
public GenericService getLoggingManager() {
return mLogger; }
// indicates whether to set autoCommit(true) on connections
private boolean mAutoCommit = false;
/** if set to true, then autoCommit will be set to true on all connections used to
* execute SQL. otherwise, autoCommit will not be altered from what is set by the
* DataSource.
*/
public void setAutoCommit( boolean pCommit ) {
mAutoCommit = pCommit; }
/** returns true if autoCommit should be set to true on all connections used to execute
* SQL.
*/
public boolean isSetAutoCommit() {
return mAutoCommit; }
/* =========== CONSTRUCTORS ============= */
/** Construct with specified DataSource
*
* @param TransactionManager manager - the TransactionManager to use to monitor transactions
* @param DataSource dataSource - the DataSource to use for db connections
**/
public SQLProcessor( TransactionManager pTxManager, DataSource pDataSource )
{
setDataSource( pDataSource );
setTransactionManager( pTxManager );
}
/** Constructor with specified user/password/driver/URL. specified parameters are used
* to create a DataSource connection to the database.
*
* @param TransactionManager manager - the TransactionManager to use to monitor transactions
* @param String username - name of user to connect to db
* @param String password - pwd to connectc to db
* @param String driver - driver specification to connect to db
* @param String url - url to connect to db
* @exception SQLException if an error occurs creating the DataSource
*/
public SQLProcessor( TransactionManager pTxManager, String pUsername, String pPassword, String pDriver, String pURL )
throws SQLException
{
setDataSource( createBasicDataSource( pUsername, pPassword, pDriver, pURL ) );
setTransactionManager( pTxManager );
}
// ==================== PUBLIC METHODS ===========================
/** creates and returns a DataSource based on the user/pwd/driver/url info
* supplied.
*/
public static DataSource createBasicDataSource( String pUsername,
String pPassword,
String pDriver,
String pURL )
{
BasicDataSource datasource = new BasicDataSource();
datasource.setUser( pUsername );
datasource.setPassword( pPassword );
datasource.setDriver( pDriver );
datasource.setURL( pURL );
return datasource;
}
/**
* Perform the specified SQL statement in a new transaction which is commited. Autocommit
* on the connection is set to true if isSetAutoCommit() is true.
*
* @param pSQL SQL to execute
*
* @exception SQLException if there is DB problem
* @exception TransactionDemarcationException if there is a tx problem
**/
public void executeSQL(String pSQL)
throws SQLException, TransactionDemarcationException
{
TransactionDemarcation td = new TransactionDemarcation();
try
{
td.begin ( getTransactionManager(), TransactionDemarcation.REQUIRES_NEW);
Connection c = null;
Statement s = null;
try
{
// get DB connection
c = getConnection();
if ( isSetAutoCommit() )
c.setAutoCommit( true );
//most of this method is annoying try/catch/finally blocks
//inflicted on us by JTA. the real work is here.
s = c.createStatement();
debug("Executing SQL [" + pSQL + "]");
s.execute(pSQL);
}
finally
{
close(s);
close(c);
}
}
finally
{
td.end();
}
}
/** executes the specified query and returns a List of values for the specified column name.
* for example, executeQuery( "select * from user", "first_name" ) would return a List of
* the first names of all entries in the user table.
*
* @return List of Object values
* @exception SQLException if a sql error occurs
* @exception TransactionDemarcationException if a tx error occurs
*/
public List> executeQuery( String pQuery, String pColumnName )
throws SQLException, TransactionDemarcationException
{
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy