private SqlFragment HandleFunction(DbFunctionAggregate fa, SqlFragment arg)
        {
            Debug.Assert(fa.Arguments.Count == 1);

            if (fa.Function.NamespaceName != "Edm")
            {
                throw new NotSupportedException();
            }

            FunctionFragment fragment = new FunctionFragment();

            fragment.Name = fa.Function.Name;
            if (fa.Function.Name == "BigCount")
            {
                fragment.Name = "COUNT";
            }
            else
            {
                fragment.Name = fa.Function.Name.ToUpperInvariant();
            }

            fragment.Distinct = fa.Distinct;
            fragment.Argument = arg;
            return(fragment);
            //return new CastExpression(aggregate, GetDbType(functionAggregate.ResultType.EdmType));
        }
        private SqlFragment UserDefinedFunction(DbFunctionExpression e)
        {
            FunctionFragment f = new FunctionFragment();

            f.Name = Metadata.TryGetValueMetadataProperty <string>(e.Function,
                                                                   "StoreFunctionNameAttribute");

            if (String.IsNullOrEmpty(f.Name))
            {
                f.Name = e.Function.Name;
            }

            f.Quoted = !Metadata.TryGetValueMetadataProperty <bool>(e.Function, "BuiltInAttribute");

            bool isFuncNiladic = Metadata.TryGetValueMetadataProperty <bool>(e.Function, "NiladicFunctionAttribute");

            if (isFuncNiladic && e.Arguments.Count > 0)
            {
                throw new InvalidOperationException("Niladic functions cannot have parameters");
            }

            ListFragment list      = new ListFragment();
            string       delimiter = "";

            foreach (DbExpression arg in e.Arguments)
            {
                if (delimiter.Length > 0)
                {
                    list.Append(new LiteralFragment(delimiter));
                }
                list.Append(arg.Accept(callingGenerator));
                delimiter = ", ";
            }
            f.Argument = list;

            return(f);
        }
        /// <summary>
        /// If input sqlFragment is a group by structure, is flattened to remove some nested correlation queries.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private SqlFragment TryFlatteningGroupBy(SqlFragment input)
        {
            SelectStatement select = null;
            SelectStatement tmpFrag = null, tmpFrag2 = null, tmpFrag3 = null;
            InputFragment   table = null;
            string          objName = null, colName = null, queryName = null;

            // First part assert is a kind-of structure we are looking for
            tmpFrag = input as SelectStatement;
            if (tmpFrag == null)
            {
                goto NoChanges;
            }
            tmpFrag = (tmpFrag).From as SelectStatement;
            if (tmpFrag == null)
            {
                goto NoChanges;
            }
            queryName = tmpFrag.Name;
            if (tmpFrag.Columns.Count < 2)
            {
                goto NoChanges;
            }
            if (!(tmpFrag.Columns[0] is ColumnFragment))
            {
                goto NoChanges;
            }
            colName  = objName = (tmpFrag.Columns[0] as ColumnFragment).ActualColumnName;
            tmpFrag2 = tmpFrag.From as SelectStatement;
            if (tmpFrag2 == null)
            {
                goto NoChanges;
            }
            if (tmpFrag2.Columns.Count < 1 || !(tmpFrag2.Columns[0] is ColumnFragment))
            {
                goto NoChanges;
            }
            if (string.CompareOrdinal(objName, tmpFrag2.Columns[0].ActualColumnName) != 0)
            {
                goto NoChanges;
            }
            if (tmpFrag.Columns[1].Literal == null)
            {
                goto NoChanges;
            }
            tmpFrag2 = tmpFrag.Columns[1].Literal as SelectStatement;
            if (tmpFrag2 == null)
            {
                goto NoChanges;
            }
            if ((tmpFrag2.Columns.Count != 1) || !(tmpFrag2.Columns[0].Literal != null))
            {
                goto NoChanges;
            }
            tmpFrag3 = tmpFrag2.From as SelectStatement;
            if (tmpFrag3 == null)
            {
                goto NoChanges;
            }
            table = tmpFrag3.From as InputFragment;
            if (table == null)
            {
                goto NoChanges;
            }
            FunctionFragment func = tmpFrag2.Columns[0].Literal as FunctionFragment;

            if (tmpFrag3.Columns.Count != 1 || !(tmpFrag3.Columns[0] is ColumnFragment))
            {
                goto NoChanges;
            }
            if (func == null)
            {
                goto NoChanges;
            }
            // Yes it is the kind-of type we like, then optimize it
            select     = new SelectStatement(this);
            table.Name = null;
            string        tableName = null;
            TableFragment t         = tmpFrag3.From as TableFragment;

            if (t == null)
            {
                tableName = tmpFrag3.Columns[0].TableName;
            }
            else
            {
                tableName = t.Table;
            }
            select.From = table;
            select.Columns.Add(new ColumnFragment(tableName, colName));
            select.Columns.Add(new ColumnFragment(tableName, "C0")
            {
                ColumnAlias = "C1",
                Literal     = new FunctionFragment()
                {
                    Argument = new ColumnFragment(tableName, tmpFrag3.Columns[0].ActualColumnName),
                    Distinct = tmpFrag3.IsDistinct,
                    Name     = func.Name
                }
            });
            select.Wrap(null);
            select.Name = queryName;
            select.AddGroupBy(select.Columns[0]);
            (input as SelectStatement).From = select;
NoChanges:
            return(input);
        }
Example #4
0
 public void Visit(FunctionFragment f)
 {
 }