Search in sources :

Example 1 with DbQuery

use of com.axway.ats.common.dbaccess.DbQuery 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 : DbQuery(com.axway.ats.common.dbaccess.DbQuery) 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) 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)

Example 2 with DbQuery

use of com.axway.ats.common.dbaccess.DbQuery in project ats-framework by Axway.

the class AbstractDbProvider method rowCount.

/**
     * @param tableName         the name of the table
     * @param whereCondition    the where condition ( without the WHERE keyword )
     * @return                  returns the number of the rows that
     *                          match the where statement as a int. Returns 0 if there is
     *                          an error or the rowcount is 0
     */
@Override
public int rowCount(String tableName, String whereCondition) throws DbException, NumberValidationException {
    int returnCount;
    String sql = " SELECT " + " COUNT(*)" + " FROM " + tableName;
    if (whereCondition != null && whereCondition.length() > 0) {
        sql += " WHERE " + whereCondition;
    }
    DbRecordValuesList[] records = select(new DbQuery(sql, new ArrayList<Object>()), DbReturnModes.STRING);
    try {
        returnCount = records == null ? 0 : Integer.parseInt((String) records[0].get("COUNT(*)"));
    } catch (NumberFormatException nfe) {
        throw new NumberValidationException("The row count could not be converted to integer", nfe);
    }
    return returnCount;
}
Also used : NumberValidationException(com.axway.ats.core.validation.exceptions.NumberValidationException) DbQuery(com.axway.ats.common.dbaccess.DbQuery) ArrayList(java.util.ArrayList)

Example 3 with DbQuery

use of com.axway.ats.common.dbaccess.DbQuery in project ats-framework by Axway.

the class AbstractDbProvider method selectValue.

public InputStream selectValue(String tableName, String[] keyColumns, String[] keyValues, String queryColumn, int recordNumber) throws DbException, ValidationException {
    InputStream queryValueStream = null;
    //all operands are "equals"
    String[] whereOperands = new String[keyColumns.length];
    for (int i = 0; i < whereOperands.length; i++) {
        whereOperands[i] = "=";
    }
    String sql = constructSelectStatement(new String[] { queryColumn }, tableName, keyColumns, keyValues, whereOperands);
    DbRecordValuesList[] records = select(new DbQuery(sql, new ArrayList<Object>()), DbReturnModes.INPUT_STREAM);
    if (records == null) {
        throw new DbRecordsException();
    }
    if (records.length < recordNumber + 1) {
        throw new DbRecordsException(records.length, recordNumber + 1);
    } else {
        queryValueStream = (InputStream) records[recordNumber].get(queryColumn);
    }
    return queryValueStream;
}
Also used : DbQuery(com.axway.ats.common.dbaccess.DbQuery) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) DbRecordsException(com.axway.ats.core.dbaccess.exceptions.DbRecordsException)

Example 4 with DbQuery

use of com.axway.ats.common.dbaccess.DbQuery in project ats-framework by Axway.

the class CassandraDbProvider method rowCount.

@Override
public int rowCount(String tableName, String whereCondition) throws DbException, NumberValidationException {
    int returnCount;
    String sql = "SELECT " + " COUNT(*)" + " FROM " + tableName;
    if (whereCondition != null && whereCondition.length() > 0) {
        sql += " WHERE " + whereCondition;
    }
    DbRecordValuesList[] records = select(new DbQuery(sql, new ArrayList<Object>()), DbReturnModes.STRING);
    try {
        returnCount = records == null ? 0 : ((Long) records[0].get("count")).intValue();
    } catch (NumberFormatException nfe) {
        throw new NumberValidationException("The row count could not be converted to integer", nfe);
    }
    return returnCount;
}
Also used : NumberValidationException(com.axway.ats.core.validation.exceptions.NumberValidationException) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) ArrayList(java.util.ArrayList)

Example 5 with DbQuery

use of com.axway.ats.common.dbaccess.DbQuery in project ats-framework by Axway.

the class AbstractDbProvider method selectValue.

public InputStream selectValue(String tableName, String keyColumn, String keyValue, String queryColumn, int recordNumber) throws DbException {
    InputStream queryValueStream = null;
    String sql = " SELECT " + queryColumn + " FROM " + tableName + " WHERE " + keyColumn + " = '" + escapeSql(keyValue) + "'";
    DbRecordValuesList[] records = select(new DbQuery(sql, new ArrayList<Object>()), DbReturnModes.INPUT_STREAM);
    if (records == null) {
        throw new DbRecordsException();
    }
    if (records.length < recordNumber + 1) {
        throw new DbRecordsException(records.length, recordNumber + 1);
    }
    queryValueStream = (InputStream) records[recordNumber].get(queryColumn);
    return queryValueStream;
}
Also used : DbQuery(com.axway.ats.common.dbaccess.DbQuery) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) DbRecordsException(com.axway.ats.core.dbaccess.exceptions.DbRecordsException)

Aggregations

DbQuery (com.axway.ats.common.dbaccess.DbQuery)6 ArrayList (java.util.ArrayList)4 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)3 DbRecordsException (com.axway.ats.core.dbaccess.exceptions.DbRecordsException)2 NumberValidationException (com.axway.ats.core.validation.exceptions.NumberValidationException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 DatabaseCell (com.axway.ats.action.dbaccess.model.DatabaseCell)1 DatabaseRow (com.axway.ats.action.dbaccess.model.DatabaseRow)1 DatabaseOperationsException (com.axway.ats.action.exceptions.DatabaseOperationsException)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)1 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)1 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)1 DatabaseEnvironmentCleanupException (com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException)1 DbTable (com.axway.ats.environment.database.model.DbTable)1 LinkedList (java.util.LinkedList)1