Ejemplo n.º 1
0
        protected override Expression VisitConstantExpression(ConstantExpression expression)
        {
            // Determine the effective expression type (removing Nullable<T> wrapper)
            var expressionType = expression.Type;

            if (expression.Type.IsGenericType && expression.Type.GetGenericTypeDefinition().Equals(typeof(System.Nullable <>)))
            {
                expressionType = expression.Type.GetGenericArguments()[0];
            }
            if (typeof(String).IsAssignableFrom(expressionType))
            {
                _filterExpressionBuilder.Append(MakeSparqlStringConstant(_regexEscaping ? Regex.Escape(expression.Value as String) : expression.Value as string));
                var dt = GetDatatype(typeof(string));
                if (!String.IsNullOrEmpty(dt))
                {
                    _filterExpressionBuilder.AppendFormat("^^<{0}>", dt);
                }
                return(expression);
            }
            if (NumericTypes.Contains(expressionType))
            {
                _filterExpressionBuilder.Append(MakeSparqlStringConstant(MakeSparqlNumericConstant(expression.Value)));
                var dt = GetDatatype(expressionType);
                if (!String.IsNullOrEmpty(dt))
                {
                    _filterExpressionBuilder.AppendFormat("^^<{0}>", dt);
                }
                return(expression);
            }
            if (expressionType.Equals(typeof(bool)))
            {
                _filterExpressionBuilder.Append(((bool)expression.Value) ? "true" : "false");

                /*var dt = GetDatatype(typeof (bool));
                 * if (!String.IsNullOrEmpty(dt))
                 * {
                 *  _filterExpressionBuilder.AppendFormat("^^<{0}>", dt);
                 * }
                 */
                return(expression);
            }
            if (expressionType.Equals(typeof(DateTime)))
            {
                _filterExpressionBuilder.Append(MakeSparqlStringConstant(((DateTime)expression.Value).ToString("O")));
                var dt = GetDatatype(typeof(DateTime));
                if (!String.IsNullOrEmpty(dt))
                {
                    _filterExpressionBuilder.AppendFormat("^^<{0}>", dt);
                }
                return(expression);
            }
            if (expressionType == typeof(PlainLiteral))
            {
                var pl = expression.Value as PlainLiteral;
                _filterExpressionBuilder.Append(MakeSparqlStringConstant(pl.Value));
                if (!pl.Language.Equals(String.Empty))
                {
                    _filterExpressionBuilder.AppendFormat("@{0}", pl.Language);
                }
                return(expression);
            }
            if (typeof(IEnumerable).IsAssignableFrom(expression.Type))
            {
                var  enumerable = expression.Value as IEnumerable;
                bool isFirst    = true;
                foreach (var o in enumerable)
                {
                    if (o != null)
                    {
                        if (!isFirst)
                        {
                            _filterExpressionBuilder.Append(",");
                        }
                        else
                        {
                            isFirst = false;
                        }
                        _filterExpressionBuilder.Append(MakeSparqlConstant(o));
                        var dt = GetDatatype(typeof(string));
                        if (!String.IsNullOrEmpty(dt))
                        {
                            _filterExpressionBuilder.AppendFormat("^^<{0}>", dt);
                        }
                    }
                }
                return(expression);
            }
            if (typeof(IEntityObject).IsAssignableFrom(expression.Type) ||
                EntityMappingStore.IsKnownInterface(expression.Type))
            {
                var obj     = expression.Value as IEntityObject;
                var address = QueryBuilder.Context.GetResourceAddress(obj);
                _filterExpressionBuilder.AppendFormat("<{0}>", address);
                return(expression);
            }
            return(base.VisitConstantExpression(expression));
        }
Ejemplo n.º 2
0
        protected override Expression VisitMethodCallExpression(MethodCallExpression expression)
        {
            if (expression.Method.Name.Equals("Equals"))
            {
                if (EntityMappingStore.IsKnownInterface(expression.Object.Type))
                {
                    // Query is testing equality on a property that maps to an entity type
                    if (expression.Arguments[0].NodeType == ExpressionType.Constant)
                    {
                        _filterExpressionBuilder.Append("sameterm(");
                        VisitExpression(expression.Object);
                        _filterExpressionBuilder.Append(",");
                        VisitExpression(expression.Arguments[0]);
                        _filterExpressionBuilder.Append(")");
                        return(expression);
                    }
                }
                if (expression.Object.NodeType == ExpressionType.MemberAccess)
                {
                    var ma = expression.Object as MemberExpression;
                    if (ma.Member is PropertyInfo)
                    {
                        var targetProperty = ma.Member as PropertyInfo;
                        var propertyHint   = QueryBuilder.Context.GetPropertyHint(targetProperty);
                        if (propertyHint != null && propertyHint.MappingType == PropertyMappingType.Id)
                        {
                            if (expression.Arguments[0].NodeType == ExpressionType.Constant)
                            {
                                var constant = expression.Arguments[0] as ConstantExpression;
                                _filterExpressionBuilder.Append("sameterm(");
                                VisitExpression(expression.Object);
                                _filterExpressionBuilder.Append(",");
                                _filterExpressionBuilder.AppendFormat(
                                    "<{0}>",
                                    QueryBuilder.Context.MapIdToUri(targetProperty, constant.Value.ToString()));
                                _filterExpressionBuilder.Append(")");
                                return(expression);
                            }
                            throw new NotSupportedException("Entity ID properties can only be compared to constant values");
                        }
                        // Otherwise fall through to default handling
                    }
                }
                _filterExpressionBuilder.Append('(');
                VisitExpression(expression.Object);
                _filterExpressionBuilder.Append("=");
                VisitExpression(expression.Arguments[0]);
                _filterExpressionBuilder.Append(')');
                return(expression);
            }
            if (expression.Object != null && expression.Object.Type == typeof(String))
            {
                if (expression.Method.Name.Equals("StartsWith"))
                {
                    if (expression.Arguments[0].NodeType == ExpressionType.Constant)
                    {
                        var constantExpression = expression.Arguments[0] as ConstantExpression;
                        if (expression.Arguments.Count == 1)
                        {
                            WriteFunction("STRSTARTS", expression.Object, expression.Arguments[0]);
                        }
                        else
                        {
                            var flags = SparqlGeneratorWhereExpressionTreeVisitor.GetStringComparisonFlags(expression.Arguments[1]);
                            WriteRegexFilter(expression.Object,
                                             "^" + Regex.Escape(constantExpression.Value.ToString()),
                                             flags);
                            return(expression);
                        }
                        return(expression);
                    }
                }
                if (expression.Method.Name.Equals("EndsWith"))
                {
                    if (expression.Arguments[0].NodeType == ExpressionType.Constant)
                    {
                        var constantExpression = expression.Arguments[0] as ConstantExpression;
                        if (expression.Arguments.Count == 1)
                        {
                            WriteFunction("STRENDS", expression.Object, expression.Arguments[0]);
                        }
                        if (expression.Arguments.Count > 1 && expression.Arguments[1] is ConstantExpression)
                        {
                            string flags = SparqlGeneratorWhereExpressionTreeVisitor.GetStringComparisonFlags(expression.Arguments[1]);
                            WriteRegexFilter(expression.Object,
                                             Regex.Escape(constantExpression.Value.ToString()) + "$",
                                             flags);
                        }
                        return(expression);
                    }
                }
                if (expression.Method.Name.Equals("Contains"))
                {
                    var seekValue = expression.Arguments[0] as ConstantExpression;
                    if (seekValue != null)
                    {
                        if (expression.Arguments.Count == 1)
                        {
                            WriteFunction("CONTAINS", expression.Object, seekValue);
                        }
                        else if (expression.Arguments[1] is ConstantExpression)
                        {
                            var flags = SparqlGeneratorWhereExpressionTreeVisitor.GetStringComparisonFlags(expression.Arguments[1]);
                            WriteRegexFilter(expression.Object, Regex.Escape(seekValue.Value.ToString()),
                                             flags);
                        }
                        return(expression);
                    }
                }
                if (expression.Method.Name.Equals("Substring"))
                {
                    Expression start;
                    if (expression.Arguments[0] is ConstantExpression &&
                        expression.Arguments[0].Type == typeof(int))
                    {
                        start = Expression.Constant(((int)(expression.Arguments[0] as ConstantExpression).Value) + 1);
                    }
                    else
                    {
                        start = Expression.Add(expression.Arguments[0], Expression.Constant(1));
                    }
                    if (expression.Arguments.Count == 1)
                    {
                        WriteFunction("SUBSTR", expression.Object, start);
                        return(expression);
                    }
                    if (expression.Arguments.Count == 2)
                    {
                        WriteFunction("SUBSTR", expression.Object, start, expression.Arguments[1]);
                        return(expression);
                    }
                }
                if (expression.Method.Name.Equals("ToUpper"))
                {
                    WriteFunction("UCASE", expression.Object);
                    return(expression);
                }
                if (expression.Method.Name.Equals("ToLower"))
                {
                    WriteFunction("LCASE", expression.Object);
                    return(expression);
                }
                if (expression.Method.Name.Equals("Replace"))
                {
                    _regexEscaping = true;
                    WriteFunction("REPLACE", expression.Object, expression.Arguments[0], expression.Arguments[1]);
                    _regexEscaping = false;
                    return(expression);
                }
            }
            if (expression.Object == null)
            {
                // Static method
                if (expression.Method.DeclaringType.Equals(typeof(Regex)))
                {
                    if (expression.Method.Name.Equals("IsMatch"))
                    {
                        var sourceExpression = expression.Arguments[0];
                        var regexExpression  = expression.Arguments[1] as ConstantExpression;
                        var flagsExpression  = expression.Arguments.Count > 2
                                                  ? expression.Arguments[2] as ConstantExpression
                                                  : null;
                        if (regexExpression != null)
                        {
                            var    regex = regexExpression.Value.ToString();
                            string flags = String.Empty;
                            if (flagsExpression != null && flagsExpression.Type == typeof(RegexOptions))
                            {
                                var regexOptions = (RegexOptions)flagsExpression.Value;
                                if ((regexOptions & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase)
                                {
                                    flags += "i";
                                }
                                if ((regexOptions & RegexOptions.Multiline) == RegexOptions.Multiline)
                                {
                                    flags += "m";
                                }
                                if ((regexOptions & RegexOptions.Singleline) == RegexOptions.Singleline)
                                {
                                    flags += "s";
                                }
                                if ((regexOptions & RegexOptions.IgnorePatternWhitespace) == RegexOptions.IgnorePatternWhitespace)
                                {
                                    flags += "x";
                                }
                            }
                            WriteRegexFilter(sourceExpression, regex, String.Empty.Equals(flags) ? null : flags);
                            return(expression);
                        }
                    }
                }
                if (typeof(String).Equals(expression.Method.DeclaringType))
                {
                    if (expression.Method.Name.Equals("Concat"))
                    {
                        WriteFunction("CONCAT", expression.Arguments.ToArray());
                        return(expression);
                    }
                }
                if (typeof(Math).Equals(expression.Method.DeclaringType))
                {
                    string fnName = null;
                    switch (expression.Method.Name)
                    {
                    case "Round":
                        fnName = "ROUND";
                        break;

                    case "Floor":
                        fnName = "FLOOR";
                        break;

                    case "Ceiling":
                        fnName = "CEIL";
                        break;
                    }
                    if (fnName != null)
                    {
                        WriteFunction(fnName, expression.Arguments[0]);
                        return(expression);
                    }
                }
            }
            return(base.VisitMethodCallExpression(expression));
        }
        public string MakeSparqlConstant(object value, bool regexEscaping)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            // Determine the effective expression type (removing Nullable<T> wrapper)
            var expressionType = value.GetType();

            if (expressionType.IsGenericType && expressionType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                expressionType = expressionType.GetGenericArguments()[0];
            }
            if (typeof(string).IsAssignableFrom(expressionType))
            {
                var strValue = value as string;
                if (strValue != null)
                {
                    var dt  = Context.GetDatatype(typeof(string));
                    var ret = MakeSparqlStringConstant(regexEscaping ? Regex.Escape(strValue) : strValue);
                    if (!string.IsNullOrEmpty(dt))
                    {
                        ret += string.Format("^^<{0}>", dt);
                    }
                    return(ret);
                }
            }
            if (NumericTypes.Contains(expressionType))
            {
                var ret = MakeSparqlStringConstant(MakeSparqlNumericConstant(value));
                var dt  = Context.GetDatatype(expressionType);
                if (!string.IsNullOrEmpty(dt))
                {
                    ret += string.Format("^^<{0}>", dt);
                }
                return(ret);
            }
            if (expressionType == typeof(bool))
            {
                return(((bool)value) ? "true" : "false");
            }
            if (expressionType == typeof(DateTime))
            {
                var ret = MakeSparqlStringConstant(((DateTime)value).ToString("O"));
                var dt  = Context.GetDatatype(typeof(DateTime));
                if (!String.IsNullOrEmpty(dt))
                {
                    ret += String.Format("^^<{0}>", dt);
                }
                return(ret);
            }
            if (expressionType == typeof(Guid))
            {
                var ret = MakeSparqlStringConstant(((Guid)value).ToString("D"));
                var dt  = Context.GetDatatype(typeof(Guid));
                if (!string.IsNullOrEmpty(dt))
                {
                    ret += String.Format("^^<{0}>", dt);
                }
                return(ret);
            }
            if (expressionType == typeof(PlainLiteral))
            {
                var pl  = value as PlainLiteral;
                var ret = MakeSparqlStringConstant(pl.Value);
                if (!pl.Language.Equals(string.Empty))
                {
                    ret += String.Format("@{0}", pl.Language);
                }
                return(ret);
            }
            if (typeof(IEnumerable).IsAssignableFrom(expressionType))
            {
                var ret        = new StringBuilder();
                var enumerable = value as IEnumerable;
                var isFirst    = true;
                foreach (var o in enumerable)
                {
                    if (o != null)
                    {
                        if (!isFirst)
                        {
                            ret.Append(",");
                        }
                        else
                        {
                            isFirst = false;
                        }
                        ret.Append(MakeSparqlConstant(o, regexEscaping));
                        //var dt = GetDatatype(typeof (string));
                        //if (!string.IsNullOrEmpty(dt))
                        //{
                        //    ret.AppendFormat("^^<{0}>", dt);
                        //}
                    }
                }
                return(ret.ToString());
            }
            if (typeof(IEntityObject).IsAssignableFrom(expressionType) ||
                EntityMappingStore.IsKnownInterface(expressionType))
            {
                var obj     = value as IEntityObject;
                var address = Context.GetResourceAddress(obj);
                return(string.Format("<{0}>", address));
            }
            throw new ArgumentException(string.Format("Unable to serialize value {0} ({1}) as a SPARQL constant", value, value.GetType()));
        }