Search in sources :

Example 11 with DbRecordValue

use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.

the class OracleDbProvider 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();
    OracleColumnDescription columnDescription = new OracleColumnDescription(name, type);
    if (columnDescription.isTypeBinary()) {
        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) OracleColumnDescription(com.axway.ats.core.dbaccess.OracleColumnDescription)

Example 12 with DbRecordValue

use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.

the class CassandraEnvironmentHandler method writeTableToFile.

@Override
protected void writeTableToFile(List<ColumnDescription> columns, DbTable table, DbRecordValuesList[] records, FileWriter fileWriter) throws IOException, ParseException {
    if (this.includeDeleteStatements) {
        fileWriter.write("TRUNCATE " + table.getTableName() + ";" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
    }
    if (records.length > 0) {
        String counterColumnName = getCounterColumn(records);
        if (counterColumnName == null) {
            final String insertBegin = "INSERT INTO " + table.getTableName() + "(" + getColumnsString(columns) + ") VALUES (";
            final String insertEnd = ");" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR;
            StringBuilder insertStatement = new StringBuilder();
            for (DbRecordValuesList dbRow : records) {
                insertStatement.setLength(0);
                insertStatement.append(insertBegin);
                for (int i = 0; i < dbRow.size(); i++) {
                    // extract specific values depending on their type
                    insertStatement.append(extractValue(columns.get(i), dbRow.get(i).getValue()));
                    insertStatement.append(",");
                }
                //remove the last comma
                insertStatement.delete(insertStatement.length() - 1, insertStatement.length());
                insertStatement.append(insertEnd);
                fileWriter.write(insertStatement.toString());
            }
        } else {
            // This is the way to insert rows for tables having counter data type. Only one column could have such type.
            // others are UUIDs. SET counterColumnName=+3/-2 just increases/decreases such counter. If not already existing
            // then 0 is assumed as initial value.
            String insertBegin = "UPDATE " + table.getTableName() + " SET " + counterColumnName + " = " + counterColumnName + " + <the counter value> WHERE ";
            final String insertEnd = EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR;
            StringBuilder insertStatement = new StringBuilder();
            for (DbRecordValuesList dbRow : records) {
                insertStatement.setLength(0);
                insertStatement.append(insertBegin);
                String counterValue = "";
                for (int i = 0; i < dbRow.size(); i++) {
                    DbRecordValue dbValue = dbRow.get(i);
                    String dbColumnName = dbValue.getDbColumn().getColumnName();
                    if (dbColumnName.equals(counterColumnName)) {
                        // this is a counter, we will apply it later
                        counterValue = dbValue.getValue().toString();
                    } else {
                        // extract specific values depending on their type
                        insertStatement.append(dbColumnName + " = " + extractValue(columns.get(i), dbValue.getValue()));
                        insertStatement.append(" AND ");
                    }
                }
                //remove the last 'AND'
                insertStatement.delete(insertStatement.length() - 5, insertStatement.length());
                insertStatement.append(insertEnd);
                fileWriter.write(insertStatement.toString().replace("<the counter value>", counterValue));
            }
        }
    }
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue)

Example 13 with DbRecordValue

use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.

the class MockDbProvider method select.

public DbRecordValuesList[] select(DbQuery query) {
    final int resultCount = 2;
    DbRecordValuesList[] resultSet = new DbRecordValuesList[resultCount];
    for (int i = 0; i < resultCount; i++) {
        DbRecordValuesList result = new DbRecordValuesList();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Timestamp stamp = new Timestamp(calendar.getTime().getTime());
        result.add(new DbRecordValue("asd", "key0", "value" + i + seed));
        result.add(new DbRecordValue("", "key1", "value" + i + seed));
        result.add(new DbRecordValue("", "key2", "value" + i + seed));
        result.add(new DbRecordValue("numeric", "key", i));
        result.add(new DbRecordValue("binary", "key", new byte[] { 1, 2, (byte) i }));
        result.add(new DbRecordValue("boolean", "keyString", "0"));
        result.add(new DbRecordValue("boolean", "keyNumber", 1L));
        result.add(new DbRecordValue("Date", "today", stamp));
        if (!useChangedValues) {
            result.add(new DbRecordValue("", "test_key", "test_value"));
        } else {
            result.add(new DbRecordValue("", "test_key", "test_value_changed"));
        }
        resultSet[i] = result;
    }
    if (useChangedValues) {
        useChangedValues = false;
    }
    return resultSet;
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) Calendar(java.util.Calendar) Timestamp(java.sql.Timestamp)

Example 14 with DbRecordValue

use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.

the class DbFolder method refresh.

private void refresh() throws RbvException {
    newMetaDataMap = new HashMap<String, MetaData>();
    //store the current meta data map and clear the map holding all meta data
    //this way we will be able to detect any changes including added and removed
    //meta data
    HashMap<String, MetaData> oldMetaDataMap = allMetaDataMap;
    allMetaDataMap = new HashMap<String, MetaData>();
    log.debug("Run DB query '" + this.searchQuery.getQuery() + "'");
    DbRecordValuesList[] queryResults;
    try {
        queryResults = dbProvider.select(this.searchQuery);
    } catch (DbException dbe) {
        throw new RbvException(dbe);
    }
    if (queryResults != null) {
        for (DbRecordValuesList queryResult : queryResults) {
            DbMetaData currentData = new DbMetaData();
            StringBuffer metaDataHash = new StringBuffer();
            for (DbRecordValue recordValue : queryResult) {
                DbMetaDataKey key = new DbMetaDataKey(recordValue.getDbColumn());
                Object value = recordValue.getValue();
                currentData.putProperty(key.toString(), value);
                //calculate the hash
                metaDataHash.append(key.toString());
                metaDataHash.append(recordValue.getValueAsString());
            }
            try {
                //compute MD5 so we don't keep the whole StringBuffer in memory
                MessageDigest metaDataHashDigest = MessageDigest.getInstance("MD5");
                String metaDataSum = new String(metaDataHashDigest.digest(metaDataHash.toString().getBytes()));
                if (!oldMetaDataMap.containsKey(metaDataSum)) {
                    newMetaDataMap.put(metaDataSum, currentData);
                }
                //always put the record in the map holding all meta data
                allMetaDataMap.put(metaDataSum, currentData);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
        }
        didPollingOccured = true;
    }
}
Also used : RbvException(com.axway.ats.rbv.model.RbvException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) MetaData(com.axway.ats.rbv.MetaData) MessageDigest(java.security.MessageDigest)

Example 15 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 : 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)

Aggregations

DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)23 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)21 DbQuery (com.axway.ats.common.dbaccess.DbQuery)8 BaseTest (com.axway.ats.environment.BaseTest)8 DbTable (com.axway.ats.environment.database.model.DbTable)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)4 MysqlEnvironmentHandler (com.axway.ats.environment.database.MysqlEnvironmentHandler)4 OracleEnvironmentHandler (com.axway.ats.environment.database.OracleEnvironmentHandler)4 HashMap (java.util.HashMap)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 OracleColumnDescription (com.axway.ats.core.dbaccess.OracleColumnDescription)2 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)1 DbColumn (com.axway.ats.core.dbaccess.DbColumn)1 MssqlColumnDescription (com.axway.ats.core.dbaccess.MssqlColumnDescription)1 MetaData (com.axway.ats.rbv.MetaData)1