Exemplo n.º 1
0
 public static DbFunctionExpression GetReparentedValue(this DbExpression hierarchyIdValue, DbExpression oldRoot, DbExpression newRoot)
 {
     Check.NotNull(hierarchyIdValue, "hierarchyIdValue");
     Check.NotNull(oldRoot, "oldRoot");
     Check.NotNull(newRoot, "newRoot");
     return(EdmFunctions.InvokeCanonicalFunction("GetReparentedValue", hierarchyIdValue, oldRoot, newRoot));
 }
Exemplo n.º 2
0
        public static IEdmModel GetEdmModel()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            #region ENTITY SETs

            builder.EntitySet <Agenda>("Agenda");
            builder.EntitySet <Cep>("Cep");
            builder.EntitySet <Produto>("Produtos");
            builder.EntitySet <Loja>("Lojas");
            builder.EntitySet <Cliente>("Clientes");
            builder.EntitySet <ContaBancaria>("ContaBancaria");
            builder.EntitySet <ContaPagar>("ContaPagar");
            builder.EntitySet <ContaReceber>("ContaReceber");
            builder.EntitySet <Despesa>("Despesas");
            builder.EntitySet <DocumentoEntrada>("DocumentoEntrada");
            builder.EntitySet <DocumentoEntradaItem>("DocumentoEntradaItem");
            builder.EntitySet <Fornecedor>("Fornecedores");
            builder.EntitySet <Funcionario>("Funcionarios");
            builder.EntitySet <Historico>("Historicos");
            builder.EntitySet <Marca>("Marcas");
            builder.EntitySet <MovimentoEstoque>("MovimentoEstoque");
            builder.EntitySet <Operadora>("Operadoras");
            builder.EntitySet <PedidoVenda>("PedidoVenda");
            builder.EntitySet <PedidoVendaItem>("PedidoVendaItem");
            builder.EntitySet <PreVenda>("PreVenda");
            builder.EntitySet <PreVendaItem>("PreVendaItem");
            builder.EntitySet <Produto>("Produtos");
            builder.EntitySet <RecebimentoConta>("RecebimentoConta");
            builder.EntitySet <SaldoEstoque>("SaldoEstoque");
            builder.EntitySet <Secao>("Secao");
            builder.EntitySet <Tabela>("Tabelas");
            builder.EntitySet <Tipo>("Tipos");
            builder.EntitySet <Usuario>("Usuarios");
            builder.EntitySet <Venda>("Vendas");
            builder.EntitySet <Vendedor>("Vendedores");
            builder.EntitySet <ContaCorrente>("ContaCorrente");
            builder.EntitySet <Parametro>("Parametros");
            builder.EntitySet <EntradaAntecipada>("EntradaAntecipada");
            builder.EntitySet <Cheque>("Cheques");
            builder.EntitySet <MovimentoCaixa>("MovimentoCaixa");
            builder.EntitySet <Cancelada>("Canceladas");
            builder.EntitySet <ContaReceberTemp>("ContaReceberTemp");
            builder.EntitySet <ContaPagarTemp>("ContaPagarTemp");
            builder.EntitySet <HistoricoCliente>("HistoricoCliente");
            builder.EntitySet <CondicaoPagamento>("CondicaoPagamento");
            builder.EntitySet <Relatorios>("Relatorios");
            builder.EntitySet <Pergunta>("Perguntas");
            builder.EntitySet <FechamentoCaixa>("FechamentoCaixa");
            builder.EntitySet <Modulo>("Modulos");
            builder.EntitySet <ModuloAplicativo>("ModuloAplicativos");
            builder.EntitySet <Aplicativo>("Aplicativos");
            builder.EntitySet <UsuarioFuncao>("UsuarioFuncoes");

            #endregion

            EdmFunctions.BuildFunctions(builder);

            return(builder.GetEdmModel());
        }
Exemplo n.º 3
0
 /// <summary>
 ///     Creates a <see cref="DbFunctionExpression" /> that invokes the canonical 'GetDescendant' function with the
 ///     specified argument, which must have a HierarchyId result type.
 ///     The result type of the expression is Edm.HierarchyId.
 /// </summary>
 /// <param name="hierarchyIdValue"> An expression that specifies the hierarchyid value. </param>
 /// <param name="child1"> An expression that provides a hierarchyid value. </param>
 /// <param name="child2"> An expression that provides a hierarchyid value. </param>
 /// <returns> A new DbFunctionExpression that returns a hierarchyid. </returns>
 /// <exception cref="ArgumentNullException">
 ///     <paramref name="hierarchyIdValue" />
 ///     or
 ///     <paramref name="child1" />
 ///     or
 ///     <paramref name="child2" />
 ///     is null.
 /// </exception>
 /// <exception cref="ArgumentException">
 ///     No overload of the canonical 'GetDescendant' function accept an argument with the result type of
 ///     <paramref name="child1" />
 ///     and
 ///     <paramref name="child2" />
 ///     .
 /// </exception>
 public static DbFunctionExpression GetDescendant(this DbExpression hierarchyIdValue, DbExpression child1, DbExpression child2)
 {
     Check.NotNull(hierarchyIdValue, "hierarchyIdValue");
     Check.NotNull(child1, "child1");
     Check.NotNull(child2, "child2");
     return(EdmFunctions.InvokeCanonicalFunction("GetDescendant", hierarchyIdValue, child1, child2));
 }
        private static string BuildSqlForDateTimeOffset(DateTimeOffset dateTimeOffset, SqlVersion sqlVersion)
        {
            var builder = new StringBuilder();

            var sqlGenerator = new SqlGenerator(sqlVersion);

            var functionExpression = EdmFunctions.CreateDateTimeOffset(
                DbExpression.FromInt32(dateTimeOffset.Year),
                DbExpression.FromInt32(dateTimeOffset.Month),
                DbExpression.FromInt32(dateTimeOffset.Day),
                DbExpression.FromInt32(dateTimeOffset.Hour),
                DbExpression.FromInt32(dateTimeOffset.Minute),
                DbExpression.FromInt32(dateTimeOffset.Second),
                DbExpression.FromInt32(Convert.ToInt32(dateTimeOffset.Offset.TotalMinutes)));

            var sqlFragment = SqlFunctionCallHandler.GenerateFunctionCallSql(
                sqlGenerator, functionExpression);

            using (var sqlWriter = new SqlWriter(builder))
            {
                sqlFragment.WriteSql(sqlWriter, sqlGenerator);
            }

            return(builder.ToString());
        }
Exemplo n.º 5
0
            public override DbExpression Visit(DbNewInstanceExpression expression)
            {
                var arguments = expression.Arguments.Select(a =>
                {
                    var propertyArg = a as DbPropertyExpression;
                    if (propertyArg != null && _typesToTrim.Contains(propertyArg.Property.TypeUsage.EdmType.Name))
                    {
                        return(EdmFunctions.Trim(a));
                    }

                    return(a);
                });

                var temp = new List <DbExpression>();

                foreach (var a in expression.Arguments)
                {
                    var propertyArg = a as DbPropertyExpression;
                    if (propertyArg != null && _typesToTrim.Contains(propertyArg.Property.TypeUsage.EdmType.Name))
                    {
                        temp.Add(EdmFunctions.Trim(a));
                    }
                    else
                    {
                        temp.Add(a);
                    }
                }

                return(DbExpressionBuilder.New(expression.ResultType, temp));
            }
        public override DbExpression Visit(DbNewInstanceExpression expression)
        {
            var argments = expression.Arguments.Select(t =>
            {
                if (t is DbPropertyExpression propertyArg && _typeToTrim.Contains(propertyArg.Property.TypeUsage.EdmType.Name))
                {
                    return(EdmFunctions.Trim(t));
                }
                return(t);
            });

            return(DbExpressionBuilder.New(expression.ResultType, argments));
        }
            public override DbExpression Visit(DbNewInstanceExpression expression)
            {
                var arguments = expression.Arguments.Select(a =>
                {
                    var propertyArg = a as DbPropertyExpression;
                    if (propertyArg != null && _typesToTrim.Contains(propertyArg.Property.TypeUsage.EdmType.Name))
                    {
                        return(EdmFunctions.Trim(a));
                    }

                    return(a);
                });

                return(DbExpressionBuilder.New(expression.ResultType, arguments));
            }
        private static string BuildSqlForTruncateTime(DateTimeOffset dateTimeOffset, SqlVersion sqlVersion)
        {
            var builder = new StringBuilder();

            var sqlGenerator = new SqlGenerator(sqlVersion);

            var functionExpression = EdmFunctions.TruncateTime(
                DbExpression.FromDateTimeOffset(dateTimeOffset));

            var sqlFragment = SqlFunctionCallHandler.GenerateFunctionCallSql(
                sqlGenerator, functionExpression);

            using (var sqlWriter = new SqlWriter(builder))
            {
                sqlFragment.WriteSql(sqlWriter, sqlGenerator);
            }

            return(builder.ToString());
        }
        public static IEdmModel GetEdmModel()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet <Produto>("Produtos");
            builder.EntitySet <Loja>("Lojas");
            builder.EntitySet <Cliente>("Clientes");
            builder.EntitySet <PedidoVenda>("PedidoVenda");
            builder.EntitySet <PedidoVendaItem>("PedidoVendaItem");
            builder.EntitySet <PreVenda>("PreVenda");
            builder.EntitySet <PreVendaItem>("PreVendaItem");
            builder.EntitySet <Produto>("Produtos");
            builder.EntitySet <Vendedor>("Vendedores");
            builder.EntitySet <ContaReceberTemp>("ContaReceberTemp");
            builder.EntitySet <Operadora>("Operadoras");

            EdmFunctions.BuildFunctions(builder);

            return(builder.GetEdmModel());
        }
        protected override Expression VisitBinary(BinaryExpression node)
        {
#if (DEBUG_VISITS)
            System.Diagnostics.Debug.Print("VisitBinary: {0}", node);
#endif

            var expression = base.VisitBinary(node) as BinaryExpression;

            DbExpression dbExpression;

            //  Need special handling for comparisons against the null constant.  If we don't translate these
            //  using an "IsNull" expression, EF will convert it literally as "= null" which doesn't work in SQL Server.
            if (IsNullConstantExpression(expression.Right))
            {
                dbExpression = MapNullComparison(expression.Left, expression.NodeType);
            }
            else if (IsNullConstantExpression(expression.Left))
            {
                dbExpression = MapNullComparison(expression.Right, expression.NodeType);
            }
            else
            {
                DbExpression leftExpression  = GetDbExpressionForExpression(expression.Left);
                DbExpression rightExpression = GetDbExpressionForExpression(expression.Right);

                switch (expression.NodeType)
                {
                case ExpressionType.Equal:
                    //  DbPropertyExpression = class property that has been mapped to a database column
                    //  DbParameterReferenceExpression = lambda parameter
                    if (IsNullableExpressionOfType <DbPropertyExpression>(leftExpression) && IsNullableExpressionOfType <DbParameterReferenceExpression>(rightExpression))
                    {
                        dbExpression = CreateEqualComparisonOfNullablePropToNullableParam(leftExpression, rightExpression);
                    }
                    else if (IsNullableExpressionOfType <DbPropertyExpression>(rightExpression) && IsNullableExpressionOfType <DbParameterReferenceExpression>(leftExpression))
                    {
                        dbExpression = CreateEqualComparisonOfNullablePropToNullableParam(rightExpression, leftExpression);
                    }
                    else
                    {
                        dbExpression = DbExpressionBuilder.Equal(leftExpression, rightExpression);
                    }
                    break;

                case ExpressionType.NotEqual:
                    dbExpression = DbExpressionBuilder.NotEqual(leftExpression, rightExpression);
                    break;

                case ExpressionType.GreaterThan:
                    dbExpression = DbExpressionBuilder.GreaterThan(leftExpression, rightExpression);
                    break;

                case ExpressionType.GreaterThanOrEqual:
                    dbExpression = DbExpressionBuilder.GreaterThanOrEqual(leftExpression, rightExpression);
                    break;

                case ExpressionType.LessThan:
                    dbExpression = DbExpressionBuilder.LessThan(leftExpression, rightExpression);
                    break;

                case ExpressionType.LessThanOrEqual:
                    dbExpression = DbExpressionBuilder.LessThanOrEqual(leftExpression, rightExpression);
                    break;

                case ExpressionType.AndAlso:
                    dbExpression = DbExpressionBuilder.And(leftExpression, rightExpression);
                    break;

                case ExpressionType.OrElse:
                    dbExpression = DbExpressionBuilder.Or(leftExpression, rightExpression);
                    break;

                case ExpressionType.And:
                    dbExpression = EdmFunctions.BitwiseAnd(leftExpression, rightExpression);
                    break;

                case ExpressionType.Or:
                    dbExpression = EdmFunctions.BitwiseOr(leftExpression, rightExpression);
                    break;

                case ExpressionType.ExclusiveOr:
                    dbExpression = EdmFunctions.BitwiseXor(leftExpression, rightExpression);
                    break;

                default:
                    throw new NotImplementedException(string.Format("Unhandled NodeType of {0} in LambdaToDbExpressionVisitor.VisitBinary", expression.NodeType));
                }
            }

            MapExpressionToDbExpression(expression, dbExpression);

            return(expression);
        }
Exemplo n.º 11
0
 public static DbFunctionExpression GetAncestor(this DbExpression hierarchyIdValue, DbExpression n)
 {
     Check.NotNull(hierarchyIdValue, "hierarchyIdValue");
     Check.NotNull(n, "n");
     return(EdmFunctions.InvokeCanonicalFunction("GetAncestor", hierarchyIdValue, n));
 }
Exemplo n.º 12
0
 public static DbFunctionExpression HierarchyIdGetRoot()
 {
     return(EdmFunctions.InvokeCanonicalFunction("HierarchyIdGetRoot"));
 }
Exemplo n.º 13
0
        // HierarchyId ‘Static’ Functions

        /// <summary>
        ///     Creates a <see cref="DbFunctionExpression" /> that invokes the canonical 'HierarchyIdParse' function with the
        ///     specified argument, which must have a string result type.
        ///     The result type of the expression is Edm.HierarchyId.
        /// </summary>
        /// <param name="input"> An expression that provides the canonical representation of the hierarchyid value. </param>
        /// <returns> A new DbFunctionExpression that returns a new hierarchyid value based on the specified value. </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="input" />
        ///     is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     No overload of the canonical 'HierarchyIdParse' function accept an argument with the result type of
        ///     <paramref name="input" />
        ///     .
        /// </exception>
        public static DbFunctionExpression HierarchyIdParse(DbExpression input)
        {
            Check.NotNull(input, "input");
            return(EdmFunctions.InvokeCanonicalFunction("HierarchyIdParse", input));
        }
Exemplo n.º 14
0
 /// <summary>
 ///     Creates a <see cref="DbFunctionExpression" /> that invokes the canonical 'IsDescendantOf' function with the
 ///     specified argument, which must have a HierarchyId result type.
 ///     The result type of the expression is Int32.
 /// </summary>
 /// <param name="hierarchyIdValue"> An expression that specifies the hierarchyid value. </param>
 /// <param name="parent"> An expression that provides a hierarchyid value. </param>
 /// <returns> A new DbFunctionExpression that returns an integer value. </returns>
 /// <exception cref="ArgumentNullException">
 ///     <paramref name="hierarchyIdValue" />
 ///     or
 ///     <paramref name="parent" />
 ///     is null.
 /// </exception>
 /// <exception cref="ArgumentException">
 ///     No overload of the canonical 'IsDescendantOf' function accept an argument with the result type of
 ///     <paramref name="parent" />
 ///     .
 /// </exception>
 public static DbFunctionExpression IsDescendantOf(this DbExpression hierarchyIdValue, DbExpression parent)
 {
     Check.NotNull(hierarchyIdValue, "hierarchyIdValue");
     Check.NotNull(parent, "parent");
     return(EdmFunctions.InvokeCanonicalFunction("IsDescendantOf", hierarchyIdValue, parent));
 }
Exemplo n.º 15
0
 /// <summary>
 ///     Creates a <see cref="DbFunctionExpression" /> that invokes the canonical 'GetLevel' function.
 ///     The result type of the expression is Int32.
 /// </summary>
 /// <param name="hierarchyIdValue"> An expression that specifies the hierarchyid value. </param>
 /// <returns> A new DbFunctionExpression that returns the level of the given hierarchyid. </returns>
 /// <exception cref="ArgumentNullException">
 ///     <paramref name="hierarchyIdValue" />
 ///     is null.
 /// </exception>
 public static DbFunctionExpression GetLevel(this DbExpression hierarchyIdValue)
 {
     Check.NotNull(hierarchyIdValue, "hierarchyIdValue");
     return(EdmFunctions.InvokeCanonicalFunction("GetLevel", hierarchyIdValue));
 }
Exemplo n.º 16
0
        public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
        {
            if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace)
            {
                var insertCommand = interceptionContext.Result as DbInsertCommandTree;
                if (insertCommand != null)
                {
                    List <DbModificationClause> finalSetClauses =
                        new List <DbModificationClause>(
                            (IEnumerable <DbModificationClause>)insertCommand.SetClauses.Select(
                                a =>
                    {
                        var dbSetClause = a as DbSetClause;
                        if (dbSetClause != null)
                        {
                            var dbPropertyExpression = dbSetClause.Property as DbPropertyExpression;

                            if (dbPropertyExpression != null)
                            {
                                var edmProperty = dbPropertyExpression.Property as EdmProperty;
                                if (edmProperty != null && edmProperty.MaxLength != null &&
                                    _typesToTrim.Contains(edmProperty.TypeName))
                                {
                                    var dbConstantExpression = dbSetClause.Value as DbConstantExpression;
                                    if (dbConstantExpression != null && dbConstantExpression.Value != null)
                                    {
                                        var value = dbConstantExpression.Value.ToString();
                                        if (!string.IsNullOrEmpty(value) &&
                                            value.Length > (int)edmProperty.MaxLength)
                                        {
                                            return(DbExpressionBuilder.SetClause(
                                                       DbExpressionBuilder.Property(
                                                           DbExpressionBuilder.Variable(
                                                               insertCommand.Target.VariableType,
                                                               insertCommand.Target.VariableName),
                                                           dbPropertyExpression.Property.Name),
                                                       EdmFunctions.Trim(
                                                           dbConstantExpression.Value.ToString()
                                                           .Substring(0, (int)edmProperty.MaxLength))));
                                        }
                                    }
                                }
                            }
                        }

                        return(a);
                    }));

                    var newInsertCommand = new DbInsertCommandTree(
                        insertCommand.MetadataWorkspace,
                        insertCommand.DataSpace,
                        insertCommand.Target,
                        new ReadOnlyCollection <DbModificationClause>(finalSetClauses),
                        insertCommand.Returning);

                    interceptionContext.Result = newInsertCommand;
                }
            }

            var updateCommand = interceptionContext.Result as DbUpdateCommandTree;

            if (updateCommand != null)
            {
                List <DbModificationClause> finalSetClauses =
                    new List <DbModificationClause>(
                        (IEnumerable <DbModificationClause>)updateCommand.SetClauses.Select(
                            a =>
                {
                    var dbSetClause = a as DbSetClause;
                    if (dbSetClause != null)
                    {
                        var dbPropertyExpression = dbSetClause.Property as DbPropertyExpression;

                        if (dbPropertyExpression != null)
                        {
                            var edmProperty = dbPropertyExpression.Property as EdmProperty;
                            if (edmProperty != null && edmProperty.MaxLength != null &&
                                _typesToTrim.Contains(edmProperty.TypeName))
                            {
                                var dbConstantExpression = dbSetClause.Value as DbConstantExpression;
                                if (dbConstantExpression != null && dbConstantExpression.Value != null)
                                {
                                    var value = dbConstantExpression.Value.ToString();
                                    if (!string.IsNullOrEmpty(value) &&
                                        value.Length > (int)edmProperty.MaxLength)
                                    {
                                        return(DbExpressionBuilder.SetClause(
                                                   DbExpressionBuilder.Property(
                                                       DbExpressionBuilder.Variable(
                                                           updateCommand.Target.VariableType,
                                                           updateCommand.Target.VariableName),
                                                       dbPropertyExpression.Property.Name),
                                                   EdmFunctions.Trim(
                                                       dbConstantExpression.Value.ToString()
                                                       .Substring(0, (int)edmProperty.MaxLength))));
                                    }
                                }
                            }
                        }
                    }

                    return(a);
                }));

                var newInsertCommand = new DbUpdateCommandTree(
                    updateCommand.MetadataWorkspace,
                    updateCommand.DataSpace,
                    updateCommand.Target,
                    updateCommand.Predicate,
                    new ReadOnlyCollection <DbModificationClause>(finalSetClauses),
                    updateCommand.Returning);

                interceptionContext.Result = newInsertCommand;
            }
        }