本文整理汇总了C#中SqlExpression类的典型用法代码示例。如果您正苦于以下问题:C# SqlExpression类的具体用法?C# SqlExpression怎么用?C# SqlExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SqlExpression类属于命名空间,在下文中一共展示了SqlExpression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SqlClientWhen
internal SqlClientWhen(SqlExpression match, SqlExpression value) {
// 'match' may be null when this when represents the ELSE condition.
if (value == null)
throw Error.ArgumentNull("value");
this.Match = match;
this.Value = value;
}
示例2: ConvertPredicateToValue
internal override SqlExpression ConvertPredicateToValue(SqlExpression predicateExpression) {
// Transform the 'Predicate' expression into a 'Bit' by forming the
// following operation:
// CASE
// WHEN predicateExpression THEN 1
// ELSE NOT(predicateExpression) THEN 0
// ELSE NULL
// END
// Possible simplification to the generated SQL would be to detect when 'predicateExpression'
// is SqlUnary(NOT) and use its operand with the literal 1 and 0 below swapped.
SqlExpression valueTrue = sql.ValueFromObject(true, false, predicateExpression.SourceExpression);
SqlExpression valueFalse = sql.ValueFromObject(false, false, predicateExpression.SourceExpression);
if (SqlExpressionNullability.CanBeNull(predicateExpression) != false) {
SqlExpression valueNull = sql.Value(valueTrue.ClrType, valueTrue.SqlType, null, false, predicateExpression.SourceExpression);
return new SqlSearchedCase(
predicateExpression.ClrType,
new SqlWhen[] {
new SqlWhen(predicateExpression, valueTrue),
new SqlWhen(new SqlUnary(SqlNodeType.Not, predicateExpression.ClrType, predicateExpression.SqlType, predicateExpression, predicateExpression.SourceExpression), valueFalse)
},
valueNull,
predicateExpression.SourceExpression
);
}
else {
return new SqlSearchedCase(
predicateExpression.ClrType,
new SqlWhen[] { new SqlWhen(predicateExpression, valueTrue) },
valueFalse,
predicateExpression.SourceExpression
);
}
}
示例3: Visit
internal override SqlNode Visit(SqlNode node) {
if (node == null)
return null;
sourceExpression = node as SqlExpression;
if (sourceExpression != null) {
Type type = sourceExpression.ClrType;
UnwrapStack unwrap = this.UnwrapSequences;
while (unwrap != null) {
if (unwrap.Unwrap) {
type = TypeSystem.GetElementType(type);
}
unwrap = unwrap.Last;
}
sourceType = type;
}
if (sourceType != null && TypeSystem.GetNonNullableType(sourceType).IsValueType) {
return node; // Value types can't also have a dynamic type.
}
if (sourceType != null && TypeSystem.HasIEnumerable(sourceType)) {
return node; // Sequences can't be polymorphic.
}
switch (node.NodeType) {
case SqlNodeType.ScalarSubSelect:
case SqlNodeType.Multiset:
case SqlNodeType.Element:
case SqlNodeType.SearchedCase:
case SqlNodeType.ClientCase:
case SqlNodeType.SimpleCase:
case SqlNodeType.Member:
case SqlNodeType.DiscriminatedType:
case SqlNodeType.New:
case SqlNodeType.FunctionCall:
case SqlNodeType.MethodCall:
case SqlNodeType.Convert: // Object identity does not survive convert. It does survive Cast.
// Dig no further.
return node;
case SqlNodeType.TypeCase:
sourceType = ((SqlTypeCase)node).RowType.Type;
return node;
case SqlNodeType.Link:
sourceType = ((SqlLink)node).RowType.Type;
return node;
case SqlNodeType.Table:
sourceType = ((SqlTable)node).RowType.Type;
return node;
case SqlNodeType.Value:
SqlValue val = (SqlValue)node;
if (val.Value != null) {
// In some cases the ClrType of a Value node may
// differ from the actual runtime type of the value.
// Therefore, we ensure here that the correct type is set.
sourceType = val.Value.GetType();
}
return node;
}
return base.Visit(node);
}
示例4: VisitLink
internal override SqlNode VisitLink(SqlLink link) {
SqlExpression expansion = this.VisitExpression(link.Expansion);
SqlExpression[] exprs = new SqlExpression[link.KeyExpressions.Count];
for (int i = 0, n = exprs.Length; i < n; i++) {
exprs[i] = this.VisitExpression(link.KeyExpressions[i]);
}
return new SqlLink(link.Id, link.RowType, link.ClrType, link.SqlType, link.Expression, link.Member, exprs, expansion, link.SourceExpression);
}
示例5: Function
public IExpressionBuilder Function(ObjectName functionName, params SqlExpression[] args)
{
expression = SqlExpression.FunctionCall(functionName, args);
VerifyUnary();
return this;
}
示例6: IfNull
/// <summary>
/// Renders IfNull SqlExpression
/// </summary>
/// <param name="builder"></param>
/// <param name="expr"></param>
protected override void IfNull(StringBuilder builder, SqlExpression expr)
{
builder.Append("ifnull(");
Expression(builder, expr.SubExpr1);
builder.Append(", ");
Expression(builder, expr.SubExpr2);
builder.Append(")");
}
示例7: Quantified
public IExpressionBuilder Quantified(SqlExpressionType quantifyType, Action<IExpressionBuilder> exp)
{
var builder = new ExpressionBuilder();
exp(builder);
expression = SqlExpression.Quantified(quantifyType, builder.Build());
return this;
}
示例8: ConvertToMax
// returns CONVERT(VARCHAR/NVARCHAR/VARBINARY(MAX), expr) if provType is one of Text, NText or Image
// otherwise just returns expr
// changed is true if CONVERT(...(MAX),...) was added
private SqlExpression ConvertToMax(SqlExpression expr, out bool changed) {
changed = false;
if (!expr.SqlType.IsLargeType)
return expr;
ProviderType newType = sql.TypeProvider.GetBestLargeType(expr.SqlType);
changed = true;
if (expr.SqlType != newType) {
return ConvertToMax(expr, newType);
}
changed = false;
return expr;
}
示例9: SetTable
public override void SetTable(SqlTable table, MemberInfo member, IEnumerable<Expression> expArgs, IEnumerable<ISqlExpression> sqlArgs)
{
var aargs = sqlArgs.ToArray();
var arr = ConvertArgs(member, aargs).ToList();
var method = (MethodInfo)member;
{
var ttype = method.GetGenericArguments()[0];
var tbl = new SqlTable(ttype);
var database = Convert(tbl.Database);
var owner = Convert(tbl.Owner);
var physicalName = Convert(tbl.PhysicalName);
var name = "";
if (database != null)
name = database + "." + (owner == null ? "." : owner + ".");
else if (owner != null)
name = owner + ".";
name += physicalName;
arr.Add(new SqlExpression(name, Precedence.Primary));
}
{
var field = ((ConstantExpression)expArgs.First()).Value;
if (field is string)
{
arr[0] = new SqlExpression(field.ToString(), Precedence.Primary);
}
else if (field is LambdaExpression)
{
var body = ((LambdaExpression)field).Body;
if (body is MemberExpression)
{
var name = ((MemberExpression)body).Member.Name;
if (name.Length > 0 && name[0] != '[')
name = "[" + name + "]";
arr[0] = new SqlExpression(name, Precedence.Primary);
}
}
}
table.SqlTableType = SqlTableType.Expression;
table.Name = "FREETEXTTABLE({6}, {2}, {3}) {1}";
table.TableArguments = arr.ToArray();
}
示例10: Expression
/// <summary>
/// Renders SqlExpression
/// </summary>
/// <param name="builder"></param>
/// <param name="expr"></param>
protected override void Expression(StringBuilder builder, SqlExpression expr)
{
SqlExpressionType type = expr.Type;
if (type == SqlExpressionType.Field)
{
QualifiedIdentifier(builder, expr.TableAlias, expr.Value.ToString());
}
else if (type == SqlExpressionType.Function)
{
Function(builder, expr.AggFunction, expr.SubExpr1);
}
else if (type == SqlExpressionType.Constant)
{
Constant(builder, (SqlConstant) expr.Value);
}
else if (type == SqlExpressionType.SubQueryText)
{
builder.AppendFormat("({0})", (string) expr.Value);
}
else if (type == SqlExpressionType.SubQueryObject)
{
builder.AppendFormat("({0})", RenderSelect((SelectQuery) expr.Value));
}
else if (type == SqlExpressionType.PseudoField)
{
builder.AppendFormat("{0}", (string) expr.Value);
}
else if (type == SqlExpressionType.Parameter)
{
builder.AppendFormat("{0}", (string) expr.Value);
}
else if (type == SqlExpressionType.LikeExpressionParameter)
{
builder.AppendFormat("'%' + {0} + '%'", (string) expr.Value);
}
else if (type == SqlExpressionType.Raw)
{
builder.AppendFormat("{0}", (string) expr.Value);
}
else if (type == SqlExpressionType.IfNull)
{
IfNull(builder, expr);
}
else if (type == SqlExpressionType.Null)
{
builder.Append("null");
}
else
{
throw new InvalidQueryException("Unkown expression type: " + type.ToString());
}
}
示例11: ConvertValueToPredicate
internal override SqlExpression ConvertValueToPredicate(SqlExpression valueExpression) {
// Transform the 'Bit' expression into a 'Predicate' by forming the
// following operation:
// OriginalExpr = 1
// Yukon and later could also handle:
// OriginalExpr = 'true'
// but Sql2000 does not support this.
return new SqlBinary(SqlNodeType.EQ,
valueExpression.ClrType, sql.TypeProvider.From(typeof(bool)),
valueExpression,
sql.Value(typeof(bool), valueExpression.SqlType, true, false, valueExpression.SourceExpression)
);
}
示例12: Binary
public IExpressionBuilder Binary(SqlExpressionType binaryType, Action<IExpressionBuilder> right)
{
if (expression == null)
throw new InvalidOperationException();
var builder = new ExpressionBuilder();
right(builder);
expression = SqlExpression.Binary(expression, binaryType, builder.Build());
VerifyUnary();
return this;
}
示例13: DistributeOperatorIntoCase
/// <summary>
/// Helper for VisitBinaryOperator. Builds the new case with distributed valueds.
/// </summary>
private SqlExpression DistributeOperatorIntoCase(SqlNodeType nt, SqlSimpleCase sc, SqlExpression expr) {
if (nt!=SqlNodeType.EQ && nt!=SqlNodeType.NE && nt!=SqlNodeType.EQ2V && nt!=SqlNodeType.NE2V)
throw Error.ArgumentOutOfRange("nt");
object val = Eval(expr);
List<SqlExpression> values = new List<SqlExpression>();
List<SqlExpression> matches = new List<SqlExpression>();
foreach(SqlWhen when in sc.Whens) {
matches.Add(when.Match);
object whenVal = Eval(when.Value);
bool eq = when.Value.SqlType.AreValuesEqual(whenVal, val);
values.Add(sql.ValueFromObject((nt==SqlNodeType.EQ || nt==SqlNodeType.EQ2V) == eq, false, sc.SourceExpression));
}
return this.VisitExpression(sql.Case(typeof(bool), sc.Expression, matches, values, sc.SourceExpression));
}
示例14: InferName
protected string InferName(SqlExpression exp, string def)
{
if (exp == null)
return null;
SqlExpressionType nodeType = exp.NodeType;
switch (nodeType)
{
case SqlExpressionType.Field:
return ((SqlField)exp).Name;
case SqlExpressionType.Alias:
return InferName(((SqlAlias)exp).Expression, def);
case SqlExpressionType.ExprSet:
return InferName(((SqlExpressionSet)exp).First(), def);
}
return def;
}
示例15: AddParentheses
protected void AddParentheses(SqlExpression node, SqlExpression outer)
{
switch (node.NodeType)
{
case SqlExpressionType.Function:
case SqlExpressionType.TableValuedFunction:
case SqlExpressionType.Raw:
case SqlExpressionType.Parameter:
case SqlExpressionType.Constant:
case SqlExpressionType.Variable:
case SqlExpressionType.Field:
Visit(node);
break;
case SqlExpressionType.Binary:
SqlBinary binary = (SqlBinary)node;
switch (binary.Operation)
{
case SqlBinaryOperation.Add:
case SqlBinaryOperation.And:
case SqlBinaryOperation.BitAnd:
case SqlBinaryOperation.BitNot:
case SqlBinaryOperation.BitOr:
case SqlBinaryOperation.BitXor:
case SqlBinaryOperation.Multiply:
case SqlBinaryOperation.Or:
if (node.NodeType == outer.NodeType)
Visit(node);
break;
default:
_builder.Append('(');
Visit(node);
_builder.Append(')');
break;
}
break;
default:
_builder.Append('(');
Visit(node);
_builder.Append(')');
break;
}
}