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 void AddAssociationAtRevisionRestriction(QueryBuilder rootQueryBuilder,
														string revisionProperty,
														string revisionEndProperty,
														bool addAlias,
														MiddleIdData referencingIdData,
														string versionsMiddleEntityName,
														string eeOriginalIdPropertyPath,
														string revisionPropertyPath,
														string originalIdPropertyName,
														params MiddleComponentData[] componentDatas)
        {
            var rootParameters = rootQueryBuilder.RootParameters;

            // SELECT max(ee2.revision) FROM middleEntity ee2
            var maxEeRevQb = rootQueryBuilder.NewSubQueryBuilder(versionsMiddleEntityName, "ee2");
            maxEeRevQb.AddProjection("max", revisionPropertyPath, false);
            // WHERE
            var maxEeRevQbParameters = maxEeRevQb.RootParameters;
            // ee2.revision <= :revision
            maxEeRevQbParameters.AddWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
            // ee2.originalId.* = ee.originalId.*
            var ee2OriginalIdPropertyPath = "ee2." + originalIdPropertyName;
            referencingIdData.PrefixedMapper.AddIdsEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);

            foreach (var componentData in componentDatas)
            {
                componentData.ComponentMapper.AddMiddleEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
            }

            // add subquery to rootParameters
            rootParameters.AddWhere(revisionProperty, addAlias, "=", maxEeRevQb);
        }
        public OneAuditEntityQueryGenerator(GlobalConfiguration globalCfg, AuditEntitiesConfiguration verEntCfg,
            MiddleIdData referencingIdData, String referencedEntityName,
            IIdMapper referencedIdMapper)
        {
            this.referencingIdData = referencingIdData;

            /*
             * The query that we need to create:
             *   SELECT new list(e) FROM versionsReferencedEntity e
             *   WHERE
             * (only entities referenced by the association; id_ref_ing = id of the referencing entity)
             *     e.id_ref_ing = :id_ref_ing AND
             * (selecting e entities at revision :revision)
             *     e.revision = (SELECT max(e2.revision) FROM versionsReferencedEntity e2
             *       WHERE e2.revision <= :revision AND e2.id = e.id) AND
             * (only non-deleted entities)
             *     e.revision_type != DEL
             */
            String revisionPropertyPath = verEntCfg.RevisionNumberPath;
            String originalIdPropertyName = verEntCfg.OriginalIdPropName;

            String versionsReferencedEntityName = verEntCfg.GetAuditEntityName(referencedEntityName);

            // SELECT new list(e) FROM versionsEntity e
            QueryBuilder qb = new QueryBuilder(versionsReferencedEntityName, "e");
            qb.AddProjection("new list", "e", false, false);
            // WHERE
            Parameters rootParameters = qb.RootParameters;
            // e.id_ref_ed = :id_ref_ed
            referencingIdData.PrefixedMapper.AddNamedIdEqualsToQuery(rootParameters, null, true);

            // SELECT max(e.revision) FROM versionsReferencedEntity e2
            QueryBuilder maxERevQb = qb.NewSubQueryBuilder(versionsReferencedEntityName, "e2");
            maxERevQb.AddProjection("max", revisionPropertyPath, false);
            // WHERE
            Parameters maxERevQbParameters = maxERevQb.RootParameters;
            // e2.revision <= :revision
            maxERevQbParameters.AddWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
            // e2.id = e.id
            referencedIdMapper.AddIdsEqualToQuery(maxERevQbParameters,
                    "e." + originalIdPropertyName, "e2." + originalIdPropertyName);

            // e.revision = (SELECT max(...) ...)
            rootParameters.AddWhere(revisionPropertyPath, false, globalCfg.getCorrelatedSubqueryOperator(), maxERevQb);

            // e.revision_type != DEL
            rootParameters.AddWhereWithNamedParam(verEntCfg.RevisionTypePropName, false, "!=", "delrevisiontype");

            StringBuilder sb = new StringBuilder();
            qb.Build(sb, EmptyDictionary<String, object>.Instance);
            queryString = sb.ToString();
        }
        public OneEntityQueryGenerator(AuditEntitiesConfiguration verEntCfg,
            String versionsMiddleEntityName,
            MiddleIdData referencingIdData,
            IEnumerable<MiddleComponentData> componentDatas)
        {
            this._referencingIdData = referencingIdData;

            /*
             * The query that we need to create:
             *   SELECT new list(ee) FROM middleEntity ee WHERE
             * (only entities referenced by the association; id_ref_ing = id of the referencing entity)
             *     ee.originalId.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;

            // SELECT new list(ee) FROM middleEntity ee
            QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee");
            qb.AddProjection("new list", "ee", false, false);
            // WHERE
            Parameters rootParameters = qb.RootParameters;
            // ee.originalId.id_ref_ing = :id_ref_ing
            referencingIdData.PrefixedMapper.AddNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true);
            // SELECT max(ee2.revision) FROM middleEntity ee2
            QueryBuilder maxRevQb = qb.NewSubQueryBuilder(versionsMiddleEntityName, "ee2");
            maxRevQb.AddProjection("max", revisionPropertyPath, false);
            // WHERE
            Parameters maxRevQbParameters = maxRevQb.RootParameters;
            // ee2.revision <= :revision
            maxRevQbParameters.AddWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
            // ee2.originalId.* = ee.originalId.*
            String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName;
            String ee2OriginalIdPropertyPath = "ee2." + originalIdPropertyName;
            referencingIdData.PrefixedMapper.AddIdsEqualToQuery(maxRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
            foreach (MiddleComponentData componentData in componentDatas) {
                componentData.ComponentMapper.AddMiddleEqualToQuery(maxRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
            }
            // ee.revision = (SELECT max(...) ...)
            rootParameters.AddWhere(revisionPropertyPath, "=", maxRevQb);
            // ee.revision_type != DEL
            rootParameters.AddWhereWithNamedParam(verEntCfg.RevisionTypePropName, "!=", "delrevisiontype");

            StringBuilder sb = new StringBuilder();
            qb.Build(sb, EmptyDictionary<String, object>.Instance);
            _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();
        }
        public static void AddEntityAtRevision(GlobalConfiguration globalCfg, QueryBuilder qb, Parameters rootParameters,
            MiddleIdData idData, String revisionPropertyPath, String originalIdPropertyName,
            String alias1, String alias2)
        {
            // SELECT max(e.revision) FROM versionsReferencedEntity e2
            QueryBuilder maxERevQb = qb.NewSubQueryBuilder(idData.AuditEntityName, alias2);
            maxERevQb.AddProjection("max", revisionPropertyPath, false);
            // WHERE
            Parameters maxERevQbParameters = maxERevQb.RootParameters;
            // e2.revision <= :revision
            maxERevQbParameters.AddWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
            // e2.id_ref_ed = e.id_ref_ed
            idData.OriginalMapper.AddIdsEqualToQuery(maxERevQbParameters,
                    alias1 + "." + originalIdPropertyName, alias2 +"." + originalIdPropertyName);

            // e.revision = (SELECT max(...) ...)
            rootParameters.AddWhere("e." + revisionPropertyPath, false, globalCfg.getCorrelatedSubqueryOperator(), maxERevQb);
        }
        public void AddEntityAtRevisionRestriction(GlobalConfiguration globalCfg, QueryBuilder rootQueryBuilder, string revisionProperty, string revisionEndProperty, bool addAlias, MiddleIdData idData, string revisionPropertyPath, string originalIdPropertyName, string alias1, string alias2)
        {
            var rootParameters = rootQueryBuilder.RootParameters;

            // create a subquery builder
            // SELECT max(e.revision) FROM versionsReferencedEntity e2
            var maxERevQb = rootQueryBuilder.NewSubQueryBuilder(idData.AuditEntityName, alias2);
            maxERevQb.AddProjection("max", revisionPropertyPath, false);
            // WHERE
            var maxERevQbParameters = maxERevQb.RootParameters;
            // e2.revision <= :revision
            maxERevQbParameters.AddWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
            // e2.id_ref_ed = e.id_ref_ed
            idData.OriginalMapper.AddIdsEqualToQuery(maxERevQbParameters,
                    alias1 + "." + originalIdPropertyName, alias2 + "." + originalIdPropertyName);

            // add subquery to rootParameters
            var subqueryOperator = globalCfg.CorrelatedSubqueryOperator;
            rootParameters.AddWhere(revisionProperty, addAlias, subqueryOperator, maxERevQb);
        }
        public static void AddAssociationAtRevision(QueryBuilder qb, Parameters rootParameters,
            MiddleIdData referencingIdData, String versionsMiddleEntityName,
            String eeOriginalIdPropertyPath, String revisionPropertyPath,
            String originalIdPropertyName, IEnumerable<MiddleComponentData> componentDatas)
        {
            // SELECT max(ee2.revision) FROM middleEntity ee2
            QueryBuilder maxEeRevQb = qb.NewSubQueryBuilder(versionsMiddleEntityName, "ee2");
            maxEeRevQb.AddProjection("max", revisionPropertyPath, false);
            // WHERE
            Parameters maxEeRevQbParameters = maxEeRevQb.RootParameters;
            // ee2.revision <= :revision
            maxEeRevQbParameters.AddWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
            // ee2.originalId.* = ee.originalId.*
            String ee2OriginalIdPropertyPath = "ee2." + originalIdPropertyName;
            referencingIdData.PrefixedMapper.AddIdsEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
            foreach (MiddleComponentData componentData in componentDatas) {
                componentData.ComponentMapper.AddMiddleEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
            }

            // ee.revision = (SELECT max(...) ...)
            rootParameters.AddWhere(revisionPropertyPath, "=", maxEeRevQb);
        }
    public void AddWhere(String left, bool addAlias, String op, QueryBuilder right) {
        StringBuilder expression = new StringBuilder();

        if (addAlias) {
            expression.Append(alias).Append(".");
        }

        expression.Append(left);

        expression.Append(" ").Append(op).Append(" ");

        expression.Append("(");
        right.Build(expression, localQueryParamValues);
        expression.Append(")");        

        expressions.Add(expression.ToString());
    }
 public void AddWhere(String left, String op, QueryBuilder right) {
     AddWhere(left, true, op, right);
 }