Ejemplo n.º 1
0
        static bool CheckColumn(SelectQuery.Column column, ISqlExpression expr, SelectQuery query, bool optimizeValues, bool optimizeColumns)
        {
            if (expr is SqlField || expr is SelectQuery.Column)
            {
                return(false);
            }

            if (expr is SqlValue)
            {
                return(!optimizeValues && 1.Equals(((SqlValue)expr).Value));
            }

            if (expr is SqlBinaryExpression)
            {
                var e = (SqlBinaryExpression)expr;

                if (e.Operation == "*" && e.Expr1 is SqlValue)
                {
                    var value = (SqlValue)e.Expr1;

                    if (value.Value is int && (int)value.Value == -1)
                    {
                        return(CheckColumn(column, e.Expr2, query, optimizeValues, optimizeColumns));
                    }
                }
            }

            var visitor = new QueryVisitor();

            if (optimizeColumns &&
                QueryVisitor.Find(expr, e => e is SelectQuery || IsAggregationFunction(e)) == null)
            {
                var n = 0;
                var q = query.ParentSelect ?? query;

                visitor.VisitAll(q, e => { if (e == column)
                                           {
                                               n++;
                                           }
                                 });

                return(n > 2);
            }

            return(true);
        }
Ejemplo n.º 2
0
        internal void ResolveWeakJoins(List <ISqlTableSource> tables)
        {
            Func <SelectQuery.TableSource, bool> findTable = null; findTable = table =>

            {
                if (tables.Contains(table.Source))
                {
                    return(true);
                }

                foreach (var join in table.Joins)
                {
                    if (findTable(join.Table))
                    {
                        join.IsWeak = false;
                        return(true);
                    }
                }

                if (table.Source is SelectQuery)
                {
                    foreach (var t in ((SelectQuery)table.Source).From.Tables)
                    {
                        if (findTable(t))
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            };

            var areTablesCollected = false;

            _selectQuery.ForEachTable(table =>
            {
                for (var i = 0; i < table.Joins.Count; i++)
                {
                    var join = table.Joins[i];

                    if (join.IsWeak)
                    {
                        if (!areTablesCollected)
                        {
                            areTablesCollected = true;

                            Action <IQueryElement> tableCollector = expr =>
                            {
                                var field = expr as SqlField;

                                if (field != null && !tables.Contains(field.Table))
                                {
                                    tables.Add(field.Table);
                                }
                            };

                            var visitor = new QueryVisitor();

                            visitor.VisitAll(_selectQuery.Select, tableCollector);
                            visitor.VisitAll(_selectQuery.Where, tableCollector);
                            visitor.VisitAll(_selectQuery.GroupBy, tableCollector);
                            visitor.VisitAll(_selectQuery.Having, tableCollector);
                            visitor.VisitAll(_selectQuery.OrderBy, tableCollector);

                            if (_selectQuery.IsInsert)
                            {
                                visitor.VisitAll(_selectQuery.Insert, tableCollector);
                            }

                            if (_selectQuery.IsUpdate)
                            {
                                visitor.VisitAll(_selectQuery.Update, tableCollector);
                            }

                            if (_selectQuery.IsDelete)
                            {
                                visitor.VisitAll(_selectQuery.Delete, tableCollector);
                            }

                            visitor.VisitAll(_selectQuery.From, expr =>
                            {
                                var tbl = expr as SqlTable;

                                if (tbl != null && tbl.TableArguments != null)
                                {
                                    var v = new QueryVisitor();

                                    foreach (var arg in tbl.TableArguments)
                                    {
                                        v.VisitAll(arg, tableCollector);
                                    }
                                }
                            });
                        }

                        if (findTable(join.Table))
                        {
                            join.IsWeak = false;
                        }
                        else
                        {
                            table.Joins.RemoveAt(i);
                            i--;
                        }
                    }
                }
            }, new HashSet <SelectQuery>());
        }