diff options
Diffstat (limited to 'mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MySQLMixin.java')
-rwxr-xr-x | mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MySQLMixin.java | 80 |
1 files changed, 51 insertions, 29 deletions
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 ec91ceb..8cab635 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 @@ -37,7 +37,6 @@ import java.util.TreeSet; import java.util.UUID; import org.apache.commons.lang3.tuple.Pair; import org.json.JSONObject; -import org.json.JSONTokener; import org.onap.music.exceptions.MDBCServiceException; import org.onap.music.logging.EELFLoggerDelegate; import org.onap.music.mdbc.Configuration; @@ -45,7 +44,6 @@ import org.onap.music.mdbc.MDBCUtils; import org.onap.music.mdbc.Range; import org.onap.music.mdbc.TableInfo; import org.onap.music.mdbc.query.SQLOperation; -import org.onap.music.mdbc.query.SQLOperationType; import org.onap.music.mdbc.tables.Operation; import org.onap.music.mdbc.tables.StagingTable; import net.sf.jsqlparser.JSQLParserException; @@ -814,15 +812,31 @@ public class MySQLMixin implements DBInterface { private ArrayList<String> getMusicKey(String tbl, String cmd, String sql) { ArrayList<String> musicKeys = new ArrayList<String>(); /* - * if (cmd.equalsIgnoreCase("insert")) { //create key, return key musicKeys.add(msm.generatePrimaryKey()); } - * else if (cmd.equalsIgnoreCase("update") || cmd.equalsIgnoreCase("delete")) { try { - * net.sf.jsqlparser.statement.Statement stmt = CCJSqlParserUtil.parse(sql); String where; if (stmt instanceof - * Update) { where = ((Update) stmt).getWhere().toString(); } else if (stmt instanceof Delete) { where = - * ((Delete) stmt).getWhere().toString(); } else { System.err.println("Unknown type: " +stmt.getClass()); where - * = ""; } ResultSet rs = executeSQLRead("SELECT * FROM " + tbl + " WHERE " + where); musicKeys = - * msm.getMusicKeysWhere(tbl, Utils.parseResults(getTableInfo(tbl), rs)); } catch (JSQLParserException e) { - * - * e.printStackTrace(); } catch (SQLException e) { //Not a valid sql query e.printStackTrace(); } } + if (cmd.equalsIgnoreCase("insert")) { + //create key, return key + musicKeys.add(msm.generatePrimaryKey()); + } else if (cmd.equalsIgnoreCase("update") || cmd.equalsIgnoreCase("delete")) { + try { + net.sf.jsqlparser.statement.Statement stmt = CCJSqlParserUtil.parse(sql); + String where; + if (stmt instanceof Update) { + where = ((Update) stmt).getWhere().toString(); + } else if (stmt instanceof Delete) { + where = ((Delete) stmt).getWhere().toString(); + } else { + System.err.println("Unknown type: " +stmt.getClass()); + where = ""; + } + ResultSet rs = executeSQLRead("SELECT * FROM " + tbl + " WHERE " + where); + musicKeys = msm.getMusicKeysWhere(tbl, Utils.parseResults(getTableInfo(tbl), rs)); + } catch (JSQLParserException e) { + + e.printStackTrace(); + } catch (SQLException e) { + //Not a valid sql query + e.printStackTrace(); + } + } */ return musicKeys; } @@ -939,17 +953,7 @@ public class MySQLMixin implements DBInterface { ArrayList<String> cols = new ArrayList<String>(); ArrayList<Object> vals = new ArrayList<Object>(); - Iterator<String> colIterator = jsonOp.keys(); - while (colIterator.hasNext()) { - String col = colIterator.next(); - // FIXME: should not explicitly refer to cassandramixin - if (col.equals(MusicMixin.MDBC_PRIMARYKEY_NAME)) { - // reserved name - continue; - } - cols.add(col); - vals.add(jsonOp.get(col)); - } + constructColValues(jsonOp, cols, vals); // build and replay the queries StringBuilder sql = constructSQL(op, cols, vals); @@ -965,7 +969,11 @@ public class MySQLMixin implements DBInterface { logger.warn("Error Replaying operation: " + sql.toString() + "; Replacing insert/replace/viceversa and replaying "); - buildAndExecuteSQLInverse(jdbcStmt, op, cols, vals); + try { + buildAndExecuteSQLInverse(jdbcStmt, op, cols, vals); + } catch (Exception e) { + logger.warn(" Error replaying inverse operation; " + sql + "Ignore the exception"); + } } } catch (SQLException sqlE) { // This applies for replaying transactions involving Eventually Consistent tables @@ -977,6 +985,20 @@ public class MySQLMixin implements DBInterface { } } + public void constructColValues(JSONObject jsonOp, ArrayList<String> cols, + ArrayList<Object> vals) { + Iterator<String> colIterator = jsonOp.keys(); + while(colIterator.hasNext()) { + String col = colIterator.next(); + //FIXME: should not explicitly refer to cassandramixin + if (col.equals(MusicMixin.MDBC_PRIMARYKEY_NAME)) { + //reserved name + continue; + } + cols.add(col); + vals.add(jsonOp.get(col)); + } + } protected void buildAndExecuteSQLInverse(Statement jdbcStmt, Operation op, ArrayList<String> cols, ArrayList<Object> vals) throws SQLException, MDBCServiceException { @@ -999,7 +1021,7 @@ public class MySQLMixin implements DBInterface { * @throws MDBCServiceException */ - protected StringBuilder constructSQLInverse(Operation op, ArrayList<String> cols, ArrayList<Object> vals) + public StringBuilder constructSQLInverse(Operation op, ArrayList<String> cols, ArrayList<Object> vals) throws MDBCServiceException { StringBuilder sqlInverse = null; switch (op.getOperationType()) { @@ -1015,7 +1037,7 @@ public class MySQLMixin implements DBInterface { return sqlInverse; } - protected StringBuilder constructSQL(Operation op, ArrayList<String> cols, ArrayList<Object> vals) + public StringBuilder constructSQL(Operation op, ArrayList<String> cols, ArrayList<Object> vals) throws MDBCServiceException { StringBuilder sql = null; switch (op.getOperationType()) { @@ -1059,7 +1081,7 @@ public class MySQLMixin implements DBInterface { sql.append(") VALUES ("); sep = ""; for (Object val : vals) { - sql.append(sep + "\"" + val + "\""); + sql.append(sep + (val!=JSONObject.NULL?"\"" + val +"\"":"null")); sep = ", "; } sql.append(");"); @@ -1074,7 +1096,7 @@ public class MySQLMixin implements DBInterface { sql.append(r + " SET "); sep = ""; for (int i = 0; i < cols.size(); i++) { - sql.append(sep + cols.get(i) + "=\"" + vals.get(i) + "\""); + sql.append(sep + cols.get(i) + (vals.get(i)!=JSONObject.NULL?"=\"" + vals.get(i) +"\"":"=null")); sep = ", "; } sql.append(" WHERE "); @@ -1095,7 +1117,7 @@ public class MySQLMixin implements DBInterface { String and = ""; for (String key : primaryKeys.keySet()) { // We cannot use the default primary key for the sql table and operations - if (!key.equals(mi.getMusicDefaultPrimaryKeyName())) { + if(!key.equals(MusicMixin.MDBC_PRIMARYKEY_NAME)) { Object val = primaryKeys.get(key); keyCondStmt.append(and + key + "=\"" + val + "\""); and = " AND "; @@ -1162,4 +1184,4 @@ public class MySQLMixin implements DBInterface { } } -} +}
\ No newline at end of file |