Search in sources :

Example 6 with DbRecordValuesList

use of com.axway.ats.core.dbaccess.DbRecordValuesList 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 7 with DbRecordValuesList

use of com.axway.ats.core.dbaccess.DbRecordValuesList 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 8 with DbRecordValuesList

use of com.axway.ats.core.dbaccess.DbRecordValuesList 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 9 with DbRecordValuesList

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

the class MockDbProvider method select.

@Override
public DbRecordValuesList[] select(DbQuery dbQuery) throws DbException {
    DbRecordValuesList[] resultSet = null;
    DbRecordValuesList result = new DbRecordValuesList();
    result.add(new DbRecordValue("asd", "key0", "value00"));
    //testing db exception
    if (dbQuery.getQuery().contains("wrongTableName")) {
        throw new DbException("wrong table name");
    //test more than one result values
    } else if (dbQuery.getQuery().contains("moreValues")) {
        resultSet = new DbRecordValuesList[2];
        resultSet[0] = result;
        DbRecordValuesList sresult = new DbRecordValuesList();
        sresult.add(new DbRecordValue("dfg", "key0", "value11"));
        resultSet[1] = result;
    //testing with DatabaseRow and DatabaseCell classes
    } else if (dbQuery.getQuery().contains("tableWithManyRows")) {
        if (dbQuery.getQuery().contains("*")) {
            resultSet = new DbRecordValuesList[2];
            DbRecordValuesList firstDbRow = new DbRecordValuesList();
            firstDbRow.add(new DbRecordValue("tableWithManyRows", "firstColumnName", "value01"));
            firstDbRow.add(new DbRecordValue("tableWithManyRows", "secondColumnName", "value02"));
            resultSet[0] = firstDbRow;
            DbRecordValuesList secondDbRow = new DbRecordValuesList();
            secondDbRow.add(new DbRecordValue("tableWithManyRows", "firstColumnName", "value11"));
            secondDbRow.add(new DbRecordValue("tableWithManyRows", "secondColumnName", "value12"));
            resultSet[1] = secondDbRow;
        } else {
            resultSet = new DbRecordValuesList[2];
            DbRecordValuesList firstDbRow = new DbRecordValuesList();
            firstDbRow.add(new DbRecordValue("tableWithManyRows", "selectColumnName", "value1"));
            resultSet[0] = firstDbRow;
            DbRecordValuesList secondDbRow = new DbRecordValuesList();
            secondDbRow.add(new DbRecordValue("tableWithManyRows", "selectColumnName", "value2"));
            resultSet[1] = secondDbRow;
        }
    //testing no value result
    } else if (!dbQuery.getQuery().contains("nullValue")) {
        resultSet = new DbRecordValuesList[1];
        resultSet[0] = result;
    }
    return resultSet;
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 10 with DbRecordValuesList

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

the class Test_OracleEnvironmentHandler method createBackupNoForeignKeysNoLockNoDelete.

@Test
public void createBackupNoForeignKeysNoLockNoDelete() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException {
    DbTable table1 = new DbTable("table1");
    DbTable table2 = new DbTable("table2");
    //the columns meta data
    DbRecordValuesList column1MetaData = new DbRecordValuesList();
    column1MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name1"));
    column1MetaData.add(new DbRecordValue("", "DATA_TYPE", "varchar(32)"));
    column1MetaData.add(new DbRecordValue("", "DATA_DEFAULT", true));
    DbRecordValuesList[] columnsMetaData = new DbRecordValuesList[] { column1MetaData };
    DbRecordValuesList record1Value = new DbRecordValuesList();
    record1Value.add(new DbRecordValue("table1", "name1", "value1"));
    DbRecordValuesList[] recordValues = new DbRecordValuesList[] { record1Value };
    OracleEnvironmentHandler envHandler = new OracleEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.addTable(table1);
    envHandler.addTable(table2);
    expect(mockDbConnection.getUser()).andReturn("myUserName").atLeastOnce();
    expect(mockDbProvider.select(isA(String.class))).andReturn(columnsMetaData);
    expect(mockDbProvider.select(isA(DbQuery.class), eq(DbReturnModes.ESCAPED_STRING))).andReturn(recordValues);
    expect(mockDbProvider.select(isA(String.class))).andReturn(columnsMetaData);
    expect(mockDbProvider.select(isA(DbQuery.class), eq(DbReturnModes.ESCAPED_STRING))).andReturn(recordValues);
    //expect the file writer calls
    //table1
    mockFileWriter.write("INSERT INTO table1(name1) VALUES ('value1');" + EOL_MARKER + LINE_SEPARATOR);
    //table2
    mockFileWriter.write("INSERT INTO table2(name1) VALUES ('value1');" + EOL_MARKER + LINE_SEPARATOR);
    replay(mockDbConnection);
    replay(mockDbProvider);
    replay(mockFileWriter);
    envHandler.setForeignKeyCheck(false);
    envHandler.setIncludeDeleteStatements(false);
    envHandler.setLockTables(false);
    envHandler.writeBackupToFile(mockFileWriter);
    verify(mockDbConnection);
    verify(mockDbProvider);
    verify(mockFileWriter);
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) OracleEnvironmentHandler(com.axway.ats.environment.database.OracleEnvironmentHandler) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Aggregations

DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)28 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)21 ArrayList (java.util.ArrayList)11 DbQuery (com.axway.ats.common.dbaccess.DbQuery)10 DbTable (com.axway.ats.environment.database.model.DbTable)9 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)8 BaseTest (com.axway.ats.environment.BaseTest)8 Test (org.junit.Test)8 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)5 MysqlEnvironmentHandler (com.axway.ats.environment.database.MysqlEnvironmentHandler)4 OracleEnvironmentHandler (com.axway.ats.environment.database.OracleEnvironmentHandler)4 DatabaseOperationsException (com.axway.ats.action.exceptions.DatabaseOperationsException)3 PublicAtsApi (com.axway.ats.common.PublicAtsApi)3 ColumnHasNoDefaultValueException (com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException)3 HashMap (java.util.HashMap)3 DatabaseCell (com.axway.ats.action.dbaccess.model.DatabaseCell)2 DatabaseRow (com.axway.ats.action.dbaccess.model.DatabaseRow)2 OracleColumnDescription (com.axway.ats.core.dbaccess.OracleColumnDescription)2 DatabaseSnapshotException (com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)1 DbColumn (com.axway.ats.core.dbaccess.DbColumn)1