Search in sources :

Example 81 with DatabaseException

use of liquibase.exception.DatabaseException in project openmrs-core by openmrs.

the class ConceptValidatorChangeSet method getInt.

/**
 * returns an integer resulting from the execution of an sql statement
 *
 * @param connection a DatabaseConnection
 * @param sql the sql statement to execute
 * @return integer resulting from the execution of the sql statement
 */
private int getInt(JdbcConnection connection, String sql) {
    Statement stmt = null;
    int result = 0;
    try {
        stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        if (rs.next()) {
            result = rs.getInt(1);
        } else {
            log.warn("No row returned by getInt() method");
        }
        if (rs.next()) {
            log.warn("Multiple rows returned by getInt() method");
        }
        return result;
    } catch (DatabaseException | SQLException e) {
        log.warn("Error generated", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                log.warn("Failed to close the statement object");
            }
        }
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) DatabaseException(liquibase.exception.DatabaseException)

Example 82 with DatabaseException

use of liquibase.exception.DatabaseException in project openmrs-core by openmrs.

the class ConceptValidatorChangeSet method getAllowedLocalesList.

/**
 * Retrieves the list of allowed locales from the database, sets the default locale, english and
 * the default locale will be added to the list allowed locales if not yet included
 *
 * @param connection The database connection
 * @return A list of allowed locales
 */
@SuppressWarnings("unchecked")
private List<Locale> getAllowedLocalesList(JdbcConnection connection) {
    Statement stmt = null;
    ListOrderedSet allowedLocales = new ListOrderedSet();
    try {
        // get the default locale
        stmt = connection.createStatement();
        ResultSet rsDefaultLocale = stmt.executeQuery("SELECT property_value FROM global_property WHERE property = '" + OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE + "'");
        if (rsDefaultLocale.next()) {
            String defaultLocaleStr = rsDefaultLocale.getString("property_value");
            if (!StringUtils.isBlank(defaultLocaleStr) && defaultLocaleStr.length() > 1) {
                Locale defaultLocaleGP = LocaleUtility.fromSpecification(defaultLocaleStr);
                if (defaultLocaleGP != null) {
                    defaultLocale = defaultLocaleGP;
                }
            } else {
                updateWarnings.add("'" + defaultLocaleStr + "' is an invalid value for the global property default locale");
            }
        }
        allowedLocales.add(defaultLocale);
        // get the locale.allowed.list
        ResultSet rsAllowedLocales = stmt.executeQuery("SELECT property_value FROM global_property WHERE property = '" + OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST + "'");
        if (rsAllowedLocales.next()) {
            String allowedLocaleStr = rsAllowedLocales.getString("property_value");
            if (!StringUtils.isBlank(allowedLocaleStr)) {
                String[] localesArray = allowedLocaleStr.split(",");
                for (String localeStr : localesArray) {
                    if (localeStr.trim().length() > 1) {
                        allowedLocales.add(LocaleUtility.fromSpecification(localeStr.trim()));
                    } else {
                        updateWarnings.add("'" + localeStr + "' is an invalid value for the global property locale.allowed.list");
                    }
                }
            }
        } else {
            log.warn("The global property '" + OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST + "' isn't set");
        }
    } catch (DatabaseException | SQLException e) {
        log.warn("Error generated", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                log.warn("Failed to close the statement object");
            }
        }
    }
    // if it isn't among
    allowedLocales.add(new Locale("en"));
    return allowedLocales.asList();
}
Also used : Locale(java.util.Locale) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ListOrderedSet(org.apache.commons.collections.set.ListOrderedSet) DatabaseException(liquibase.exception.DatabaseException)

Example 83 with DatabaseException

use of liquibase.exception.DatabaseException in project openmrs-core by openmrs.

the class CreateCodedOrderFrequencyForDrugOrderFrequencyChangeset method insertUniqueFrequencies.

private void insertUniqueFrequencies(JdbcConnection connection, Set<String> uniqueFrequencies) throws CustomChangeException, SQLException, DatabaseException {
    PreparedStatement insertOrderFrequencyStatement = null;
    Boolean autoCommit = null;
    try {
        autoCommit = connection.getAutoCommit();
        connection.setAutoCommit(false);
        insertOrderFrequencyStatement = connection.prepareStatement("insert into order_frequency " + "(concept_id, creator, date_created, retired, uuid) values (?, ?, ?, ?, ?)");
        Date date = new Date(new java.util.Date().getTime());
        for (String frequency : uniqueFrequencies) {
            if (StringUtils.isBlank(frequency)) {
                continue;
            }
            Integer conceptIdForFrequency = UpgradeUtil.getConceptIdForUnits(frequency);
            if (conceptIdForFrequency == null) {
                throw new CustomChangeException("No concept mapping found for frequency: " + frequency);
            }
            Integer orderFrequencyId = UpgradeUtil.getOrderFrequencyIdForConceptId(connection.getUnderlyingConnection(), conceptIdForFrequency);
            if (orderFrequencyId != null) {
                // a single concept is mapped to more than one text or there is an order frequency already
                continue;
            }
            // Generating UUID for order frequency. Generated UUIDs will be the same if concepts UUIDs are the same.
            String uuid = UpgradeUtil.getConceptUuid(connection.getUnderlyingConnection(), conceptIdForFrequency);
            // Adding random value for order frequency
            uuid += "-6925ebb0-7c69-11e3-baa7-0800200c9a66";
            uuid = UUID.nameUUIDFromBytes(uuid.getBytes(StandardCharsets.UTF_8)).toString();
            insertOrderFrequencyStatement.setInt(1, conceptIdForFrequency);
            insertOrderFrequencyStatement.setInt(2, 1);
            insertOrderFrequencyStatement.setDate(3, date);
            insertOrderFrequencyStatement.setBoolean(4, false);
            insertOrderFrequencyStatement.setString(5, uuid);
            insertOrderFrequencyStatement.executeUpdate();
            insertOrderFrequencyStatement.clearParameters();
        }
        connection.commit();
    } catch (DatabaseException | SQLException e) {
        handleError(connection, e);
    } finally {
        if (autoCommit != null) {
            connection.setAutoCommit(autoCommit);
        }
        if (insertOrderFrequencyStatement != null) {
            insertOrderFrequencyStatement.close();
        }
    }
}
Also used : SQLException(java.sql.SQLException) CustomChangeException(liquibase.exception.CustomChangeException) PreparedStatement(java.sql.PreparedStatement) DatabaseException(liquibase.exception.DatabaseException) Date(java.sql.Date)

Example 84 with DatabaseException

use of liquibase.exception.DatabaseException in project openmrs-core by openmrs.

the class DisableTriggersChangeSet method execute.

@Override
public void execute(Database database) throws CustomChangeException {
    JdbcConnection connection = (JdbcConnection) database.getConnection();
    DatabaseMetaData metadata;
    try {
        metadata = connection.getMetaData();
        String[] types = { "TABLE" };
        ResultSet rs = metadata.getTables(null, null, "%", types);
        while (rs.next()) {
            String tableName = rs.getString(3);
            connection.prepareStatement("ALTER TABLE " + tableName + " DISABLE TRIGGER ALL").execute();
        }
    } catch (DatabaseException | SQLException ex) {
        throw new CustomChangeException("Error disabling trigger: " + ex);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseMetaData(java.sql.DatabaseMetaData) DatabaseException(liquibase.exception.DatabaseException)

Example 85 with DatabaseException

use of liquibase.exception.DatabaseException in project openmrs-core by openmrs.

the class EnableTriggersChangeSet method execute.

@Override
public void execute(Database database) throws CustomChangeException {
    JdbcConnection connection = (JdbcConnection) database.getConnection();
    DatabaseMetaData metadata;
    try {
        metadata = connection.getMetaData();
        String[] types = { "TABLE" };
        ResultSet rs = metadata.getTables(null, null, "%", types);
        while (rs.next()) {
            String tableName = rs.getString(3);
            connection.prepareStatement("ALTER TABLE " + tableName + " ENABLE TRIGGER ALL").execute();
        }
    } catch (DatabaseException | SQLException ex) {
        throw new CustomChangeException("Error enabling trigger: " + ex);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseMetaData(java.sql.DatabaseMetaData) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

DatabaseException (liquibase.exception.DatabaseException)139 SQLException (java.sql.SQLException)65 JdbcConnection (liquibase.database.jvm.JdbcConnection)34 PreparedStatement (java.sql.PreparedStatement)33 ResultSet (java.sql.ResultSet)28 Statement (java.sql.Statement)24 Database (liquibase.database.Database)24 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)24 CustomChangeException (liquibase.exception.CustomChangeException)22 CatalogAndSchema (liquibase.CatalogAndSchema)17 LiquibaseException (liquibase.exception.LiquibaseException)16 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)14 InvalidExampleException (liquibase.snapshot.InvalidExampleException)14 RawSqlStatement (liquibase.statement.core.RawSqlStatement)14 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)13 CachedRow (liquibase.snapshot.CachedRow)13 SqlStatement (liquibase.statement.SqlStatement)13 DatabaseConnection (liquibase.database.DatabaseConnection)12 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)12 ArrayList (java.util.ArrayList)11