Search in sources :

Example 16 with DbRecordValuesList

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

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

the class AbstractEnvironmentHandler method writeBackupToFile.

/**
     * Write the backup to a file
     *
     * @param fileWriter the file writer
     * @throws IOException on io error
     * @throws DatabaseEnvironmentCleanupException on error
     * @throws DbException on error reading from the database
     * @throws ParseException
     */
protected void writeBackupToFile(FileWriter fileWriter) throws IOException, DatabaseEnvironmentCleanupException, DbException, ParseException {
    if (disableForeignKeys) {
        fileWriter.write(disableForeignKeyChecksStart());
    }
    for (DbTable dbTable : dbTables) {
        if (log.isDebugEnabled()) {
            log.debug("Preparing data for backup of table " + (dbTable != null ? dbTable.getTableName() : null));
        }
        List<ColumnDescription> columnsToSelect = null;
        columnsToSelect = getColumnsToSelect(dbTable, dbConnection.getUser());
        if (columnsToSelect == null || columnsToSelect.size() == 0) {
            // it contains some meaningful data and so it has columns
            throw new DatabaseEnvironmentCleanupException("No columns to backup for table " + (dbTable != null ? dbTable.getTableName() : ""));
        }
        StringBuilder selectQuery = new StringBuilder();
        selectQuery.append("SELECT ");
        selectQuery.append(getColumnsString(columnsToSelect));
        selectQuery.append(" FROM ");
        selectQuery.append(dbTable.getTableName());
        DbQuery query = new DbQuery(selectQuery.toString());
        DbRecordValuesList[] records = dbProvider.select(query, DbReturnModes.ESCAPED_STRING);
        writeTableToFile(columnsToSelect, dbTable, records, fileWriter);
    }
    if (disableForeignKeys) {
        fileWriter.write(disableForeignKeyChecksEnd());
    }
}
Also used : DatabaseEnvironmentCleanupException(com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) ColumnDescription(com.axway.ats.core.dbaccess.ColumnDescription) DbTable(com.axway.ats.environment.database.model.DbTable)

Example 18 with DbRecordValuesList

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

Example 19 with DbRecordValuesList

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

the class DatabaseOperations method getValue.

/**
     * Gets a single value from a database. </br>
     * It returns the first value of the first returned row.
     *
     * <p><strong>Note</strong>: If the selected DB object is array - BLOB, LONG BLOB ... we return it this way:
     *  <code>[ 10, 24, -35]</code>
     *  </p>
     *
     * @param sqlQuery the SQL query to run
     * @return
     */
@PublicAtsApi
public String getValue(String sqlQuery) {
    try {
        log.debug("Executing query: " + sqlQuery);
        DbRecordValuesList[] rsList = dbProvider.select(sqlQuery);
        if (rsList != null && rsList.length > 0) {
            if (rsList.length > 1) {
                log.warn("SQL query '" + sqlQuery + "' returned " + rsList.length + " rows of results");
            }
            if (rsList[0].size() > 1) {
                log.warn("SQL query '" + sqlQuery + "' returned " + rsList[0].size() + " values per row");
            }
            return rsList[0].get(0).getValueAsString();
        }
    } catch (DbException e) {
        throw new DatabaseOperationsException("Error getting value from DB with query '" + sqlQuery + "'", e);
    }
    return null;
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DatabaseOperationsException(com.axway.ats.action.exceptions.DatabaseOperationsException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 20 with DbRecordValuesList

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

the class CassandraDbProvider method rowCount.

@Override
public int rowCount(String tableName, String whereCondition) throws DbException, NumberValidationException {
    int returnCount;
    String sql = "SELECT " + " COUNT(*)" + " FROM " + tableName;
    if (whereCondition != null && whereCondition.length() > 0) {
        sql += " WHERE " + whereCondition;
    }
    DbRecordValuesList[] records = select(new DbQuery(sql, new ArrayList<Object>()), DbReturnModes.STRING);
    try {
        returnCount = records == null ? 0 : ((Long) records[0].get("count")).intValue();
    } catch (NumberFormatException nfe) {
        throw new NumberValidationException("The row count could not be converted to integer", nfe);
    }
    return returnCount;
}
Also used : NumberValidationException(com.axway.ats.core.validation.exceptions.NumberValidationException) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) ArrayList(java.util.ArrayList)

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