Search in sources :

Example 6 with SqlException

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

the class PostgreSqlDatabasePlatform method getCreateSymTriggerPermission.

@Override
public PermissionResult getCreateSymTriggerPermission() {
    String delimiter = getDatabaseInfo().getDelimiterToken();
    delimiter = delimiter != null ? delimiter : "";
    String triggerSql = "CREATE OR REPLACE FUNCTION TEST_TRIGGER() RETURNS trigger AS $$ BEGIN END $$ LANGUAGE plpgsql";
    PermissionResult result = new PermissionResult(PermissionType.CREATE_TRIGGER, Status.FAIL);
    try {
        getSqlTemplate().update(triggerSql);
        result.setStatus(Status.PASS);
    } catch (SqlException e) {
        result.setException(e);
        result.setSolution("Grant CREATE TRIGGER permission and/or DROP TRIGGER permission");
    }
    return result;
}
Also used : PermissionResult(org.jumpmind.db.platform.PermissionResult) SqlException(org.jumpmind.db.sql.SqlException)

Example 7 with SqlException

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

the class DbExportImportTest method importUniqueKeywordTable.

@Test
public void importUniqueKeywordTable() throws Exception {
    ISymmetricEngine engine = getSymmetricEngine();
    DbImport dbImport = new DbImport(engine.getDatabasePlatform());
    dbImport.setFormat(DbImport.Format.XML);
    dbImport.setDropIfExists(true);
    dbImport.setAlterCaseToMatchDatabaseDefaultCase(true);
    dbImport.importTables(getClass().getResourceAsStream("/test-dbimport-unique.xml"));
    IDatabasePlatform platform = engine.getSymmetricDialect().getPlatform();
    Database testTables = platform.readDatabaseFromXml("/test-dbimport-unique.xml", true);
    Table table = testTables.findTable("test_db_import_unique", false);
    Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
    Assert.assertEquals(table.getColumnWithName("string_required_value").isUnique(), true);
    DbImport importCsv = new DbImport(engine.getDatabasePlatform());
    importCsv.setFormat(DbImport.Format.SQL);
    importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-unique-good.sql"));
    Assert.assertEquals(5, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
    dbImport.importTables(getClass().getResourceAsStream("/test-dbimport-unique.xml"));
    try {
        importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-unique-bad-line-2.sql"));
        Assert.fail("Expected a sql exception");
    } catch (SqlException ex) {
    }
    Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
    importCsv.setCommitRate(1);
    importCsv.setForceImport(true);
    importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-unique-bad-line-2.sql"));
    Assert.assertEquals(4, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) Table(org.jumpmind.db.model.Table) Database(org.jumpmind.db.model.Database) SqlException(org.jumpmind.db.sql.SqlException) DbImport(org.jumpmind.symmetric.io.data.DbImport) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 8 with SqlException

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

the class DbFill method insertRandomRecord.

/**
     * Select a random row from the table and update all columns except for primary and foreign keys.
     *
     * @param sqlTemplate
     * @param table
     */
private void insertRandomRecord(ISqlTransaction tran, Table table) {
    DmlStatement insertStatement = createInsertDmlStatement(table);
    Row row = createRandomInsertValues(insertStatement, table);
    try {
        tran.prepareAndExecute(insertStatement.getSql(), insertStatement.getValueArray(row.toArray(table.getColumnNames()), row.toArray(table.getPrimaryKeyColumnNames())));
    } catch (SqlException ex) {
        log.info("Failed to insert into {}: {}", table.getName(), ex.getMessage());
        if (continueOnError) {
            if (debug) {
                logRow(row);
                log.info("", ex);
            }
            selectRandomRecord(tran, table);
        } else {
            throw ex;
        }
    }
}
Also used : SqlException(org.jumpmind.db.sql.SqlException) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 9 with SqlException

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

the class DbFill method deleteRandomRecord.

/**
     * Delete a random row in the given table or delete all rows matching selectColumns
     * in the given table.
     *
     * @param table Table to delete from.
     * @param selectColumns If provided, the rows that match this criteria are deleted.
     */
private void deleteRandomRecord(ISqlTransaction tran, Table table) {
    DmlStatement deleteStatement = createDeleteDmlStatement(table);
    Row row = selectRandomRow(tran, table);
    try {
        tran.prepareAndExecute(deleteStatement.getSql(), row.toArray(table.getPrimaryKeyColumnNames()));
    } catch (SqlException ex) {
        log.info("Failed to delete from {}: {}", table.getName(), ex.getMessage());
        if (continueOnError) {
            if (debug) {
                logRow(row);
                log.info("", ex);
            }
        } else {
            throw ex;
        }
    }
}
Also used : SqlException(org.jumpmind.db.sql.SqlException) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 10 with SqlException

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

the class SqliteDatabasePlatform method getCreateSymTriggerPermission.

@Override
protected PermissionResult getCreateSymTriggerPermission() {
    String delimiter = getDatabaseInfo().getDelimiterToken();
    delimiter = delimiter != null ? delimiter : "";
    String triggerSql = "CREATE TRIGGER TEST_TRIGGER AFTER UPDATE ON " + delimiter + PERMISSION_TEST_TABLE_NAME + delimiter + "FOR EACH ROW BEGIN SELECT 1; END";
    PermissionResult result = new PermissionResult(PermissionType.CREATE_TRIGGER, Status.FAIL);
    try {
        getSqlTemplate().update(triggerSql);
        result.setStatus(Status.PASS);
    } catch (SqlException e) {
        result.setException(e);
        result.setSolution("Grant CREATE TRIGGER permission or TRIGGER permission");
    }
    return result;
}
Also used : PermissionResult(org.jumpmind.db.platform.PermissionResult) SqlException(org.jumpmind.db.sql.SqlException)

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