From 999f808ea68eeeacf7af36f44a0fea8f9fe6ed88 Mon Sep 17 00:00:00 2001 From: "Tschaen, Brendan" Date: Tue, 26 Mar 2019 17:28:39 -0400 Subject: Include primary key in insert query Allows replays to easily modify an insert into an update if needed Change-Id: Ibd0c077240a23aeff3d82ec171c1cecb3274e211 Issue-ID: MUSIC-371 Signed-off-by: Tschaen, Brendan --- .../onap/music/mdbc/examples/MdbcTestClient.java | 4 +-- .../org/onap/music/mdbc/mixins/MySQLMixin.java | 29 +++++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/mdbc-server/src/main/java/org/onap/music/mdbc/examples/MdbcTestClient.java b/mdbc-server/src/main/java/org/onap/music/mdbc/examples/MdbcTestClient.java index fd8651a..8ca8517 100644 --- a/mdbc-server/src/main/java/org/onap/music/mdbc/examples/MdbcTestClient.java +++ b/mdbc-server/src/main/java/org/onap/music/mdbc/examples/MdbcTestClient.java @@ -123,9 +123,9 @@ public class MdbcTestClient { } try { - //execute = insertStmt.execute(insertSQL); + execute = insertStmt.execute(insertSQL); execute = insertStmt.execute(insertSQL1); - //execute = insertStmt.execute(insertSQL2); + execute = insertStmt.execute(insertSQL2); //execute = insertStmt.execute(insertSQL3); //execute = insertStmt.execute(insertSQL4); diff --git a/mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MySQLMixin.java b/mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MySQLMixin.java index 0afacea..aecee24 100755 --- a/mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MySQLMixin.java +++ b/mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MySQLMixin.java @@ -346,10 +346,10 @@ NEW.field refers to the new value .append("'").append(col).append("', ") .append(isdelete ? "OLD." : "NEW.") .append(col); - if (isupdate && (ti.iskey(col) || !ti.hasKey())) { + if (!isdelete && (ti.iskey(col) || !ti.hasKey())) { keyJson.append(kfx) .append("'").append(col).append("', ") - .append("OLD.") + .append(isupdate ? "OLD." : "NEW.") .append(col); kfx = ", "; } @@ -373,7 +373,7 @@ NEW.field refers to the new value .append("', ") .append(isdelete ? "'D'" : (op.equals("INSERT") ? "'I'" : "'U'")) .append(", ") - .append(isupdate ? keyJson.toString() : "NULL") + .append(!isdelete ? keyJson.toString() : "NULL") .append(", ") .append(newJson.toString()) .append(", ") @@ -904,21 +904,38 @@ NEW.field refers to the new value buildAndExecuteSQLInverse(jdbcStmt, op, cols, vals); } } catch (SQLException sqlE) { - // This applies only for replaying transactions involving Eventually Consistent tables - logger.warn("Error Replaying operation: " + sql.toString() + "; Replacing insert/replace/viceversa and replaying "); + // This applies for replaying transactions involving Eventually Consistent tables + // or transactions that replay on top of existing keys + logger.warn("Error Replaying operation: " + sql.toString() + ";" + + "Replacing insert/replace/viceversa and replaying "); buildAndExecuteSQLInverse(jdbcStmt, op, cols, vals); } } + protected void buildAndExecuteSQLInverse(Statement jdbcStmt, Operation op, ArrayList cols, ArrayList vals) throws SQLException, MDBCServiceException { - StringBuilder sqlInverse = constructSQLInverse( op, cols, vals); + StringBuilder sqlInverse = constructSQLInverse(op, cols, vals); if(sqlInverse == null) return; logger.info("Replaying operation: " + sqlInverse.toString()); jdbcStmt.executeUpdate(sqlInverse.toString()); } + + /** + * Construct an update statement from an insert, or + * construct an insert statement from an update + * + * useful when replaying logic, if the primary key value is already present/not present + * + * @param op + * @param cols + * @param vals + * @return + * @throws MDBCServiceException + */ + protected StringBuilder constructSQLInverse(Operation op, ArrayList cols, ArrayList vals) throws MDBCServiceException { StringBuilder sqlInverse = null; -- cgit 1.2.3-korg