Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,33 @@ public class SqlAuditor implements LifecycleAuditWriter, AuditReader {
private final DataSource dataSource;
private final String auditTableName;
private final boolean autoCreate;
private final SqlAuditorDialectHelper dialectHelper;
private SqlAuditorDialectHelper dialectHelper = null;

public SqlAuditor(DataSource dataSource, String auditTableName, boolean autoCreate) {
this.dataSource = dataSource;
this.auditTableName = auditTableName;
this.autoCreate = autoCreate;
this.dialectHelper = new SqlAuditorDialectHelper(dataSource);
}

public void initialize() {
if (autoCreate) {
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement()) {
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement()) {
this.dialectHelper = new SqlAuditorDialectHelper(conn);
if (autoCreate) {
stmt.executeUpdate(dialectHelper.getCreateTableSqlString(auditTableName));
} catch (SQLException e) {
// Firebird throws an error when table already exists; ignore that specific case
if (dialectHelper.getSqlDialect() == io.flamingock.internal.common.sql.SqlDialect.FIREBIRD) {
int errorCode = e.getErrorCode();
String sqlState = e.getSQLState();
String msg = e.getMessage() != null ? e.getMessage().toLowerCase() : "";
}
} catch (SQLException e) {
// Firebird throws an error when table already exists; ignore that specific case
if (dialectHelper != null && dialectHelper.getSqlDialect() == SqlDialect.FIREBIRD) {
int errorCode = e.getErrorCode();
String sqlState = e.getSQLState();
String msg = e.getMessage() != null ? e.getMessage().toLowerCase() : "";

if (errorCode == 335544351 || "42000".equals(sqlState) || msg.contains("already exists")) {
return;
}
if (errorCode == 335544351 || "42000".equals(sqlState) || msg.contains("already exists")) {
return;
}
throw new RuntimeException("Failed to initialize audit table", e);
}
throw new RuntimeException("Failed to initialize audit table", e);
}
}

Expand All @@ -69,7 +69,7 @@ public Result writeEntry(AuditEntry auditEntry) {
conn = dataSource.getConnection();

// For Informix, ensure autoCommit is enabled for audit writes
if (dialectHelper.getSqlDialect() == SqlDialect.INFORMIX) {
if (dialectHelper != null && dialectHelper.getSqlDialect() == SqlDialect.INFORMIX) {
conn.setAutoCommit(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import io.flamingock.internal.common.sql.AbstractSqlDialectHelper;
import io.flamingock.internal.common.sql.SqlDialect;

import javax.sql.DataSource;
import java.sql.Connection;

public final class SqlAuditorDialectHelper extends AbstractSqlDialectHelper {

public SqlAuditorDialectHelper(DataSource dataSource) {
super(dataSource);
public SqlAuditorDialectHelper(Connection connection) {
super(connection);
}

public SqlAuditorDialectHelper(SqlDialect dialect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
import io.flamingock.internal.common.sql.SqlDialect;
import io.flamingock.internal.core.external.store.lock.LockStatus;

import javax.sql.DataSource;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.Objects;

public final class SqlLockDialectHelper extends AbstractSqlDialectHelper {

public SqlLockDialectHelper(DataSource dataSource) {
super(dataSource);
public SqlLockDialectHelper(Connection connection) {
super(connection);
}

public SqlLockDialectHelper(SqlDialect dialect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,37 @@ public class SqlLockService implements CommunityLockService {

private final DataSource dataSource;
private final String lockRepositoryName;
private final SqlLockDialectHelper dialectHelper;
private SqlLockDialectHelper dialectHelper = null;

public SqlLockService(DataSource dataSource, String lockRepositoryName) {
this.dataSource = dataSource;
this.lockRepositoryName = lockRepositoryName;
this.dialectHelper = new SqlLockDialectHelper(dataSource);
}

public void initialize(boolean autoCreate) {
if (autoCreate) {
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement()) {
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement()) {
this.dialectHelper = new SqlLockDialectHelper(conn);
if (autoCreate) {
stmt.executeUpdate(dialectHelper.getCreateTableSqlString(lockRepositoryName));
} catch (SQLException e) {
// For Informix, ignore "Table or view already exists" error (SQLCODE -310)
if (dialectHelper.getSqlDialect() == SqlDialect.INFORMIX &&
(e.getErrorCode() == -310 || e.getSQLState() != null && e.getSQLState().startsWith("42S01"))) {
return;
}
// Firebird throws an error when table already exists; ignore that specific case
if (dialectHelper.getSqlDialect() == io.flamingock.internal.common.sql.SqlDialect.FIREBIRD) {
int errorCode = e.getErrorCode();
String sqlState = e.getSQLState();
String msg = e.getMessage() != null ? e.getMessage().toLowerCase() : "";
}
} catch (SQLException e) {
// For Informix, ignore "Table or view already exists" error (SQLCODE -310)
if (dialectHelper != null && dialectHelper.getSqlDialect() == SqlDialect.INFORMIX &&
(e.getErrorCode() == -310 || e.getSQLState() != null && e.getSQLState().startsWith("42S01"))) {
return;
}
// Firebird throws an error when table already exists; ignore that specific case
if (dialectHelper != null && dialectHelper.getSqlDialect() == SqlDialect.FIREBIRD) {
int errorCode = e.getErrorCode();
String sqlState = e.getSQLState();
String msg = e.getMessage() != null ? e.getMessage().toLowerCase() : "";

if (errorCode == 335544351 || "42000".equals(sqlState) || msg.contains("already exists")) {
return;
}
if (errorCode == 335544351 || "42000".equals(sqlState) || msg.contains("already exists")) {
return;
}
throw new RuntimeException("Failed to initialize lock table", e);
}
throw new RuntimeException("Failed to initialize lock table", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import io.flamingock.internal.common.sql.AbstractSqlDialectHelper;
import io.flamingock.internal.common.sql.SqlDialect;

import javax.sql.DataSource;
import java.sql.Connection;

public final class SqlAuditMarkerDialectHelper extends AbstractSqlDialectHelper {

public SqlAuditMarkerDialectHelper(DataSource dataSource) {
super(dataSource);
public SqlAuditMarkerDialectHelper(Connection connection) {
super(connection);
}

public SqlAuditMarkerDialectHelper(SqlDialect dialect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public static class Builder {
public Builder(DataSource dataSource, TransactionManager<Connection> txManager) {
this.dataSource = dataSource;
this.txManager = txManager;
this.dialectHelper = new SqlAuditMarkerDialectHelper(dataSource);
}

public Builder withTableName(String tableName) {
Expand All @@ -132,6 +131,11 @@ public Builder withSqlDialect(SqlDialect sqlDialect) {
}

public SqlTargetSystemAuditMarker build() {
try (Connection connection = dataSource.getConnection()) {
this.dialectHelper = new SqlAuditMarkerDialectHelper(connection);
} catch (SQLException ex) {
throw new FlamingockException(ex);
}
if (autoCreate) {
createTableIfNotExists();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@
*/
package io.flamingock.internal.common.sql;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class AbstractSqlDialectHelper {

protected static SqlDialect sqlDialect;

public AbstractSqlDialectHelper(DataSource dataSource) {
public AbstractSqlDialectHelper(SqlDialect sqlDialect) {
this.sqlDialect = sqlDialect;
}

public AbstractSqlDialectHelper(Connection connection) {
try {
sqlDialect = fromDatabaseProductName(dataSource.getConnection().getMetaData().getDatabaseProductName());
sqlDialect = fromDatabaseProductName(connection.getMetaData().getDatabaseProductName());
} catch (SQLException e) {
throw new IllegalStateException("Unable to obtain database product name from DataSource", e);
}
}

public AbstractSqlDialectHelper(SqlDialect sqlDialect) {
this.sqlDialect = sqlDialect;
public SqlDialect getSqlDialect() {
return sqlDialect;
}

private SqlDialect fromDatabaseProductName(String productName) {
Expand Down
Loading