Search in sources :

Example 21 with DbRecordValuesList

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

use of com.axway.ats.core.dbaccess.DbRecordValuesList 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,INDEX_NAME,NON_UNIQUE,SEQ_IN_INDEX,COLUMN_NAME,COLLATION,SUB_PART,PACKED,NULLABLE,INDEX_TYPE " + "FROM INFORMATION_SCHEMA.STATISTICS " + "WHERE TABLE_NAME='" + tableName + "'";
    String indexName = null;
    Map<String, String> indexes = new HashMap<>();
    for (DbRecordValuesList valueList : select(sql)) {
        StringBuilder info = new StringBuilder();
        for (DbRecordValue dbValue : valueList) {
            String value = dbValue.getValueAsString();
            String name = dbValue.getDbColumn().getColumnName();
            if ("INDEX_NAME".equalsIgnoreCase(name)) {
                indexName = value;
            } else {
                info.append(", " + name + "=" + value);
            }
        }
        if (indexName == null) {
            indexName = "NULL_NAME_FOUND_FOR_INDEX_OF_TABLE_" + tableName;
            log.warn("IndexName column not found in query polling for index properties:\nQuery: " + sql + "\nQuery result: " + valueList.toString() + "\nWe will use the following as an index name: " + indexName);
        }
        indexes.put(indexName, info.toString());
    }
    return indexes;
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) HashMap(java.util.HashMap) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue)

Example 23 with DbRecordValuesList

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

the class MysqlEnvironmentHandler method writeTableToFile.

@Override
protected void writeTableToFile(List<ColumnDescription> columns, DbTable table, DbRecordValuesList[] records, FileWriter fileWriter) throws IOException {
    if (this.addLocks && table.isLockTable()) {
        fileWriter.write("LOCK TABLES `" + table.getTableName() + "` WRITE;" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
    }
    if (this.includeDeleteStatements) {
        fileWriter.write("DELETE FROM `" + table.getTableName() + "`;" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
    }
    if (table.getAutoIncrementResetValue() != null) {
        fileWriter.write("SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO';" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
        fileWriter.write("ALTER TABLE `" + table.getTableName() + "` AUTO_INCREMENT = " + table.getAutoIncrementResetValue() + ";" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
        // To handle this, lock the table again
        if (this.addLocks && table.isLockTable()) {
            fileWriter.write("LOCK TABLES `" + table.getTableName() + "` WRITE;" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
        }
    }
    if (records.length > 0) {
        StringBuilder insertStatement = new StringBuilder();
        String insertBegin = "INSERT INTO `" + table.getTableName() + "` (" + getColumnsString(columns) + ") VALUES(";
        String insertEnd = ");" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR;
        for (DbRecordValuesList record : records) {
            // clear the StringBuilder current data
            // it is a little better (almost the same) than insertStatement.setLength( 0 ); as performance
            insertStatement.delete(0, insertStatement.length());
            insertStatement.append(insertBegin);
            for (int i = 0; i < record.size(); i++) {
                DbRecordValue recordValue = record.get(i);
                String fieldValue = (String) recordValue.getValue();
                if (fieldValue == null) {
                    fieldValue = "NULL";
                }
                // extract specific values depending on their type
                insertStatement.append(extractValue(columns.get(i), fieldValue));
                insertStatement.append(",");
            }
            //remove the last comma
            insertStatement.delete(insertStatement.length() - 1, insertStatement.length());
            insertStatement.append(insertEnd);
            fileWriter.write(insertStatement.toString());
            fileWriter.flush();
        }
    }
    if (this.addLocks && table.isLockTable()) {
        fileWriter.write("UNLOCK TABLES;" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
    }
    fileWriter.write(AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue)

Example 24 with DbRecordValuesList

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

the class OracleEnvironmentHandler method writeTableToFile.

@Override
protected void writeTableToFile(List<ColumnDescription> columns, DbTable table, DbRecordValuesList[] records, FileWriter fileWriter) throws IOException, ParseException {
    if (this.includeDeleteStatements) {
        fileWriter.write("DELETE FROM " + table.getTableName() + ";" + EOL_MARKER + LINE_SEPARATOR);
    }
    if (records.length > 0) {
        if (containsBinaryTypes(columns)) {
            /*
                 * If the binary value is too long the INSERT operation will fail.
                 * The workaround is to assign the binary value to a variable and then use it in the INSERT(or UPDATE) statement
                 * We have to use DECLARE/BEGIN/END anonymous block statement like this:
                 *
                   DECLARE
                      binValue_0 TABLE_NAME.BLOB_COLUMN_NAME%type;
                    BEGIN
                      binValue_0 := to_blob('3C3F786D6C2076657273696F6E3D22312E3022');
                      INSERT INTO TABLE_NAME(NAME,BLOB_COLUMN_NAME,DESCRIPTION) VALUES ('index.xhtml', binValue_0, 'Index file');
                    END; -- ATS EOL;
                 */
            final String INDENTATION = "  ";
            final String VAR_PREFIX = "binValue_";
            StringBuilder stmtBlockBuilder = new StringBuilder("DECLARE" + LINE_SEPARATOR);
            int variableIndex = 0;
            for (ColumnDescription column : columns) {
                if (column.isTypeBinary()) {
                    stmtBlockBuilder.append(INDENTATION + VAR_PREFIX + (variableIndex++) + " " + table.getTableName() + "." + column.getName() + "%type;" + LINE_SEPARATOR);
                }
            }
            stmtBlockBuilder.append("BEGIN" + LINE_SEPARATOR);
            int stmtBlockStart = stmtBlockBuilder.length();
            String insertBegin = INDENTATION + "INSERT INTO " + table.getTableName() + "(" + getColumnsString(columns) + ") VALUES (";
            String insertEnd = ");" + LINE_SEPARATOR;
            for (DbRecordValuesList record : records) {
                StringBuilder insertStatement = new StringBuilder();
                variableIndex = 0;
                for (int i = 0; i < record.size(); i++) {
                    ColumnDescription column = columns.get(i);
                    DbRecordValue recordValue = record.get(i);
                    // extract the value depending on the column type
                    String fieldValue = extractValue(column, (String) recordValue.getValue()).toString();
                    if (column.isTypeBinary()) {
                        String varName = VAR_PREFIX + (variableIndex++);
                        stmtBlockBuilder.append(INDENTATION + varName + " := " + fieldValue + ";" + LINE_SEPARATOR);
                        insertStatement.append(varName);
                    } else {
                        insertStatement.append(fieldValue);
                    }
                    insertStatement.append(",");
                }
                //remove the last comma
                insertStatement.delete(insertStatement.length() - 1, insertStatement.length());
                stmtBlockBuilder.append(insertBegin);
                stmtBlockBuilder.append(insertStatement.toString());
                stmtBlockBuilder.append(insertEnd);
                stmtBlockBuilder.append("END;" + EOL_MARKER + LINE_SEPARATOR);
                fileWriter.write(stmtBlockBuilder.toString());
                fileWriter.flush();
                // clear to block BEGIN tag
                stmtBlockBuilder.delete(stmtBlockStart, stmtBlockBuilder.length());
            }
        } else {
            StringBuilder insertStatement = new StringBuilder();
            String insertBegin = "INSERT INTO " + table.getTableName() + "(" + getColumnsString(columns) + ") VALUES (";
            String insertEnd = ");" + EOL_MARKER + LINE_SEPARATOR;
            for (DbRecordValuesList record : records) {
                insertStatement.append(insertBegin);
                for (int i = 0; i < record.size(); i++) {
                    DbRecordValue recordValue = record.get(i);
                    String fieldValue = (String) recordValue.getValue();
                    // extract specific values depending on their type
                    insertStatement.append(extractValue(columns.get(i), fieldValue));
                    insertStatement.append(",");
                }
                //remove the last comma
                insertStatement.delete(insertStatement.length() - 1, insertStatement.length());
                insertStatement.append(insertEnd);
            }
            fileWriter.write(insertStatement.toString());
        }
    }
// TODO : exclusive table locks END
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) OracleColumnDescription(com.axway.ats.core.dbaccess.OracleColumnDescription) ColumnDescription(com.axway.ats.core.dbaccess.ColumnDescription)

Example 25 with DbRecordValuesList

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

the class Test_OracleEnvironmentHandler method createBackupNegativeSkippedColumnIsNotNullable.

@Test(expected = ColumnHasNoDefaultValueException.class)
public void createBackupNegativeSkippedColumnIsNotNullable() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException {
    DbTable table1 = new DbTable("table1");
    List<String> columnsToSkip = new ArrayList<String>();
    columnsToSkip.add("name2");
    DbTable table2 = new DbTable("table2", columnsToSkip);
    //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 column2MetaData = new DbRecordValuesList();
    column2MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name2"));
    column2MetaData.add(new DbRecordValue("", "DATA_TYPE", "varchar(32)"));
    column2MetaData.add(new DbRecordValue("", "DATA_DEFAULT", null));
    DbRecordValuesList column3MetaData = new DbRecordValuesList();
    column3MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name3"));
    column3MetaData.add(new DbRecordValue("", "DATA_TYPE", "bit"));
    column3MetaData.add(new DbRecordValue("", "DATA_DEFAULT", 1));
    DbRecordValuesList[] columnsMetaData = new DbRecordValuesList[] { column1MetaData, column2MetaData, column3MetaData };
    DbRecordValuesList record1Value = new DbRecordValuesList();
    record1Value.add(new DbRecordValue("table1", "name1", "value1"));
    record1Value.add(new DbRecordValue("table1", "name2", null));
    record1Value.add(new DbRecordValue("table1", "name3", new String(new char[] { 1 })));
    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);
    replay(mockDbConnection);
    replay(mockDbProvider);
    envHandler.writeBackupToFile(mockFileWriter);
    verify(mockDbConnection);
    verify(mockDbProvider);
}
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) ArrayList(java.util.ArrayList) 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