Search in sources :

Example 16 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 17 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,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 18 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 19 with DbRecordValue

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

the class MssqlEnvironmentHandler 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 + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
    }
    if (table.getAutoIncrementResetValue() != null) {
        fileWriter.write("DBCC CHECKIDENT ('" + table.getTableName() + "', RESEED, " + table.getAutoIncrementResetValue() + ");" + 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 = null;
        if (table.isIdentityColumnPresent()) {
            insertBegin = "SET IDENTITY_INSERT " + table.getTableName() + " ON; " + insertBegin;
            insertEnd = "); SET IDENTITY_INSERT " + table.getTableName() + " OFF;" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR;
        } else {
            insertEnd = ");" + EOL_MARKER + AtsSystemProperties.SYSTEM_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());
    }
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue)

Example 20 with DbRecordValue

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

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