Skip to content

Commit

Permalink
#13309 - MySQL Upsert SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
acurionedotcms committed Jan 5, 2018
1 parent f347926 commit de84e31
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2291,10 +2291,20 @@ private void deleteInsertPermission(Permissionable permissionable, String type,
if((inodeList != null && inodeList.size()>0) || (identifierList!=null && identifierList.size()>0)){
dc1.executeUpdate(DELETE_PERMISSIONABLE_REFERENCE_SQL, permissionId);

String query = SQLUtil.generateUpsertSQL("permission_reference", "asset_id", "?",
new String[]{"id","asset_id","reference_id","permission_type"},
new String[]{"nextval('permission_reference_seq')","?", "?", "?"});
dc1.executeUpdate(query, permissionId, newReference.getPermissionId(), type, permissionId, newReference.getPermissionId(), type);
if (DbConnectionFactory.isPostgres()) {
String query = SQLUtil.generateUpsertSQL("permission_reference", "asset_id", null,
new String[]{"id", "asset_id", "reference_id", "permission_type"},
new String[]{"nextval('permission_reference_seq')", "?", "?", "?"});
dc1.executeUpdate(query, permissionId, newReference.getPermissionId(), type,
permissionId, newReference.getPermissionId(), type);
}
if (DbConnectionFactory.isMySql()) {
String query = SQLUtil.generateUpsertSQL("permission_reference", "asset_id", null,
new String[]{"asset_id", "reference_id", "permission_type"},
new String[]{"?", "?", "?"});
dc1.executeUpdate(query, permissionId, newReference.getPermissionId(), type,
permissionId, newReference.getPermissionId(), type);
}
}

} catch(Exception exception){
Expand Down
30 changes: 22 additions & 8 deletions dotCMS/src/main/java/com/dotmarketing/common/util/SQLUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,35 @@ private static boolean isValidSQLCharacter (final char c) {
+ "VALUES (%s) ON CONFLICT (%s) "
+ "DO UPDATE SET %s";

private final static String MYSQL_UPSERT_QUERY =
"INSERT INTO %s (%s) "
+ "VALUES (%s) ON DUPLICATE KEY "
+ "UPDATE %s";

public static String generateUpsertSQL (String table, String conditionalColumn, String conditionalValue, String[] columns, String[] values) {
String query = null;

if (DbConnectionFactory.isPostgres()) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < columns.length; i++) {
buffer.append(columns[i] + " = " + values[i]);
if (i < (columns.length -1)) {
buffer.append(", ");
}
//Generate column = value pairs, used for the Update part
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < columns.length; i++) {
buffer.append(columns[i] + " = " + values[i]);
if (i < (columns.length -1)) {
buffer.append(", ");
}
}

if (DbConnectionFactory.isPostgres()) {
query = String.format(POSTGRES_UPSERT_QUERY, table,
StringUtil.merge(columns),
StringUtil.merge(values),
conditionalColumn, buffer.toString());
conditionalColumn,
buffer.toString());
}
if (DbConnectionFactory.isMySql()) {
query = String.format(MYSQL_UPSERT_QUERY, table,
StringUtil.merge(columns),
StringUtil.merge(values),
buffer.toString());
}

return query;
Expand Down

0 comments on commit de84e31

Please sign in to comment.