Search in sources :

Example 31 with SqlException

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

the class DbFill method updateRandomRecord.

private void updateRandomRecord(ISqlTransaction tran, Table table) {
    DmlStatement updStatement = createUpdateDmlStatement(table);
    Row row = createRandomUpdateValues(tran, updStatement, table);
    Object[] values = new Object[table.getColumnCount()];
    int i = 0;
    for (Column column : table.getColumns()) {
        if (!column.isPrimaryKey()) {
            values[i++] = row.get(column.getName());
        }
    }
    for (Column column : table.getPrimaryKeyColumns()) {
        values[i++] = row.get(column.getName());
    }
    try {
        tran.prepareAndExecute(updStatement.getSql(), values);
    } catch (SqlException ex) {
        log.info("Failed to update {}: {}", table.getName(), ex.getMessage());
        if (continueOnError) {
            if (debug) {
                logRow(row);
                log.info("", ex);
            }
        } else {
            throw ex;
        }
    }
}
Also used : Column(org.jumpmind.db.model.Column) SqlException(org.jumpmind.db.sql.SqlException) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 32 with SqlException

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

the class DefaultDatabaseWriter method logFailureDetails.

@Override
protected void logFailureDetails(Throwable e, CsvData data, boolean logLastDmlDetails) {
    StringBuilder failureMessage = new StringBuilder();
    failureMessage.append("Failed to process ");
    failureMessage.append(data.getDataEventType().toString().toLowerCase());
    failureMessage.append(" event in batch ");
    failureMessage.append(batch.getBatchId());
    failureMessage.append(".\n");
    if (logLastDmlDetails && this.currentDmlStatement != null) {
        failureMessage.append("Failed sql was: ");
        failureMessage.append(this.currentDmlStatement.getSql());
        failureMessage.append("\n");
    }
    if (logLastDmlDetails && this.currentDmlValues != null) {
        failureMessage.append("Failed sql parameters: ");
        failureMessage.append(StringUtils.abbreviate(Arrays.toString(currentDmlValues), CsvData.MAX_DATA_SIZE_TO_PRINT_TO_LOG));
        failureMessage.append("\n");
        failureMessage.append("Failed sql parameters types: ");
        failureMessage.append(Arrays.toString(this.currentDmlStatement.getTypes()));
        failureMessage.append("\n");
    }
    if (logLastDmlDetails && e instanceof SqlException && e.getCause() instanceof SQLException) {
        SQLException se = (SQLException) e.getCause();
        failureMessage.append("Failed sql state and code: ").append(se.getSQLState());
        failureMessage.append(" (").append(se.getErrorCode()).append(")");
        failureMessage.append("\n");
    }
    data.writeCsvDataDetails(failureMessage);
    log.info(failureMessage.toString(), e);
}
Also used : SQLException(java.sql.SQLException) SqlException(org.jumpmind.db.sql.SqlException)

Example 33 with SqlException

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

the class AbstractDatabaseWriter method write.

public void write(CsvData data) {
    context.remove(AbstractDatabaseWriter.CONFLICT_ERROR);
    /* If the startTable has been called and the targetTable is required then check
         * to see if the writer has been configured to ignore this data event
         */
    if (sourceTable != null && targetTable == null && data.requiresTable() && (writerSettings.isIgnoreMissingTables() || batch.getBatchId() == IoConstants.IGNORE_TABLES_BATCH)) {
        String qualifiedName = sourceTable.getFullyQualifiedTableName();
        if (!missingTables.contains(qualifiedName)) {
            log.warn("Did not find the {} table in the target database", qualifiedName);
            missingTables.add(qualifiedName);
        }
    } else {
        context.put(CONFLICT_ERROR, null);
        if (data.requiresTable() && sourceTable != null && targetTable == null && data.getDataEventType() != DataEventType.SQL) {
            Table lastTable = context.getLastParsedTable();
            if (lastTable != null && lastTable.getFullyQualifiedTableNameLowerCase().equals(sourceTable.getFullyQualifiedTableNameLowerCase())) {
                // if we cross batches and the table isn't specified, then
                // use the last table we used
                start(lastTable);
            }
        }
        if (targetTable != null || !data.requiresTable() || (targetTable == null && data.getDataEventType() == DataEventType.SQL)) {
            try {
                statistics.get(batch).increment(DataWriterStatisticConstants.STATEMENTCOUNT);
                statistics.get(batch).increment(DataWriterStatisticConstants.LINENUMBER);
                if (filterBefore(data)) {
                    LoadStatus loadStatus = LoadStatus.SUCCESS;
                    switch(data.getDataEventType()) {
                        case UPDATE:
                            loadStatus = update(data, writerSettings.isApplyChangesOnly(), true);
                            break;
                        case INSERT:
                            loadStatus = insert(data);
                            break;
                        case DELETE:
                            loadStatus = delete(data, true);
                            break;
                        case BSH:
                            script(data);
                            break;
                        case SQL:
                            sql(data);
                            break;
                        case CREATE:
                            create(data);
                            break;
                        default:
                            break;
                    }
                    if (loadStatus == LoadStatus.CONFLICT) {
                        if (conflictResolver != null) {
                            conflictResolver.needsResolved(this, data, loadStatus);
                        } else {
                            throw new ConflictException(data, targetTable, false, writerSettings.pickConflict(targetTable, batch), (Exception) context.get(AbstractDatabaseWriter.CONFLICT_ERROR));
                        }
                    }
                    uncommittedCount++;
                    lastData = data;
                    filterAfter(data);
                    checkForEarlyCommit();
                }
            } catch (IgnoreBatchException ex) {
                rollback();
                throw ex;
            } catch (RuntimeException ex) {
                if (filterError(data, ex)) {
                    if (!(ex instanceof SqlException)) {
                        /*
                             * SQL exceptions should have already been logged
                             */
                        logFailureDetails(ex, data, false);
                    }
                    throw ex;
                } else {
                    uncommittedCount++;
                    statistics.get(batch).increment(DataWriterStatisticConstants.IGNORECOUNT);
                    checkForEarlyCommit();
                }
            }
        } else {
            if (sourceTable != null) {
                // still unknown throw an exception
                throw new SqlException(String.format("Could not find the target table %s", sourceTable.getFullyQualifiedTableName()));
            } else {
                throw new SqlException("The target table was not specified");
            }
        }
    }
}
Also used : Table(org.jumpmind.db.model.Table) SqlException(org.jumpmind.db.sql.SqlException)

Example 34 with SqlException

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

the class FirebirdDatabasePlatform method getCreateSymTriggerPermission.

@Override
public PermissionResult getCreateSymTriggerPermission() {
    String delimiter = getDatabaseInfo().getDelimiterToken();
    delimiter = delimiter != null ? delimiter : "";
    String triggerSql = "CREATE TRIGGER TEST_TRIGGER FOR " + delimiter + PERMISSION_TEST_TABLE_NAME + delimiter + " AFTER UPDATE AS BEGIN 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)

Example 35 with SqlException

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

the class HsqlDb2DatabasePlatform method getCreateSymTriggerPermission.

@Override
public 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 INSERT INTO " + delimiter + PERMISSION_TEST_TABLE_NAME + delimiter + " VALUES(NULL,NULL)";
    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