Search in sources :

Example 1 with DbException

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

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

the class ConnectionPool method getConnection.

/**
     * Get JDBC connection - already existing or create a new one if needed.
     * Searches the hash map if this connection already exists and returns it or it creates a new connection,
     * adds it to the map and returns it.
     *
     * Note: Each connection is identified by "connection string" identifying particular DB/schema instance
     *
     * @param dbConnection The connection descriptor
     * @return a JDBC Connection
     * @throws DbException on error
     */
public static synchronized Connection getConnection(DbConnection dbConnection) throws DbException {
    // create the connection identifier
    String connectionDescription = dbConnection.getConnHash();
    DataSource dataSource;
    if (dataSourceMap.containsKey(connectionDescription)) {
        // use the cached connection
        dataSource = dataSourceMap.get(connectionDescription);
    } else {
        dataSource = dbConnection.getDataSource();
        dataSourceMap.put(connectionDescription, dataSource);
    }
    try {
        Connection newConnection;
        if (dataSource instanceof BasicDataSource) {
            // DBCP BasicDataSource does not support getConnection(user,pass) method
            newConnection = dataSource.getConnection();
        } else {
            newConnection = dataSource.getConnection(dbConnection.getUser(), dbConnection.getPassword());
        }
        return newConnection;
    } catch (SQLException sqle) {
        throw new DbException("Unable to connect to database using location '" + dbConnection.getURL() + "' and user '" + dbConnection.getUser() + "'", sqle);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) DataSource(javax.sql.DataSource) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 3 with DbException

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

the class AbstractDbProvider method getTableDescriptions.

/**
     * @return description about all tables present in the database
     */
@Override
public List<TableDescription> getTableDescriptions() {
    ResultSet tablesResultSet = null;
    List<TableDescription> tables = new ArrayList<TableDescription>();
    try (Connection connection = ConnectionPool.getConnection(dbConnection)) {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        tablesResultSet = databaseMetaData.getTables(null, null, null, new String[] { "TABLE" });
        while (tablesResultSet.next()) {
            if (this instanceof OracleDbProvider && !tablesResultSet.getString(2).equalsIgnoreCase(dbConnection.user)) {
                // Oracle gives us all tables from all databases, we filter here only ours
                continue;
            }
            String tableName = tablesResultSet.getString("TABLE_NAME");
            if (!isTableAccepted(tablesResultSet, dbConnection.db, tableName)) {
                // Table is skipped
                continue;
            }
            log.debug("Extracting description about '" + tableName + "' table");
            TableDescription table = new TableDescription();
            table.setName(tableName);
            table.setSchema(tablesResultSet.getString("TABLE_SCHEM"));
            table.setPrimaryKeyColumn(exctractPrimaryKeyColumn(tableName, databaseMetaData));
            table.setIndexes(extractTableIndexes(tableName, databaseMetaData, connection.getCatalog()));
            List<String> columnDescriptions = new ArrayList<>();
            extractTableColumns(tableName, databaseMetaData, columnDescriptions);
            table.setColumnDescriptions(columnDescriptions);
            tables.add(table);
        }
    } catch (SQLException sqle) {
        throw new DbException("Error extracting DB schema information", sqle);
    } finally {
        if (tablesResultSet != null) {
            try {
                tablesResultSet.close();
            } catch (SQLException e) {
                log.warn("Result set resouce could not be closed!", e);
            }
        }
    }
    return tables;
}
Also used : OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 4 with DbException

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

the class AbstractDbProvider method extractTableIndexes.

protected Map<String, String> extractTableIndexes(String tableName, DatabaseMetaData databaseMetaData, String catalog) throws DbException {
    Map<String, String> indexes = new HashMap<>();
    try {
        ResultSet indexInformation = databaseMetaData.getIndexInfo(catalog, null, tableName, true, true);
        while (indexInformation.next()) {
            StringBuilder sb = new StringBuilder();
            String indexName = indexInformation.getString("INDEX_NAME");
            if (!StringUtils.isNullOrEmpty(indexName)) {
                sb.append(extractResultSetAttribute(indexInformation, "COLUMN_NAME", "column", tableName));
                sb.append(extractResultSetAttribute(indexInformation, "INDEX_QUALIFIER", "index catalog", tableName));
                sb.append(", type=" + SQL_COLUMN_TYPES.get(indexInformation.getInt("TYPE")));
                sb.append(extractResultSetAttribute(indexInformation, "ASC_OR_DESC", "asc/desc", tableName));
                sb.append(extractResultSetAttribute(indexInformation, "NON_UNIQUE", "non-unique", tableName));
                sb.append(extractResultSetAttribute(indexInformation, "FILTER_CONDITION", "filter condition", tableName));
                sb.append(extractResultSetAttribute(indexInformation, "ORDINAL_POSITION", "sequence number", tableName));
                //                sb.append( extractIndexAttribute( indexInformation, "TABLE_NAME" ) );
                //                sb.append( extractResultSetAttribute( indexInformation, "TYPE", "type" ) );
                //                sb.append( extractIndexAttribute( indexInformation, "CARDINALITY" ) );
                //                sb.append( extractIndexAttribute( indexInformation, "PAGES" ) );
                indexes.put(indexName, sb.toString());
            }
        }
    } catch (SQLException e) {
        throw new DbException("Error extracting table indexes info", e);
    }
    return indexes;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 5 with DbException

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

the class DatabaseOperations method delete.

/**
     * Run delete query
     *
     * @param tableName the table we will delete in
     * @param whereClause the where clause, pass 'null' if want to delete all table values.
     * <br/><b>Note: </b>The where clause must content inside any needed parameter escaping.
     * @return the number of deleted rows
     */
@PublicAtsApi
public int delete(String tableName, String whereClause) {
    String sqlQuery = "DELETE FROM " + tableName;
    if (whereClause != null) {
        whereClause = whereClause.trim();
        if (whereClause.length() > 0) {
            sqlQuery = sqlQuery + " WHERE " + whereClause;
        }
    }
    int rowsDeleted;
    try {
        log.debug("Executing query: '" + sqlQuery + "'");
        rowsDeleted = dbProvider.executeUpdate(sqlQuery);
        if (rowsDeleted == 0) {
            log.warn("SQL query '" + sqlQuery + "' affected 0 rows");
        } else {
            log.debug("SQL query '" + sqlQuery + "' affected '" + rowsDeleted + "' rows");
        }
    } catch (DbException e) {
        throw new DatabaseOperationsException("Error executing query '" + sqlQuery + "'", e);
    }
    return rowsDeleted;
}
Also used : DatabaseOperationsException(com.axway.ats.action.exceptions.DatabaseOperationsException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

DbException (com.axway.ats.core.dbaccess.exceptions.DbException)31 SQLException (java.sql.SQLException)11 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)8 DatabaseOperationsException (com.axway.ats.action.exceptions.DatabaseOperationsException)7 PublicAtsApi (com.axway.ats.common.PublicAtsApi)7 Connection (java.sql.Connection)6 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 PreparedStatement (java.sql.PreparedStatement)5 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)4 DatabaseEnvironmentCleanupException (com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException)4 BufferedReader (java.io.BufferedReader)4 File (java.io.File)4 FileReader (java.io.FileReader)4 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)3 CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)3 ColumnHasNoDefaultValueException (com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException)3 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 DatabaseCell (com.axway.ats.action.dbaccess.model.DatabaseCell)2