Search in sources :

Example 1 with DatabaseOperationsException

use of com.axway.ats.action.exceptions.DatabaseOperationsException 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 2 with DatabaseOperationsException

use of com.axway.ats.action.exceptions.DatabaseOperationsException 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 3 with DatabaseOperationsException

use of com.axway.ats.action.exceptions.DatabaseOperationsException 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 4 with DatabaseOperationsException

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

the class DatabaseOperations method delete.

/**
 * Run delete query
 *
 * @param tableName the table we will delete in
 * @param whereClause the where clause, pass 'null' if want to delete all table values.
 * <br><b>Note: </b>The where clause must content inside any needed parameter escaping.
 * @return the number of deleted rows
 */
@PublicAtsApi
public int delete(String tableName, String whereClause) {
    String sqlQuery = "DELETE FROM " + tableName;
    if (whereClause != null) {
        whereClause = whereClause.trim();
        if (whereClause.length() > 0) {
            sqlQuery = sqlQuery + " WHERE " + whereClause;
        }
    }
    int rowsDeleted;
    try {
        log.debug("Executing query: '" + sqlQuery + "'");
        rowsDeleted = dbProvider.executeUpdate(sqlQuery);
        if (rowsDeleted == 0) {
            log.warn("SQL query '" + sqlQuery + "' affected 0 rows");
        } else {
            log.debug("SQL query '" + sqlQuery + "' affected '" + rowsDeleted + "' rows");
        }
    } catch (DbException e) {
        throw new DatabaseOperationsException("Error executing query '" + sqlQuery + "'", e);
    }
    return rowsDeleted;
}
Also used : DatabaseOperationsException(com.axway.ats.action.exceptions.DatabaseOperationsException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 5 with DatabaseOperationsException

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

the class DatabaseOperations method getDatabaseDataAsStrings.

/**
 * <p>
 * Gets data from a database table. All values as returned as String representations as needed.<br>
 * For example:
 * <ul>
 *  <li>TIMESTAMPs are retuned as java.sql.Timestamp.toString() format
 *  (yyyy-MM-dd HH:mm:ss.SSS and millis instead of nanos as documented in JavaSE Doc)</li>
 *  <li>BLOBs bytes are represented in hex format. Beware not to select too big data cells.</li>
 * </ul>
 * </p>
 * @param sqlQuery the SQL SELECT query to run. Client is resposible for any
 *      possible escaping needed so thread this as not security-safe method
 * @return the found database data
 */
@PublicAtsApi
public DatabaseRow[] getDatabaseDataAsStrings(String sqlQuery) {
    List<DatabaseRow> dbRows = new LinkedList<DatabaseRow>();
    try {
        log.debug("Executing query: " + sqlQuery);
        DbRecordValuesList[] rsList = dbProvider.select(new DbQuery(sqlQuery), DbReturnModes.STRING);
        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) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DatabaseCell(com.axway.ats.action.dbaccess.model.DatabaseCell) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) DatabaseOperationsException(com.axway.ats.action.exceptions.DatabaseOperationsException) DatabaseRow(com.axway.ats.action.dbaccess.model.DatabaseRow) LinkedList(java.util.LinkedList) 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