Skip to content

Commit

Permalink
testing for all supported databases
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Jul 8, 2019
1 parent 1f37705 commit e1b57ce
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 118 deletions.
Expand Up @@ -245,7 +245,7 @@ private String generateFullQuery(Map<String, Object> parameters, boolean ordered
parameters.remove(PARAMETER_CHANGED_ITEM);
}
if (filteredOnValueRefTargetNames) {
conditions.add("rv.targetName.orig in ( :valueRefTargetNames )");
conditions.add("rv.targetName_orig in ( :valueRefTargetNames )");
} else {
parameters.remove(PARAMETER_VALUE_REF_TARGET_NAMES);
}
Expand Down
Expand Up @@ -1306,9 +1306,9 @@ private <F extends ObjectType> void auditEvent(LensContext<F> context, AuditEven
PrismObject<SystemConfigurationType> systemConfiguration = systemObjectCache.getSystemConfiguration(result);

SystemConfigurationAuditEventRecordingType auditEventRecordingType;
if(systemConfiguration != null) {
if (systemConfiguration != null) {
PrismContainer<SystemConfigurationAuditEventRecordingType> auditEventRecording = (PrismContainer) systemConfiguration.getValue().findItem(ItemPath.create("audit", "eventRecording"));
if(auditEventRecording != null && auditEventRecording.getRealValue() != null) {
if (auditEventRecording != null && auditEventRecording.getRealValue() != null) {
auditEventRecordingType = auditEventRecording.getRealValue();
} else {
auditEventRecordingType = new SystemConfigurationAuditEventRecordingType();
Expand All @@ -1321,25 +1321,30 @@ private <F extends ObjectType> void auditEvent(LensContext<F> context, AuditEven
if (primaryObject != null) {
auditRecord.setTarget(primaryObject.clone(), prismContext);

if(Boolean.TRUE.equals(auditEventRecordingType.isRecordResourceOids())) {
if(primaryObject.getRealValue() instanceof FocusType) {
if (Boolean.TRUE.equals(auditEventRecordingType.isRecordResourceOids())) {
if (primaryObject.getRealValue() instanceof FocusType) {
FocusType focus = (FocusType) primaryObject.getRealValue();
for(ObjectReferenceType shadowRef : focus.getLinkRef()) {
PrismObject<ShadowType> shadow;
try {
shadow = repositoryService.getObject(ShadowType.class, shadowRef.getOid(),
null, task.getResult());
ObjectReferenceType resource = shadow.getRealValue().getResourceRef();
if(resource != null && resource.getOid() != null) {
auditRecord.addResourceOid(resource.getOid());
}
} catch (ObjectNotFoundException e) {
LOGGER.error("Couldn't load shadow from reference " + shadowRef, e);
for (ObjectReferenceType shadowRef : focus.getLinkRef()) {
LensProjectionContext projectionContext = context.findProjectionContextByOid(shadowRef.getOid());
if (projectionContext != null && StringUtils.isNotBlank(projectionContext.getResourceOid())) {
auditRecord.addResourceOid(projectionContext.getResourceOid());
}
// PrismObject<ShadowType> shadow;
// try {
//
// shadow = repositoryService.getObject(ShadowType.class, shadowRef.getOid(),
// null, task.getResult());
// ObjectReferenceType resource = shadow.getRealValue().getResourceRef();
// if (resource != null && resource.getOid() != null) {
// auditRecord.addResourceOid(resource.getOid());
// }
// } catch (ObjectNotFoundException e) {
// LOGGER.error("Couldn't load shadow from reference " + shadowRef, e);
// }
}
} else if(primaryObject.getRealValue() instanceof ShadowType) {
} else if (primaryObject.getRealValue() instanceof ShadowType) {
ObjectReferenceType resource = ((ShadowType)primaryObject.getRealValue()).getResourceRef();
if(resource != null && resource.getOid() != null) {
if (resource != null && resource.getOid() != null) {
auditRecord.addResourceOid(resource.getOid());
}
}
Expand Down
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2010-2013 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.repo.sql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
/**
* @author skublik
*/
public abstract class ConverterSqlAndJavaObject {

public abstract <T> T convertToValue(ResultSet rs, String nameOfColumn, Class<T> javaClazz) throws SQLException;

public abstract <T> T convertToValue(ResultSet rs, int index, Class<T> javaClazz) throws SQLException;

public abstract Types getSqlType(Object value);

}
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2010-2013 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.repo.sql;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
/**
* @author skublik
*/
public class OracleConverter {//extends ConverterSqlAndJavaObject {

// public <T> T convertToValue(ResultSet rs, String nameOfColumn, Class<T> javaClazz) throws SQLException {
// int index = rs.findColumn(nameOfColumn);
// return convertToValue(rs, index, javaClazz);
// }
//
// public <T> T convertToValue(ResultSet rs, int index, Class<T> javaClazz) throws SQLException {
// ResultSetMetaData metadata = rs.getMetaData();
// int type = metadata.getColumnType(index);
//// if(javaClazz.equals(Long.class)) {
////
//// }
// if(Types.)
// if(javaClazz.equals(String.class)) {
// return (T) rs.getString(nameOfColumn);
// }
// }

// public Types getSqlType(Object value) {
//
// }
}
Expand Up @@ -69,6 +69,9 @@
import javax.persistence.criteria.CriteriaQuery;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.*;
import java.util.*;
import java.util.Date;
Expand Down Expand Up @@ -289,13 +292,23 @@ public void execute(Connection con) throws SQLException {

int count = 0;
String basicQuery = query;
if (StringUtils.isBlank(basicQuery)) {
basicQuery = "select * from m_audit_event as aer where 1=1 order by aer.timestampValue desc";
if (StringUtils.isBlank(query)) {
basicQuery = "select * from m_audit_event "
+ (database.equals(Database.ORACLE) ? "" : "as ")
+ "aer where 1=1 order by aer.timestampValue desc";
}
String deltaQuery = "select * from m_audit_delta as delta where delta.record_id=?";
String propertyQuery = "select * from m_audit_prop_value as prop where prop.record_id=?";
String refQuery = "select * from m_audit_ref_value as ref where ref.record_id=?";
String resourceQuery = "select * from m_audit_resource as res where res.record_id=?";
String deltaQuery = "select * from m_audit_delta "
+ (database.equals(Database.ORACLE) ? "" : "as ")
+ "delta where delta.record_id=?";
String propertyQuery = "select * from m_audit_prop_value "
+ (database.equals(Database.ORACLE) ? "" : "as ")
+ "prop where prop.record_id=?";
String refQuery = "select * from m_audit_ref_value "
+ (database.equals(Database.ORACLE) ? "" : "as ")
+ "ref where ref.record_id=?";
String resourceQuery = "select * from m_audit_resource "
+ (database.equals(Database.ORACLE) ? "" : "as ")
+ "res where res.record_id=?";
SelectQueryBuilder queryBuilder = new SelectQueryBuilder(database, basicQuery);
setParametersToQuery(queryBuilder, params);

Expand All @@ -318,7 +331,7 @@ public void execute(Connection con) throws SQLException {

//query for deltas
PreparedStatement subStmt = con.prepareStatement(deltaQuery);
subStmt.setString(1, resultList.getString(RAuditEventRecord.ID_COLUMN_NAME));
subStmt.setLong(1, resultList.getLong(RAuditEventRecord.ID_COLUMN_NAME));
ResultSet subResultList = subStmt.executeQuery();

try {
Expand All @@ -339,7 +352,7 @@ public void execute(Connection con) throws SQLException {

//query for properties
subStmt = con.prepareStatement(propertyQuery);
subStmt.setString(1, resultList.getString(RAuditEventRecord.ID_COLUMN_NAME));
subStmt.setLong(1, resultList.getLong(RAuditEventRecord.ID_COLUMN_NAME));
subResultList = subStmt.executeQuery();

try {
Expand All @@ -354,7 +367,7 @@ public void execute(Connection con) throws SQLException {

//query for references
subStmt = con.prepareStatement(refQuery);
subStmt.setString(1, resultList.getString(RAuditEventRecord.ID_COLUMN_NAME));
subStmt.setLong(1, resultList.getLong(RAuditEventRecord.ID_COLUMN_NAME));
subResultList = subStmt.executeQuery();

try {
Expand All @@ -369,7 +382,7 @@ public void execute(Connection con) throws SQLException {

//query for target resource oids
subStmt = con.prepareStatement(resourceQuery);
subStmt.setString(1, resultList.getString(RAuditEventRecord.ID_COLUMN_NAME));
subStmt.setLong(1, resultList.getLong(RAuditEventRecord.ID_COLUMN_NAME));
subResultList = subStmt.executeQuery();

try {
Expand Down Expand Up @@ -463,11 +476,10 @@ private void setParametersToQuery(SelectQueryBuilder queryBuilder, Map<String, O
return;
}

if (params.containsKey(QUERY_FIRST_RESULT)) {
queryBuilder.addFirstResult(QUERY_FIRST_RESULT);
}
if (params.containsKey(QUERY_MAX_RESULT)) {
queryBuilder.addMaxResult(QUERY_MAX_RESULT);
if (params.containsKey(QUERY_FIRST_RESULT) && params.containsKey(QUERY_MAX_RESULT)) {
queryBuilder.addPaging((int)params.get(QUERY_FIRST_RESULT), (int)params.get(QUERY_MAX_RESULT));
params.remove(QUERY_FIRST_RESULT);
params.remove(QUERY_MAX_RESULT);
}
queryBuilder.addParameters(params);
}
Expand Down Expand Up @@ -523,23 +535,25 @@ private void auditAttempt(AuditEventRecord record) {
Session session = null;
try {
session = baseHelper.beginTransaction();

SingleSqlQuery query = RAuditEventRecord.toRepo(record, customColumn);
// RAuditEventRecord newRecord = RAuditEventRecord.toRepo(record, getPrismContext(), true);
// session.save(newRecord);
SingleSqlQuery query = RAuditEventRecord.toRepo(record, customColumn);
Session localSession = session;
session.doWork(new Work() {

@Override
public void execute(Connection connection) throws SQLException {
Database database = baseHelper.getConfiguration().getDatabase();
PreparedStatement smtp = query.createPreparedStatement(connection, true);
Database database = getConfiguration().getDatabase();
String[] keyColumn = {RAuditEventRecord.ID_COLUMN_NAME};
PreparedStatement smtp = query.createPreparedStatement(connection, keyColumn);
Long id = null;
try {
smtp.execute();
smtp.executeUpdate();
ResultSet resultSet = smtp.getGeneratedKeys();

if (resultSet.next()) {
id = resultSet.getLong(1);
id = resultSet.getLong(1);

}
} finally {
smtp.close();
Expand All @@ -549,8 +563,8 @@ public void execute(Connection connection) throws SQLException {
}


BatchSqlQuery deltaBatchQuery = new BatchSqlQuery();
BatchSqlQuery itemBatchQuery = new BatchSqlQuery();
BatchSqlQuery deltaBatchQuery = new BatchSqlQuery(database);
BatchSqlQuery itemBatchQuery = new BatchSqlQuery(database);

for (ObjectDeltaOperation<?> delta : record.getDeltas()) {
if (delta == null) {
Expand Down Expand Up @@ -587,7 +601,7 @@ public void execute(Connection connection) throws SQLException {
itemBatchQuery.execute(connection);
}

BatchSqlQuery propertyBatchQuery = new BatchSqlQuery();
BatchSqlQuery propertyBatchQuery = new BatchSqlQuery(database);
for (Map.Entry<String, Set<String>> propertyEntry : record.getProperties().entrySet()) {
for (String propertyValue : propertyEntry.getValue()) {
SingleSqlQuery propertyQuery = RAuditPropertyValue.toRepo(
Expand All @@ -600,7 +614,7 @@ public void execute(Connection connection) throws SQLException {
propertyBatchQuery.execute(connection);
}

BatchSqlQuery referenceBatchQuery = new BatchSqlQuery();
BatchSqlQuery referenceBatchQuery = new BatchSqlQuery(database);
for (Map.Entry<String, Set<AuditReferenceValue>> referenceEntry : record.getReferences().entrySet()) {
for (AuditReferenceValue referenceValue : referenceEntry.getValue()) {
SingleSqlQuery referenceQuery = RAuditReferenceValue.toRepo(id, referenceEntry.getKey(), referenceValue);
Expand All @@ -612,7 +626,7 @@ public void execute(Connection connection) throws SQLException {
referenceBatchQuery.execute(connection);
}

BatchSqlQuery resourceOidBatchQuery = new BatchSqlQuery();
BatchSqlQuery resourceOidBatchQuery = new BatchSqlQuery(database);
for (String resourceOid : record.getResourceOids()) {
SingleSqlQuery resourceOidQuery = RTargetResourceOid.toRepo(id, resourceOid);
resourceOidBatchQuery.addQueryForBatch(resourceOidQuery);
Expand Down Expand Up @@ -966,11 +980,13 @@ public long countObjects(String query, Map<String, Object> params) {

@Override
public void execute(Connection connection) throws SQLException {
Database database = baseHelper.getConfiguration().getDatabase();
Database database = getConfiguration().getDatabase();

String basicQuery = query;
if (StringUtils.isBlank(query)) {
basicQuery = "select count (*) from m_audit_event as aer where 1 = 1";
basicQuery = "select count (*) from m_audit_event "
+ (database.equals(Database.ORACLE) ? "" : "as ")
+ "aer where 1 = 1";
}
SelectQueryBuilder queryBuilder = new SelectQueryBuilder(database, basicQuery);
setParametersToQuery(queryBuilder, params);
Expand Down
Expand Up @@ -23,11 +23,20 @@

import org.apache.commons.lang3.StringUtils;

import com.evolveum.midpoint.repo.sql.SqlRepositoryConfiguration.Database;

/**
* @author skublik
*/
public class BatchSqlQuery extends SqlQuery {

public BatchSqlQuery() {
}

public BatchSqlQuery(Database database) {
setDatabase(database);
}

private String query;
Set<SingleSqlQuery> queriesForBatch = new HashSet<SingleSqlQuery>();

Expand Down

0 comments on commit e1b57ce

Please sign in to comment.