Search in sources :

Example 6 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class DatabaseOperations method getDatabaseData.

/**
     * Gets some data from a database table. </br>
     * <em>Note</em> that client may need to inspect returned type of each value and
     * optionally convert it to get value in specific format.
     *
     * @param sqlQuery the SQL SELECT query to run. Client is resposible for any
     *      possible escaping needed so this is not security-safe method
     * @return the found database data
     */
@PublicAtsApi
public DatabaseRow[] getDatabaseData(String sqlQuery) {
    List<DatabaseRow> dbRows = new ArrayList<DatabaseRow>();
    try {
        log.debug("Executing query: " + sqlQuery);
        DbRecordValuesList[] rsList = dbProvider.select(sqlQuery);
        if (rsList != null) {
            for (DbRecordValuesList rs : rsList) {
                Iterator<DbRecordValue> it = rs.iterator();
                if (it.hasNext()) {
                    DatabaseRow dbRow = new DatabaseRow();
                    while (it.hasNext()) {
                        DbRecordValue dbRecordValue = it.next();
                        dbRow.addCell(new DatabaseCell(dbRecordValue.getDbColumn().getColumnName(), dbRecordValue.getValueAsString()));
                    }
                    dbRows.add(dbRow);
                }
            }
        }
        return dbRows.toArray(new DatabaseRow[dbRows.size()]);
    } catch (DbException e) {
        throw new DatabaseOperationsException("Error getting data from DB with query '" + sqlQuery + "'", e);
    }
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DatabaseCell(com.axway.ats.action.dbaccess.model.DatabaseCell) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) DatabaseOperationsException(com.axway.ats.action.exceptions.DatabaseOperationsException) ArrayList(java.util.ArrayList) DatabaseRow(com.axway.ats.action.dbaccess.model.DatabaseRow) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 7 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class DatabaseOperations method execute.

/**
     *
     * Executes SQL statement, which must be an SQL Data Manipulation Language (DML) statement,
     * such as INSERT, UPDATE or DELETE, or an SQL statement that returns nothing, such as a DDL statement.
     *
     * @param sqlQuery the SQL query to run
     */
@PublicAtsApi
public void execute(String sqlQuery) {
    try {
        log.debug("Executing query: '" + sqlQuery + "'");
        dbProvider.executeUpdate(sqlQuery);
    } catch (DbException e) {
        throw new DatabaseOperationsException("Error executing query '" + sqlQuery + "'", e);
    }
}
Also used : DatabaseOperationsException(com.axway.ats.action.exceptions.DatabaseOperationsException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 8 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class DatabaseOperations method insertValues.

/**
     * Run insert query
     *
     * @param tableName the table to insert values in
     * @param columnNames the columns where values will be inserted
     * @param columnValues the values to insert
     */
@PublicAtsApi
public void insertValues(String tableName, String[] columnNames, String[] columnValues) {
    Map<String, String> columns = new HashMap<String, String>();
    if (columnNames.length != columnValues.length) {
        throw new DatabaseOperationsException("The number of columns [" + columnNames.length + "] is not the same as the number of values [" + columnValues.length + "]");
    }
    for (int i = 0; i < columnNames.length; i++) {
        columns.put(columnNames[i], columnValues[i]);
    }
    try {
        log.debug("Executing insert query in table '" + tableName + "'");
        int rowsInserted = dbProvider.insertRow(tableName, columns);
        if (rowsInserted == 0) {
            // this should never happen
            throw new DatabaseOperationsException("No data was inserted into table '" + tableName + "'");
        }
    } catch (DbException e) {
        throw new DatabaseOperationsException("Error executing insert query in table '" + tableName + "'", e);
    }
}
Also used : HashMap(java.util.HashMap) DatabaseOperationsException(com.axway.ats.action.exceptions.DatabaseOperationsException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 9 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class DatabaseProviderFactory method loadDbConnection.

private static DbConnection loadDbConnection(String className, String dbType, String dbHost, String dbName, String dbUser, String dbPass, int dbPort, Map<String, Object> customProperties) {
    Class<?> dbConnectionClass = null;
    // load db connection class
    try {
        dbConnectionClass = Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new DbException("Unable to get database provider for type '" + dbType + "'", e);
    }
    // load db connection constructor
    Constructor<?> constructor = null;
    try {
        constructor = new MethodFinder(dbConnectionClass).findConstructor(new Class[] { String.class, String.class, String.class, String.class, Map.class });
    } catch (NoSuchMethodException | AmbiguousMethodException e) {
        throw new DbException("Unable to get database provider for type '" + dbType + "'", e);
    }
    // create DbConnection class for this dbType
    DbConnection dbConnection = null;
    try {
        dbConnection = (DbConnection) constructor.newInstance(new Object[] { dbHost, dbName, dbUser, dbPass, customProperties });
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new DbException("Unable to get database provider for type '" + dbType + "'", e);
    }
    return dbConnection;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) MethodFinder(com.axway.ats.core.reflect.MethodFinder) HashMap(java.util.HashMap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) AmbiguousMethodException(com.axway.ats.core.reflect.AmbiguousMethodException)

Example 10 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class DatabaseProviderFactory method loadCustomDbProvider.

private static DbProvider loadCustomDbProvider(String className, DbConnection dbConnection) {
    Class<?> dbProviderClass = null;
    // load db provider class
    try {
        dbProviderClass = Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new DbException("Unable to get database provider for type '" + dbConnection.getDbType() + "'", e);
    }
    // load db provider constructor
    Constructor<?> constructorDbProvider = null;
    try {
        constructorDbProvider = new MethodFinder(dbProviderClass).findConstructor(new Class[] { DbConnection.class });
    } catch (NoSuchMethodException | AmbiguousMethodException e) {
        throw new DbException("Unable to get database provider for type '" + dbConnection.getDbType() + "'", e);
    }
    // create DbProvider class for this dbType
    try {
        return (DbProvider) constructorDbProvider.newInstance(new Object[] { dbConnection });
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new DbException("Unable to get database provider for type '" + dbConnection.getDbType() + "'", e);
    }
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) MethodFinder(com.axway.ats.core.reflect.MethodFinder) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider) OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) MssqlDbProvider(com.axway.ats.core.dbaccess.mssql.MssqlDbProvider) MysqlDbProvider(com.axway.ats.core.dbaccess.mysql.MysqlDbProvider) AmbiguousMethodException(com.axway.ats.core.reflect.AmbiguousMethodException)

Aggregations

DbException (com.axway.ats.core.dbaccess.exceptions.DbException)31 SQLException (java.sql.SQLException)11 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)8 DatabaseOperationsException (com.axway.ats.action.exceptions.DatabaseOperationsException)7 PublicAtsApi (com.axway.ats.common.PublicAtsApi)7 Connection (java.sql.Connection)6 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 PreparedStatement (java.sql.PreparedStatement)5 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)4 DatabaseEnvironmentCleanupException (com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException)4 BufferedReader (java.io.BufferedReader)4 File (java.io.File)4 FileReader (java.io.FileReader)4 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)3 CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)3 ColumnHasNoDefaultValueException (com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException)3 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 DatabaseCell (com.axway.ats.action.dbaccess.model.DatabaseCell)2