Search in sources :

Example 31 with DbRecordValue

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

Example 32 with DbRecordValue

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);
        }
    }
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue)

Example 33 with DbRecordValue

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[] {});
}
Also used : DbColumn(com.axway.ats.core.dbaccess.DbColumn) DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) ArrayList(java.util.ArrayList) ResultSet(com.datastax.driver.core.ResultSet) Definition(com.datastax.driver.core.ColumnDefinitions.Definition) Row(com.datastax.driver.core.Row)

Example 34 with DbRecordValue

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;
}
Also used : DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) MssqlColumnDescription(com.axway.ats.core.dbaccess.MssqlColumnDescription)

Example 35 with DbRecordValue

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;
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) HashMap(java.util.HashMap) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue)

Aggregations

DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)35 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)33 DbQuery (com.axway.ats.common.dbaccess.DbQuery)12 BaseTest (com.axway.ats.environment.BaseTest)12 DbTable (com.axway.ats.environment.database.model.DbTable)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)8 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)4 Connection (java.sql.Connection)4 HashMap (java.util.HashMap)4 OracleColumnDescription (com.axway.ats.core.dbaccess.OracleColumnDescription)3 DatabaseCell (com.axway.ats.action.dbaccess.model.DatabaseCell)2 DatabaseRow (com.axway.ats.action.dbaccess.model.DatabaseRow)2 DatabaseOperationsException (com.axway.ats.action.exceptions.DatabaseOperationsException)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)2 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)2 DbColumn (com.axway.ats.core.dbaccess.DbColumn)1 MssqlColumnDescription (com.axway.ats.core.dbaccess.MssqlColumnDescription)1 MetaData (com.axway.ats.rbv.MetaData)1 RbvException (com.axway.ats.rbv.model.RbvException)1