1717
1818import io .flamingock .internal .common .core .audit .AuditEntry ;
1919import io .flamingock .internal .common .core .audit .AuditReader ;
20+ import io .flamingock .internal .common .core .audit .AuditTxType ;
2021import io .flamingock .internal .core .store .audit .LifecycleAuditWriter ;
2122import io .flamingock .internal .util .Result ;
2223
@@ -30,27 +31,20 @@ public class SqlAuditor implements LifecycleAuditWriter, AuditReader {
3031 private final DataSource dataSource ;
3132 private final String auditTableName ;
3233 private final boolean autoCreate ;
34+ private final SqlAuditorDialectHelper dialectHelper ;
3335
3436 public SqlAuditor (DataSource dataSource , String auditTableName , boolean autoCreate ) {
3537 this .dataSource = dataSource ;
3638 this .auditTableName = auditTableName ;
3739 this .autoCreate = autoCreate ;
40+ this .dialectHelper = new SqlAuditorDialectHelper (dataSource );
3841 }
3942
4043 public void initialize () {
4144 if (autoCreate ) {
4245 try (Connection conn = dataSource .getConnection ();
4346 Statement stmt = conn .createStatement ()) {
44- stmt .executeUpdate (
45- "CREATE TABLE IF NOT EXISTS " + auditTableName + " (" +
46- "id BIGINT AUTO_INCREMENT PRIMARY KEY," +
47- "execution_id VARCHAR(255)," +
48- "author VARCHAR(255)," +
49- "task_id VARCHAR(255)," +
50- "state VARCHAR(255)," +
51- "created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP" +
52- ")"
53- );
47+ stmt .executeUpdate (dialectHelper .getCreateTableSqlString (auditTableName ));
5448 } catch (SQLException e ) {
5549 throw new RuntimeException ("Failed to initialize audit table" , e );
5650 }
@@ -61,12 +55,26 @@ public void initialize() {
6155 public Result writeEntry (AuditEntry auditEntry ) {
6256 try (Connection conn = dataSource .getConnection ();
6357 PreparedStatement ps = conn .prepareStatement (
64- "INSERT INTO " + auditTableName + " (execution_id, author, task_id, state, created_at) VALUES (?, ?, ?, ?, ?)" )) {
58+ dialectHelper . getInsertSqlString ( auditTableName ) )) {
6559 ps .setString (1 , auditEntry .getExecutionId ());
66- ps .setString (2 , auditEntry .getAuthor ());
60+ ps .setString (2 , auditEntry .getStageId ());
6761 ps .setString (3 , auditEntry .getTaskId ());
68- ps .setString (4 , auditEntry .getState (). name ());
62+ ps .setString (4 , auditEntry .getAuthor ());
6963 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 ());
7078 ps .executeUpdate ();
7179 return Result .OK ();
7280 } catch (SQLException e ) {
@@ -79,16 +87,28 @@ public List<AuditEntry> getAuditHistory() {
7987 List <AuditEntry > entries = new ArrayList <>();
8088 try (Connection conn = dataSource .getConnection ();
8189 Statement stmt = conn .createStatement ();
82- ResultSet rs = stmt .executeQuery ("SELECT execution_id, author, task_id, state, created_at FROM " + auditTableName + " ORDER BY created_at DESC" )) {
90+ ResultSet rs = stmt .executeQuery (dialectHelper . getSelectHistorySqlString ( auditTableName ) )) {
8391 while (rs .next ()) {
8492 AuditEntry entry = new AuditEntry (
8593 rs .getString ("execution_id" ),
86- null ,
94+ rs . getString ( "stage_id" ) ,
8795 rs .getString ("task_id" ),
8896 rs .getString ("author" ),
8997 rs .getTimestamp ("created_at" ).toLocalDateTime (),
90- AuditEntry .Status .valueOf (rs .getString ("state" )),
91- null , null , null , 0L , null , null , false , null , null
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
92112 );
93113 entries .add (entry );
94114 }
0 commit comments