public override void Translate(
            MethodCallExpression m, TranslationState state, UniqueNameGenerator nameGenerator)
        {
            var arguments = state.ResultStack.Pop();
            var dbSelect  = (IDbSelect)state.ResultStack.Pop();

            var selections = SqlTranslationHelper.ProcessSelection(arguments, _dbFactory);

            foreach (var selectable in selections)
            {
                SqlTranslationHelper.UpdateJoinType(selectable.Ref);
                dbSelect.Selection.Add(selectable);
            }

            var newSelectRef = _dbFactory.BuildRef(dbSelect);
            var newSelect    = _dbFactory.BuildSelect(newSelectRef);

            newSelectRef.Alias = nameGenerator.GenerateAlias(dbSelect, TranslationConstants.SubSelectPrefix, true);

            selections = selections.Concat(dbSelect.Selection.Where(s => s.IsJoinKey)).ToArray();
            foreach (var selectable in selections)
            {
                var newSelectable = CreateNewSelectableForWrappingSelect(selectable, newSelectRef, _dbFactory);

                newSelect.Selection.Add(newSelectable);
            }

            state.ResultStack.Push(newSelect);
        }
Example #2
0
        public override void Translate(MethodCallExpression m, TranslationState state, UniqueNameGenerator nameGenerator)
        {
            MemberExpression member   = (m.Object ?? m.Arguments[0]) as MemberExpression;
            IDbObject        dbBinary = null;

            if (member.Type == typeof(string))
            {
                var dbConstants = (IDbConstant)state.ResultStack.Pop();
                dbConstants.Val = $"%{dbConstants.Val}%";

                var dbExpression = (IDbSelectable)state.ResultStack.Pop();
                dbBinary = _dbFactory.BuildBinary(dbExpression, DbOperator.Like, dbConstants);
            }
            else if (member.Type.IsArray)
            {
                var dbConstants = (IDbConstant)state.ResultStack.Pop();

                var dbExpression = (IDbSelectable)state.ResultStack.Pop();
                var dbFunc       = _dbFactory.BuildFunc(SqlTranslationHelper.GetSqlOperator(DbOperator.Any), false, m.Method.ReturnType, dbExpression);
                dbBinary = _dbFactory.BuildBinary(dbConstants, DbOperator.Equal, dbFunc);
            }
            else
            {
                throw new NotSupportedException($"Type {member.Type} is not supported");
            }

            state.ResultStack.Push(dbBinary);
        }
Example #3
0
        public override string ToString()
        {
            var left  = Left.ToString();
            var right = Right.ToString();
            var optr  = SqlTranslationHelper.GetSqlOperator(Operator);

            var result = $"{left} {optr} {right}";

            return(UseParentheses ? $"({result})" : result);
        }
Example #4
0
        public override void Translate(
            MethodCallExpression m, TranslationState state, UniqueNameGenerator nameGenerator)
        {
            // group by can be a column, a expression, or a list of columns / expressions
            var arguments = state.ResultStack.Pop();
            var dbSelect  = (IDbSelect)state.ResultStack.Pop();

            var groupBys = dbSelect.GroupBys;

            groupBys.IsSingleKey = !(arguments is IDbList <DbKeyValue>);

            var selections = SqlTranslationHelper.ProcessSelection(arguments, _dbFactory);

            foreach (var selectable in selections)
            {
                SqlTranslationHelper.UpdateJoinType(selectable.Ref);

                var refCol = selectable as IDbRefColumn;
                // if we group on a ref column, we are actaully group by on the primary key
                // of the entity that ref column referring to. In the refering entity is actually
                // another ref column, then we will need to get the primay key recursive from RefTo
                if (refCol?.RefTo != null)
                {
                    foreach (var pk in refCol.GetPrimaryKeys())
                    {
                        refCol.RefTo.AddToReferedSelect(_dbFactory, pk.Name, pk.ValType);
                    }
                }

                dbSelect.GroupBys.Add(selectable);
            }

            // if the group by is a single expression like groupby(x => x.Children.Count())
            // it will be translated into a expression and will not have alias
            // in this case, we will need to give it an alias which will be used later
            if (groupBys.IsSingleKey)
            {
                var column = groupBys.Single();
                if (column.Alias == null)
                {
                    column.Alias = "Key";
                }
            }

            state.ResultStack.Push(dbSelect);
        }
Example #5
0
        public override void Translate(MethodCallExpression m, TranslationState state, UniqueNameGenerator nameGenerator)
        {
            // group by can be a column, a expression, or a list of columns / expressions
            var arguments = state.ResultStack.Pop();
            var dbSelect  = (IDbSelect)state.ResultStack.Pop();

            var direction = m.Method.Name.EndsWith("Descending") ? DbOrderDirection.Desc : DbOrderDirection.Asc;

            var selections = SqlTranslationHelper.ProcessSelection(arguments, _dbFactory);

            foreach (var selectable in selections)
            {
                SqlTranslationHelper.UpdateJoinType(selectable.Ref);
                var orderByCol = _dbFactory.BuildOrderByColumn(selectable, direction);
                dbSelect.OrderBys.Add(orderByCol);
            }

            state.ResultStack.Push(dbSelect);
        }
 public virtual DbOperator GetDbOperator(ExpressionType eType, Type tl, Type tr)
 {
     return(SqlTranslationHelper.GetDbOperator(eType));
 }