Exemplo n.º 1
0
        public override string Translate(SqlCompilerContext context, SqlJoinExpression node, JoinSection section)
        {
            switch (section)
            {
            case JoinSection.Specification:
                if (node.Expression == null)
                {
                    switch (node.JoinType)
                    {
                    case SqlJoinType.InnerJoin:
                    case SqlJoinType.LeftOuterJoin:
                    case SqlJoinType.RightOuterJoin:
                    case SqlJoinType.FullOuterJoin:
                        throw new NotSupportedException();

                    case SqlJoinType.CrossApply:
                        return("CROSS APPLY");

                    case SqlJoinType.LeftOuterApply:
                        return("OUTER APPLY");
                    }
                }
                var joinHint = TryFindJoinHint(context, node);
                return(Translate(node.JoinType)
                       + (joinHint != null ? " " + Translate(joinHint.Method) : string.Empty) + " JOIN");
            }
            return(base.Translate(context, node, section));
        }
Exemplo n.º 2
0
        protected override Expression VisitJoin(SqlJoinExpression join)
        {
            join = (SqlJoinExpression)base.VisitJoin(join);

            if (join.JoinType == SqlJoinType.Cross && this.currentWhere != null)
            {
                var declaredLeft  = SqlDeclaredAliasGatherer.Gather(join.Left);
                var declaredRight = SqlDeclaredAliasGatherer.Gather(join.Right);
                var declared      = new HashSet <string>(declaredLeft.Union(declaredRight));
                var exprs         = this.currentWhere.Split(ExpressionType.And, ExpressionType.AndAlso);
                var good          = exprs.Where(e => CanBeJoinCondition(e, declaredLeft, declaredRight, declared)).ToList();

                if (good.Count > 0)
                {
                    var condition = good.Join(ExpressionType.And);

                    join = this.UpdateJoin(join, SqlJoinType.Inner, join.Left, join.Right, condition);

                    var newWhere = exprs.Where(e => !good.Contains(e)).Join(ExpressionType.And);
                    this.currentWhere = newWhere;
                }
            }

            return(join);
        }
Exemplo n.º 3
0
        public override string Translate(SqlCompilerContext context, SqlJoinExpression node, JoinSection section)
        {
            switch (section)
            {
            case JoinSection.Specification: {
                if (node.Expression == null)
                {
                    switch (node.JoinType)
                    {
                    case SqlJoinType.InnerJoin:
                    case SqlJoinType.LeftOuterJoin:
                    case SqlJoinType.RightOuterJoin:
                    case SqlJoinType.FullOuterJoin:
                        throw new NotSupportedException();

                    case SqlJoinType.CrossApply:
                        return("CROSS JOIN LATERAL");

                    case SqlJoinType.LeftOuterApply:
                        return("LEFT JOIN LATERAL");
                    }
                }
                return(Translate(node.JoinType) + " JOIN");
            }

            case JoinSection.Exit: {
                if (node.JoinType == SqlJoinType.LeftOuterApply)
                {
                    return("ON TRUE");
                }
                return(string.Empty);
            }
            }
            return(base.Translate(context, node, section));
        }
Exemplo n.º 4
0
        protected SqlJoinExpression UpdateJoin(SqlJoinExpression join, SqlJoinType joinType, Expression left, Expression right, Expression condition)
        {
            if (joinType != join.JoinType || left != join.Left || right != join.Right || condition != join.JoinCondition)
            {
                return(new SqlJoinExpression(join.Type, joinType, left, right, condition));
            }

            return(join);
        }
Exemplo n.º 5
0
        protected override Expression VisitJoin(SqlJoinExpression join)
        {
            if (this.hasAggregate)
            {
                return(join);
            }

            return(base.VisitJoin(join));
        }
Exemplo n.º 6
0
        public void JoinTest()
        {
            var left      = new SqlTableExpression("Tab1", new SqlColumnExpression[] {});
            var right     = new SqlTableExpression("Tab2", new SqlColumnExpression[] { });
            var condition = new SqlBinaryExpression(SqlExpressionType.Equal, constants[0], constants[0]);
            var join      = new SqlJoinExpression(left, right, condition, SqlJoinType.Inner);
            var value     = _generator.GenerateSql(join);

            Assert.Equal("[Tab1] INNER JOIN [Tab2] ON (2 = 2)", value);
        }
Exemplo n.º 7
0
        protected override Expression VisitJoin(SqlJoinExpression join)
        {
            this.VisitSource(join.Left);

            this.WriteLine();

            switch (join.JoinType)
            {
            case SqlJoinType.Cross:
                this.Write(" CROSS JOIN ");
                break;

            case SqlJoinType.Inner:
                this.Write(" INNER JOIN ");
                break;

            case SqlJoinType.Left:
                this.Write(" LEFT JOIN ");
                break;

            case SqlJoinType.Right:
                this.Write(" RIGHT JOIN ");
                break;

            case SqlJoinType.Outer:
                this.Write(" FULL OUTER JOIN ");
                break;

            case SqlJoinType.CrossApply:
                this.Write(" CROSS APPLY ");
                break;

            case SqlJoinType.OuterApply:
                this.Write(" OUTER APPLY ");
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(join), join.JoinType, "Join type incorrect");
            }

            this.VisitSource(join.Right);

            if (join.JoinCondition != null)
            {
                using (this.AcquireIndentationContext())
                {
                    this.Write("ON ");

                    this.Visit(join.JoinCondition);
                }
            }

            return(join);
        }
Exemplo n.º 8
0
        protected virtual Expression VisitJoin(SqlJoinExpression join)
        {
            var left      = this.Visit(join.Left);
            var right     = this.Visit(join.Right);
            var condition = this.Visit(join.JoinCondition);

            if (left != join.Left || right != join.Right || condition != join.JoinCondition)
            {
                return(new SqlJoinExpression(join.Type, join.JoinType, left, right, condition));
            }

            return(join);
        }
Exemplo n.º 9
0
 public void Visit(SqlJoinExpression node)
 {
     if (!node.Expression.IsNullReference())
     {
         Visit(node.Expression);
     }
     if (node.Left != null)
     {
         Visit(node.Left);
     }
     if (node.Right != null)
     {
         Visit(node.Right);
     }
 }
Exemplo n.º 10
0
        protected override Expression VisitJoin(SqlJoinExpression join)
        {
            // Visit join in reverse order

            var condition = this.Visit(join.JoinCondition);
            var right     = this.VisitSource(join.Right);
            var left      = this.VisitSource(join.Left);

            if (left != join.Left || right != join.Right || condition != join.JoinCondition)
            {
                return(new SqlJoinExpression(join.Type, join.JoinType, left, right, condition));
            }

            return(join);
        }
Exemplo n.º 11
0
        protected override Expression VisitJoin(SqlJoinExpression join)
        {
            this.Visit(join.Left);
            this.Visit(join.Right);

            if (this.inProjection)
            {
                var saveCanBeColumn = this.canBeColumn;

                this.canBeColumn = c => c is SqlColumnExpression;

                this.Visit(join.JoinCondition);

                this.canBeColumn = saveCanBeColumn;
            }

            return(join);
        }
Exemplo n.º 12
0
        protected override Expression VisitJoin(SqlJoinExpression join)
        {
            var left      = this.Visit(join.Left);
            var right     = this.Visit(join.Right);
            var condition = this.Visit(join.JoinCondition);

            if (condition?.Type.GetUnwrappedNullableType() == typeof(bool) && condition is BitBooleanExpression)
            {
                condition = Expression.Equal(condition, Expression.Constant(true, condition.Type));
            }

            if (left != join.Left || right != join.Right || condition != join.JoinCondition)
            {
                return(new SqlJoinExpression(join.Type, join.JoinType, left, right, condition));
            }

            return(join);
        }
Exemplo n.º 13
0
        protected override Expression VisitJoin(SqlJoinExpression join)
        {
            join = (SqlJoinExpression)base.VisitJoin(join);

            if (join.JoinType == SqlJoinType.CrossApply || join.JoinType == SqlJoinType.OuterApply)
            {
                if (join.Right is SqlTableExpression)
                {
                    return(new SqlJoinExpression(join.Type, SqlJoinType.Cross, join.Left, join.Right, null));
                }
                else
                {
                    var select = join.Right as SqlSelectExpression;

                    if (select != null && select.Take == null && select.Skip == null && !SqlAggregateChecker.HasAggregates(select) && (select.GroupBy == null || select.GroupBy.Count == 0))
                    {
                        var selectWithoutWhere = select.ChangeWhere(null);
                        var referencedAliases  = SqlReferencedAliasGatherer.Gather(selectWithoutWhere);
                        var declaredAliases    = SqlDeclaredAliasGatherer.Gather(join.Left);

                        referencedAliases.IntersectWith(declaredAliases);

                        if (referencedAliases.Count == 0)
                        {
                            var where = select.Where;

                            select = selectWithoutWhere;

                            var pc = ColumnProjector.ProjectColumns(new Nominator(Nominator.CanBeColumn), where, select.Columns, select.Alias, SqlDeclaredAliasGatherer.Gather(select.From));

                            select = select.ChangeColumns(pc.Columns);
                            where  = pc.Projector;

                            var joinType = (where == null) ? SqlJoinType.Cross : (join.JoinType == SqlJoinType.CrossApply ? SqlJoinType.Inner : SqlJoinType.Left);

                            return(new SqlJoinExpression(typeof(void), joinType, join.Left, select, where));
                        }
                    }
                }
            }

            return(join);
        }
Exemplo n.º 14
0
        protected override Expression VisitJoin(SqlJoinExpression join)
        {
            this.VisitSource(join.Left);

            this.WriteLine();

            switch (join.JoinType)
            {
            case SqlJoinType.CrossJoin:
                this.Write(" CROSS JOIN ");
                break;

            case SqlJoinType.InnerJoin:
                this.Write(" INNER JOIN ");
                break;

            case SqlJoinType.LeftJoin:
                this.Write(" LEFT JOIN ");
                break;

            case SqlJoinType.RightJoin:
                this.Write(" RIGHT JOIN ");
                break;

            case SqlJoinType.OuterJoin:
                this.Write(" FULL OUTER JOIN ");
                break;
            }

            this.VisitSource(join.Right);

            if (join.JoinCondition != null)
            {
                using (AcquireIndentationContext())
                {
                    this.Write("ON ");

                    this.Visit(join.JoinCondition);
                }
            }

            return(join);
        }
Exemplo n.º 15
0
        protected override Expression VisitJoin(SqlJoinExpression join)
        {
            this.ignoreSet.Add(join.Left);
            this.ignoreSet.Add(join.Right);

            var left  = this.Visit(join.Left);
            var right = this.Visit(join.Right);

            this.ignoreSet.Remove(join.Left);
            this.ignoreSet.Remove(join.Right);

            var condition = this.Visit(join.JoinCondition);

            if (left != join.Left || right != join.Right || condition != join.JoinCondition)
            {
                return(new SqlJoinExpression(join.Type, join.JoinType, left, right, condition));
            }

            return(join);
        }
Exemplo n.º 16
0
        protected override Expression VisitJoin(SqlJoinExpression join)
        {
            var left       = VisitSource(join.Left);
            var leftOrders = this.gatheredOrderings;

            this.gatheredOrderings = null;

            var right = VisitSource(join.Right);

            PrependOrderings(leftOrders);

            var condition = Visit(join.JoinCondition);

            if (left != join.Left || right != join.Right || condition != join.JoinCondition)
            {
                return(new SqlJoinExpression(join.Type, join.JoinType, left, right, condition));
            }

            return(join);
        }
Exemplo n.º 17
0
        private static SqlJoinHint TryFindJoinHint(SqlCompilerContext context, SqlJoinExpression node)
        {
            SqlQueryStatement statement = null;

            for (int i = 0, count = context.GetTraversalPath().Length; i < count; i++)
            {
                if (context.GetTraversalPath()[i] is SqlQueryStatement)
                {
                    statement = context.GetTraversalPath()[i] as SqlQueryStatement;
                }
            }
            if (statement == null || statement.Hints.Count == 0)
            {
                return(null);
            }
            var candidate = statement.Hints
                            .OfType <SqlJoinHint>()
                            .FirstOrDefault(hint => hint.Table == node.Right);

            return(candidate);
        }
Exemplo n.º 18
0
 void addContentIdsFromContentTable(Dictionary<int, int> classIdByContentId)
 {
     var q = new SqlQuery(Engine.Dao);
     var node = new SqlAliasNode();
     var content = new SqlAliasContent();
     var je = new SqlJoinExpression();
     je.Add(node.Id, content.NodeId);
     SqlFromLeftJoin join = new SqlFromLeftJoin(node, content, je);
     q.From(join);
     var rClassId = q.Select(node.ContentClassId);
     var rContentId = q.Select(content.ContentId);
     using (var rs = q.ExecuteReader()) {
         while (rs.Read()) {
             updateDescription("Collecting valid content ids " + classIdByContentId.Count.ToString("### ### ##0") + ".");
             if (Info.BreakExecution) return;
             if (!classIdByContentId.ContainsKey(rContentId.Value)) {
                 classIdByContentId.Add(rContentId.Value, rClassId.Value);
             }
         }
     }
 }
 /// <summary>
 /// Visits the specified SQL join expression.
 /// </summary>
 /// <param name="sqlJoinExpression">The SQL join expression.</param>
 /// <returns>
 /// Returns processed value from espression.
 /// </returns>
 string ISqlVisitor <string> .Visit(SqlJoinExpression sqlJoinExpression)
 {
     return
         ($"{Visit(sqlJoinExpression.Left)} {Visit(sqlJoinExpression.JoinType)} JOIN {Visit(sqlJoinExpression.Right)} ON {Visit(sqlJoinExpression.Condition)}");
 }
Exemplo n.º 20
0
 public virtual void Visit(SqlJoinExpression node)
 {
     VisitInternal(node.Expression);
     VisitInternal(node.Left);
     VisitInternal(node.Right);
 }
Exemplo n.º 21
0
    void ensureInegrity(object stateInfo)
    {
        try {
            setThreadDescription("Ensuring data uniqueness");
            SqlQuery query = new SqlQuery(WAFRuntime.Engine.Dao);
            DataAccessObject dao = WAFRuntime.Engine.Dao;

            // Node table
            setThreadDescription("Fixing Node table..."); int fixCount = 0;
            foreach (int nodeId in dao.GetDuplicateNodeRecords()) {
                if (dao.FixDuplicateNodeRecords(nodeId)) fixCount++;
            }
            //if (fixCount > 0) WFContext.Notify("Fixed " + fixCount + " duplicate records in the node table. ");

            // NodeCsd table
            setThreadDescription("Fixing Node Csd table..."); fixCount = 0;
            foreach (int[] ids in dao.GetDuplicateNodeCsdRecords()) {
                if (dao.FixDuplicateNodeCsdRecords(ids[0], ids[1])) fixCount++;
            }
            //if (fixCount > 0) WFContext.Notify("Fixed " + fixCount + " duplicate records in the nodeCsd table. ");

            // Content table
            setThreadDescription("Fixing Content table..."); fixCount = 0;
            foreach (int[] ids in dao.GetDuplicateContentBaseRecords()) {
                if (dao.FixDuplicateContentBaseRecords(ids[0], ids[1], ids[2])) fixCount++;
            }
            //if (fixCount > 0) WAFRuntime.Notify("Fixed " + fixCount + " duplicate records in the content table. ");

            // Class tables
            foreach (MemDefContentClass classDef in WAFRuntime.Engine.Definition.ContentClass.Values) {
                setThreadDescription("Fixing Class table '" + classDef.ClassName + "'..."); fixCount = 0;
                List<int> contentIds = dao.GetDuplicateContentClassRecords(classDef.Id);
                foreach (int contentId in contentIds) {
                    if (dao.FixDuplicateContentClassRecords(classDef.Id, contentId)) fixCount++;
                }
            }
            //if (fixCount > 0) WAFRuntime.Notify("Fixed " + fixCount + " duplicate records in class tables. ");

            foreach (MemDefRelation relDef in WAFRuntime.Engine.Definition.Relation.Values) {
            }

            foreach (MemDefProperty propDef in WAFRuntime.Engine.Definition.Property.Values) {
                if (propDef.BasePropertyClassId == PropertyBaseClass.InnerContents) {
                }
            }

            AqlAlias alias = new AqlAlias();
            alias.IgnoreSessionCulture = true;
            alias.IgnoreSessionRevision = true;
            alias.IncludeDeletedContents = true;
            alias.IncludeDeletedNodes = true;

            setThreadDescription("Refreshing derived flag...");
            foreach (int siteId in WAFRuntime.Engine.GetAllSiteIds()) {
                foreach (int lcid in WAFRuntime.Engine.GetSiteAllLCIDs(siteId)) {
                    UniqueList<int> cultInSite = new UniqueList<int>(WAFRuntime.Engine.GetSiteLCIDs(siteId));
                    if (!cultInSite.Contains(lcid)) {
                        // if lcid is not in site, set all contents in this lcis to derived...
                        // get all nodes in site
                        SqlAliasNode node = new SqlAliasNode();
                        SqlAliasContent content = new SqlAliasContent();
                        SqlJoinExpression joinExp = new SqlJoinExpression();
                        joinExp.Add(node.Id, content.NodeId);
                        SqlFromInnerJoin join = new SqlFromInnerJoin(node, content, joinExp);
                        SqlQuery select = new SqlQuery(WAFRuntime.Engine.Dao);
                        select.Select(content.ContentId);
                        select.From(join);
                        select.Where(node.SiteId == siteId);
                        select.Where(content.LCID == lcid);
                        select.Where(content.IsDerived == false);
                        List<int> cIds = new List<int>();
                        using (SqlDataReader dr = select.ExecuteReader()) {
                            while (dr.Read()) cIds.Add(dr.GetInt(0));
                        }
                        foreach (int contentId in cIds) {
                            SqlQuery update = new SqlQuery(WAFRuntime.Engine.Dao);
                            update.Update(Sql.Table.Content);
                            update.Set(Sql.Field.Content.IsDerived, true);
                            update.Where(Sql.Field.Content.LCID == lcid);
                            update.Where(Sql.Field.Content.ContentId == contentId);
                            update.ExecuteNonQuery();
                        }
                        if (isThreadCancelled()) return;
                    }
                }
            }

            setThreadDescription("Retrieving node ids...");
            SqlQuery sqlQuery = new SqlQuery(WAFRuntime.Engine.Dao);
            List<int> nodeIds = new List<int>();
            List<int> classIds = new List<int>();
            sqlQuery.From(Sql.Table.Node);
            sqlQuery.Distinct = true;
            sqlQuery.Select(Sql.Field.Node.Id);
            sqlQuery.Select(Sql.Field.Node.ContentClassId);
            using (SqlDataReader sqlDr = sqlQuery.ExecuteReader()) {
                while (sqlDr.Read()) {
                    nodeIds.Add(sqlDr.GetInt(0));
                    classIds.Add(sqlDr.GetInt(1));
                }
            }
            int n = 0;
            foreach (int nodeId in nodeIds) {
                WAFRuntime.Engine.Dao.RebuildDerivedContents(nodeId, classIds[n]);
                setThreadDescription("Ensuring derived content records: " + (++n) + " of " + nodeIds.Count + "...");
                if (isThreadCancelled()) return;
            }

            setThreadDescription("Retrieving content ids...");

            AqlQuery q = WAFRuntime.SystemSession.CreateQuery();
            alias.IgnoreSessionRevision = true;
            alias.IgnoreSessionCulture = true;
            q.From(alias);
            q.Select(alias.ContentId);
            q.Select(alias.ContentClassId);
            List<int> coIds = new List<int>();
            classIds = new List<int>();
            AqlResultSet rs = q.Distinct().Execute();
            while (rs.Read()) {
                coIds.Add((int)rs[0]);
                classIds.Add((int)rs[1]);
            }
            WAFRuntime.Engine.Dao.AddContentIdsInnerContent(ref coIds, ref classIds);

            n = 0;
            foreach (int contentId in coIds) {
                WAFRuntime.Engine.Dao.EnsureContentRecords(contentId, classIds[n], 0);
                setThreadDescription("Ensuring class table records: " + (++n) + " of " + coIds.Count + "...");
                if (isThreadCancelled()) return;
            }

            WAFRuntime.Engine.ClearCache();

            completeThread("DONE. Database integrity check is complete.");
        } catch (Exception error) {
            completeThread("ERROR: " + error.Message);
        }
    }
Exemplo n.º 22
0
 public override NextCall Invoke(WorkflowMethod invoker)
 {
     WAFRuntime.Engine.Dao.DeleteIndex();
     SqlQuery q = new SqlQuery(Session.Engine.Dao);
     SqlAliasNode aNode = new SqlAliasNode();
     SqlAliasContent aContent = new SqlAliasContent();
     SqlJoinExpression je = new SqlJoinExpression();
     je.Add(aNode.Id, aContent.NodeId);
     SqlFromInnerJoin join = new SqlFromInnerJoin(aNode, aContent, je);
     q.From(join);
     var rNodeId = q.Select(aContent.NodeId);
     var rLCID = q.Select(aContent.LCID);
     var rRevision = q.Select(aContent.Revision);
     var rClassId = q.Select(aNode.ContentClassId);
     q.Distinct = true;
     List<CKeyNLRC> keys = new List<CKeyNLRC>();
     using (var result = q.ExecuteReader()) {
         while (result.Read()) keys.Add(new CKeyNLRC(rNodeId, rLCID, rRevision, rClassId));
     }
     WFContext.Engine.QueIndexContents(keys);
     WFContext.Notify("Indexing is cued for " + keys.Count + " contents.");
     return null;
 }