Skip to content

Commit ac2d27c

Browse files
committed
refactor: make sql-util independent from flamingock core
1 parent 1016cce commit ac2d27c

4 files changed

Lines changed: 14 additions & 15 deletions

File tree

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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,14 @@ public String getDeleteLockSqlString(String tableName) {
254254
}
255255
}
256256

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

260260
if (sqlDialect == SqlDialect.DB2) {
261261
// UPDATE first
262262
try (PreparedStatement update = conn.prepareStatement(
263263
"UPDATE " + tableName + " SET status = ?, owner = ?, expires_at = ? WHERE lock_key = ?")) {
264-
update.setString(1, LockStatus.LOCK_HELD.name());
264+
update.setString(1, lockStatus);
265265
update.setString(2, owner);
266266
update.setTimestamp(3, Timestamp.valueOf(expiresAt));
267267
update.setString(4, key);
@@ -275,7 +275,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
275275
try (PreparedStatement insert = conn.prepareStatement(
276276
"INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)")) {
277277
insert.setString(1, key);
278-
insert.setString(2, LockStatus.LOCK_HELD.name());
278+
insert.setString(2, lockStatus);
279279
insert.setString(3, owner);
280280
insert.setTimestamp(4, Timestamp.valueOf(expiresAt));
281281
insert.executeUpdate();
@@ -287,7 +287,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
287287
// Try UPDATE first
288288
try (PreparedStatement update = conn.prepareStatement(
289289
"UPDATE " + tableName + " SET status = ?, owner = ?, expires_at = ? WHERE lock_key = ?")) {
290-
update.setString(1, LockStatus.LOCK_HELD.name());
290+
update.setString(1, lockStatus);
291291
update.setString(2, owner);
292292
update.setTimestamp(3, Timestamp.valueOf(expiresAt));
293293
update.setString(4, key);
@@ -301,7 +301,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
301301
try (PreparedStatement insert = conn.prepareStatement(
302302
"INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)")) {
303303
insert.setString(1, key);
304-
insert.setString(2, LockStatus.LOCK_HELD.name());
304+
insert.setString(2, lockStatus);
305305
insert.setString(3, owner);
306306
insert.setTimestamp(4, Timestamp.valueOf(expiresAt));
307307
insert.executeUpdate();
@@ -313,12 +313,12 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
313313
// For SQL Server/Sybase, use Statement and format SQL
314314
try (Statement stmt = conn.createStatement()) {
315315
String formattedSql = sql
316-
.replaceFirst("\\?", "'" + LockStatus.LOCK_HELD.name() + "'")
316+
.replaceFirst("\\?", "'" + lockStatus + "'")
317317
.replaceFirst("\\?", "'" + owner + "'")
318318
.replaceFirst("\\?", "'" + Timestamp.valueOf(expiresAt) + "'")
319319
.replaceFirst("\\?", "'" + key + "'")
320320
.replaceFirst("\\?", "'" + key + "'")
321-
.replaceFirst("\\?", "'" + LockStatus.LOCK_HELD.name() + "'")
321+
.replaceFirst("\\?", "'" + lockStatus + "'")
322322
.replaceFirst("\\?", "'" + owner + "'")
323323
.replaceFirst("\\?", "'" + Timestamp.valueOf(expiresAt) + "'");
324324
stmt.execute(formattedSql);
@@ -328,7 +328,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
328328

329329
if (sqlDialect == SqlDialect.FIREBIRD) {
330330
try (PreparedStatement ps = conn.prepareStatement(sql)) {
331-
ps.setString(1, LockStatus.LOCK_HELD.name());
331+
ps.setString(1, lockStatus);
332332
ps.setString(2, owner);
333333
ps.setTimestamp(3, Timestamp.valueOf(expiresAt));
334334
ps.setString(4, key);
@@ -337,7 +337,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
337337
String insertSql = "INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)";
338338
try (PreparedStatement ins = conn.prepareStatement(insertSql)) {
339339
ins.setString(1, key);
340-
ins.setString(2, LockStatus.LOCK_HELD.name());
340+
ins.setString(2, lockStatus);
341341
ins.setString(3, owner);
342342
ins.setTimestamp(4, Timestamp.valueOf(expiresAt));
343343
ins.executeUpdate();
@@ -352,7 +352,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
352352
try (PreparedStatement insert = conn.prepareStatement(
353353
"INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)")) {
354354
insert.setString(1, key);
355-
insert.setString(2, LockStatus.LOCK_HELD.name());
355+
insert.setString(2, lockStatus);
356356
insert.setString(3, owner);
357357
insert.setTimestamp(4, Timestamp.valueOf(expiresAt));
358358
insert.executeUpdate();
@@ -364,7 +364,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
364364
// Default case for other dialects
365365
try (PreparedStatement ps = conn.prepareStatement(sql)) {
366366
ps.setString(1, key);
367-
ps.setString(2, LockStatus.LOCK_HELD.name());
367+
ps.setString(2, lockStatus);
368368
ps.setString(3, owner);
369369
ps.setTimestamp(4, Timestamp.valueOf(expiresAt));
370370
ps.executeUpdate();

0 commit comments

Comments
 (0)