protected override Expression VisitConstant(ConstantExpression c) { IMappedFunction f = c.Value as IMappedFunction; IMappedQueryable q = c.Value as IMappedQueryable; IMappedStringQueryable s = c.Value as IMappedStringQueryable; if (f != null) { // assume constant nodes w/ IMappedFunction are table valued function references List <string> parameters = new List <string>(); int fpCount = 0; foreach (int pKey in f.IndexedParameters.Keys.OrderBy(k => k)) { string name = "@FP" + fpCount; parameters.Add(name); predicate.Parameters.Add(name, f.GetType().GetProperty(f.IndexedParameters[pKey]).GetValue(f)); fpCount++; } string fields = String.Join(", ", f.TableMapping.Values.Select(v => Alias + "." + v.ColumnName)); ProcedureCall = $"CALL {f.FunctionName} ({String.Join(",", parameters)}); "; sb.Append($"SELECT {fields} FROM Temp{f.FunctionName} AS {Alias}"); Type type = f.ElementType; } else if (q != null) { // assume constant nodes w/ IMappedQueryable are table references string fields = String.Join(", ", q.TableMapping.Values.Select(v => Alias + "." + v.ColumnName)); sb.Append($"SELECT {fields} FROM {q.TableName} AS {Alias}"); Type type = q.ElementType; } else if (s != null) { sb.Append($"SELECT * FROM ({c.ToString()}) AS {Alias}"); } else if (c.Value == null) { sb.Append("NULL"); } else { string parameterName = "@P" + (predicate.Parameters.Count + Initial); switch (Type.GetTypeCode(c.Value.GetType())) { case TypeCode.Object: if (c.Value is TimeSpan) { sb.Append(parameterName); predicate.Parameters.Add(parameterName, c.Value); break; } throw new NotSupportedException(string.Format("The constant for '{0}' is not supported. ", c.Value)); default: sb.Append(parameterName); predicate.Parameters.Add(parameterName, c.Value); break; } } return(c); }
protected override Expression VisitConstant(ConstantExpression c) { IMappedFunction f = c.Value as IMappedFunction; IMappedQueryable q = c.Value as IMappedQueryable; IMappedStringQueryable s = c.Value as IMappedStringQueryable; if (f != null) { // assume constant nodes w/ IMappedFunction are table valued function references List <string> parameters = new List <string>(); int fpCount = 0; foreach (int pKey in f.IndexedParameters.Keys.OrderBy(k => k)) { string name = "@FP" + fpCount; parameters.Add(name); predicate.Parameters.Add(name, f.GetType().GetProperty(f.IndexedParameters[pKey]).GetValue(f)); fpCount++; } string fields = String.Join(", ", f.TableMapping.Values.Select(v => Alias + "." + "[" + v.ColumnName + "]")); string AppendToQuery = string.Empty; if (OffsetRequired) { HasOffset = true; OrderByRequired = true; OffsetRequired = false; AppendToQuery = " OFFSET 0 ROWS "; } if (OrderByRequired && !HasOrderBy) { OrderByRequired = false; HasOrderBy = true; AppendToQuery = " ORDER BY 1 " + AppendToQuery; } if (IsNested && HasOrderBy && !HasOffset) //adding top <big number> when order by exists without offset { sb.Append("SELECT TOP 10000000 "); } else { sb.Append("SELECT "); } sb.Append(fields + " FROM " + f.FunctionName + "(" + String.Join(",", parameters) + ") AS " + Alias + AppendToQuery); Type type = f.ElementType; } else if (q != null) { // assume constant nodes w/ IQueryables are table references string fields = String.Join(", ", q.TableMapping.Values.Select(v => Alias + "." + "[" + v.ColumnName + "]")); string AppendToQuery = string.Empty; if (OffsetRequired) { HasOffset = true; OrderByRequired = true; OffsetRequired = false; AppendToQuery = " OFFSET 0 ROWS "; } if (OrderByRequired && !HasOrderBy) { OrderByRequired = false; HasOrderBy = true; AppendToQuery = " ORDER BY 1 " + AppendToQuery; } if (IsNested && HasOrderBy && !HasOffset) //adding top <big number> when order by exists without offset { sb.Append("SELECT TOP 10000000 "); } else { sb.Append("SELECT "); } sb.Append(fields + " FROM " + q.TableName + " AS " + Alias + AppendToQuery); Type type = q.ElementType; } else if (s != null) { sb.Append(c.ToString()); } else if (c.Value == null) { sb.Append("NULL"); } else { string parameterName = "@P" + (predicate.Parameters.Count + Initial); switch (Type.GetTypeCode(c.Value.GetType())) { case TypeCode.Object: if (c.Value is TimeSpan) { sb.Append(parameterName); predicate.Parameters.Add(parameterName, c.Value); break; } if (c.Value is DateTimeOffset) { sb.Append(parameterName); predicate.Parameters.Add(parameterName, c.Value); break; } throw new NotSupportedException(string.Format("The constant for '{0}' is not supported. ", c.Value)); default: sb.Append(parameterName); predicate.Parameters.Add(parameterName, c.Value); break; } } return(c); }