public Select(DataTable table, string filterExpression, string sort, DataViewRowState recordStates) { this.table = table; IndexFields = table.ParseSortString(sort); if (filterExpression != null && filterExpression.Length > 0) { this.rowFilter = new DataExpression(this.table, filterExpression); this.expression = this.rowFilter.ExpressionNode; } this.recordStates = recordStates; }
public Select(DataTable table, string filterExpression, string sort, DataViewRowState recordStates) { _table = table; _indexFields = table.ParseSortString(sort); if (filterExpression != null && filterExpression.Length > 0) { _rowFilter = new DataExpression(_table, filterExpression); _expression = _rowFilter.ExpressionNode; } _recordStates = recordStates; }
private Index(DataTable table, IndexField[] indexFields, System.Comparison <DataRow> comparison, DataViewRowState recordStates, IFilter rowFilter) { Bid.Trace("<ds.Index.Index|API> %d#, table=%d, recordStates=%d{ds.DataViewRowState}\n", ObjectID, (table != null) ? table.ObjectID : 0, (int)recordStates); Debug.Assert(indexFields != null); Debug.Assert(null != table, "null table"); if ((recordStates & (~(DataViewRowState.CurrentRows | DataViewRowState.OriginalRows))) != 0) { throw ExceptionBuilder.RecordStateRange(); } this.table = table; _listeners = new Listeners <DataViewListener>(ObjectID, delegate(DataViewListener listener) { return(null != listener); }); IndexFields = indexFields; this.recordStates = recordStates; _comparison = comparison; DataColumnCollection columns = table.Columns; isSharable = (rowFilter == null) && (comparison == null); // a filter or comparison make an index unsharable if (null != rowFilter) { this.rowFilter = new WeakReference(rowFilter); DataExpression expr = (rowFilter as DataExpression); if (null != expr) { _hasRemoteAggregate = expr.HasRemoteAggregate(); } } InitRecords(rowFilter); // do not AddRef in ctor, every caller should be responsible to AddRef it // if caller does not AddRef, it is expected to be a one-time read operation because the index won't be maintained on writes }
private bool AcceptRecord(int record) { DataRow row = _table._recordManager[record]; if (row == null) { return(true); } DataRowVersion version = DataRowVersion.Default; if (row._oldRecord == record) { version = DataRowVersion.Original; } else if (row._newRecord == record) { version = DataRowVersion.Current; } else if (row._tempRecord == record) { version = DataRowVersion.Proposed; } object val = _linearExpression.Eval(row, version); bool result; try { result = DataExpression.ToBoolean(val); } catch (Exception e) when(ADP.IsCatchableExceptionType(e)) { throw ExprException.FilterConvertion(_rowFilter.Expression); } return(result); }
private bool AcceptRecord(int record) { DataRow row = table.recordManager[record]; if (row == null) { return(true); } // UNDONE: perf switch to (row, version) internaly DataRowVersion version = DataRowVersion.Default; if (row.oldRecord == record) { version = DataRowVersion.Original; } else if (row.newRecord == record) { version = DataRowVersion.Current; } else if (row.tempRecord == record) { version = DataRowVersion.Proposed; } object val = this.linearExpression.Eval(row, version); bool result; try { result = DataExpression.ToBoolean(val); } catch (Exception) { throw ExprException.FilterConvertion(this.rowFilter.Expression); } return(result); }
/// <devdoc> /// <para>Computes the given expression on the current rows that pass the filter criteria.</para> /// </devdoc> public object Compute(string expression, string filter) { DataRow[] rows = Select(filter, "", DataViewRowState.CurrentRows); DataExpression expr = new DataExpression(this, expression); return expr.Evaluate(rows); }
private object EvalUnaryOp(int op, object vl) { object value = DBNull.Value; if (DataExpression.IsUnknown(vl)) { return(DBNull.Value); } StorageType storageType; switch (op) { case Operators.Noop: return(vl); case Operators.UnaryPlus: storageType = DataStorage.GetStorageType(vl.GetType()); if (ExpressionNode.IsNumericSql(storageType)) { return(vl); } throw ExprException.TypeMismatch(ToString()); case Operators.Negative: // the have to be better way for doing this.. storageType = DataStorage.GetStorageType(vl.GetType()); if (ExpressionNode.IsNumericSql(storageType)) { switch (storageType) { case StorageType.Byte: value = -(byte)vl; break; case StorageType.Int16: value = -(short)vl; break; case StorageType.Int32: value = -(int)vl; break; case StorageType.Int64: value = -(long)vl; break; case StorageType.Single: value = -(float)vl; break; case StorageType.Double: value = -(double)vl; break; case StorageType.Decimal: value = -(decimal)vl; break; case StorageType.SqlDecimal: value = -(SqlDecimal)vl; break; case StorageType.SqlDouble: value = -(SqlDouble)vl; break; case StorageType.SqlSingle: value = -(SqlSingle)vl; break; case StorageType.SqlMoney: value = -(SqlMoney)vl; break; case StorageType.SqlInt64: value = -(SqlInt64)vl; break; case StorageType.SqlInt32: value = -(SqlInt32)vl; break; case StorageType.SqlInt16: value = -(SqlInt16)vl; break; default: Debug.Fail("Missing a type conversion"); value = DBNull.Value; break; } return(value); } throw ExprException.TypeMismatch(ToString()); case Operators.Not: if (vl is SqlBoolean) { if (((SqlBoolean)vl).IsFalse) { return(SqlBoolean.True); } else if (((SqlBoolean)vl).IsTrue) { return(SqlBoolean.False); } throw ExprException.UnsupportedOperator(op); // or should the result of not SQLNull be SqlNull ? } else { if (DataExpression.ToBoolean(vl) != false) { return(false); } return(true); } default: throw ExprException.UnsupportedOperator(op); } }
private object EvalUnaryOp(int op, object vl) { if (!DataExpression.IsUnknown(vl)) { switch (op) { case 0: return(vl); case 1: { StorageType storageType = DataStorage.GetStorageType(vl.GetType()); if (!ExpressionNode.IsNumericSql(storageType)) { throw ExprException.TypeMismatch(this.ToString()); } switch (storageType) { case StorageType.Byte: return((int)-((byte)vl)); case StorageType.Int16: return((int)-((short)vl)); case StorageType.Int32: return(-((int)vl)); case StorageType.Int64: return(-((long)vl)); case StorageType.Single: return(-((float)vl)); case StorageType.Double: return(-((double)vl)); case StorageType.Decimal: return(-((decimal)vl)); case StorageType.SqlDecimal: return(-((SqlDecimal)vl)); case StorageType.SqlDouble: return(-((SqlDouble)vl)); case StorageType.SqlInt16: return(-((SqlInt16)vl)); case StorageType.SqlInt32: return(-((SqlInt32)vl)); case StorageType.SqlInt64: return(-((SqlInt64)vl)); case StorageType.SqlMoney: return(-((SqlMoney)vl)); case StorageType.SqlSingle: return(-((SqlSingle)vl)); } break; } case 2: if (!ExpressionNode.IsNumericSql(DataStorage.GetStorageType(vl.GetType()))) { throw ExprException.TypeMismatch(this.ToString()); } return(vl); case 3: { if (!(vl is SqlBoolean)) { if (DataExpression.ToBoolean(vl)) { return(false); } return(true); } SqlBoolean flag2 = (SqlBoolean)vl; if (!flag2.IsFalse) { SqlBoolean flag = (SqlBoolean)vl; if (!flag.IsTrue) { throw ExprException.UnsupportedOperator(op); } return(SqlBoolean.False); } return(SqlBoolean.True); } default: throw ExprException.UnsupportedOperator(op); } } return(DBNull.Value); }
private object EvalFunction(FunctionId id, object[] argumentValues, DataRow row, DataRowVersion version) { StorageType storageType; switch (id) { case FunctionId.Abs: Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider)); storageType = DataStorage.GetStorageType(argumentValues[0].GetType()); if (ExpressionNode.IsInteger(storageType)) { return(Math.Abs((long)argumentValues[0])); } if (ExpressionNode.IsNumeric(storageType)) { return(Math.Abs((double)argumentValues[0])); } throw ExprException.ArgumentTypeInteger(s_funcs[_info]._name, 1); case FunctionId.cBool: Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider)); storageType = DataStorage.GetStorageType(argumentValues[0].GetType()); switch (storageType) { case StorageType.Boolean: return((bool)argumentValues[0]); case StorageType.Int32: return((int)argumentValues[0] != 0); case StorageType.Double: return((double)argumentValues[0] != 0.0); case StorageType.String: return(bool.Parse((string)argumentValues[0])); default: throw ExprException.DatatypeConvertion(argumentValues[0].GetType(), typeof(bool)); } case FunctionId.cInt: Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider)); return(Convert.ToInt32(argumentValues[0], FormatProvider)); case FunctionId.cDate: Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider)); return(Convert.ToDateTime(argumentValues[0], FormatProvider)); case FunctionId.cDbl: Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider)); return(Convert.ToDouble(argumentValues[0], FormatProvider)); case FunctionId.cStr: Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider)); return(Convert.ToString(argumentValues[0], FormatProvider)); case FunctionId.Charindex: Debug.Assert(_argumentCount == 2, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider)); Debug.Assert(argumentValues[0] is string, "Invalid argument type for " + s_funcs[_info]._name); Debug.Assert(argumentValues[1] is string, "Invalid argument type for " + s_funcs[_info]._name); if (DataStorage.IsObjectNull(argumentValues[0]) || DataStorage.IsObjectNull(argumentValues[1])) { return(DBNull.Value); } if (argumentValues[0] is SqlString) { argumentValues[0] = ((SqlString)argumentValues[0]).Value; } if (argumentValues[1] is SqlString) { argumentValues[1] = ((SqlString)argumentValues[1]).Value; } return(((string)argumentValues[1]).IndexOf((string)argumentValues[0], StringComparison.Ordinal)); case FunctionId.Iif: Debug.Assert(_argumentCount == 3, "Invalid argument argumentCount: " + _argumentCount.ToString(FormatProvider)); object first = _arguments[0].Eval(row, version); if (DataExpression.ToBoolean(first) != false) { return(_arguments[1].Eval(row, version)); } else { return(_arguments[2].Eval(row, version)); } case FunctionId.In: // we never evaluate IN directly: IN as a binary operator, so evaluation of this should be in // BinaryNode class throw ExprException.NYI(s_funcs[_info]._name); case FunctionId.IsNull: Debug.Assert(_argumentCount == 2, "Invalid argument argumentCount: "); if (DataStorage.IsObjectNull(argumentValues[0])) { return(argumentValues[1]); } else { return(argumentValues[0]); } case FunctionId.Len: Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider)); Debug.Assert((argumentValues[0] is string) || (argumentValues[0] is SqlString), "Invalid argument type for " + s_funcs[_info]._name); if (argumentValues[0] is SqlString) { if (((SqlString)argumentValues[0]).IsNull) { return(DBNull.Value); } else { argumentValues[0] = ((SqlString)argumentValues[0]).Value; } } return(((string)argumentValues[0]).Length); case FunctionId.Substring: Debug.Assert(_argumentCount == 3, "Invalid argument argumentCount: " + _argumentCount.ToString(FormatProvider)); Debug.Assert((argumentValues[0] is string) || (argumentValues[0] is SqlString), "Invalid first argument " + argumentValues[0].GetType().FullName + " in " + s_funcs[_info]._name); Debug.Assert(argumentValues[1] is int, "Invalid second argument " + argumentValues[1].GetType().FullName + " in " + s_funcs[_info]._name); Debug.Assert(argumentValues[2] is int, "Invalid third argument " + argumentValues[2].GetType().FullName + " in " + s_funcs[_info]._name); // work around the differences in .NET and VBA implementation of the Substring function // 1. The <index> Argument is 0-based in .NET, and 1-based in VBA // 2. If the <Length> argument is longer then the string length .NET throws an ArgumentException // but our users still want to get a result. int start = (int)argumentValues[1] - 1; int length = (int)argumentValues[2]; if (start < 0) { throw ExprException.FunctionArgumentOutOfRange("index", "Substring"); } if (length < 0) { throw ExprException.FunctionArgumentOutOfRange("length", "Substring"); } if (length == 0) { return(string.Empty); } if (argumentValues[0] is SqlString) { argumentValues[0] = ((SqlString)argumentValues[0]).Value; } int src_length = ((string)argumentValues[0]).Length; if (start > src_length) { return(DBNull.Value); } if (start + length > src_length) { length = src_length - start; } return(((string)argumentValues[0]).Substring(start, length)); case FunctionId.Trim: { Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider)); Debug.Assert((argumentValues[0] is string) || (argumentValues[0] is SqlString), "Invalid argument type for " + s_funcs[_info]._name); if (DataStorage.IsObjectNull(argumentValues[0])) { return(DBNull.Value); } if (argumentValues[0] is SqlString) { argumentValues[0] = ((SqlString)argumentValues[0]).Value; } return(((string)argumentValues[0]).Trim()); } case FunctionId.Convert: if (_argumentCount != 2) { throw ExprException.FunctionArgumentCount(_name); } if (argumentValues[0] == DBNull.Value) { return(DBNull.Value); } Type type = (Type)argumentValues[1]; StorageType mytype = DataStorage.GetStorageType(type); storageType = DataStorage.GetStorageType(argumentValues[0].GetType()); if (mytype == StorageType.DateTimeOffset) { if (storageType == StorageType.String) { return(SqlConvert.ConvertStringToDateTimeOffset((string)argumentValues[0], FormatProvider)); } } if (StorageType.Object != mytype) { if ((mytype == StorageType.Guid) && (storageType == StorageType.String)) { return(new Guid((string)argumentValues[0])); } if (ExpressionNode.IsFloatSql(storageType) && ExpressionNode.IsIntegerSql(mytype)) { if (StorageType.Single == storageType) { return(SqlConvert.ChangeType2((float)SqlConvert.ChangeType2(argumentValues[0], StorageType.Single, typeof(float), FormatProvider), mytype, type, FormatProvider)); } else if (StorageType.Double == storageType) { return(SqlConvert.ChangeType2((double)SqlConvert.ChangeType2(argumentValues[0], StorageType.Double, typeof(double), FormatProvider), mytype, type, FormatProvider)); } else if (StorageType.Decimal == storageType) { return(SqlConvert.ChangeType2((decimal)SqlConvert.ChangeType2(argumentValues[0], StorageType.Decimal, typeof(decimal), FormatProvider), mytype, type, FormatProvider)); } } // The Convert function can be called lazily, outside of a previous Serialization Guard scope. // If there was a type limiter scope on the stack at the time this Convert function was created, // we must manually re-enter the Serialization Guard scope. DeserializationToken deserializationToken = (_capturedLimiter != null) ? SerializationInfo.StartDeserialization() : default; using (deserializationToken) { return(SqlConvert.ChangeType2(argumentValues[0], mytype, type, FormatProvider)); } } return(argumentValues[0]); case FunctionId.DateTimeOffset: if (argumentValues[0] == DBNull.Value || argumentValues[1] == DBNull.Value || argumentValues[2] == DBNull.Value) { return(DBNull.Value); } switch (((DateTime)argumentValues[0]).Kind) { case DateTimeKind.Utc: if ((int)argumentValues[1] != 0 && (int)argumentValues[2] != 0) { throw ExprException.MismatchKindandTimeSpan(); } break; case DateTimeKind.Local: if (DateTimeOffset.Now.Offset.Hours != (int)argumentValues[1] && DateTimeOffset.Now.Offset.Minutes != (int)argumentValues[2]) { throw ExprException.MismatchKindandTimeSpan(); } break; case DateTimeKind.Unspecified: break; } if ((int)argumentValues[1] < -14 || (int)argumentValues[1] > 14) { throw ExprException.InvalidHoursArgument(); } if ((int)argumentValues[2] < -59 || (int)argumentValues[2] > 59) { throw ExprException.InvalidMinutesArgument(); } // range should be within -14 hours and +14 hours if ((int)argumentValues[1] == 14 && (int)argumentValues[2] > 0) { throw ExprException.InvalidTimeZoneRange(); } if ((int)argumentValues[1] == -14 && (int)argumentValues[2] < 0) { throw ExprException.InvalidTimeZoneRange(); } return(new DateTimeOffset((DateTime)argumentValues[0], new TimeSpan((int)argumentValues[1], (int)argumentValues[2], 0))); default: throw ExprException.UndefinedFunction(s_funcs[_info]._name); } }
internal bool CanRemove(DataColumn column, bool fThrowException) { if (column == null) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.ArgumentNull(nameof(column)); } } if (column._table != _table) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveColumn(); } } // allow subclasses to complain first. _table.OnRemoveColumnInternal(column); // We need to make sure the column is not involved in any Relations or Constriants if (_table._primaryKey != null && _table._primaryKey.Key.ContainsColumn(column)) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemovePrimaryKey(); } } for (int i = 0; i < _table.ParentRelations.Count; i++) { if (_table.ParentRelations[i].ChildKey.ContainsColumn(column)) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveChildKey(_table.ParentRelations[i].RelationName); } } } for (int i = 0; i < _table.ChildRelations.Count; i++) { if (_table.ChildRelations[i].ParentKey.ContainsColumn(column)) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveChildKey(_table.ChildRelations[i].RelationName); } } } for (int i = 0; i < _table.Constraints.Count; i++) { if (_table.Constraints[i].ContainsColumn(column)) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveConstraint(_table.Constraints[i].ConstraintName, _table.Constraints[i].Table.TableName); } } } if (_table.DataSet != null) { for (ParentForeignKeyConstraintEnumerator en = new ParentForeignKeyConstraintEnumerator(_table.DataSet, _table); en.GetNext();) { Constraint constraint = en.GetConstraint(); if (((ForeignKeyConstraint)constraint).ParentKey.ContainsColumn(column)) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveConstraint(constraint.ConstraintName, constraint.Table.TableName); } } } } if (column._dependentColumns != null) { for (int i = 0; i < column._dependentColumns.Count; i++) { DataColumn col = column._dependentColumns[i]; if (_fInClear && (col.Table == _table || col.Table == null)) { continue; } if (col.Table == null) { continue; } Debug.Assert(col.Computed, "invalid (non an expression) column in the expression dependent columns"); DataExpression expr = col.DataExpression; if ((expr != null) && (expr.DependsOn(column))) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveExpression(col.ColumnName, col.Expression); } } } } // you can't remove a column participating in an index, // while index events are suspended else the indexes won't be properly maintained. // However, all the above checks should catch those participating columns. // except when a column is in a DataView RowFilter or Sort clause foreach (Index index in _table.LiveIndexes) { } return(true); }
internal void HandleDependentColumnList(DataExpression oldExpression, DataExpression newExpression) { DataColumn[] dependency; // remove this column from the dependentColumn list of the columns this column depends on. if (oldExpression != null) { dependency = oldExpression.GetDependency(); foreach (DataColumn col in dependency) { Debug.Assert(null != col, "null datacolumn in expression dependencies"); col.RemoveDependentColumn(this); if (col._table != _table) { _table.RemoveDependentColumn(this); } } _table.RemoveDependentColumn(this); } if (newExpression != null) { // get the list of columns that this expression depends on dependency = newExpression.GetDependency(); // add this column to dependent column list of each column this column depends on foreach (DataColumn col in dependency) { col.AddDependentColumn(this); if (col._table != _table) { _table.AddDependentColumn(this); } } _table.AddDependentColumn(this); } }
internal long Compare(object vLeft, object vRight, Type type, int op) { //Debug.WriteLine("Compare '" + vLeft.ToString() + "' to '" + vRight.ToString() + "' , type " + type.ToString()); long result = 0; bool typeMismatch = false; try { if (type == typeof(UInt64)) { Decimal dec = Convert.ToDecimal(vLeft) - Convert.ToDecimal(vRight); if (dec == 0) { result = 0; } else if (dec > 0) { result = 1; } else { result = -1; } } else if (type == typeof(char)) { result = Convert.ToInt32(vLeft) - Convert.ToInt32(Convert.ToChar(vRight)); } else if (ExpressionNode.IsInteger(type)) { Int64 a = Convert.ToInt64(vLeft); Int64 b = Convert.ToInt64(vRight); checked { result = a - b; } } else if (type == typeof(Decimal)) { Decimal a = Convert.ToDecimal(vLeft); Decimal b = Convert.ToDecimal(vRight); result = Decimal.Compare(a, b); } else if (type == typeof(double)) { Double a = Convert.ToDouble(vLeft); Double b = Convert.ToDouble(vRight); double d; checked { d = a - b; } if (d == 0) { result = 0; } else if (d > 0) { result = 1; } else { result = -1; } } else if (type == typeof(Single)) { Single a = Convert.ToSingle(vLeft); Single b = Convert.ToSingle(vRight); Single d; checked { d = a - b; } if (d == 0) { result = 0; } else if (d > 0) { result = 1; } else { result = -1; } } else if (type == typeof(DateTime)) { //Debug.WriteLine("Compare '" + vLeft.ToString() + "' to '" + vRight.ToString() + "'"); result = DateTime.Compare(Convert.ToDateTime(vLeft), Convert.ToDateTime(vRight)); //Debug.WriteLine("result = " + result.ToString()); } else if (type == typeof(string)) { //Debug.WriteLine("Compare '" + vLeft.ToString() + "' to '" + vRight.ToString() + "'"); result = table.Compare(Convert.ToString(vLeft), Convert.ToString(vRight), CompareOptions.None); //Debug.WriteLine("result = " + result.ToString()); } else if (type == typeof(Guid)) { //Debug.WriteLine("Compare '" + vLeft.ToString() + "' to '" + vRight.ToString() + "'"); result = ((Guid)vLeft).CompareTo((Guid)vRight); //Debug.WriteLine("result = " + result.ToString()); } else if (type == typeof(bool)) { if (op == Operators.EqualTo || op == Operators.NotEqual) { object bLeft = DataExpression.ToBoolean(vLeft); object bRight = DataExpression.ToBoolean(vRight); result = Convert.ToInt32(bLeft) - Convert.ToInt32(bRight); } else { typeMismatch = true; } } else { typeMismatch = true; } } catch { SetTypeMismatchError(op, vLeft.GetType(), vRight.GetType()); } if (typeMismatch) { SetTypeMismatchError(op, vLeft.GetType(), vRight.GetType()); } return(result); }
internal bool CanRemove(DataColumn column, bool fThrowException) { if (column == null) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.ArgumentNull("column"); } } if (column.table != table) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveColumn(); } } // allow subclasses to complain first. table.OnRemoveColumn(column); // We need to make sure the column is not involved in any Relations or Constriants if (table.primaryKey != null && table.primaryKey.Key.ContainsColumn(column)) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemovePrimaryKey(); } } for (int i = 0; i < table.ParentRelations.Count; i++) { if (table.ParentRelations[i].ChildKey.ContainsColumn(column)) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveChildKey(table.ParentRelations[i].RelationName); } } } for (int i = 0; i < table.ChildRelations.Count; i++) { if (table.ChildRelations[i].ParentKey.ContainsColumn(column)) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveChildKey(table.ChildRelations[i].RelationName); } } } for (int i = 0; i < table.Constraints.Count; i++) { if (table.Constraints[i].ContainsColumn(column)) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveConstraint(table.Constraints[i].ConstraintName, table.Constraints[i].Table.TableName); } } } if (table.DataSet != null) { for (ParentForeignKeyConstraintEnumerator en = new ParentForeignKeyConstraintEnumerator(table.DataSet, table); en.GetNext();) { Constraint constraint = en.GetConstraint(); if (((ForeignKeyConstraint)constraint).ParentKey.ContainsColumn(column)) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveConstraint(constraint.ConstraintName, constraint.Table.TableName); } } } } for (int i = 0; i < ColumnQueue.columnCount; i++) { DataColumn col = ColumnQueue.columns[i]; if (fInClear && (col.Table == table || col.Table == null)) { continue; } Debug.Assert(col.Computed, "invalid (non an expression) column in the expression column queue"); DataExpression expr = col.DataExpression; if (expr.DependsOn(column)) { if (!fThrowException) { return(false); } else { throw ExceptionBuilder.CannotRemoveExpression(col.ColumnName, col.Expression); } } } return(true); }
private object EvalFunction(FunctionId id, object[] argumentValues, DataRow row, DataRowVersion version) { StorageType type; switch (id) { case FunctionId.Charindex: if (!DataStorage.IsObjectNull(argumentValues[0]) && !DataStorage.IsObjectNull(argumentValues[1])) { if (argumentValues[0] is SqlString) { SqlString str6 = (SqlString)argumentValues[0]; argumentValues[0] = str6.Value; } if (argumentValues[1] is SqlString) { SqlString str5 = (SqlString)argumentValues[1]; argumentValues[1] = str5.Value; } return(((string)argumentValues[1]).IndexOf((string)argumentValues[0], StringComparison.Ordinal)); } return(DBNull.Value); case FunctionId.Len: { if (!(argumentValues[0] is SqlString)) { goto Label_02D4; } SqlString str4 = (SqlString)argumentValues[0]; if (!str4.IsNull) { SqlString str3 = (SqlString)argumentValues[0]; argumentValues[0] = str3.Value; goto Label_02D4; } return(DBNull.Value); } case FunctionId.Substring: { int startIndex = ((int)argumentValues[1]) - 1; int length = (int)argumentValues[2]; if (startIndex < 0) { throw ExprException.FunctionArgumentOutOfRange("index", "Substring"); } if (length < 0) { throw ExprException.FunctionArgumentOutOfRange("length", "Substring"); } if (length == 0) { return(""); } if (argumentValues[0] is SqlString) { SqlString str2 = (SqlString)argumentValues[0]; argumentValues[0] = str2.Value; } int num3 = ((string)argumentValues[0]).Length; if (startIndex > num3) { return(DBNull.Value); } if ((startIndex + length) > num3) { length = num3 - startIndex; } return(((string)argumentValues[0]).Substring(startIndex, length)); } case FunctionId.IsNull: if (!DataStorage.IsObjectNull(argumentValues[0])) { return(argumentValues[0]); } return(argumentValues[1]); case FunctionId.Iif: if (!DataExpression.ToBoolean(this.arguments[0].Eval(row, version))) { return(this.arguments[2].Eval(row, version)); } return(this.arguments[1].Eval(row, version)); case FunctionId.Convert: { if (this.argumentCount != 2) { throw ExprException.FunctionArgumentCount(this.name); } if (argumentValues[0] == DBNull.Value) { return(DBNull.Value); } Type dataType = (Type)argumentValues[1]; StorageType storageType = DataStorage.GetStorageType(dataType); type = DataStorage.GetStorageType(argumentValues[0].GetType()); if ((storageType == StorageType.DateTimeOffset) && (type == StorageType.String)) { return(SqlConvert.ConvertStringToDateTimeOffset((string)argumentValues[0], base.FormatProvider)); } if (StorageType.Object == storageType) { return(argumentValues[0]); } if ((storageType == StorageType.Guid) && (type == StorageType.String)) { return(new Guid((string)argumentValues[0])); } if (ExpressionNode.IsFloatSql(type) && ExpressionNode.IsIntegerSql(storageType)) { if (StorageType.Single == type) { return(SqlConvert.ChangeType2((float)SqlConvert.ChangeType2(argumentValues[0], StorageType.Single, typeof(float), base.FormatProvider), storageType, dataType, base.FormatProvider)); } if (StorageType.Double == type) { return(SqlConvert.ChangeType2((double)SqlConvert.ChangeType2(argumentValues[0], StorageType.Double, typeof(double), base.FormatProvider), storageType, dataType, base.FormatProvider)); } if (StorageType.Decimal == type) { return(SqlConvert.ChangeType2((decimal)SqlConvert.ChangeType2(argumentValues[0], StorageType.Decimal, typeof(decimal), base.FormatProvider), storageType, dataType, base.FormatProvider)); } } return(SqlConvert.ChangeType2(argumentValues[0], storageType, dataType, base.FormatProvider)); } case FunctionId.cInt: return(Convert.ToInt32(argumentValues[0], base.FormatProvider)); case FunctionId.cBool: { StorageType type4 = DataStorage.GetStorageType(argumentValues[0].GetType()); if (type4 > StorageType.Int32) { switch (type4) { case StorageType.Double: return(!(((double)argumentValues[0]) == 0.0)); case StorageType.String: return(bool.Parse((string)argumentValues[0])); } break; } switch (type4) { case StorageType.Boolean: return((bool)argumentValues[0]); case StorageType.Int32: return(((int)argumentValues[0]) != 0); } break; } case FunctionId.cDate: return(Convert.ToDateTime(argumentValues[0], base.FormatProvider)); case FunctionId.cDbl: return(Convert.ToDouble(argumentValues[0], base.FormatProvider)); case FunctionId.cStr: return(Convert.ToString(argumentValues[0], base.FormatProvider)); case FunctionId.Abs: type = DataStorage.GetStorageType(argumentValues[0].GetType()); if (!ExpressionNode.IsInteger(type)) { if (!ExpressionNode.IsNumeric(type)) { throw ExprException.ArgumentTypeInteger(funcs[this.info].name, 1); } return(Math.Abs((double)argumentValues[0])); } return(Math.Abs((long)argumentValues[0])); case FunctionId.In: throw ExprException.NYI(funcs[this.info].name); case FunctionId.Trim: if (!DataStorage.IsObjectNull(argumentValues[0])) { if (argumentValues[0] is SqlString) { SqlString str = (SqlString)argumentValues[0]; argumentValues[0] = str.Value; } return(((string)argumentValues[0]).Trim()); } return(DBNull.Value); case FunctionId.DateTimeOffset: if (((argumentValues[0] != DBNull.Value) && (argumentValues[1] != DBNull.Value)) && (argumentValues[2] != DBNull.Value)) { DateTime time = (DateTime)argumentValues[0]; switch (time.Kind) { case DateTimeKind.Utc: if ((((int)argumentValues[1]) != 0) && (((int)argumentValues[2]) != 0)) { throw ExprException.MismatchKindandTimeSpan(); } break; case DateTimeKind.Local: if ((DateTimeOffset.Now.Offset.Hours != ((int)argumentValues[1])) && (DateTimeOffset.Now.Offset.Minutes != ((int)argumentValues[2]))) { throw ExprException.MismatchKindandTimeSpan(); } break; } if ((((int)argumentValues[1]) < -14) || (((int)argumentValues[1]) > 14)) { throw ExprException.InvalidHoursArgument(); } if ((((int)argumentValues[2]) < -59) || (((int)argumentValues[2]) > 0x3b)) { throw ExprException.InvalidMinutesArgument(); } if ((((int)argumentValues[1]) == 14) && (((int)argumentValues[2]) > 0)) { throw ExprException.InvalidTimeZoneRange(); } if ((((int)argumentValues[1]) == -14) && (((int)argumentValues[2]) < 0)) { throw ExprException.InvalidTimeZoneRange(); } return(new DateTimeOffset((DateTime)argumentValues[0], new TimeSpan((int)argumentValues[1], (int)argumentValues[2], 0))); } return(DBNull.Value); default: throw ExprException.UndefinedFunction(funcs[this.info].name); } throw ExprException.DatatypeConvertion(argumentValues[0].GetType(), typeof(bool)); Label_02D4: return(((string)argumentValues[0]).Length); }
private object EvalUnaryOp(int op, object vl) { object value = DBNull.Value; if (DataExpression.IsUnknown(vl)) { return(DBNull.Value); } switch (op) { case Operators.Noop: return(vl); case Operators.UnaryPlus: if (ExpressionNode.IsNumeric(vl.GetType())) { return(vl); } throw ExprException.TypeMismatch(this.ToString()); case Operators.Negative: // the have to be better way for doing this.. if (ExpressionNode.IsNumeric(vl.GetType())) { if (vl is byte) { value = -(Byte)vl; } else if (vl is Int16) { value = -(Int16)vl; } else if (vl is Int32) { value = -(Int32)vl; } else if (vl is Int64) { value = -(Int64)vl; } else if (vl is Single) { value = -(Single)vl; } else if (vl is Double) { value = -(Double)vl; } else if (vl is Decimal) { value = -(Decimal)vl; } else { Debug.Assert(false, "Missing a type conversion " + vl.GetType().FullName); value = DBNull.Value; } return(value); } throw ExprException.TypeMismatch(this.ToString()); case Operators.Not: if (DataExpression.ToBoolean(vl) != false) { return(false); } return(true); default: throw ExprException.UnsupportedOperator(op); } }
internal bool CanRemove(DataColumn column, bool fThrowException) { if (column == null) { if (fThrowException) { throw ExceptionBuilder.ArgumentNull("column"); } return(false); } if (column.table != this.table) { if (fThrowException) { throw ExceptionBuilder.CannotRemoveColumn(); } return(false); } this.table.OnRemoveColumnInternal(column); if ((this.table.primaryKey != null) && this.table.primaryKey.Key.ContainsColumn(column)) { if (fThrowException) { throw ExceptionBuilder.CannotRemovePrimaryKey(); } return(false); } for (int i = 0; i < this.table.ParentRelations.Count; i++) { if (this.table.ParentRelations[i].ChildKey.ContainsColumn(column)) { if (fThrowException) { throw ExceptionBuilder.CannotRemoveChildKey(this.table.ParentRelations[i].RelationName); } return(false); } } for (int j = 0; j < this.table.ChildRelations.Count; j++) { if (this.table.ChildRelations[j].ParentKey.ContainsColumn(column)) { if (fThrowException) { throw ExceptionBuilder.CannotRemoveChildKey(this.table.ChildRelations[j].RelationName); } return(false); } } for (int k = 0; k < this.table.Constraints.Count; k++) { if (this.table.Constraints[k].ContainsColumn(column)) { if (fThrowException) { throw ExceptionBuilder.CannotRemoveConstraint(this.table.Constraints[k].ConstraintName, this.table.Constraints[k].Table.TableName); } return(false); } } if (this.table.DataSet != null) { ParentForeignKeyConstraintEnumerator enumerator = new ParentForeignKeyConstraintEnumerator(this.table.DataSet, this.table); while (enumerator.GetNext()) { Constraint constraint = enumerator.GetConstraint(); if (((ForeignKeyConstraint)constraint).ParentKey.ContainsColumn(column)) { if (fThrowException) { throw ExceptionBuilder.CannotRemoveConstraint(constraint.ConstraintName, constraint.Table.TableName); } return(false); } } } if (column.dependentColumns != null) { for (int m = 0; m < column.dependentColumns.Count; m++) { DataColumn column2 = column.dependentColumns[m]; if ((!this.fInClear || ((column2.Table != this.table) && (column2.Table != null))) && (column2.Table != null)) { DataExpression dataExpression = column2.DataExpression; if ((dataExpression != null) && dataExpression.DependsOn(column)) { if (fThrowException) { throw ExceptionBuilder.CannotRemoveExpression(column2.ColumnName, column2.Expression); } return(false); } } } } using (List <Index> .Enumerator enumerator2 = this.table.LiveIndexes.GetEnumerator()) { while (enumerator2.MoveNext()) { Index current = enumerator2.Current; } } return(true); }
private object EvalFunction(FunctionId id, object[] argumentValues, DataRow row, DataRowVersion version) { #if DEBUG if (CompModSwitches.FunctionNode.TraceVerbose) { Debug.WriteLine("calculate " + this.ToString()); } #endif switch (id) { case FunctionId.Abs: Debug.Assert(argumentCount == 1, "Invalid argument argumentCount for " + funcs[info].name + " : " + argumentCount.ToString()); if (ExpressionNode.IsInteger(argumentValues[0].GetType())) { return(Math.Abs((Int64)argumentValues[0])); } if (ExpressionNode.IsNumeric(argumentValues[0].GetType())) { return(Math.Abs((double)argumentValues[0])); } throw ExprException.ArgumentTypeInteger(funcs[info].name, 1); case FunctionId.cBool: Debug.Assert(argumentCount == 1, "Invalid argument argumentCount for " + funcs[info].name + " : " + argumentCount.ToString()); if (argumentValues[0] is bool) { return((bool)argumentValues[0]); } if (argumentValues[0] is int) { return((int)argumentValues[0] != 0); } if (argumentValues[0] is double) { return((double)argumentValues[0] != 0.0); } if (argumentValues[0] is string) { return(Boolean.Parse((string)argumentValues[0])); } throw ExprException.DatatypeConvertion(argumentValues[0].GetType(), typeof(bool)); case FunctionId.cInt: Debug.Assert(argumentCount == 1, "Invalid argument argumentCount for " + funcs[info].name + " : " + argumentCount.ToString()); return(Convert.ToInt32(argumentValues[0])); case FunctionId.cDate: Debug.Assert(argumentCount == 1, "Invalid argument argumentCount for " + funcs[info].name + " : " + argumentCount.ToString()); #if DEBUG if (CompModSwitches.FunctionNode.TraceVerbose) { Debug.WriteLine("Convert " + Convert.ToString(argumentValues[0]) + " of " + argumentValues[0].GetType().Name + " to datetime"); } #endif return(Convert.ToDateTime(argumentValues[0])); case FunctionId.cDbl: Debug.Assert(argumentCount == 1, "Invalid argument argumentCount for " + funcs[info].name + " : " + argumentCount.ToString()); return(Convert.ToDouble(argumentValues[0])); case FunctionId.cStr: Debug.Assert(argumentCount == 1, "Invalid argument argumentCount for " + funcs[info].name + " : " + argumentCount.ToString()); return(Convert.ToString(argumentValues[0])); case FunctionId.Charindex: Debug.Assert(argumentCount == 2, "Invalid argument argumentCount for " + funcs[info].name + " : " + argumentCount.ToString()); Debug.Assert(argumentValues[0] is string, "Invalid argument type for " + funcs[info].name); Debug.Assert(argumentValues[1] is string, "Invalid argument type for " + funcs[info].name); if ((argumentValues[0] == DBNull.Value) || (argumentValues[1] == DBNull.Value)) { return(DBNull.Value); } return(((string)argumentValues[1]).IndexOf((string)argumentValues[0])); case FunctionId.Iif: Debug.Assert(argumentCount == 3, "Invalid argument argumentCount: " + argumentCount.ToString()); object first = this.arguments[0].Eval(row, version); if (DataExpression.ToBoolean(first) != false) { return(this.arguments[1].Eval(row, version)); } else { return(this.arguments[2].Eval(row, version)); } case FunctionId.In: // we never evaluate IN directly: IN as a binary operator, so evaluation of this should be in // BinaryNode class throw ExprException.NYI(funcs[info].name); case FunctionId.IsNull: Debug.Assert(argumentCount == 2, "Invalid argument argumentCount: "); if ((argumentValues[0]) == DBNull.Value) { return(argumentValues[1]); } else { return(argumentValues[0]); } case FunctionId.Len: Debug.Assert(argumentCount == 1, "Invalid argument argumentCount for " + funcs[info].name + " : " + argumentCount.ToString()); Debug.Assert(argumentValues[0] is string, "Invalid argument type for " + funcs[info].name); return(((string)argumentValues[0]).Length); case FunctionId.Substring: Debug.Assert(argumentCount == 3, "Invalid argument argumentCount: " + argumentCount.ToString()); Debug.Assert(argumentValues[0] is string, "Invalid first argument " + argumentValues[0].GetType().FullName + " in " + funcs[info].name); Debug.Assert(argumentValues[1] is int, "Invalid second argument " + argumentValues[1].GetType().FullName + " in " + funcs[info].name); Debug.Assert(argumentValues[2] is int, "Invalid third argument " + argumentValues[2].GetType().FullName + " in " + funcs[info].name); // work around the differences in COM+ and VBA implementation of the Substring function // 1. The <index> Argument is 0-based in COM+, and 1-based in VBA // 2. If the <Length> argument is longer then the string length COM+ throws an ArgumentException // but our users still want to get a result. int start = (int)argumentValues[1] - 1; int length = (int)argumentValues[2]; if (start < 0) { throw ExprException.FunctionArgumentOutOfRange("index", "Substring"); } if (length < 0) { throw ExprException.FunctionArgumentOutOfRange("length", "Substring"); } if (length == 0) { return(""); } int src_length = ((string)argumentValues[0]).Length; if (start > src_length) { return(DBNull.Value); } if (start + length > src_length) { length = src_length - start; } return(((string)argumentValues[0]).Substring(start, length)); case FunctionId.Trim: { Debug.Assert(argumentCount == 1, "Invalid argument argumentCount for " + funcs[info].name + " : " + argumentCount.ToString()); Debug.Assert(argumentValues[0] is string, "Invalid argument type for " + funcs[info].name); if (argumentValues[0] == DBNull.Value) { return(DBNull.Value); } return(((string)argumentValues[0]).Trim()); } case FunctionId.Convert: if (argumentCount != 2) { throw ExprException.FunctionArgumentCount(this.name); } if (argumentValues[0] == DBNull.Value) { return(DBNull.Value); } Type type = (Type)argumentValues[1]; Type _oType = argumentValues[0].GetType(); if (type != typeof(object)) { if ((type == typeof(Guid)) && (_oType == typeof(String))) { return(new Guid((string)argumentValues[0])); } if (ExpressionNode.IsFloat(_oType) && ExpressionNode.IsInteger(type)) { if (_oType == typeof(Single)) { return(Convert.ChangeType((Single)Convert.ChangeType(argumentValues[0], typeof(Single)), type)); } else if (_oType == typeof(double)) { return(Convert.ChangeType((double)Convert.ChangeType(argumentValues[0], typeof(double)), type)); } else { return(Convert.ChangeType((decimal)Convert.ChangeType(argumentValues[0], typeof(decimal)), type)); } } return(Convert.ChangeType(argumentValues[0], type)); } return(argumentValues[0]); default: throw ExprException.UndefinedFunction(funcs[info].name); } }