Search in sources :

Example 6 with DatabaseOperationsException

use of com.axway.ats.action.exceptions.DatabaseOperationsException in project ats-framework by Axway.

the class DatabaseOperations method getValue.

/**
     * Gets a single value from a database. </br>
     * It returns the first value of the first returned row.
     *
     * <p><strong>Note</strong>: If the selected DB object is array - BLOB, LONG BLOB ... we return it this way:
     *  <code>[ 10, 24, -35]</code>
     *  </p>
     *
     * @param sqlQuery the SQL query to run
     * @return
     */
@PublicAtsApi
public String getValue(String sqlQuery) {
    try {
        log.debug("Executing query: " + sqlQuery);
        DbRecordValuesList[] rsList = dbProvider.select(sqlQuery);
        if (rsList != null && rsList.length > 0) {
            if (rsList.length > 1) {
                log.warn("SQL query '" + sqlQuery + "' returned " + rsList.length + " rows of results");
            }
            if (rsList[0].size() > 1) {
                log.warn("SQL query '" + sqlQuery + "' returned " + rsList[0].size() + " values per row");
            }
            return rsList[0].get(0).getValueAsString();
        }
    } catch (DbException e) {
        throw new DatabaseOperationsException("Error getting value from DB with query '" + sqlQuery + "'", e);
    }
    return null;
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DatabaseOperationsException(com.axway.ats.action.exceptions.DatabaseOperationsException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 7 with DatabaseOperationsException

use of com.axway.ats.action.exceptions.DatabaseOperationsException in project ats-framework by Axway.

the class DatabaseOperations method updateValue.

/**
     * Run update query.
     *
     * @param sqlQuery the SQL query to run
     * @return the number of updated rows
     */
@PublicAtsApi
public int updateValue(String sqlQuery) {
    int rowsUpdated;
    try {
        log.debug("Executing update query: '" + sqlQuery + "'");
        rowsUpdated = dbProvider.executeUpdate(sqlQuery);
        if (!(dbProvider instanceof CassandraDbProvider)) {
            if (rowsUpdated == 0) {
                log.warn("SQL query '" + sqlQuery + "' updated 0 rows");
            } else {
                log.debug("SQL query '" + sqlQuery + "' updated '" + rowsUpdated + "' rows");
            }
        }
    } catch (DbException e) {
        throw new DatabaseOperationsException("Error executing update query '" + sqlQuery + "'", e);
    }
    return rowsUpdated;
}
Also used : DatabaseOperationsException(com.axway.ats.action.exceptions.DatabaseOperationsException) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

DatabaseOperationsException (com.axway.ats.action.exceptions.DatabaseOperationsException)7 PublicAtsApi (com.axway.ats.common.PublicAtsApi)7 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)7 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)3 DatabaseCell (com.axway.ats.action.dbaccess.model.DatabaseCell)2 DatabaseRow (com.axway.ats.action.dbaccess.model.DatabaseRow)2 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)2 DbQuery (com.axway.ats.common.dbaccess.DbQuery)1 CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1