|
| 1 | +/* |
| 2 | + * Copyright 2025 Flamingock (https://www.flamingock.io) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.flamingock.community.sql.internal; |
| 17 | + |
| 18 | +import io.flamingock.internal.common.core.audit.AuditEntry; |
| 19 | +import io.flamingock.internal.common.core.audit.AuditReader; |
| 20 | +import io.flamingock.internal.common.core.audit.AuditTxType; |
| 21 | +import io.flamingock.internal.core.store.audit.LifecycleAuditWriter; |
| 22 | +import io.flamingock.internal.util.Result; |
| 23 | + |
| 24 | +import javax.sql.DataSource; |
| 25 | +import java.sql.*; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +public class SqlAuditor implements LifecycleAuditWriter, AuditReader { |
| 30 | + |
| 31 | + private final DataSource dataSource; |
| 32 | + private final String auditTableName; |
| 33 | + private final boolean autoCreate; |
| 34 | + private final SqlAuditorDialectHelper dialectHelper; |
| 35 | + |
| 36 | + public SqlAuditor(DataSource dataSource, String auditTableName, boolean autoCreate) { |
| 37 | + this.dataSource = dataSource; |
| 38 | + this.auditTableName = auditTableName; |
| 39 | + this.autoCreate = autoCreate; |
| 40 | + this.dialectHelper = new SqlAuditorDialectHelper(dataSource); |
| 41 | + } |
| 42 | + |
| 43 | + public void initialize() { |
| 44 | + if (autoCreate) { |
| 45 | + try (Connection conn = dataSource.getConnection(); |
| 46 | + Statement stmt = conn.createStatement()) { |
| 47 | + stmt.executeUpdate(dialectHelper.getCreateTableSqlString(auditTableName)); |
| 48 | + } catch (SQLException e) { |
| 49 | + throw new RuntimeException("Failed to initialize audit table", e); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Result writeEntry(AuditEntry auditEntry) { |
| 56 | + try (Connection conn = dataSource.getConnection(); |
| 57 | + PreparedStatement ps = conn.prepareStatement( |
| 58 | + dialectHelper.getInsertSqlString(auditTableName))) { |
| 59 | + ps.setString(1, auditEntry.getExecutionId()); |
| 60 | + ps.setString(2, auditEntry.getStageId()); |
| 61 | + ps.setString(3, auditEntry.getTaskId()); |
| 62 | + ps.setString(4, auditEntry.getAuthor()); |
| 63 | + ps.setTimestamp(5, Timestamp.valueOf(auditEntry.getCreatedAt())); |
| 64 | + ps.setString(6, auditEntry.getState() != null ? auditEntry.getState().name() : null); |
| 65 | + ps.setString(7, auditEntry.getClassName()); |
| 66 | + ps.setString(8, auditEntry.getMethodName()); |
| 67 | + ps.setString(9, auditEntry.getMetadata() != null ? auditEntry.getMetadata().toString() : null); |
| 68 | + ps.setLong(10, auditEntry.getExecutionMillis()); |
| 69 | + ps.setString(11, auditEntry.getExecutionHostname()); |
| 70 | + ps.setString(12, auditEntry.getErrorTrace()); |
| 71 | + ps.setString(13, auditEntry.getType() != null ? auditEntry.getType().name() : null); |
| 72 | + ps.setString(14, auditEntry.getTxType() != null ? auditEntry.getTxType().name() : null); |
| 73 | + ps.setString(15, auditEntry.getTargetSystemId()); |
| 74 | + ps.setString(16, auditEntry.getOrder()); |
| 75 | + ps.setString(17, auditEntry.getRecoveryStrategy() != null ? auditEntry.getRecoveryStrategy().name() : null); |
| 76 | + ps.setObject(18, auditEntry.getTransactionFlag()); |
| 77 | + ps.setObject(19, auditEntry.getSystemChange()); |
| 78 | + ps.executeUpdate(); |
| 79 | + return Result.OK(); |
| 80 | + } catch (SQLException e) { |
| 81 | + return new Result.Error(e); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public List<AuditEntry> getAuditHistory() { |
| 87 | + List<AuditEntry> entries = new ArrayList<>(); |
| 88 | + try (Connection conn = dataSource.getConnection(); |
| 89 | + Statement stmt = conn.createStatement(); |
| 90 | + ResultSet rs = stmt.executeQuery(dialectHelper.getSelectHistorySqlString(auditTableName))) { |
| 91 | + while (rs.next()) { |
| 92 | + AuditEntry entry = new AuditEntry( |
| 93 | + rs.getString("execution_id"), |
| 94 | + rs.getString("stage_id"), |
| 95 | + rs.getString("task_id"), |
| 96 | + rs.getString("author"), |
| 97 | + rs.getTimestamp("created_at").toLocalDateTime(), |
| 98 | + rs.getString("state") != null ? AuditEntry.Status.valueOf(rs.getString("state")) : null, |
| 99 | + rs.getString("type") != null ? AuditEntry.ExecutionType.valueOf(rs.getString("type")) : null, |
| 100 | + rs.getString("class_name"), |
| 101 | + rs.getString("method_name"), |
| 102 | + rs.getLong("execution_millis"), |
| 103 | + rs.getString("execution_hostname"), |
| 104 | + rs.getString("metadata"), |
| 105 | + rs.getBoolean("system_change"), |
| 106 | + rs.getString("error_trace"), |
| 107 | + AuditTxType.fromString(rs.getString("tx_type")), |
| 108 | + rs.getString("target_system_id"), |
| 109 | + rs.getString("order_col"), |
| 110 | + rs.getString("recovery_strategy") != null ? io.flamingock.api.RecoveryStrategy.valueOf(rs.getString("recovery_strategy")) : null, |
| 111 | + rs.getObject("transaction_flag") != null ? rs.getBoolean("transaction_flag") : null |
| 112 | + ); |
| 113 | + entries.add(entry); |
| 114 | + } |
| 115 | + } catch (SQLException e) { |
| 116 | + throw new RuntimeException("Failed to read audit history", e); |
| 117 | + } |
| 118 | + return entries; |
| 119 | + } |
| 120 | +} |
0 commit comments