Search in sources :

Example 1 with CassandraDbProvider

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

the class CassandraEnvironmentHandler method restore.

public void restore(String backupFileName) throws DatabaseEnvironmentCleanupException {
    BufferedReader backupReader = null;
    try {
        log.debug("Starting restoring db backup from file '" + backupFileName + "'");
        backupReader = new BufferedReader(new FileReader(new File(backupFileName)));
        StringBuilder sql = new StringBuilder();
        String line = backupReader.readLine();
        while (line != null) {
            sql.append(line);
            if (line.endsWith(EOL_MARKER)) {
                // remove the EOL marker
                sql.delete(sql.length() - EOL_MARKER.length(), sql.length());
                dbProvider.executeUpdate(sql.toString());
                sql.delete(0, sql.length());
            } else {
                //add a new line
                //FIXME: this code will add the system line ending - it
                //is not guaranteed that this was the actual line ending
                sql.append(AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
            }
            line = backupReader.readLine();
        }
        log.debug("Finished restoring db backup from file '" + backupFileName + "'");
    } catch (IOException ioe) {
        throw new DatabaseEnvironmentCleanupException(ERROR_RESTORING_BACKUP + backupFileName, ioe);
    } catch (DbException dbe) {
        throw new DatabaseEnvironmentCleanupException(ERROR_RESTORING_BACKUP + backupFileName, dbe);
    } finally {
        try {
            if (backupReader != null) {
                backupReader.close();
            }
        } catch (IOException ioe) {
            log.error(ERROR_RESTORING_BACKUP + backupFileName, ioe);
        }
        ((CassandraDbProvider) dbProvider).disconnect();
    }
}
Also used : DatabaseEnvironmentCleanupException(com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)

Example 2 with CassandraDbProvider

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

the class CassandraEnvironmentHandler method getColumnsToSelect.

@Override
protected List<ColumnDescription> getColumnsToSelect(DbTable table, String userName) throws DbException, ColumnHasNoDefaultValueException {
    Map<String, String> columnInfo = ((CassandraDbProvider) this.dbProvider).getColumnInfo(table.getTableName());
    ArrayList<ColumnDescription> columnsToSelect = new ArrayList<ColumnDescription>();
    for (Entry<String, String> columnEntry : columnInfo.entrySet()) {
        //check if the column should be skipped in the backup
        if (!table.getColumnsToExclude().contains(columnEntry.getKey())) {
            String dataTypes = columnEntry.getValue();
            ColumnDescription colDescription;
            if (!dataTypes.contains("|")) {
                colDescription = new ColumnDescription(columnEntry.getKey(), columnEntry.getValue());
            } else {
                String[] dataTypesArray = dataTypes.split("\\|");
                String[] subDataTypesArray = new String[dataTypesArray.length - 1];
                for (int i = 0; i < subDataTypesArray.length; i++) {
                    subDataTypesArray[i] = dataTypesArray[i + 1];
                }
                colDescription = new ColumnDescription(columnEntry.getKey(), dataTypesArray[0], subDataTypesArray);
            }
            columnsToSelect.add(colDescription);
        }
    }
    return columnsToSelect;
}
Also used : ColumnDescription(com.axway.ats.core.dbaccess.ColumnDescription) ArrayList(java.util.ArrayList) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)

Example 3 with CassandraDbProvider

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

the class DatabaseProviderFactory method getDatabaseProvider.

/**
	 * Creates a database provider suitable for the given input arguments
	 * 
	 * @param dbType
	 * @param dbHost
	 * @param dbName
	 * @param dbUser
	 * @param dbPass
	 * @param dbPort
	 * @param customProperties
	 * @return
	 */
public static synchronized DbProvider getDatabaseProvider(String dbType, String dbHost, String dbName, String dbUser, String dbPass, int dbPort, Map<String, Object> customProperties) {
    DbProvider dbProvider;
    if (dbType == null) {
        throw new IllegalArgumentException("Database type is not provided");
    }
    if (dbName == null) {
        if ("MSSQL".equals(dbType)) {
            log.warn("Database name is empty! The connection will be made to the admin database!");
        } else {
            throw new IllegalArgumentException("Database name is not provided");
        }
    }
    // eventual custom property value
    if (dbPort > 0) {
        if (customProperties == null) {
            customProperties = new HashMap<String, Object>();
        }
        customProperties.put(DbKeys.PORT_KEY, dbPort);
    }
    // load a custom data provider
    if (dbProviders.containsKey(dbType)) {
        String[] classNames = dbProviders.get(dbType);
        DbConnection dbConnection = loadDbConnection(classNames[0], dbType, dbHost, dbName, dbUser, dbPass, dbPort, customProperties);
        return loadCustomDbProvider(classNames[1], dbConnection);
    }
    // load a common ATS supported data provider
    switch(dbType) {
        case DbConnSQLServer.DATABASE_TYPE:
            dbProvider = new MssqlDbProvider((DbConnSQLServer) createDbConnection(dbType, dbHost, dbName, dbUser, dbPass, customProperties));
            break;
        case DbConnMySQL.DATABASE_TYPE:
            dbProvider = new MysqlDbProvider((DbConnMySQL) createDbConnection(dbType, dbHost, dbName, dbUser, dbPass, customProperties));
            break;
        case DbConnOracle.DATABASE_TYPE:
            dbProvider = new OracleDbProvider((DbConnOracle) createDbConnection(dbType, dbHost, dbName, dbUser, dbPass, customProperties));
            break;
        case DbConnCassandra.DATABASE_TYPE:
            dbProvider = new CassandraDbProvider((DbConnCassandra) createDbConnection(dbType, dbHost, dbName, dbUser, dbPass, customProperties));
            break;
        default:
            {
                // should never happen
                throw new IllegalArgumentException("Database type " + dbType + " not supported");
            }
    }
    return dbProvider;
}
Also used : OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) MssqlDbProvider(com.axway.ats.core.dbaccess.mssql.MssqlDbProvider) DbConnSQLServer(com.axway.ats.core.dbaccess.mssql.DbConnSQLServer) MysqlDbProvider(com.axway.ats.core.dbaccess.mysql.MysqlDbProvider) DbConnMySQL(com.axway.ats.core.dbaccess.mysql.DbConnMySQL) DbConnOracle(com.axway.ats.core.dbaccess.oracle.DbConnOracle) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider) OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) MssqlDbProvider(com.axway.ats.core.dbaccess.mssql.MssqlDbProvider) MysqlDbProvider(com.axway.ats.core.dbaccess.mysql.MysqlDbProvider) DbConnCassandra(com.axway.ats.core.dbaccess.cassandra.DbConnCassandra) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)

Example 4 with CassandraDbProvider

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

the class DatabaseOperations method updateValue.

/**
     * Run update query.
     *
     * @param sqlQuery the SQL query to run
     * @return the number of updated rows
     */
@PublicAtsApi
public int updateValue(String sqlQuery) {
    int rowsUpdated;
    try {
        log.debug("Executing update query: '" + sqlQuery + "'");
        rowsUpdated = dbProvider.executeUpdate(sqlQuery);
        if (!(dbProvider instanceof CassandraDbProvider)) {
            if (rowsUpdated == 0) {
                log.warn("SQL query '" + sqlQuery + "' updated 0 rows");
            } else {
                log.debug("SQL query '" + sqlQuery + "' updated '" + rowsUpdated + "' rows");
            }
        }
    } catch (DbException e) {
        throw new DatabaseOperationsException("Error executing update query '" + sqlQuery + "'", e);
    }
    return rowsUpdated;
}
Also used : DatabaseOperationsException(com.axway.ats.action.exceptions.DatabaseOperationsException) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)4 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)2 DatabaseOperationsException (com.axway.ats.action.exceptions.DatabaseOperationsException)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)1 DbConnCassandra (com.axway.ats.core.dbaccess.cassandra.DbConnCassandra)1 DbConnSQLServer (com.axway.ats.core.dbaccess.mssql.DbConnSQLServer)1 MssqlDbProvider (com.axway.ats.core.dbaccess.mssql.MssqlDbProvider)1 DbConnMySQL (com.axway.ats.core.dbaccess.mysql.DbConnMySQL)1 MysqlDbProvider (com.axway.ats.core.dbaccess.mysql.MysqlDbProvider)1 DbConnOracle (com.axway.ats.core.dbaccess.oracle.DbConnOracle)1 OracleDbProvider (com.axway.ats.core.dbaccess.oracle.OracleDbProvider)1 DatabaseEnvironmentCleanupException (com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1