/// <summary>
        /// Convert expression to code.
        /// </summary>
        /// <param name="expression">Expression.</param>
        /// <param name="converter">Expression converter.</param>
        /// <returns>Parts.</returns>
        public override ICode Convert(MethodCallExpression expression, ExpressionConverter converter)
        {
            //ALL, DISTINCT, TOP
            int expressionIndex = expression.SkipMethodChain(0);
            var selectParts     = new ICode[expression.Arguments.Count - expressionIndex];

            selectParts[0] = string.IsNullOrEmpty(Name) ? SelectClause : Name.ToCode();
            for (int i = 0; i < selectParts.Length - 1; i++, expressionIndex++)
            {
                selectParts[i + 1] = converter.ConvertToCode(expression.Arguments[expressionIndex]);
            }

            var select = LineSpace(selectParts);

            //select elemnts.
            var selectTargets = expression.Arguments[expression.Arguments.Count - 1];

            //*
            if (typeof(AsteriskElement).IsAssignableFromEx(selectTargets.Type))
            {
                select.Add("*".ToCode());
                return(new SelectClauseCode(select));
            }

            //new []{ a, b, c} recursive.
            else if (selectTargets.Type.IsArray)
            {
                var newArrayExp = selectTargets as NewArrayExpression;
                if (newArrayExp != null)
                {
                    var array = new ICode[newArrayExp.Expressions.Count];
                    for (int i = 0; i < array.Length; i++)
                    {
                        array[i] = converter.ConvertToCode(newArrayExp.Expressions[i]);
                    }

                    var coreCode = new VCode(select, new VCode(array)
                    {
                        Indent = 1, Separator = ","
                    });
                    return(new SelectClauseCode(coreCode));
                }
            }

            //new { item = db.tbl.column }
            {
                var createInfo = ObjectCreateAnalyzer.MakeObjectCreateInfo(selectTargets);
                var elements   = new ICode[createInfo.Members.Length];
                for (int i = 0; i < elements.Length; i++)
                {
                    elements[i] = ConvertSelectedElement(converter, createInfo.Members[i]);
                }

                var coreCode = new VCode(select, new VCode(elements)
                {
                    Indent = 1, Separator = ","
                });
                return(new SelectClauseCode(coreCode));
            }
        }
        static ICode ConvertSelectedElement(ExpressionConverter converter, ObjectCreateMemberInfo element)
        {
            //single select.
            //for example, COUNT(*).
            if (string.IsNullOrEmpty(element.Name)) return converter.ConvertToCode(element.Expression);

            //normal select.
            return LineSpace(converter.ConvertToCode(element.Expression), "AS".ToCode(), element.Name.ToCode());
        }
        /// <summary>
        /// Convert expression to code.
        /// </summary>
        /// <param name="expression">Expression.</param>
        /// <param name="converter">Expression converter.</param>
        /// <returns>Parts.</returns>
        public override ICode Convert(MethodCallExpression expression, ExpressionConverter converter)
        {
            var newArray = expression.Arguments[ParamsIndex] as NewArrayExpression;

            ICode[] args;
            if (newArray != null)
            {
                args = new ICode[newArray.Expressions.Count];
                for (int i = 0; i < newArray.Expressions.Count; i++)
                {
                    var     rowExp = newArray.Expressions[i];
                    ICode[] hcore;
                    var     rowNewArray = rowExp as NewArrayExpression;
                    if (rowNewArray != null)
                    {
                        hcore = new ICode[rowNewArray.Expressions.Count];
                        for (int j = 0; j < rowNewArray.Expressions.Count; j++)
                        {
                            hcore[j] = converter.ConvertToCode(rowNewArray.Expressions[j]);
                        }
                    }
                    else
                    {
                        var row = converter.ConvertToObject(rowExp) as object[];
                        hcore = new ICode[row.Length];
                        for (int j = 0; j < row.Length; j++)
                        {
                            hcore[j] = converter.ConvertToCode(row[j]);
                        }
                    }
                    args[i] = Blanket(hcore);
                }
            }
            else
            {
                var datas = converter.ConvertToObject(expression.Arguments[ParamsIndex]) as object[][];
                args = new ICode[datas.Length];
                for (int i = 0; i < args.Length; i++)
                {
                    var row   = datas[i];
                    var count = row.Length;
                    var hcore = new ICode[count];
                    for (int j = 0; j < count; j++)
                    {
                        hcore[j] = converter.ConvertToCode(row[j]);
                    }
                    args[i] = Blanket(hcore);
                }
            }
            return(new HCode("VALUES".ToCode(), new VCode(args)
            {
                Separator = ", "
            })
            {
                AddIndentNewLine = true, Indent = 1
            });
        }
예제 #4
0
        ICode[] ConvertExpandArrayArgument(ExpressionConverter converter, ArgumentInfo argumentInfo, Expression argExp)
        {
            ICode[] code;
            var     newArrayExp = argExp as NewArrayExpression;

            if (newArrayExp != null)
            {
                code = new ICode[newArrayExp.Expressions.Count];
                for (int i = 0; i < newArrayExp.Expressions.Count; i++)
                {
                    code[i] = converter.ConvertToCode(newArrayExp.Expressions[i]);
                }
            }
            else
            {
                var obj  = converter.ConvertToObject(argExp);
                var list = new List <ICode>();
                foreach (var e in (IEnumerable)obj)
                {
                    list.Add(converter.ConvertToCode(e));
                }
                code = list.ToArray();
            }

            if (!string.IsNullOrEmpty(argumentInfo.ArrayExpandSeparator))
            {
                var isEmpty = true;
                for (int i = code.Length - 1; 0 <= i; i--)
                {
                    var currentIsEmpty = code[i].IsEmpty;
                    if (!isEmpty && !currentIsEmpty)
                    {
                        code[i] = new HCode(code[i], argumentInfo.ArrayExpandSeparator.ToCode())
                        {
                            EnableChangeLine = false
                        };
                    }
                    if (isEmpty)
                    {
                        isEmpty = currentIsEmpty;
                    }
                }
            }

            if (0 < code.Length && argumentInfo.Separator != null)
            {
                code[code.Length - 1] = new HCode(code[code.Length - 1], argumentInfo.Separator)
                {
                    EnableChangeLine = false
                };
            }

            return(code);
        }
예제 #5
0
        /// <summary>
        /// Convert expression to code.
        /// </summary>
        /// <param name="expression">Expression.</param>
        /// <param name="converter">Expression converter.</param>
        /// <returns>Parts.</returns>
        public override ICode Convert(NewExpression expression, ExpressionConverter converter)
        {
            var arg0 = new HCode(new SingleTextCode("["), converter.ConvertToCode(expression.Arguments[0]), new SingleTextCode("]"))
            {
                Separator = string.Empty
            };

            return(new HCode(arg0, converter.ConvertToCode(expression.Arguments[1]), new SingleTextCode("AS JSON"))
            {
                Separator = " "
            });
        }
        /// <summary>
        /// Convert expression to code.
        /// </summary>
        /// <param name="expression">Expression.</param>
        /// <param name="converter">Expression converter.</param>
        /// <returns>Parts.</returns>
        public override ICode Convert(MethodCallExpression expression, ExpressionConverter converter)
        {
            //ALL, DISTINCT, TOP
            int expressionIndex = expression.SkipMethodChain(0);
            var selectParts = new ICode[expression.Arguments.Count - expressionIndex];
            selectParts[0] = SelectClause;
            for (int i = 0; i < selectParts.Length - 1; i++, expressionIndex++)
            {
                selectParts[i + 1] = converter.ConvertToCode(expression.Arguments[expressionIndex]);
            }

            var select = LineSpace(selectParts);

            //select elemnts.
            var selectTargets = expression.Arguments[expression.Arguments.Count - 1];

            //*
            if (typeof(IAsterisk).IsAssignableFromEx(selectTargets.Type))
            {
                select.Add("*".ToCode());
                return new SelectClauseCode(select);
            }

            //new []{ a, b, c} recursive.
            else if (selectTargets.Type.IsArray)
            {
                var newArrayExp = selectTargets as NewArrayExpression;

                var array = new ICode[newArrayExp.Expressions.Count];
                for (int i = 0; i < array.Length; i++)
                {
                    array[i] = converter.ConvertToCode(newArrayExp.Expressions[i]);
                }

                var coreCode = new VCode(select, new VCode(array) { Indent = 1, Separator = "," });
                return new SelectClauseCode(coreCode);
            }

            //new { item = db.tbl.column }
            else
            {
                var createInfo = ObjectCreateAnalyzer.MakeSelectInfo(selectTargets);
                var elements = new ICode[createInfo.Members.Length];
                for (int i = 0; i < elements.Length; i++)
                {
                    elements[i] = ConvertSelectedElement(converter, createInfo.Members[i]);
                }

                var coreCode = new VCode(select, new VCode(elements) { Indent = 1, Separator = "," });
                return new SelectClauseCode(coreCode);
            }
        }
        static ICode ConvertSelectedElement(ExpressionConverter converter, ObjectCreateMemberInfo element)
        {
            //single select.
            //for example, COUNT(*).
            if (string.IsNullOrEmpty(element.Name))
            {
                return(converter.ConvertToCode(element.Expression));
            }

            //normal select.
            var exp = converter.ConvertToCode(element.Expression);

            return(exp.IsEmpty ? exp : LineSpace(exp, AsClause, element.Name.ToCode()));
        }
        /// <summary>
        /// Convert expression to code.
        /// </summary>
        /// <param name="expression">Expression.</param>
        /// <param name="converter">Expression converter.</param>
        /// <returns>Parts.</returns>
        public override ICode Convert(MethodCallExpression expression, ExpressionConverter converter)
        {
            var text  = (string)converter.ConvertToObject(expression.Arguments[0]);
            var array = expression.Arguments[1] as NewArrayExpression;

            return(new StringFormatCode(text, array.Expressions.Select(e => converter.ConvertToCode(e)).ToArray()));
        }
        /// <summary>
        /// Convert expression to code.
        /// </summary>
        /// <param name="expression">Expression.</param>
        /// <param name="converter">Expression converter.</param>
        /// <returns>Parts.</returns>
        public override ICode Convert(MethodCallExpression expression, ExpressionConverter converter)
        {
            //TODO これちょっとおかしい
            var startIndex = expression.SkipMethodChain(0);
            var table      = FromConverterAttribute.ConvertTable(converter, expression.Arguments[startIndex]);

            if (table.IsEmpty)
            {
                return(string.Empty.ToCode());
            }

            var condition = ((startIndex + 1) < expression.Arguments.Count) ? converter.ConvertToCode(expression.Arguments[startIndex + 1]) : null;

            var join = new HCode()
            {
                AddIndentNewLine = true, Separator = " ", Indent = 1
            };

            join.Add(_nameCode);
            join.Add(table);
            if (condition != null)
            {
                join.Add("ON".ToCode());
                join.Add(condition);
            }
            return(join);
        }
 /// <summary>
 /// Convert expression to code.
 /// </summary>
 /// <param name="expression">Expression.</param>
 /// <param name="converter">Expression converter.</param>
 /// <returns>Parts.</returns>
 public override ICode Convert(MethodCallExpression expression, ExpressionConverter converter)
 {
     var obj = converter.ConvertToObject(expression.Arguments[0]);
     var text = TowWaySqlSpec.ToStringFormat((string)obj);
     var array = expression.Arguments[1] as NewArrayExpression;
     return new StringFormatCode(text, array.Expressions.Select(e => converter.ConvertToCode(e)).ToArray());
 }
예제 #11
0
        internal static ICode ConvertTable(ExpressionConverter decoder, Expression exp)
        {
            //where query, write tables side by side.
            var arry = exp as NewArrayExpression;

            if (arry != null)
            {
                var multiTables = new ICode[arry.Expressions.Count];
                for (int i = 0; i < multiTables.Length; i++)
                {
                    multiTables[i] = ConvertTable(decoder, arry.Expressions[i]);
                }
                return(Arguments(multiTables));
            }

            var table = decoder.ConvertToCode(exp);

            if (table.IsEmpty)
            {
                return(string.Empty.ToCode());
            }

            //sub query.
            var body = GetSubQuery(exp);

            if (body != null)
            {
                return(new SubQueryAndNameCode(body, table));
            }

            return(table);
        }
        internal IEnumerable<ICode> InitAndConvertArguments(MethodCallExpression expression, ExpressionConverter converter)
        {
            lock (this)
            {
                if (string.IsNullOrEmpty(Name)) Name = expression.Method.Name.ToUpper();

                if (NameCode == null) NameCode = Name.ToCode();

                if (_startIndex == -1) _startIndex = expression.SkipMethodChain(0);

                if (_isParamArray == null)
                {
                    var paramsInfo = expression.Method.GetParameters();
                    _isParamArray = new bool[paramsInfo.Length];
                    for (int i = 0; i < paramsInfo.Length; i++)
                    {
                        _isParamArray[i] = paramsInfo[i].GetAttribute<ParamArrayAttribute>() != null;
                    }
                }
            }

            var args = new List<ICode>();
            for (int i = _startIndex; i < expression.Arguments.Count; i++)
            {
                var argExp = expression.Arguments[i];
                if (_isParamArray[i])
                {
                    var newArrayExp = argExp as NewArrayExpression;
                    if (newArrayExp != null)
                    {
                        foreach (var e in newArrayExp.Expressions) args.Add(converter.ConvertToCode(e));
                    }
                    else
                    {
                        var obj = converter.ConvertToObject(argExp);
                        foreach (var e in (IEnumerable)obj) args.Add(converter.ConvertToCode(e));
                    }
                }
                else
                {
                    args.Add(converter.ConvertToCode(argExp));
                }
            }
            return args;
        }
        /// <summary>
        /// Convert expression to code.
        /// </summary>
        /// <param name="expression">Expression.</param>
        /// <param name="converter">Expression converter.</param>
        /// <returns>Parts.</returns>
        public override ICode Convert(MethodCallExpression expression, ExpressionConverter converter)
        {
            //TODO SkipMethodChain -> other
            var startIndex = expression.SkipMethodChain(0);
            var tabkeIndex = HasTop ? startIndex + 1 : startIndex;

            //top
            var codes = new List <ICode>();

            if (HasTop)
            {
                var top = converter.ConvertToCode(expression.Arguments[startIndex]);
                codes.Add(top);
            }

            //table
            var table = converter.ConvertToCode(expression.Arguments[tabkeIndex]);

            codes.Add(table);

            //hints
            if (HasHints)
            {
                var newArray = expression.Arguments[startIndex + 1] as NewArrayExpression;
                var hints    = new ICode[newArray.Expressions.Count];
                int i        = 0;
                foreach (var e in newArray.Expressions)
                {
                    hints[i++] = converter.ConvertToCode(e);
                }
                codes.Add(Func("WITH".ToCode(), hints));
            }

            var sub = FromConverterAttribute.GetSubQuery(expression.Arguments[tabkeIndex]);

            if (sub != null)
            {
                codes.Add(sub.ToCode());
            }

            return(new VCode(codes)
            {
                Separator = " "
            });
        }
예제 #14
0
        static ICode ConvertSingleArgument(ExpressionConverter converter, ArgumentInfo argumentInfo, Expression argExp)
        {
            var argCore = converter.ConvertToCode(argExp);

            return(argumentInfo.Separator == null ?
                   argCore : new HCode(argCore, argumentInfo.Separator)
            {
                EnableChangeLine = false
            });
        }
예제 #15
0
        static ICode ConvertNormalWith(ExpressionConverter converter, NewArrayExpression arry)
        {
            var with = new VCode()
            {
                Indent = 1, Separator = ","
            };
            var names = new List <string>();

            foreach (var e in arry.Expressions)
            {
                var table = converter.ConvertToCode(e);
                var body  = FromConverterAttribute.GetSubQuery(e);
                names.Add(body);
                with.Add(Clause(LineSpace(body.ToCode(), "AS".ToCode()), table));
            }
            return(new WithEntriedCode(new VCode("WITH".ToCode(), with), names.ToArray()));
        }
예제 #16
0
        /// <summary>
        /// Create sql from expression contain formattable string.
        /// </summary>
        /// <typeparam name="T">Type of Selected object.</typeparam>
        /// <param name="exp">expression.</param>
        /// <returns>Sql object.</returns>
        public static Sql FromExpressionContainFormattableString <T>(Expression exp) where T : class
        {
            var methodExp = exp as MethodCallExpression;
            var db        = DBDefineAnalyzer.GetDbInfo <T>();
            var converter = new ExpressionConverter(db);
            var obj       = converter.ConvertToObject(methodExp.Arguments[0]);
            var text      = (string)obj;
            var array     = methodExp.Arguments[1] as NewArrayExpression;

            var args = new ICode[array.Expressions.Count];

            for (int i = 0; i < args.Length; i++)
            {
                args[i] = converter.ConvertToCode(array.Expressions[i]);
            }
            return(new Sql(new CodeParts.StringFormatCode(text, args)));
        }
        /// <summary>
        /// Convert expression to code.
        /// </summary>
        /// <param name="expression">Expression.</param>
        /// <param name="converter">Expression converter.</param>
        /// <returns>Parts.</returns>
        public override ICode Convert(NewExpression expression, ExpressionConverter converter)
        {
            var obj = converter.ConvertToObject(expression.Arguments[0]);

            return((bool)obj ? (ICode) new AroundCode(converter.ConvertToCode(expression.Arguments[1]), "(", ")") : string.Empty.ToCode());
        }
 /// <summary>
 /// Convert expression to code.
 /// </summary>
 /// <param name="expression">Expression.</param>
 /// <param name="converter">Expression converter.</param>
 /// <returns>Parts.</returns>
 public override ICode Convert(NewExpression expression, ExpressionConverter converter)
 {
     var obj = converter.ConvertToObject(expression.Arguments[0]);
     return (bool)obj ? converter.ConvertToCode(expression.Arguments[1]) : string.Empty.ToCode();
 }
예제 #19
0
        internal IEnumerable <ICode> InitAndConvertArguments(MethodCallExpression expression, ExpressionConverter converter)
        {
            lock (this)
            {
                if (string.IsNullOrEmpty(Name))
                {
                    Name = expression.Method.Name.ToUpper();
                }

                if (NameCode == null)
                {
                    NameCode = Name.ToCode();
                }

                if (_startIndex == -1)
                {
                    _startIndex = expression.SkipMethodChain(0);
                }

                if (_isParamArray == null)
                {
                    var paramsInfo = expression.Method.GetParameters();
                    _isParamArray = new bool[paramsInfo.Length];
                    for (int i = 0; i < paramsInfo.Length; i++)
                    {
                        _isParamArray[i] = paramsInfo[i].GetAttribute <ParamArrayAttribute>() != null;
                    }
                }
            }

            var args = new List <ICode>();

            for (int i = _startIndex; i < expression.Arguments.Count; i++)
            {
                var argExp = expression.Arguments[i];
                if (_isParamArray[i])
                {
                    var newArrayExp = argExp as NewArrayExpression;
                    if (newArrayExp != null)
                    {
                        bool isEmpty = true;
                        foreach (var e in newArrayExp.Expressions)
                        {
                            var argCode = converter.ConvertToCode(e);
                            if (isEmpty)
                            {
                                isEmpty = argCode.IsEmpty;
                            }
                            args.Add(argCode);
                        }
                        if (VanishIfEmptyParams && isEmpty)
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        var obj = converter.ConvertToObject(argExp);
                        foreach (var e in (IEnumerable)obj)
                        {
                            args.Add(converter.ConvertToCode(e));
                        }
                    }
                }
                else
                {
                    args.Add(converter.ConvertToCode(argExp));
                }
            }
            return(args);
        }
 /// <summary>
 /// Convert expression to code.
 /// </summary>
 /// <param name="expression">Expression.</param>
 /// <param name="converter">Expression converter.</param>
 /// <returns>Parts.</returns>
 public override ICode Convert(MethodCallExpression expression, ExpressionConverter converter)
 {
     var condition = converter.ConvertToCode(expression.Arguments[expression.SkipMethodChain(0)]);
     if (condition.IsEmpty) return string.Empty.ToCode();
     return Clause(_nameCode, condition);
 }
예제 #21
0
 public override ICode Convert(MethodCallExpression expression, ExpressionConverter converter)
 => converter.ConvertToCode(expression.Arguments[0]);