Search in sources :

Example 41 with SqlException

use of org.jumpmind.db.sql.SqlException in project symmetric-ds by JumpMind.

the class JdbcDatabasePlatformFactory method determineDatabaseNameVersionSubprotocol.

protected static String[] determineDatabaseNameVersionSubprotocol(DataSource dataSource) {
    Connection connection = null;
    String[] nameVersion = new String[3];
    try {
        connection = dataSource.getConnection();
        DatabaseMetaData metaData = connection.getMetaData();
        nameVersion[0] = metaData.getDatabaseProductName();
        nameVersion[1] = Integer.toString(metaData.getDatabaseMajorVersion());
        final String PREFIX = "jdbc:";
        String url = metaData.getURL();
        if (StringUtils.isNotBlank(url) && url.length() > PREFIX.length()) {
            url = url.substring(PREFIX.length());
            if (url.indexOf(":") > 0) {
                url = url.substring(0, url.indexOf(":"));
            }
        }
        nameVersion[2] = url;
        /* query the metadata to determine which one it is */
        if (nameVersion[0].equalsIgnoreCase("PostgreSql")) {
            if (isGreenplumDatabase(connection)) {
                nameVersion[0] = DatabaseNamesConstants.GREENPLUM;
                nameVersion[1] = Integer.toString(getGreenplumVersion(connection));
            } else if (isRedshiftDatabase(connection)) {
                nameVersion[0] = DatabaseNamesConstants.REDSHIFT;
            }
        }
        /*
             * if the productName is MySQL, it could be either MysSQL or MariaDB
             * query the metadata to determine which one it is
             */
        if (nameVersion[0].equalsIgnoreCase(DatabaseNamesConstants.MYSQL)) {
            if (isMariaDBDatabase(connection)) {
                nameVersion[0] = DatabaseNamesConstants.MARIADB;
            }
        }
        if (nameVersion[0].toLowerCase().indexOf(DatabaseNamesConstants.DB2) != -1) {
            if (nameVersion[0].toUpperCase().indexOf("Z") != -1) {
                nameVersion[0] = DatabaseNamesConstants.DB2ZOS;
            } else if (nameVersion[0].indexOf("400") != -1) {
                nameVersion[0] = DatabaseNamesConstants.DB2AS400;
            } else {
                nameVersion[0] = DatabaseNamesConstants.DB2;
            }
        }
        if (nameVersion[0].equalsIgnoreCase("AS") && nameVersion[2].equalsIgnoreCase("db2")) {
            nameVersion[0] = DatabaseNamesConstants.DB2AS400;
        }
        if (nameVersion[0].toLowerCase().startsWith(DatabaseNamesConstants.FIREBIRD)) {
            if (isFirebirdDialect1(connection)) {
                nameVersion[0] = DatabaseNamesConstants.FIREBIRD_DIALECT1;
            }
        }
        log.info("Detected database '" + nameVersion[0] + "', version '" + nameVersion[1] + "', protocol '" + nameVersion[2] + "'");
        return nameVersion;
    } catch (SQLException ex) {
        throw new SqlException("Error while reading the database metadata: " + ex.getMessage(), ex);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
            // we ignore this one
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SqlException(org.jumpmind.db.sql.SqlException) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 42 with SqlException

use of org.jumpmind.db.sql.SqlException in project symmetric-ds by JumpMind.

the class JdbcDatabasePlatformFactory method getDatabaseMinorVersion.

public static int getDatabaseMinorVersion(DataSource dataSource) {
    Connection connection = null;
    try {
        connection = dataSource.getConnection();
        DatabaseMetaData metaData = connection.getMetaData();
        return metaData.getDatabaseMinorVersion();
    } catch (SQLException ex) {
        throw new SqlException("Error while reading the database metadata: " + ex.getMessage(), ex);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
            // we ignore this one
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SqlException(org.jumpmind.db.sql.SqlException) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 43 with SqlException

use of org.jumpmind.db.sql.SqlException in project symmetric-ds by JumpMind.

the class JdbcDatabasePlatformFactory method getDatabaseMajorVersion.

public static int getDatabaseMajorVersion(DataSource dataSource) {
    Connection connection = null;
    try {
        connection = dataSource.getConnection();
        DatabaseMetaData metaData = connection.getMetaData();
        return metaData.getDatabaseMajorVersion();
    } catch (SQLException ex) {
        throw new SqlException("Error while reading the database metadata: " + ex.getMessage(), ex);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
            // we ignore this one
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SqlException(org.jumpmind.db.sql.SqlException) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 44 with SqlException

use of org.jumpmind.db.sql.SqlException in project symmetric-ds by JumpMind.

the class SqlAnywhereSymmetricDialect method switchCatalogForTriggerInstall.

@Override
protected String switchCatalogForTriggerInstall(String catalog, ISqlTransaction transaction) {
    if (catalog != null) {
        Connection c = ((JdbcSqlTransaction) transaction).getConnection();
        String previousCatalog;
        try {
            previousCatalog = c.getCatalog();
            c.setCatalog(catalog);
            return previousCatalog;
        } catch (SQLException e) {
            throw new SqlException(e);
        }
    } else {
        return null;
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SqlException(org.jumpmind.db.sql.SqlException) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction)

Example 45 with SqlException

use of org.jumpmind.db.sql.SqlException in project symmetric-ds by JumpMind.

the class AseSymmetricDialect method switchCatalogForTriggerInstall.

@Override
protected String switchCatalogForTriggerInstall(String catalog, ISqlTransaction transaction) {
    if (catalog != null) {
        Connection c = ((JdbcSqlTransaction) transaction).getConnection();
        String previousCatalog;
        try {
            previousCatalog = c.getCatalog();
            c.setCatalog(catalog);
            return previousCatalog;
        } catch (SQLException e) {
            throw new SqlException(e);
        }
    } else {
        return null;
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SqlException(org.jumpmind.db.sql.SqlException) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction)

Aggregations

SqlException (org.jumpmind.db.sql.SqlException)50 PermissionResult (org.jumpmind.db.platform.PermissionResult)18 SQLException (java.sql.SQLException)11 Connection (java.sql.Connection)10 Table (org.jumpmind.db.model.Table)8 DmlStatement (org.jumpmind.db.sql.DmlStatement)6 Row (org.jumpmind.db.sql.Row)6 Column (org.jumpmind.db.model.Column)5 DatabaseMetaData (java.sql.DatabaseMetaData)4 JdbcSqlTransaction (org.jumpmind.db.sql.JdbcSqlTransaction)4 DetectConflict (org.jumpmind.symmetric.io.data.writer.Conflict.DetectConflict)3 ArrayList (java.util.ArrayList)2 IndexColumn (org.jumpmind.db.model.IndexColumn)2 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)2 DbImport (org.jumpmind.symmetric.io.data.DbImport)2 AbstractServiceTest (org.jumpmind.symmetric.service.impl.AbstractServiceTest)2 Test (org.junit.Test)2 ResultSet (java.sql.ResultSet)1 DataSource (javax.sql.DataSource)1 Database (org.jumpmind.db.model.Database)1