Include primary key in insert query 16/83416/1
authorTschaen, Brendan <ctschaen@att.com>
Tue, 26 Mar 2019 21:28:39 +0000 (17:28 -0400)
committerTschaen, Brendan <ctschaen@att.com>
Tue, 26 Mar 2019 21:28:39 +0000 (17:28 -0400)
Allows replays to easily modify an insert into an update if needed

Change-Id: Ibd0c077240a23aeff3d82ec171c1cecb3274e211
Issue-ID: MUSIC-371
Signed-off-by: Tschaen, Brendan <ctschaen@att.com>
mdbc-server/src/main/java/org/onap/music/mdbc/examples/MdbcTestClient.java
mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MySQLMixin.java

index fd8651a..8ca8517 100644 (file)
@@ -123,9 +123,9 @@ public class MdbcTestClient {
         }\r
 \r
         try {\r
-            //execute = insertStmt.execute(insertSQL);\r
+            execute = insertStmt.execute(insertSQL);\r
             execute = insertStmt.execute(insertSQL1);\r
-            //execute = insertStmt.execute(insertSQL2);\r
+            execute = insertStmt.execute(insertSQL2);\r
             //execute = insertStmt.execute(insertSQL3);\r
             //execute = insertStmt.execute(insertSQL4);\r
 \r
index 0afacea..aecee24 100755 (executable)
@@ -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<String> cols, ArrayList<Object> 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<String> cols,
             ArrayList<Object> vals) throws MDBCServiceException {
         StringBuilder sqlInverse = null;