Esempio n. 1
0
		public override SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
		{
			return SubQuery
				.ConvertToIndex(expression, level, flags)
				.Select(idx => new SqlInfo { Sql = SubQuery.SqlQuery.Select.Columns[idx.Index], Member = idx.Member })
				.ToArray();
		}
Esempio n. 2
0
		// JoinContext has similar logic. Consider to review it.
		//
		public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
		{
			return ConvertToSql(expression, level, flags)
				.Select(idx =>
				{
					idx.Query = SqlQuery;
					idx.Index = GetIndex((SqlQuery.Column)idx.Sql);

					return idx;
				})
				.ToArray();
		}
Esempio n. 3
0
		public override SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
		{
			if (level == 0)
			{
				switch (flags)
				{
					case ConvertFlags.Field :
					case ConvertFlags.Key   :
					case ConvertFlags.All   :
						{
							var root = expression.GetRootObject();

							if (root.NodeType == ExpressionType.Parameter)
							{
								var ctx = Parser.GetContext(this, root);

								if (ctx != null)
								{
									if (ctx != this)
										return ctx.ConvertToSql(expression, 0, flags);

									return root == expression ?
										Sequence.ConvertToSql(null,       0,         flags) :
										Sequence.ConvertToSql(expression, level + 1, flags);
								}
							}

							break;
						}
				}

				throw new NotImplementedException();
			}

			throw new InvalidOperationException();

			//return Array<ISqlExpression>.Empty;
		}
Esempio n. 4
0
		public override SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
		{
			if (level == 0)
			{
				switch (flags)
				{
					case ConvertFlags.Field :
					case ConvertFlags.Key   :
					case ConvertFlags.All   :
						{
							var root = expression.GetRootObject();

							if (root.NodeType == ExpressionType.Parameter)
							{
								var ctx = Builder.GetContext(this, root);

								if (ctx != null)
								{
									if (ctx != this)
										return ctx.ConvertToSql(expression, 0, flags);

									return root == expression ?
										Sequence.ConvertToSql(null,       0,         flags) :
										Sequence.ConvertToSql(expression, level + 1, flags);
								}
							}

							break;
						}
				}

				throw new LinqException("'{0}' cannot be converted to SQL.", expression);
			}

			throw new InvalidOperationException();
		}
Esempio n. 5
0
			public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
			{
				return ConvertToSql(expression, level, flags)
					.Select(i => { i.Index = GetIndex(i.Sql); return i; })
					.ToArray();
			}
Esempio n. 6
0
            public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
            {
                var sql = ConvertToSql(expression, level, flags);

                if (sql[0].Index < 0)
                {
                    sql[0].Index = sql[0].Query.Select.Add(sql[0].Sql);
                }

                return(sql);
            }
Esempio n. 7
0
            public override SqlInfo[] ConvertToIndex(Expression?expression, int level, ConvertFlags flags)
            {
                var key = Tuple.Create(expression, level, flags);

                if (!_expressionIndex.TryGetValue(key, out var info))
                {
                    info = ConvertToSql(expression, level, flags);

                    foreach (var item in info)
                    {
                        item.Query = SelectQuery;
                        item.Index = SelectQuery.Select.Add(item.Sql);
                    }
                }

                return(info);
            }
Esempio n. 8
0
 public override SqlInfo[] ConvertToSql(Expression?expression, int level, ConvertFlags flags)
 {
     return(base.ConvertToSql(expression, level, flags));
 }
Esempio n. 9
0
        public SqlInfo[] ConvertToSql(Expression?expression, int level, ConvertFlags flags)
        {
            if (expression == null)
            {
                var query = SelectQuery;
                var sql   = SelectQuery.Select.Columns[0];

                if (Parent != null)
                {
                    query = Parent.SelectQuery;
                }

                return(new[] { new SqlInfo {
                                   Query = query, Sql = sql
                               } });
            }

            switch (flags)
            {
            case ConvertFlags.Field:
            {
                if (expression.NodeType == ExpressionType.MemberAccess)
                {
                    var memberExpression = (MemberExpression)expression;

                    foreach (var column in _entityDescriptor.Columns)
                    {
                        if (column.MemberInfo.EqualsTo(memberExpression.Member, _elementType))
                        {
                            if (!Table.Fields.TryGetValue(column.MemberInfo.Name, out var newField))
                            {
                                newField = new SqlField(column);

                                Table.Add(newField);

                                for (var i = 0; i < _records.Count; i++)
                                {
                                    object value;
                                    if (column.MemberInfo.IsPropertyEx())
                                    {
                                        value = ((PropertyInfo)column.MemberInfo).GetValue(_records[i].Value);
                                    }
                                    else if (column.MemberInfo.IsFieldEx())
                                    {
                                        value = ((FieldInfo)column.MemberInfo).GetValue(_records[i].Value);
                                    }
                                    else
                                    {
                                        throw new InvalidOperationException();
                                    }

                                    var valueExpr = Expression.Constant(value, column.MemberType);
                                    var expr      = Builder.ConvertToSqlExpression(Parent !, valueExpr);

                                    if (expr is SqlParameter p)
                                    {
                                        // avoid parameters is source, because their number is limited
                                        p.IsQueryParameter = !Builder.MappingSchema.ValueToSqlConverter.CanConvert(p.Type.SystemType);
                                        p.Type             = p.Type.WithoutSystemType(column);
                                    }
                                    else if (expr is SqlValue val)
                                    {
                                        val.ValueType = val.ValueType.WithoutSystemType(column);
                                    }

                                    Table.Rows[i].Add(expr);
                                }
                            }
                            return(new[]
                                {
                                    new SqlInfo(column.MemberInfo)
                                    {
                                        Sql = newField,
                                        Query = SelectQuery
                                    }
                                });
                        }
                    }
                }

                break;
            }
            }
            throw new NotImplementedException();
        }
Esempio n. 10
0
            public override SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
            {
                if (_isObject)
                {
                    switch (flags)
                    {
                    case ConvertFlags.All:
                    case ConvertFlags.Key:

                        if (expression == null)
                        {
                            return(_members.Values
                                   .Select(m => ConvertToSql(m.MemberExpression, 0, ConvertFlags.Field)[0])
                                   .ToArray());
                        }

                        break;

                    case ConvertFlags.Field:

                        if (expression != null && (level == 0 || level == 1) && expression.NodeType == ExpressionType.MemberAccess)
                        {
                            var levelExpression = expression.GetLevelExpression(1);

                            if (expression == levelExpression)
                            {
                                var ma = (MemberExpression)expression;

                                Member member;

                                if (!_members.TryGetValue(ma.Member, out member))
                                {
                                    var ed = Builder.MappingSchema.GetEntityDescriptor(_type);

                                    if (ed.Aliases != null && ed.Aliases.ContainsKey(ma.Member.Name))
                                    {
                                        var alias = ed[ma.Member.Name];

                                        if (alias != null)
                                        {
                                            var cd = ed[alias.MemberName];

                                            if (cd != null)
                                            {
                                                _members.TryGetValue(cd.MemberInfo, out member);
                                            }
                                        }
                                    }
                                }

                                if (member == null)
                                {
                                    throw new LinqToDBException(
                                              string.Format("Expression '{0}' is not a field.", expression));
                                }

                                if (member.SqlQueryInfo == null)
                                {
                                    member.SqlQueryInfo = new SqlInfo(member.MemberExpression.Member)
                                    {
                                        Index = -2,
                                        Sql   = SubQuery.SelectQuery.Select.Columns[member.SequenceInfo.Index],
                                        Query = SelectQuery,
                                    };
                                }

                                return(new[] { member.SqlQueryInfo });
                            }
                        }

                        break;
                    }

                    throw new InvalidOperationException();
                }

                return(base.ConvertToSql(expression, level, flags));
            }
Esempio n. 11
0
 public override SqlInfo[] ConvertToIndex(Expression?expression, int level, ConvertFlags flags)
 {
     return(flags switch
     {
         ConvertFlags.Field =>
         _index ??= new[]
         {
             new SqlInfo(Sql !, Parent !.SelectQuery, Parent.SelectQuery.Select.Add(Sql !))
         },
Esempio n. 12
0
            public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
            {
                if (Collection != null)
                {
                    if (expression == null)
                    {
                        return(Collection.ConvertToIndex(expression, level, flags));
                    }

                    var root = expression.GetRootObject();

                    if (root != Lambda.Parameters[0])
                    {
                        return(Collection.ConvertToIndex(expression, level, flags));
                    }
                }

                return(base.ConvertToIndex(expression, level, flags));
            }
Esempio n. 13
0
 /// <summary>
 /// Pops a value from the stack that can be used in the context of
 /// compare operations.
 /// </summary>
 /// <param name="location">The current location.</param>
 /// <param name="flags">The conversion flags.</param>
 /// <returns>
 /// The popped value from the stack that can be used in the
 /// context of compare and arithmetic operations.</returns>
 public Value PopCompareValue(Location location, ConvertFlags flags) =>
 PeekBasicValueType(location) == BasicValueType.Int1
     ? Pop()
     : PopCompareOrArithmeticValue(location, flags);
Esempio n. 14
0
 public override SqlInfo[] ConvertToSql(Expression?expression, int level, ConvertFlags flags)
 {
     switch (flags)
     {
     case ConvertFlags.Field: return(new[] { new SqlInfo(Sql !, Parent !.SelectQuery) });
Esempio n. 15
0
 public virtual SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
 {
     return(Context.ConvertToIndex(expression, level, flags));
 }
Esempio n. 16
0
        Convert(String str, ConvertFlags wideFlag, ConvertFlags narrowFlag)
        {
            StringBuilder sb = new StringBuilder();

            //Console.WriteLine("Convert In: {0}", str);
            for (Int32 i = 0; i < str.Length; i++)
            {
                Char    c                = str[i];
                Boolean isNextDakuten    = (str.Length > i + 1 ? (str[i + 1] == '゙') : false);
                Boolean isNextHanDakuten = (str.Length > i + 1 ? (str[i + 1] == '゚') : false);
                //Console.WriteLine("  - char: {0}", c);
                //Console.WriteLine("  - isNextDakuten: {0}", isNextDakuten);
                //Console.WriteLine("  - isNextHanDakuten: {0}", isNextHanDakuten);

                if (((narrowFlag & ConvertFlags.Katakana) != 0) && _tableZenkakuToHankaku.ContainsKey(c))
                {
                    // 全角カナ -> 半角カナ
                    sb.Append(_tableZenkakuToHankaku[c]);
                }
                else if (((wideFlag & ConvertFlags.Katakana) != 0) && (c >= '。' && c <= '゚'))
                {
                    // 半角カナ -> 全角カナ
                    Int32 col = (c - 'ア') / 5;                     // アカサタナ行
                    Int32 row = (c - 'ア') % 5;                     // アイウエオ
                    //Console.WriteLine("    - char: {0} at {1} - {2}", c, col, row);

                    if (isNextDakuten)
                    {
                        switch (col)
                        {
                        case 1:
                        case 2:
                        case 3:
                        case 5:
                            //Console.WriteLine("      -> {0}", _tableHankakuToZenkakuDakuten[col, row]);
                            sb.Append(_tableHankakuToZenkakuDakuten[col, row]);
                            i++;
                            break;

                        default:
                            if (c == 'ウ')
                            {
                                sb.Append('ヴ');
                                i++;
                            }
                            break;
                        }
                    }
                    else if (isNextHanDakuten && col == 5)
                    {
                        sb.Append(_tableHankakuToZenkakuHanDakuten[row]);
                        i++;
                    }
                    else
                    {
                        //Console.WriteLine("      -> {0}", _tableHankakuToZenkaku[(c - '。')]);
                        sb.Append(_tableHankakuToZenkaku[(c - '。')]);
                    }
                }
                else if (((wideFlag & ConvertFlags.Alphabet) != 0) && (c >= '!' && c <= '~' && (c < '0' || c > '9')))
                {
                    // 半角アルファベット -> 全角アルファベット
                    sb.Append((Char)('!' + (c - '!')));
                }
                else if (((narrowFlag & ConvertFlags.Alphabet) != 0) && (c >= '!' && c <= '〜' && (c < '0' || c > '9')))
                {
                    // 全角アルファベット -> 半角アルファベット
                    sb.Append((Char)('!' + (c - '!')));
                }
                else if (((wideFlag & ConvertFlags.Numeric) != 0) && (c >= '0' && c <= '9'))
                {
                    // 半角数字 -> 全角数字
                    sb.Append((Char)('0' + (c - '0')));
                }
                else if (((narrowFlag & ConvertFlags.Numeric) != 0) && (c >= '0' && c <= '9'))
                {
                    // 全角数字 -> 半角数字
                    sb.Append((Char)('0' + (c - '0')));
                }
                else if (((wideFlag & ConvertFlags.Space) != 0) && (c == ' '))
                {
                    // 半角空白 -> 全角空白
                    sb.Append(' ');
                }
                else if (((narrowFlag & ConvertFlags.Space) != 0) && (c == ' '))
                {
                    // 全角空白 -> 半角空白
                    sb.Append(' ');
                }
                else
                {
                    sb.Append(c);
                }
            }
            return(sb.ToString());
        }
Esempio n. 17
0
            public override SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
            {
                if (expression == null)
                {
                    return(_key.ConvertToSql(null, 0, flags));
                }

                if (level > 0)
                {
                    switch (expression.NodeType)
                    {
                    case ExpressionType.Call:
                    {
                        var e = (MethodCallExpression)expression;

                        if (e.Method.DeclaringType == typeof(Enumerable))
                        {
                            return(new[] { new SqlInfo {
                                               Sql = ConvertEnumerable(e)
                                           } });
                        }

                        break;
                    }

                    case ExpressionType.MemberAccess:
                    {
                        var levelExpression = expression.GetLevelExpression(level);

                        if (levelExpression.NodeType == ExpressionType.MemberAccess)
                        {
                            var e = (MemberExpression)levelExpression;

                            if (e.Member.Name == "Key")
                            {
                                if (_keyProperty == null)
                                {
                                    _keyProperty = _groupingType.GetPropertyEx("Key");
                                }

                                if (e.Member == _keyProperty)
                                {
                                    if (ReferenceEquals(levelExpression, expression))
                                    {
                                        return(_key.ConvertToSql(null, 0, flags));
                                    }

                                    return(_key.ConvertToSql(expression, level + 1, flags));
                                }
                            }

                            return(Sequence.ConvertToSql(expression, level, flags));
                        }

                        break;
                    }
                    }
                }

                throw new NotImplementedException();
            }
		public SqlInfo[] ParseExpressions(IParseContext context, Expression expression, ConvertFlags queryConvertFlag)
		{
			switch (expression.NodeType)
			{
				case ExpressionType.New :
					{
						var expr = (NewExpression)expression;

// ReSharper disable ConditionIsAlwaysTrueOrFalse
// ReSharper disable HeuristicUnreachableCode
						if (expr.Members == null)
							return Array<SqlInfo>.Empty;
// ReSharper restore HeuristicUnreachableCode
// ReSharper restore ConditionIsAlwaysTrueOrFalse

						return expr.Arguments
							.Select((arg,i) =>
							{
								var sql = ParseExpression(context, arg);
								var mi  = expr.Members[i];

								if (mi is MethodInfo)
									mi = TypeHelper.GetPropertyByMethod((MethodInfo)mi);

								return new SqlInfo { Sql = sql, Member = mi };
							})
							.ToArray();
					}

				case ExpressionType.MemberInit :
					{
						var expr = (MemberInitExpression)expression;

						return expr.Bindings
							.Where(b => b is MemberAssignment)
							.Cast<MemberAssignment>()
							.Select(a =>
							{
								var sql = ParseExpression(context, a.Expression);
								var mi  = a.Member;

								if (mi is MethodInfo)
									mi = TypeHelper.GetPropertyByMethod((MethodInfo)mi);

								return new SqlInfo { Sql = sql, Member = mi };
							})
							.ToArray();
					}
			}

			var ctx = GetContext(context, expression);

			if (ctx != null)
			{
				if (ctx.IsExpression(expression, 0, RequestFor.Object))
					return ctx.ConvertToSql(expression, 0, queryConvertFlag);
			}

			return new[] { new SqlInfo { Sql = ParseExpression(context, expression) } };
		}
Esempio n. 19
0
 /// <summary>
 /// Converts the given flags into source unsigned flags.
 /// </summary>
 /// <param name="flags">The flags to convert.</param>
 /// <returns>The converted flags.</returns>
 internal static ConvertFlags ToSourceUnsignedFlags(this ConvertFlags flags) =>
 (flags & ConvertFlags.TargetUnsigned) != ConvertFlags.TargetUnsigned
     ? flags
     : (flags & ~ConvertFlags.TargetUnsigned) | ConvertFlags.SourceUnsigned;
Esempio n. 20
0
 public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
 {
     return(ConvertToSql(expression, level, flags)
            .Select(i => { i.Index = GetIndex(i.Sql); return i; })
            .ToArray());
 }
Esempio n. 21
0
            public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
            {
                if (_isObject)
                {
                    return(ConvertToSql(expression, level, flags)
                           .Select(idx =>
                    {
                        if (idx.Index < 0)
                        {
                            if (idx.Index == -2)
                            {
                                SelectQuery.Select.Columns.Add(new SelectQuery.Column(SelectQuery, idx.Sql));
                                idx.Index = SelectQuery.Select.Columns.Count - 1;
                            }
                            else
                            {
                                idx.Index = SelectQuery.Select.Add(idx.Sql);
                            }
                        }

                        return idx;
                    })
                           .ToArray());
                }

                return(base.ConvertToIndex(expression, level, flags));
            }
Esempio n. 22
0
		Convert(String str, ConvertFlags wideFlag, ConvertFlags narrowFlag)
		{
			StringBuilder sb = new StringBuilder();
			//Console.WriteLine("Convert In: {0}", str);
			for (Int32 i = 0; i < str.Length; i++) {
				Char c = str[i];
				Boolean isNextDakuten = (str.Length > i+1 ? (str[i+1] == '゙') : false);
				Boolean isNextHanDakuten = (str.Length > i+1 ? (str[i+1] == '゚') : false);
				//Console.WriteLine("  - char: {0}", c);
				//Console.WriteLine("  - isNextDakuten: {0}", isNextDakuten);
				//Console.WriteLine("  - isNextHanDakuten: {0}", isNextHanDakuten);
				
				if (((narrowFlag & ConvertFlags.Katakana) != 0) && _tableZenkakuToHankaku.ContainsKey(c)) {
					// 全角カナ -> 半角カナ
					sb.Append(_tableZenkakuToHankaku[c]);
				} else if (((wideFlag & ConvertFlags.Katakana) != 0) && (c >= '。' && c <= '゚')) {
					// 半角カナ -> 全角カナ
					Int32 col = (c - 'ア') / 5; // アカサタナ行
					Int32 row = (c - 'ア') % 5; // アイウエオ
					//Console.WriteLine("    - char: {0} at {1} - {2}", c, col, row);
					
					if (isNextDakuten) {
						switch (col) {
						case 1: case 2: case 3: case 5:
							//Console.WriteLine("      -> {0}", _tableHankakuToZenkakuDakuten[col, row]);
							sb.Append(_tableHankakuToZenkakuDakuten[col, row]);
							i++;
							break;
						default:
							if (c == 'ウ') {
								sb.Append('ヴ');
								i++;
							}
							break;
						}
					} else if (isNextHanDakuten && col == 5) {
						sb.Append(_tableHankakuToZenkakuHanDakuten[row]);
						i++;
					} else {
						//Console.WriteLine("      -> {0}", _tableHankakuToZenkaku[(c - '。')]);
						sb.Append(_tableHankakuToZenkaku[(c - '。')]);
					}
				} else if (((wideFlag & ConvertFlags.Alphabet) != 0) && (c >= '!' && c <= '~' && (c < '0' || c > '9'))) {
					// 半角アルファベット -> 全角アルファベット
					sb.Append((Char)('!' + (c - '!')));
				} else if (((narrowFlag & ConvertFlags.Alphabet) != 0) && (c >= '!' && c <= '〜' && (c < '0' || c > '9'))) {
					// 全角アルファベット -> 半角アルファベット
					sb.Append((Char)('!' + (c - '!')));
				} else if (((wideFlag & ConvertFlags.Numeric) != 0) && (c >= '0' && c <= '9')) {
					// 半角数字 -> 全角数字
					sb.Append((Char)('0' + (c - '0')));
				} else if (((narrowFlag & ConvertFlags.Numeric) != 0) && (c >= '0' && c <= '9')) {
					// 全角数字 -> 半角数字
					sb.Append((Char)('0' + (c - '0')));
				} else if (((wideFlag & ConvertFlags.Space) != 0) && (c == ' ')) {
					// 半角空白 -> 全角空白
					sb.Append(' ');
				} else if (((narrowFlag & ConvertFlags.Space) != 0) && (c == ' ')) {
					// 全角空白 -> 半角空白
					sb.Append(' ');
				} else {
					sb.Append(c);
				}
			}
			return sb.ToString();
		}
Esempio n. 23
0
 public abstract SqlInfo[]          ConvertToIndex(Expression expression, int level, ConvertFlags flags);
Esempio n. 24
0
            public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
            {
                if (_innerContext != null)
                {
                    return(_innerContext.ConvertToIndex(expression, level, flags));
                }

                return(base.ConvertToIndex(expression, level, flags));
            }
Esempio n. 25
0
 public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
 {
     throw new NotImplementedException();
 }
Esempio n. 26
0
 public SqlInfo[] ConvertToSql(Expression?expression, int level, ConvertFlags flags)
 {
     throw new NotImplementedException();
 }
Esempio n. 27
0
            public override SqlInfo[] ConvertToSql(Expression?expression, int level, ConvertFlags flags)
            {
                if (expression == null)
                {
                    if (flags == ConvertFlags.Field && !_key.IsScalar)
                    {
                        return(_element.ConvertToSql(null, 0, flags));
                    }
                    var keys = _key.ConvertToSql(null, 0, flags);
                    foreach (var key in keys)
                    {
                        key.MemberChain.Add(_keyProperty !);
                    }

                    return(keys);
                }

                if (level == 0 && expression.NodeType == ExpressionType.MemberAccess)
                {
                    level = level + 1;
                }
                if (level > 0)
                {
                    switch (expression.NodeType)
                    {
                    case ExpressionType.Call:
                    {
                        var e = (MethodCallExpression)expression;

                        if (e.IsQueryable() || e.IsAggregate(Builder.MappingSchema))
                        {
                            return(new[] { new SqlInfo {
                                               Sql = ConvertEnumerable(e)
                                           } });
                        }

                        break;
                    }

                    case ExpressionType.MemberAccess:
                    {
                        var levelExpression = expression.GetLevelExpression(Builder.MappingSchema, level);

                        if (levelExpression.NodeType == ExpressionType.MemberAccess)
                        {
                            var e = (MemberExpression)levelExpression;

                            if (e.Member.Name == "Key")
                            {
                                if (_keyProperty == null)
                                {
                                    _keyProperty = _groupingType.GetProperty("Key");
                                }

                                if (e.Member == _keyProperty)
                                {
                                    if (ReferenceEquals(levelExpression, expression))
                                    {
                                        return(_key.ConvertToSql(null, 0, flags));
                                    }

                                    return(_key.ConvertToSql(expression, level + 1, flags));
                                }
                            }

                            return(Sequence.ConvertToSql(expression, level, flags));
                        }

                        break;
                    }
                    }
                }

                throw new LinqException("Expression '{0}' cannot be converted to SQL.", expression);
            }
Esempio n. 28
0
 public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
 {
     throw new InvalidOperationException();
 }
Esempio n. 29
0
 public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
 {
     return(Sequence.ConvertToIndex(expression, level, flags));
 }
Esempio n. 30
0
 public override SqlInfo[] ConvertToSql(Expression?expression, int level, ConvertFlags flags)
 {
     return(flags switch
     {
         ConvertFlags.Field => new[] { new SqlInfo(Sql !, Parent !.SelectQuery) },
Esempio n. 31
0
            public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
            {
                switch (flags)
                {
                case ConvertFlags.Field:
                    return(_index ?? (_index = new[]
                    {
                        new SqlInfo {
                            Query = Parent.SelectQuery, Index = Parent.SelectQuery.Select.Add(Sql), Sql = Sql,
                        }
                    }));
                }

                throw new InvalidOperationException();
            }
Esempio n. 32
0
        public virtual SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
        {
            if (expression != null && level > 0 && expression.NodeType == ExpressionType.Call)
            {
                var e = (MethodCallExpression)expression;

                if (e.Method.DeclaringType == typeof(Enumerable))
                {
                    return(new[] { new SqlInfo {
                                       Sql = Builder.SubQueryToSql(this, e)
                                   } });
                }
            }

            if (IsScalar)
            {
                if (expression == null)
                {
                    return(Builder.ConvertExpressions(this, Body, flags));
                }

                switch (flags)
                {
                case ConvertFlags.Field:
                case ConvertFlags.Key:
                case ConvertFlags.All:
                {
                    if (Body.NodeType != ExpressionType.Parameter && level == 0)
                    {
                        var levelExpression = expression.GetLevelExpression(level);

                        if (levelExpression != expression)
                        {
                            if (flags != ConvertFlags.Field && IsExpression(expression, level, RequestFor.Field).Result)
                            {
                                flags = ConvertFlags.Field;
                            }
                        }
                    }

                    return(ProcessScalar(
                               expression,
                               level,
                               (ctx, ex, l) => ctx.ConvertToSql(ex, l, flags),
                               () => new[] { new SqlInfo {
                                                 Sql = Builder.ConvertToSql(this, expression)
                                             } }));
                }
                }
            }
            else
            {
                if (expression == null)
                {
                    if (flags != ConvertFlags.Field)
                    {
                        var q =
                            from m in Members
                            where !(m.Key is MethodInfo)
                            select ConvertMember(m.Key, m.Value, flags) into mm
                            from m in mm
                            select m;

                        return(q.ToArray());
                    }

                    throw new NotImplementedException();
                }

                switch (flags)
                {
                case ConvertFlags.All:
                case ConvertFlags.Key:
                case ConvertFlags.Field:
                {
                    var levelExpression = expression.GetLevelExpression(level);

                    switch (levelExpression.NodeType)
                    {
                    case ExpressionType.MemberAccess:
                    {
                        if (level != 0 && levelExpression == expression)
                        {
                            var member = ((MemberExpression)levelExpression).Member;

                            SqlInfo[] sql;

                            if (!_sql.TryGetValue(member, out sql))
                            {
                                var memberExpression = GetMemberExpression(
                                    member, levelExpression == expression, levelExpression.Type, expression);

                                sql = ConvertExpressions(memberExpression, flags)
                                      .Select(si => si.Clone(member)).ToArray();

                                _sql.Add(member, sql);
                            }

                            return(sql);
                        }

                        return(ProcessMemberAccess(
                                   expression, (MemberExpression)levelExpression, level,
                                   (n, ctx, ex, l, mex) =>
                                {
                                    switch (n)
                                    {
                                    case 0:
                                        var buildExpression = GetExpression(expression, levelExpression, mex);
                                        return ConvertExpressions(buildExpression, flags);

                                    default:
                                        return ctx.ConvertToSql(ex, l, flags);
                                    }
                                }));
                    }

                    case ExpressionType.Parameter:
                        if (levelExpression != expression)
                        {
                            return(GetSequence(expression, level).ConvertToSql(expression, level + 1, flags));
                        }

                        if (level == 0)
                        {
                            return(GetSequence(expression, level).ConvertToSql(null, 0, flags));
                        }

                        break;

                    default:
                        if (level == 0)
                        {
                            return(Builder.ConvertExpressions(this, expression, flags));
                        }
                        break;
                    }

                    break;
                }
                }
            }

            throw new NotImplementedException();
        }
Esempio n. 33
0
		public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
		{
			throw new InvalidOperationException();
		}
Esempio n. 34
0
            public override SqlInfo[] ConvertToIndex(Expression?expression, int level, ConvertFlags flags)
            {
                switch (flags)
                {
                case ConvertFlags.Field:
                {
                    var result = _index ??= new[]
                    {
                        new SqlInfo(Sql !, Parent !.SelectQuery, Parent.SelectQuery.Select.Add(Sql !))
                    };

                    return(result);
                }
                }


                throw new InvalidOperationException();
            }
Esempio n. 35
0
 SqlInfo[] ConvertMember(MemberInfo member, Expression expression, ConvertFlags flags)
 {
     return(ConvertExpressions(expression, flags)
            .Select(si => si.Clone(member))
            .ToArray());
 }
Esempio n. 36
0
		public virtual SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
		{
			if (IsScalar)
			{
				if (expression == null)
					return Builder.ConvertExpressions(this, Body, flags);

				switch (flags)
				{
					case ConvertFlags.Field :
					case ConvertFlags.Key   :
					case ConvertFlags.All   :
						{
							if (Body.NodeType != ExpressionType.Parameter && level == 0)
							{
								var levelExpression = expression.GetLevelExpression(level);

								if (levelExpression != expression)
									if (flags != ConvertFlags.Field && IsExpression(expression, level, RequestFor.Field))
										flags = ConvertFlags.Field;
							}

							return ProcessScalar(
								expression,
								level,
								(ctx, ex, l) => ctx.ConvertToSql(ex, l, flags),
								() => new[] { new SqlInfo { Sql = Builder.ConvertToSql(this, expression) } });
						}
				}
			}
			else
			{
				if (expression == null)
				{
					if (flags != ConvertFlags.Field)
					{
						var q =
							from m in Members
							where !(m.Key is MethodInfo)
							select ConvertMember(m.Key, m.Value, flags) into mm
							from m in mm
							select m;

						return q.ToArray();
					}

					throw new NotImplementedException();
				}

				switch (flags)
				{
					case ConvertFlags.All   :
					case ConvertFlags.Key   :
					case ConvertFlags.Field :
						{
							var levelExpression = expression.GetLevelExpression(level);

							switch (levelExpression.NodeType)
							{
								case ExpressionType.MemberAccess :
									{
										if (level != 0 && levelExpression == expression)
										{
											var member = ((MemberExpression)levelExpression).Member;

											SqlInfo[] sql;

											if (!_sql.TryGetValue(member, out sql))
											{
												sql = ConvertExpressions(Members[member], flags);

												if (sql.Length == 1)
													sql[0].Member = member;

												_sql.Add(member, sql);
											}

											return sql;
										}

										return ProcessMemberAccess(
											expression, levelExpression, level,
											(n,ctx,ex,l,mex) =>
											{
												switch (n)
												{
													case 0 :
													//case 2 :
														var buildExpression = GetExpression(expression, levelExpression, mex);
														return ConvertExpressions(buildExpression, flags);
													default:
														return ctx.ConvertToSql(ex, l, flags);
												}
											});
									}

								case ExpressionType.Parameter:
									if (levelExpression != expression)
										return GetSequence(expression, level).ConvertToSql(expression, level + 1, flags);

									if (level == 0)
										return GetSequence(expression, level).ConvertToSql(null, 0, flags);

									break;
							}

							break;
						}
				}
			}

			throw new NotImplementedException();
		}
Esempio n. 37
0
			public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
			{
				return Sequence.ConvertToIndex(expression, level, flags);
			}
Esempio n. 38
0
 SqlInfo[] ConvertExpressions(Expression expression, ConvertFlags flags)
 {
     return(Builder.ConvertExpressions(this, expression, flags)
            .Select(CheckExpression)
            .ToArray());
 }
Esempio n. 39
0
        SqlInfo[] ConvertToIndexInternal(Expression expression, int level, ConvertFlags flags)
        {
            if (IsScalar)
            {
                if (Body.NodeType == ExpressionType.Parameter)
                {
                    for (var i = 0; i < Sequence.Length; i++)
                    {
                        if (Body == Lambda.Parameters[i])
                        {
                            return(Sequence[i].ConvertToIndex(expression, level, flags));
                        }
                    }
                }

                if (expression == null)
                {
                    var key = Tuple.Create((MemberInfo)null, flags);

                    SqlInfo[] idx;

                    if (!_memberIndex.TryGetValue(key, out idx))
                    {
                        idx = ConvertToSql(null, 0, flags);

                        foreach (var info in idx)
                        {
                            SetInfo(info);
                        }

                        _memberIndex.Add(key, idx);
                    }

                    return(idx);
                }

                switch (flags)
                {
                case ConvertFlags.Field:
                case ConvertFlags.All:
                    return(ProcessScalar(
                               expression,
                               level,
                               (ctx, ex, l) => ctx.ConvertToIndex(ex, l, flags),
                               () => GetSequence(expression, level).ConvertToIndex(expression, level + 1, flags)));
                }
            }
            else
            {
                if (expression == null)
                {
                    switch (flags)
                    {
                    case ConvertFlags.Field:
                    case ConvertFlags.Key:
                    case ConvertFlags.All:
                    {
                        var p = Expression.Parameter(Body.Type, "p");
                        var q =
                            from m in Members.Keys
                            where !(m is MethodInfo)
                            select new SqlData
                        {
                            Sql    = ConvertToIndex(Expression.MakeMemberAccess(p, m), 1, flags),
                            Member = m
                        } into mm
                        from m in mm.Sql.Select(s => s.Clone(mm.Member))
                        select m;

                        return(q.ToArray());
                    }
                    }
                }

                switch (flags)
                {
                case ConvertFlags.All:
                case ConvertFlags.Key:
                case ConvertFlags.Field:
                {
                    if (level == 0)
                    {
                        var idx = Builder.ConvertExpressions(this, expression, flags);

                        foreach (var info in idx)
                        {
                            SetInfo(info);
                        }

                        return(idx);
                    }

                    var levelExpression = expression.GetLevelExpression(level);

                    switch (levelExpression.NodeType)
                    {
                    case ExpressionType.MemberAccess:
                    {
                        if (levelExpression == expression)
                        {
                            var member = Tuple.Create(((MemberExpression)levelExpression).Member, flags);

                            SqlInfo[] idx;

                            if (!_memberIndex.TryGetValue(member, out idx))
                            {
                                idx = ConvertToSql(expression, level, flags);

                                if (flags == ConvertFlags.Field && idx.Length != 1)
                                {
                                    throw new InvalidOperationException();
                                }

                                foreach (var info in idx)
                                {
                                    SetInfo(info);
                                }

                                _memberIndex.Add(member, idx);
                            }

                            return(idx);
                        }

                        return(ProcessMemberAccess(
                                   expression,
                                   (MemberExpression)levelExpression,
                                   level,
                                   (n, ctx, ex, l, _) => n == 0 ?
                                   GetSequence(expression, level).ConvertToIndex(expression, level + 1, flags) :
                                   ctx.ConvertToIndex(ex, l, flags)));
                    }

                    case ExpressionType.Parameter:

                        if (levelExpression != expression)
                        {
                            return(GetSequence(expression, level).ConvertToIndex(expression, level + 1, flags));
                        }
                        break;
                    }

                    break;
                }
                }
            }

            throw new NotImplementedException();
        }
Esempio n. 40
0
			public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
			{
				if (expression != null && expression == _counterExpression)
					return _counterInfo ?? (_counterInfo = new[]
					{
						new SqlInfo
						{
							Query = CounterSql.ParentSql,
							Index = CounterSql.ParentSql.Select.Add(CounterSql),
							Sql   = CounterSql
						}
					});

				return base.ConvertToIndex(expression, level, flags);
			}