Skip to content

Commit

Permalink
#13309 - Use constants and add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
acurionedotcms committed Jan 8, 2018
1 parent 1cc3b0c commit afc3d8a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@

import com.liferay.portal.model.User;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -82,6 +81,12 @@ public class PermissionBitFactoryImpl extends PermissionFactory {
: null
;

private static final String PERMISSION_REFERENCE = "permission_reference";
private static final String ASSET_ID = "asset_id";
private static final String REFERENCE_ID = "reference_id";
private static final String PERMISSION_TYPE = "permission_type";
private static final String ID = "id";

//SQL Queries used to maintain permissions

/*
Expand Down Expand Up @@ -2270,6 +2275,7 @@ private List<Permission> loadPermissions(Permissionable permissionable) throws D

}


@WrapInTransaction
private void deleteInsertPermission(Permissionable permissionable, String type,
Permissionable newReference) throws DotDataException {
Expand All @@ -2293,30 +2299,30 @@ private void deleteInsertPermission(Permissionable permissionable, String type,
dc1.executeUpdate(DELETE_PERMISSIONABLE_REFERENCE_SQL, permissionId);

if (DbConnectionFactory.isPostgres()) {
String query = SQLUtil.generateUpsertSQL("permission_reference", "asset_id",
new String[]{"id", "asset_id", "reference_id", "permission_type"},
String query = SQLUtil.generateUpsertSQL(PERMISSION_REFERENCE, ASSET_ID,
new String[]{ID, ASSET_ID, REFERENCE_ID, PERMISSION_TYPE},
new String[]{"nextval('permission_reference_seq')", SQLUtil.PARAMETER, SQLUtil.PARAMETER, SQLUtil.PARAMETER});
dc1.executeUpdate(query, permissionId, newReference.getPermissionId(), type,
permissionId, newReference.getPermissionId(), type);
}
if (DbConnectionFactory.isMySql()) {
String query = SQLUtil.generateUpsertSQL("permission_reference", "asset_id",
new String[]{"asset_id", "reference_id", "permission_type"},
String query = SQLUtil.generateUpsertSQL(PERMISSION_REFERENCE, ASSET_ID,
new String[]{ASSET_ID, REFERENCE_ID, PERMISSION_TYPE},
new String[]{SQLUtil.PARAMETER, SQLUtil.PARAMETER, SQLUtil.PARAMETER});
dc1.executeUpdate(query, permissionId, newReference.getPermissionId(), type,
permissionId, newReference.getPermissionId(), type);
}
if (DbConnectionFactory.isMsSql()) {
String query = SQLUtil.generateUpsertSQL("permission_reference", "asset_id",
new String[]{"asset_id", "reference_id", "permission_type"},
String query = SQLUtil.generateUpsertSQL(PERMISSION_REFERENCE, ASSET_ID,
new String[]{ASSET_ID, REFERENCE_ID, PERMISSION_TYPE},
new String[]{SQLUtil.PARAMETER, SQLUtil.PARAMETER, SQLUtil.PARAMETER});
dc1.executeUpdate(query, permissionId,
permissionId, newReference.getPermissionId(), type,
permissionId, newReference.getPermissionId(), type);
}
if (DbConnectionFactory.isOracle()) {
String query = SQLUtil.generateUpsertSQL("permission_reference", "asset_id",
new String[]{"id", "asset_id", "reference_id", "permission_type"},
String query = SQLUtil.generateUpsertSQL(PERMISSION_REFERENCE, ASSET_ID,
new String[]{ID, ASSET_ID, REFERENCE_ID, PERMISSION_TYPE},
new String[]{"permission_reference_seq.NEXTVAL", SQLUtil.PARAMETER, SQLUtil.PARAMETER, SQLUtil.PARAMETER});
try {
//In Oracle the Upsert (Merge) is not thread safe. Attempt to insert first:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,15 @@ private static boolean isValidSQLCharacter (final char c) {
+ " INSERT (%s) "
+ " VALUES (%s)";

/**
* This method generates an Upsert SQL query (Insert/Update)
* Note: Oracle Upsert is not thread safe and when executed can throw SQLException: Unique constraint violation
* @param table name
* @param conditionalColumn The unique column to verify if the record exists or not. A parameter (?) will be added to the SQL Query for the conditionalValue, required in MSSQL and Oracle only.
* @param columns to be inserted/updated
* @param values to be inserted/updated... You can use SQLUtil.Parameter to specify a ? for a value
* @return String with the SQL query
*/
public static String generateUpsertSQL (String table, String conditionalColumn, String[] columns, String[] values) {
String query = null;

Expand Down

0 comments on commit afc3d8a

Please sign in to comment.