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);
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations