protected SelectStatement GenerateReturningSql(DbModificationCommandTree tree, DbExpression returning)
        {
            SelectStatement select = new SelectStatement();

            Debug.Assert(returning is DbNewInstanceExpression);
            VisitNewInstanceExpression(select, returning as DbNewInstanceExpression);

            select.From = (InputFragment)tree.Target.Expression.Accept(this);

            ListFragment where = new ListFragment();
            where.Append(" row_count() > 0");

            EntitySetBase table         = ((DbScanExpression)tree.Target.Expression).Target;
            bool          foundIdentity = false;

            foreach (EdmMember keyMember in table.ElementType.KeyMembers)
            {
                SqlFragment value;
                if (!values.TryGetValue(keyMember, out value))
                {
                    if (foundIdentity)
                    {
                        throw new NotSupportedException();
                    }
                    foundIdentity = true;
                    value         = new LiteralFragment("last_insert_id()");
                }
                where.Append(String.Format(" AND `{0}`=", keyMember));
                where.Append(value);
            }
            select.Where = where;
            return(select);
        }
        public override SqlFragment Visit(DbArithmeticExpression expression)
        {
            if (expression.ExpressionKind == DbExpressionKind.UnaryMinus)
            {
                ListFragment f = new ListFragment();
                f.Append("-(");
                f.Append(expression.Arguments[0].Accept(this));
                f.Append(")");
                return(f);
            }

            string op = String.Empty;

            switch (expression.ExpressionKind)
            {
            case DbExpressionKind.Divide:
                op = "/"; break;

            case DbExpressionKind.Minus:
                op = "-"; break;

            case DbExpressionKind.Modulo:
                op = "%"; break;

            case DbExpressionKind.Multiply:
                op = "*"; break;

            case DbExpressionKind.Plus:
                op = "+"; break;

            default:
                throw new NotSupportedException();
            }
            return(VisitBinaryExpression(expression.Arguments[0], expression.Arguments[1], op));
        }
        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.Argmument = list;

            return(f);
        }
        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.Argmument = list;

            return f;
        }