use of com.axway.ats.core.dbaccess.DbRecordValue 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.core.dbaccess.DbRecordValue in project ats-framework by Axway.
the class PostgreSqlDbProvider method obtainPartitionedTables.
private void obtainPartitionedTables() {
String partitionCreatedTable = "partitioned_created_table";
String query = "SELECT i.inhrelid::regclass AS " + partitionCreatedTable + " FROM pg_inherits i;";
DbRecordValuesList[] recordsLists = this.select(query);
for (DbRecordValuesList recordList : recordsLists) {
for (DbRecordValue value : recordList) {
String partitionedCreatedTableName = value.getValueAsString();
partitionedTables.add(partitionedCreatedTableName);
}
}
}
use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.
the class CassandraDbProvider method select.
@Override
public DbRecordValuesList[] select(DbQuery dbQuery, DbReturnModes dbReturnMode) throws DbException {
connect();
ArrayList<DbRecordValuesList> dbRecords = new ArrayList<DbRecordValuesList>();
String sqlQuery = dbQuery.getQuery();
if (allowFiltering) {
sqlQuery += " ALLOW FILTERING";
}
if (log.isDebugEnabled()) {
log.debug(sqlQuery);
}
ResultSet results = session.execute(sqlQuery);
int currentRow = 0;
Iterator<Row> it = results.iterator();
while (it.hasNext()) {
Row row = it.next();
currentRow++;
if (log.isDebugEnabled()) {
log.debug("Result row number: " + currentRow);
}
DbRecordValuesList recordList = new DbRecordValuesList();
for (Definition columnDefinition : row.getColumnDefinitions()) {
DbColumn dbColumn = new DbColumn(columnDefinition.getTable(), columnDefinition.getName());
dbColumn.setColumnType(columnDefinition.getType().getName().toString());
Object value = extractObjectFromResultSet(row, columnDefinition);
DbRecordValue recordValue = new DbRecordValue(dbColumn, value);
recordList.add(recordValue);
}
dbRecords.add(recordList);
}
return dbRecords.toArray(new DbRecordValuesList[] {});
}
use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.
the class MssqlDbProvider method parseDbRecordAsObject.
@Override
protected DbRecordValue parseDbRecordAsObject(DbColumn dbColumn, ResultSet res, int columnIndex) throws IOException, SQLException {
DbRecordValue recordValue = null;
String type = dbColumn.getColumnType().toLowerCase();
String name = dbColumn.getColumnName().toLowerCase();
MssqlColumnDescription columnDescription = new MssqlColumnDescription(name, type);
// otherwise we get the object address
if (columnDescription.isTypeBinary() || "text".equals(type) || "ntext".equals(type)) {
/*
* http://jtds.sourceforge.net/faq.html
* By default useLOBs is true. In such cases calling getObject on the result set returns
* a LOB object, not a java String. In order to get java String it is needed to use the getString method.
* If useLOBs is false - getObject returns java String
*
* "useLOBs=false;" can be added in the connection URL, or we can simply call the getString method here
*/
recordValue = new DbRecordValue(dbColumn, res.getString(columnIndex));
} else {
recordValue = new DbRecordValue(dbColumn, res.getObject(columnIndex));
}
return recordValue;
}
use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.
the class MysqlDbProvider method extractTableIndexes.
@Override
protected Map<String, String> extractTableIndexes(String tableName, DatabaseMetaData databaseMetaData, String catalog) throws DbException {
String sql = "SELECT TABLE_NAME, CONCAT('" + IndexProperties.COLUMN_NAME + "=', " + IndexProperties.COLUMN_NAME + ", ', " + IndexProperties.INDEX_NAME + "=', " + IndexProperties.INDEX_NAME + ") AS " + IndexProperties.INDEX_UID + "," + IndexProperties.NON_UNIQUE + "," + IndexProperties.SEQ_IN_INDEX + "," + IndexProperties.COLUMN_NAME + "," + IndexProperties.COLLATION + "," + IndexProperties.SUB_PART + "," + IndexProperties.PACKED + "," + IndexProperties.NULLABLE + "," + IndexProperties.INDEX_TYPE + " " + "FROM INFORMATION_SCHEMA.STATISTICS " + "WHERE TABLE_NAME='" + tableName + "' AND TABLE_SCHEMA = '" + dbConnection.getDb() + "'";
String indexUid = null;
Map<String, String> indexes = new HashMap<>();
for (DbRecordValuesList valueList : select(sql)) {
StringBuilder info = new StringBuilder();
boolean firstTime = true;
for (DbRecordValue dbValue : valueList) {
String value = dbValue.getValueAsString();
String name = dbValue.getDbColumn().getColumnName();
if (IndexProperties.INDEX_UID.equalsIgnoreCase(name)) {
indexUid = value;
} else {
if (firstTime) {
firstTime = false;
info.append(name + "=" + value);
} else {
info.append(", " + name + "=" + value);
}
}
}
if (indexUid == null) {
indexUid = "NULL_UID_FOUND_FOR_INDEX_OF_TABLE_" + tableName;
log.warn("" + IndexProperties.INDEX_UID + " column not found in query polling for index properties:\nQuery: " + sql + "\nQuery result: " + valueList.toString() + "\nWe will use the following as an index uid: " + indexUid);
}
indexes.put(indexUid, info.toString());
}
return indexes;
}
Aggregations