Skip to content

Commit 6fb757f

Browse files
authored
refactor: make sql-util independent from flamingock core (#878) (#879)
1 parent e973667 commit 6fb757f

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

community/flamingock-auditstore-sql/src/main/java/io/flamingock/store/sql/internal/SqlLockService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,6 @@ private CommunityLockEntry getLockEntry(Connection conn, String key) throws SQLE
286286

287287

288288
private void upsertLockEntry(Connection conn, String key, String owner, LocalDateTime expiresAt) throws SQLException {
289-
dialectHelper.upsertLockEntry(conn, lockRepositoryName, key, owner, expiresAt);
289+
dialectHelper.upsertLockEntry(conn, lockRepositoryName, key, owner, LockStatus.LOCK_HELD.name(), expiresAt);
290290
}
291291
}

utils/sql-test-kit/src/main/java/io/flamingock/sql/kit/SqlLockStorage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void storeLock(LockKey key, LockAcquisition acquisition) {
105105
delete.setString(1, keyStr);
106106
delete.executeUpdate();
107107
}
108-
dialectHelper.upsertLockEntry(connection, lockTableName, keyStr, owner.toString(), expiresAt);
108+
dialectHelper.upsertLockEntry(connection, lockTableName, keyStr, owner.toString(), LockStatus.LOCK_HELD.name(), expiresAt);
109109
connection.commit();
110110
return;
111111
} else {
@@ -119,7 +119,7 @@ public void storeLock(LockKey key, LockAcquisition acquisition) {
119119
if (existing == null ||
120120
owner.toString().equals(existing.getOwner()) ||
121121
LocalDateTime.now().isAfter(existing.getExpiresAt())) {
122-
dialectHelper.upsertLockEntry(connection, lockTableName, keyStr, owner.toString(), expiresAt);
122+
dialectHelper.upsertLockEntry(connection, lockTableName, keyStr, owner.toString(), LockStatus.LOCK_HELD.name(), expiresAt);
123123
// Commit for all dialects except Informix (which uses auto-commit above)
124124
if (dialectHelper.getSqlDialect() != SqlDialect.INFORMIX) {
125125
connection.commit();

utils/sql-util/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ java {
77
}
88

99
dependencies {
10-
implementation(project(":core:flamingock-core"))
1110
implementation("com.zaxxer:HikariCP:3.4.5")
1211
implementation("org.testcontainers:testcontainers-junit-jupiter:2.0.2")
1312
// SQL Testcontainers

utils/sql-util/src/main/java/io/flamingock/internal/common/sql/dialectHelpers/SqlLockDialectHelper.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import io.flamingock.internal.common.sql.SqlDialectFactory;
1919
import io.flamingock.internal.common.sql.SqlDialect;
20-
import io.flamingock.internal.core.external.store.lock.LockStatus;
2120

2221
import java.sql.*;
2322
import java.time.LocalDateTime;
@@ -254,14 +253,14 @@ public String getDeleteLockSqlString(String tableName) {
254253
}
255254
}
256255

257-
public void upsertLockEntry(Connection conn, String tableName, String key, String owner, LocalDateTime expiresAt) throws SQLException {
256+
public void upsertLockEntry(Connection conn, String tableName, String key, String owner, String lockStatus, LocalDateTime expiresAt) throws SQLException {
258257
String sql = getInsertOrUpdateLockSqlString(tableName);
259258

260259
if (sqlDialect == SqlDialect.DB2) {
261260
// UPDATE first
262261
try (PreparedStatement update = conn.prepareStatement(
263262
"UPDATE " + tableName + " SET status = ?, owner = ?, expires_at = ? WHERE lock_key = ?")) {
264-
update.setString(1, LockStatus.LOCK_HELD.name());
263+
update.setString(1, lockStatus);
265264
update.setString(2, owner);
266265
update.setTimestamp(3, Timestamp.valueOf(expiresAt));
267266
update.setString(4, key);
@@ -275,7 +274,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
275274
try (PreparedStatement insert = conn.prepareStatement(
276275
"INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)")) {
277276
insert.setString(1, key);
278-
insert.setString(2, LockStatus.LOCK_HELD.name());
277+
insert.setString(2, lockStatus);
279278
insert.setString(3, owner);
280279
insert.setTimestamp(4, Timestamp.valueOf(expiresAt));
281280
insert.executeUpdate();
@@ -287,7 +286,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
287286
// Try UPDATE first
288287
try (PreparedStatement update = conn.prepareStatement(
289288
"UPDATE " + tableName + " SET status = ?, owner = ?, expires_at = ? WHERE lock_key = ?")) {
290-
update.setString(1, LockStatus.LOCK_HELD.name());
289+
update.setString(1, lockStatus);
291290
update.setString(2, owner);
292291
update.setTimestamp(3, Timestamp.valueOf(expiresAt));
293292
update.setString(4, key);
@@ -301,7 +300,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
301300
try (PreparedStatement insert = conn.prepareStatement(
302301
"INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)")) {
303302
insert.setString(1, key);
304-
insert.setString(2, LockStatus.LOCK_HELD.name());
303+
insert.setString(2, lockStatus);
305304
insert.setString(3, owner);
306305
insert.setTimestamp(4, Timestamp.valueOf(expiresAt));
307306
insert.executeUpdate();
@@ -313,12 +312,12 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
313312
// For SQL Server/Sybase, use Statement and format SQL
314313
try (Statement stmt = conn.createStatement()) {
315314
String formattedSql = sql
316-
.replaceFirst("\\?", "'" + LockStatus.LOCK_HELD.name() + "'")
315+
.replaceFirst("\\?", "'" + lockStatus + "'")
317316
.replaceFirst("\\?", "'" + owner + "'")
318317
.replaceFirst("\\?", "'" + Timestamp.valueOf(expiresAt) + "'")
319318
.replaceFirst("\\?", "'" + key + "'")
320319
.replaceFirst("\\?", "'" + key + "'")
321-
.replaceFirst("\\?", "'" + LockStatus.LOCK_HELD.name() + "'")
320+
.replaceFirst("\\?", "'" + lockStatus + "'")
322321
.replaceFirst("\\?", "'" + owner + "'")
323322
.replaceFirst("\\?", "'" + Timestamp.valueOf(expiresAt) + "'");
324323
stmt.execute(formattedSql);
@@ -328,7 +327,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
328327

329328
if (sqlDialect == SqlDialect.FIREBIRD) {
330329
try (PreparedStatement ps = conn.prepareStatement(sql)) {
331-
ps.setString(1, LockStatus.LOCK_HELD.name());
330+
ps.setString(1, lockStatus);
332331
ps.setString(2, owner);
333332
ps.setTimestamp(3, Timestamp.valueOf(expiresAt));
334333
ps.setString(4, key);
@@ -337,7 +336,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
337336
String insertSql = "INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)";
338337
try (PreparedStatement ins = conn.prepareStatement(insertSql)) {
339338
ins.setString(1, key);
340-
ins.setString(2, LockStatus.LOCK_HELD.name());
339+
ins.setString(2, lockStatus);
341340
ins.setString(3, owner);
342341
ins.setTimestamp(4, Timestamp.valueOf(expiresAt));
343342
ins.executeUpdate();
@@ -352,7 +351,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
352351
try (PreparedStatement insert = conn.prepareStatement(
353352
"INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)")) {
354353
insert.setString(1, key);
355-
insert.setString(2, LockStatus.LOCK_HELD.name());
354+
insert.setString(2, lockStatus);
356355
insert.setString(3, owner);
357356
insert.setTimestamp(4, Timestamp.valueOf(expiresAt));
358357
insert.executeUpdate();
@@ -364,7 +363,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
364363
// Default case for other dialects
365364
try (PreparedStatement ps = conn.prepareStatement(sql)) {
366365
ps.setString(1, key);
367-
ps.setString(2, LockStatus.LOCK_HELD.name());
366+
ps.setString(2, lockStatus);
368367
ps.setString(3, owner);
369368
ps.setTimestamp(4, Timestamp.valueOf(expiresAt));
370369
ps.executeUpdate();

0 commit comments

Comments
 (0)