public void Interpret( MethodCallExpression expression, SqlBuilderState state )
      {
        var primaryKeyExpression = expression.Arguments[ 1 ];
        if ( primaryKeyExpression is ConstantExpression )
        {
          // create a new expression to do the where clause
          var elementType = expression.Type;
          var tableInfo = GetTableInfo( elementType );
          
          // check that there is a primary key
          if ( tableInfo.PrimaryKeyFieldNames.Count == 0 )
            throw new NotSupportedException( string.Format( "Type {0} does not have any members with the [Key] attribute applied", elementType.Name ) );

          // can only deal with single element primary keys at the moment
          if ( tableInfo.PrimaryKeyFieldNames.Count > 1 )
            throw new NotImplementedException( string.Format( "Type {0} has more than one member with the [Key] attribute applied", elementType.Name ) );

          // add the parameter and where clause
          string parameterName = state.GetNextParameter();
          state.Parameters.Add( parameterName, ( primaryKeyExpression as ConstantExpression ).Value );
          state.Where.AppendFormat( "( [{0}] = @{1} )", tableInfo.PrimaryKeyFieldNames[0], parameterName );
        }
        else
          throw new NotSupportedException( "Unable to resolve value for RowAt()" );
      }
            public void Interpret(MethodCallExpression expression, SqlBuilderState state)
            {
                var primaryKeyExpression = expression.Arguments[1];

                if (primaryKeyExpression is ConstantExpression)
                {
                    // create a new expression to do the where clause
                    var elementType = expression.Type;
                    var tableInfo   = GetTableInfo(elementType);

                    // check that there is a primary key
                    if (tableInfo.PrimaryKeyFieldNames.Count == 0)
                    {
                        throw new NotSupportedException(string.Format("Type {0} does not have any members with the [Key] attribute applied", elementType.Name));
                    }

                    // can only deal with single element primary keys at the moment
                    if (tableInfo.PrimaryKeyFieldNames.Count > 1)
                    {
                        throw new NotImplementedException(string.Format("Type {0} has more than one member with the [Key] attribute applied", elementType.Name));
                    }

                    // add the parameter and where clause
                    string parameterName = state.GetNextParameter();
                    state.Parameters.Add(parameterName, (primaryKeyExpression as ConstantExpression).Value);
                    state.Where.AppendFormat("( [{0}] = @{1} )", tableInfo.PrimaryKeyFieldNames[0], parameterName);
                }
                else
                {
                    throw new NotSupportedException("Unable to resolve value for RowAt()");
                }
            }
Ejemplo n.º 3
0
            private void MethodCallExpression(MethodCallExpression body, SqlBuilderState state, bool inverse = false)
            {
                string negative = inverse ? "NOT " : string.Empty;

                if (body.Method == String_IsNullOrEmpty)
                {
                    string fieldName = GetFieldName((body.Arguments[0] as MemberExpression).Member);
                    state.Where.AppendFormat(" ( t.[{0}] IS {1}NULL ) ", fieldName, negative);
                }
                else if (body.Method == String_EndsWith || body.Method == String_EndsWithAndComparison)
                {
                    string     fieldName       = GetFieldName((body.Object as MemberExpression).Member);
                    Expression valueExpression = body.Arguments[0];

                    // if comparison is provide ensure it's one that SQL server will respect
                    if (body.Arguments.Count == 2)
                    {
                        CheckArgumentIsValue <StringComparison>(body.Arguments[1], StringComparison.OrdinalIgnoreCase, "EndsWith can only be used with StringComparison.OrdinalIgnoreCase");
                    }

                    if (valueExpression is ConstantExpression)
                    {
                        var constantValueExpression = valueExpression as ConstantExpression;
                        var parameter = state.GetNextParameter();
                        if (constantValueExpression.Type == typeof(string) && constantValueExpression.Value != null)
                        {
                            string value = (string)constantValueExpression.Value;
                            state.Parameters.Add(parameter, "%" + value);
                            state.Where.AppendFormat(" ( t.[{0}] {1}LIKE @{2} )", fieldName, negative, parameter);
                        }
                        else
                        {
                            throw new NotSupportedException();
                        }
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
                else if (body.Method == String_StartsWith || body.Method == String_StartsWithAndComparison)
                {
                    string     fieldName       = GetFieldName((body.Object as MemberExpression).Member);
                    Expression valueExpression = body.Arguments[0];

                    // if comparison is provide ensure it's one that SQL server will respect
                    if (body.Arguments.Count == 2)
                    {
                        CheckArgumentIsValue <StringComparison>(body.Arguments[1], StringComparison.OrdinalIgnoreCase, "StartsWith can only be used with StringComparison.OrdinalIgnoreCase");
                    }

                    if (valueExpression is ConstantExpression)
                    {
                        var constantValueExpression = valueExpression as ConstantExpression;
                        var parameter = state.GetNextParameter();
                        if (constantValueExpression.Type == typeof(string) && constantValueExpression.Value != null)
                        {
                            string value = (string)constantValueExpression.Value;
                            state.Parameters.Add(parameter, value + "%");
                            state.Where.AppendFormat(" ( t.[{0}] {1}LIKE @{2} )", fieldName, negative, parameter);
                        }
                        else
                        {
                            throw new NotSupportedException();
                        }
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
                else if (body.Method == String_Contains)
                {
                    string     fieldName       = GetFieldName((body.Object as MemberExpression).Member);
                    Expression valueExpression = body.Arguments[0];
                    if (valueExpression is ConstantExpression)
                    {
                        var constantValueExpression = valueExpression as ConstantExpression;
                        var parameter = state.GetNextParameter();
                        if (constantValueExpression.Type == typeof(string) && constantValueExpression.Value != null)
                        {
                            string value = (string)constantValueExpression.Value;
                            state.Parameters.Add(parameter, "%" + value + "%");
                            state.Where.AppendFormat(" ( t.[{0}] {1}LIKE @{2} )", fieldName, negative, parameter);
                        }
                        else
                        {
                            throw new NotSupportedException();
                        }
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
Ejemplo n.º 4
0
            private void BinaryExpression(BinaryExpression body, SqlBuilderState state, bool inverse = false)
            {
                var left  = body.Left;
                var right = body.Right;

                if (body.NodeType == ExpressionType.AndAlso || body.NodeType == ExpressionType.OrElse)
                {
                    if (inverse)
                    {
                        state.Where.Append("( NOT ");
                    }
                    state.Where.Append("( ");
                    ProcessExpression(left, state);
                    state.Where.Append(body.NodeType == ExpressionType.AndAlso ? " AND " : " OR ");
                    ProcessExpression(right, state);
                    state.Where.Append(" )");
                    if (inverse)
                    {
                        state.Where.Append(" )");
                    }
                }
                else
                {
                    // reverse where applicable
                    bool reversed = false;
                    if (left is ConstantExpression && right is MemberExpression)
                    {
                        reversed = true;
                        var swap = right;
                        right = left;
                        left  = swap;
                    }

                    if (right is ConstantExpression && left is MemberExpression)
                    {
                        string fieldName = GetFieldName((left as MemberExpression).Member);
                        object value     = (right as ConstantExpression).Value;
                        // caching this would lead to caching one or the other depending on whether the original value was NULL or not!
                        // could optimise out non-nullable values
                        if (value == null)
                        {
                            if (body.NodeType == ExpressionType.Equal || body.NodeType == ExpressionType.NotEqual)
                            {
                                bool equal = body.NodeType == ExpressionType.Equal;
                                if (inverse)
                                {
                                    equal = !equal;
                                }
                                state.Where.AppendFormat(" ( t.[{0}] {1} NULL ) ", fieldName, equal ? "IS" : "IS NOT");
                                return;
                            }
                            else
                            {
                                throw new NotSupportedException();
                            }
                        }
                        else
                        {
                            Operator op;
                            if (!_comparisonOperators.TryGetValue(body.NodeType, out op))
                            {
                                throw new NotSupportedException();
                            }

                            string parameterName = state.GetNextParameter();
                            if (!reversed)
                            {
                                state.Where.AppendFormat(" ( t.[{0}] {1} @{2} ) ", fieldName, op.Value(inverse), parameterName);
                            }
                            else
                            {
                                state.Where.AppendFormat(" ( @{2} {1} t.[{0}] ) ", fieldName, op.Value(inverse), parameterName);
                            }

                            state.Parameters.Add(parameterName, value);
                        }
                    }
                    else if (left is MemberExpression && right is MemberExpression)
                    {
                        Operator op;
                        if (!_comparisonOperators.TryGetValue(body.NodeType, out op))
                        {
                            throw new NotSupportedException();
                        }

                        if (!reversed)
                        {
                            state.Where.AppendFormat(" ( t.[{0}] {1} t.[{2}] ) ", GetFieldName((left as MemberExpression).Member), op.Value(inverse), GetFieldName((right as MemberExpression).Member));
                        }
                        else
                        {
                            state.Where.AppendFormat(" ( t.[{2}] {1} t.[{0}] ) ", GetFieldName((left as MemberExpression).Member), op.Value(inverse), GetFieldName((right as MemberExpression).Member));
                        }
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
            }