public TwoEntityOneAuditedQueryGenerator(AuditEntitiesConfiguration verEntCfg, IAuditStrategy auditStrategy, string versionsMiddleEntityName, MiddleIdData referencingIdData, MiddleIdData referencedIdData, IEnumerable<MiddleComponentData> componentDatas) { this.referencingIdData = referencingIdData; /* * The query that we need to create: * SELECT new list(ee, e) FROM referencedEntity e, middleEntity ee * WHERE * (entities referenced by the middle table; id_ref_ed = id of the referenced entity) * ee.id_ref_ed = e.id_ref_ed AND * (only entities referenced by the association; id_ref_ing = id of the referencing entity) * ee.id_ref_ing = :id_ref_ing AND * * (the association at revision :revision) * --> For DefaultAuditStrategy: * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2 * WHERE ee2.revision <= :revision AND ee2.originalId.* = ee.originalId.*) * --> for ValidityAuditStrategy: * ee.revision <= :revision and (ee.endRevision > :revision or ee.endRevision is null) * * AND * * (only non-deleted entities and associations) * ee.revision_type != DEL */ var revisionPropertyPath = verEntCfg.RevisionNumberPath; var originalIdPropertyName = verEntCfg.OriginalIdPropName; var eeOriginalIdPropertyPath = "ee." + originalIdPropertyName; // SELECT new list(ee) FROM middleEntity ee var qb = new QueryBuilder(versionsMiddleEntityName, "ee"); qb.AddFrom(referencedIdData.EntityName, "e"); qb.AddProjection("new list", "ee, e", false, false); // WHERE var rootParameters = qb.RootParameters; // ee.id_ref_ed = e.id_ref_ed referencedIdData.PrefixedMapper.AddIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath, referencedIdData.OriginalMapper, "e"); // ee.originalId.id_ref_ing = :id_ref_ing referencingIdData.PrefixedMapper.AddNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true); // (with ee association at revision :revision) // --> based on auditStrategy (see above) auditStrategy.AddAssociationAtRevisionRestriction(qb, revisionPropertyPath, verEntCfg.RevisionEndFieldName, true, referencingIdData, versionsMiddleEntityName, eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas.ToArray()); // ee.revision_type != DEL rootParameters.AddWhereWithNamedParam(verEntCfg.RevisionTypePropName, "!=", "delrevisiontype"); var sb = new StringBuilder(); qb.Build(sb, null); queryString = sb.ToString(); }
public TwoEntityOneAuditedQueryGenerator( AuditEntitiesConfiguration verEntCfg, String versionsMiddleEntityName, MiddleIdData referencingIdData, MiddleIdData referencedIdData, IEnumerable<MiddleComponentData> componentDatas) { this.referencingIdData = referencingIdData; /* * The query that we need to create: * SELECT new list(ee, e) FROM referencedEntity e, middleEntity ee * WHERE * (entities referenced by the middle table; id_ref_ed = id of the referenced entity) * ee.id_ref_ed = e.id_ref_ed AND * (only entities referenced by the association; id_ref_ing = id of the referencing entity) * ee.id_ref_ing = :id_ref_ing AND * (the association at revision :revision) * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2 * WHERE ee2.revision <= :revision AND ee2.originalId.* = ee.originalId.*) AND * (only non-deleted entities and associations) * ee.revision_type != DEL */ String revisionPropertyPath = verEntCfg.RevisionNumberPath; String originalIdPropertyName = verEntCfg.OriginalIdPropName; String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName; // SELECT new list(ee) FROM middleEntity ee QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee"); qb.AddFrom(referencedIdData.EntityName, "e"); qb.AddProjection("new list", "ee, e", false, false); // WHERE Parameters rootParameters = qb.RootParameters; // ee.id_ref_ed = e.id_ref_ed referencedIdData.PrefixedMapper.AddIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath, referencedIdData.OriginalMapper, "e"); // ee.originalId.id_ref_ing = :id_ref_ing referencingIdData.PrefixedMapper.AddNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true); // ee.revision = (SELECT max(...) ...) QueryGeneratorTools.AddAssociationAtRevision(qb, rootParameters, referencingIdData, versionsMiddleEntityName, eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas); // ee.revision_type != DEL rootParameters.AddWhereWithNamedParam(verEntCfg.RevisionTypePropName, "!=", "delrevisiontype"); StringBuilder sb = new StringBuilder(); qb.Build(sb, EmptyDictionary<String, Object>.Instance); queryString = sb.ToString(); }