public void SetCondition(Expression e) { ExpressionType type = e.Type; Expression e1 = e.Arg; Expression e2 = e.Arg2; if (type == ExpressionType.And) { SetCondition(e1); SetCondition(e2); return; } int candidate; switch (type) { case ExpressionType.NotEqual: case ExpressionType.Like: // todo: maybe use index case ExpressionType.In: candidate = 0; break; case ExpressionType.Equal: candidate = 1; break; case ExpressionType.Bigger: case ExpressionType.BiggerEqual: candidate = 2; break; case ExpressionType.Smaller: case ExpressionType.SmallerEqual: candidate = 3; break; default: // not a condition so forget it return; } if (e1.Filter == this) { // ok include this } else if (e2.Filter == this && candidate != 0) { // swap and try again to allow index usage e.SwapCondition(); SetCondition(e); return; } else { // unrelated: don't include return; } Trace.Assert(e1.Filter == this, "setCondition"); if (!e2.IsResolved) { return; } if (candidate == 0) { AddAndCondition(e); return; } int i = e1.ColumnNumber; Index index = tTable.GetIndexForColumn(i); if (index == null || (iIndex != index && iIndex != null)) { // no index or already another index is used AddAndCondition(e); return; } iIndex = index; if (candidate == 1) { // candidate for both start & end if (eStart != null || eEnd != null) { AddAndCondition(e); return; } eStart = new Expression(e); eEnd = eStart; } else if (candidate == 2) { // candidate for start if (eStart != null) { AddAndCondition(e); return; } eStart = new Expression(e); } else if (candidate == 3) { // candidate for end if (eEnd != null) { AddAndCondition(e); return; } eEnd = new Expression(e); } e.SetTrue(); }
public void SetCondition(Expression e) { ExpressionType type = e.Type; Expression e1 = e.Arg; Expression e2 = e.Arg2; if (type == ExpressionType.And) { SetCondition(e1); SetCondition(e2); return; } int candidate; switch (type) { case ExpressionType.NotEqual: case ExpressionType.Like: // todo: maybe use index case ExpressionType.In: candidate = 0; break; case ExpressionType.Equal: candidate = 1; break; case ExpressionType.Bigger: case ExpressionType.BiggerEqual: candidate = 2; break; case ExpressionType.Smaller: case ExpressionType.SmallerEqual: candidate = 3; break; default: // not a condition so forget it return; } if (e1.Filter == this) { // ok include this } else if (e2.Filter == this && candidate != 0) { // swap and try again to allow index usage e.SwapCondition(); SetCondition(e); return; } else { // unrelated: don't include return; } TracingHelper.Assert(e1.Filter == this, "setCondition"); if (!e2.IsResolved) { return; } if (candidate == 0) { AddAndCondition(e); return; } int i = e1.ColumnNumber; Index index = tTable.GetIndexForColumn(i); if (index == null || (iIndex != index && iIndex != null)) { // no index or already another index is used AddAndCondition(e); return; } iIndex = index; if (candidate == 1) { // candidate for both start & end if (eStart != null || eEnd != null) { AddAndCondition(e); return; } eStart = new Expression(e); eEnd = eStart; } else if (candidate == 2) { // candidate for start if (eStart != null) { AddAndCondition(e); return; } eStart = new Expression(e); } else if (candidate == 3) { // candidate for end if (eEnd != null) { AddAndCondition(e); return; } eEnd = new Expression(e); } e.SetTrue(); }