internal static PrimitiveTypeKind ToPrimitiveTypeKind(this TypeUsage typeUsage) { PrimitiveTypeKind returnValue; if (!typeUsage.TryGetPrimitiveTypeKind(out returnValue)) { throw new NotSupportedException("Cannot create parameter of non-primitive type"); } return returnValue; }
private string VisitConstantExpression(TypeUsage expressionType, object expressionValue) { var result = new StringBuilder(); PrimitiveTypeKind typeKind; // Model Types can be (at the time of this implementation): // Binary, Boolean, Byte, DateTime, Decimal, Double, Guid, Int16, Int32, Int64,Single, String if (expressionType.TryGetPrimitiveTypeKind(out typeKind)) { switch (typeKind) { case PrimitiveTypeKind.Int32: case PrimitiveTypeKind.Byte: result.Append(expressionValue); break; case PrimitiveTypeKind.Binary: result.Append(LiteralHelpers.ToSqlString((byte[])expressionValue)); break; case PrimitiveTypeKind.Boolean: result.Append(LiteralHelpers.ToSqlString((bool)expressionValue)); break; case PrimitiveTypeKind.DateTime: result.Append(LiteralHelpers.SqlDateTime((DateTime)expressionValue)); break; case PrimitiveTypeKind.Time: result.Append(LiteralHelpers.SqlDayTime((DateTime)expressionValue)); break; case PrimitiveTypeKind.DateTimeOffset: throw new NotImplementedException("Jet does not implement DateTimeOffset"); case PrimitiveTypeKind.Decimal: var strDecimal = ((decimal)expressionValue).ToString(CultureInfo.InvariantCulture); // if the decimal value has no decimal part, cast as decimal to preserve type // if the number has precision > int64 max precision, it will be handled as decimal by sql server // and does not need cast. if precision is lest then 20, then cast using Max(literal precision, sql default precision) if (-1 == strDecimal.IndexOf('.') && strDecimal.TrimStart('-').Length < 20) { var precision = (byte)strDecimal.Length; FacetDescription precisionFacetDescription; if (!expressionType.EdmType.TryGetTypeFacetDescriptionByName("precision", out precisionFacetDescription)) { throw new InvalidOperationException("Decimal primitive type must have Precision facet"); } if (precisionFacetDescription.DefaultValue != null) { precision = Math.Max(precision, (byte)precisionFacetDescription.DefaultValue); } if (precision <= 0) { throw new InvalidOperationException("Precision must be greater than zero"); } result.Append("cast("); result.Append(strDecimal); result.Append(" as decimal("); result.Append(precision.ToString(CultureInfo.InvariantCulture)); result.Append("))"); } else { result.Append(strDecimal); } break; case PrimitiveTypeKind.Double: result.Append(((double)expressionValue).ToString(CultureInfo.InvariantCulture)); break; case PrimitiveTypeKind.Single: result.Append(((float)expressionValue).ToString(CultureInfo.InvariantCulture)); break; case PrimitiveTypeKind.Int16: case PrimitiveTypeKind.Int64: result.Append(expressionValue); break; case PrimitiveTypeKind.String: result.Append(LiteralHelpers.ToSqlString(expressionValue as string)); break; case PrimitiveTypeKind.Guid: result.Append(LiteralHelpers.ToSqlString((Guid)expressionValue)); break; default: // all known scalar types should been handled already. throw new NotSupportedException("Primitive type kind " + typeKind + " is not supported by the Jet Provider"); } } else { throw new NotSupportedException(); } return(result.ToString()); }