|
8 | 8 | import java.sql.SQLException;
|
9 | 9 | import java.sql.Timestamp;
|
10 | 10 | import java.util.ArrayList;
|
| 11 | +import java.util.Map; |
11 | 12 | import java.util.Map.Entry;
|
12 | 13 | import javax.json.Json;
|
13 | 14 | import javax.json.JsonObject;
|
@@ -95,9 +96,6 @@ abstract public void executeInsert(Connection connection, String tableName, Json
|
95 | 96 | abstract public void executeUpdate(Connection connection, String tableName, String idColumn,
|
96 | 97 | String idValue, JsonObject body) throws SQLException;
|
97 | 98 |
|
98 |
| - abstract public void executeUpsert(Connection connection, String idColumn, |
99 |
| - JsonObject body) throws SQLException; |
100 |
| - |
101 | 99 | public ArrayList executeSelectTrigger(Connection connection, String sqlQuery)
|
102 | 100 | throws SQLException {
|
103 | 101 | try (PreparedStatement stmt = connection.prepareStatement(sqlQuery)) {
|
@@ -142,6 +140,119 @@ public ArrayList executeSelectQuery(Connection connection, String sqlQuery, Json
|
142 | 140 | }
|
143 | 141 | }
|
144 | 142 |
|
| 143 | + public JsonObject executeUpsert(Connection connection, String idColumn, |
| 144 | + JsonObject body) throws SQLException { |
| 145 | + validateQuery(); |
| 146 | + JsonObject foundRow; |
| 147 | + JsonObjectBuilder row = Json.createObjectBuilder(); |
| 148 | + int rowsCount = 0; |
| 149 | + int i; |
| 150 | + ResultSet rs; |
| 151 | + ResultSetMetaData metaData; |
| 152 | + |
| 153 | + StringBuilder keys = new StringBuilder(); |
| 154 | + StringBuilder values = new StringBuilder(); |
| 155 | + StringBuilder setString = new StringBuilder(); |
| 156 | + for (Map.Entry<String, JsonValue> entry : body.entrySet()) { |
| 157 | + if (keys.length() > 0) { |
| 158 | + keys.append(","); |
| 159 | + } |
| 160 | + keys.append(entry.getKey()); |
| 161 | + if (values.length() > 0) { |
| 162 | + values.append(","); |
| 163 | + } |
| 164 | + values.append("?"); |
| 165 | + if (!entry.getKey().equals(idColumn)) { |
| 166 | + if (setString.length() > 0) { |
| 167 | + setString.append(","); |
| 168 | + } |
| 169 | + setString.append(entry.getKey()).append(" = ?"); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + String sqlSELECT = |
| 174 | + " SELECT" + |
| 175 | + " *" + |
| 176 | + " FROM " + tableName + |
| 177 | + " WHERE " + idColumn + " = ?"; |
| 178 | + String sqlInsert = "INSERT INTO " + tableName + |
| 179 | + " (" + keys.toString() + ")" + |
| 180 | + " VALUES (" + values.toString() + ")"; |
| 181 | + String sqlUpdate = "UPDATE " + tableName + |
| 182 | + " SET " + setString.toString() + |
| 183 | + " WHERE " + idColumn + " = ?"; |
| 184 | + |
| 185 | + PreparedStatement stmtSelect = null; |
| 186 | + PreparedStatement stmtInsert = null; |
| 187 | + PreparedStatement stmtUpdate = null; |
| 188 | + |
| 189 | + try { |
| 190 | + connection.setAutoCommit(false); |
| 191 | + |
| 192 | + stmtSelect = connection.prepareStatement(sqlSELECT); |
| 193 | + Utils.setStatementParam(stmtSelect, 1, idColumn, body); |
| 194 | + rs = stmtSelect.executeQuery(); |
| 195 | + metaData = rs.getMetaData(); |
| 196 | + while (rs.next()) { |
| 197 | + for (i = 1; i <= metaData.getColumnCount(); i++) { |
| 198 | + row = Utils.getColumnDataByType(rs, metaData, i, row); |
| 199 | + } |
| 200 | + rowsCount++; |
| 201 | + if (rowsCount > 1) { |
| 202 | + throw new RuntimeException("Error: the number of matching rows is not exactly one"); |
| 203 | + } |
| 204 | + } |
| 205 | + foundRow = row.build(); |
| 206 | + |
| 207 | + i = 1; |
| 208 | + if (foundRow.size() == 0) { |
| 209 | + stmtInsert = connection.prepareStatement(sqlInsert); |
| 210 | + for (Map.Entry<String, JsonValue> entry : body.entrySet()) { |
| 211 | + Utils.setStatementParam(stmtInsert, i, entry.getKey(), body); |
| 212 | + i++; |
| 213 | + } |
| 214 | + stmtInsert.execute(); |
| 215 | + } else { |
| 216 | + stmtUpdate = connection.prepareStatement(sqlUpdate); |
| 217 | + for (Map.Entry<String, JsonValue> entry : body.entrySet()) { |
| 218 | + if (!entry.getKey().equals(idColumn)) { |
| 219 | + Utils.setStatementParam(stmtUpdate, i, entry.getKey(), body); |
| 220 | + i++; |
| 221 | + } |
| 222 | + } |
| 223 | + Utils.setStatementParam(stmtUpdate, i, idColumn, body); |
| 224 | + stmtUpdate.execute(); |
| 225 | + } |
| 226 | + |
| 227 | + rs = stmtSelect.executeQuery(); |
| 228 | + metaData = rs.getMetaData(); |
| 229 | + rowsCount = 0; |
| 230 | + while (rs.next()) { |
| 231 | + for (i = 1; i <= metaData.getColumnCount(); i++) { |
| 232 | + row = Utils.getColumnDataByType(rs, metaData, i, row); |
| 233 | + } |
| 234 | + rowsCount++; |
| 235 | + if (rowsCount > 1) { |
| 236 | + throw new RuntimeException("Error: the number of matching rows is not exactly one"); |
| 237 | + } |
| 238 | + } |
| 239 | + connection.commit(); |
| 240 | + |
| 241 | + } finally { |
| 242 | + if (stmtSelect != null) { |
| 243 | + stmtSelect.close(); |
| 244 | + } |
| 245 | + if (stmtInsert != null) { |
| 246 | + stmtInsert.close(); |
| 247 | + } |
| 248 | + if (stmtUpdate != null) { |
| 249 | + stmtUpdate.close(); |
| 250 | + } |
| 251 | + connection.setAutoCommit(true); |
| 252 | + } |
| 253 | + return row.build(); |
| 254 | + } |
| 255 | + |
145 | 256 | public void validateQuery() {
|
146 | 257 | if (tableName == null) {
|
147 | 258 | throw new RuntimeException("Table name is required field");
|
|
0 commit comments