public static SqlInt16 ConvertToSqlInt16(object value) { if (value == DBNull.Value) { return(SqlInt16.Null); } Type type = value.GetType(); switch (DataStorageHelper.GetStorageType(type)) { case StorageType.Byte: return((SqlInt16)((short)(byte)value)); case StorageType.Int16: return((SqlInt16)((short)value)); case StorageType.SqlByte: return((SqlInt16)((SqlByte)value)); case StorageType.SqlInt16: return((SqlInt16)value); default: throw InvalidExpressionException.SqlConvertFailed(type, typeof(SqlInt16)); } }
public virtual object Abs(object value) { if (value == null || DBNull.Value == value) { return((object)null); } StorageType storageType = DataStorageHelper.GetStorageType(value.GetType()); if (!DataStorageHelper.IsInteger(storageType)) { if (!DataStorageHelper.IsNumeric(storageType)) { throw InvalidExpressionException.ArgumentTypeInteger(nameof(Abs), 1); } try { return((object)Math.Abs((Decimal)value)); } catch (Exception ex) { return((object)Math.Abs((double)value)); } } else { try { return((object)Math.Abs((int)value)); } catch (Exception ex) { return((object)Math.Abs((long)value)); } } }
public static bool ToBoolean(object value) { if (DataStorageHelper.IsUnknown(value)) { return(false); } if (value is bool) { return((bool)value); } if (value is SqlBoolean) { return(((SqlBoolean)value).IsTrue); } if (!(value is string)) { throw InvalidExpressionException.DatavalueConvertion(value, typeof(bool), (Exception)null); } try { return(bool.Parse((string)value)); } catch (Exception ex) { throw InvalidExpressionException.DatavalueConvertion(value, typeof(bool), ex); } }
public virtual int Sign(object value) { StorageType storageType = DataStorageHelper.GetStorageType(value.GetType()); if (!DataStorageHelper.IsInteger(storageType)) { if (!DataStorageHelper.IsNumeric(storageType)) { throw InvalidExpressionException.ArgumentTypeInteger(nameof(Sign), 1); } try { return(Math.Sign((Decimal)value)); } catch (Exception ex) { return(Math.Sign((double)value)); } } else { try { return(Math.Sign((int)value)); } catch (Exception ex) { return(Math.Sign((long)value)); } } }
public virtual object Min(object value1, object value2) { StorageType storageType1 = DataStorageHelper.GetStorageType(value1.GetType()); StorageType storageType2 = DataStorageHelper.GetStorageType(value2.GetType()); if (!(DataStorageHelper.IsInteger(storageType1) && DataStorageHelper.IsInteger(storageType2))) { if (!(DataStorageHelper.IsNumeric(storageType1) && DataStorageHelper.IsNumeric(storageType2))) { throw InvalidExpressionException.ArgumentTypeInteger("Min", 1); } try { return(Math.Min((decimal)value1, (decimal)value2)); } catch (Exception) { return(Math.Min((double)value1, (double)value2)); } } try { return(Math.Min((int)value1, (int)value2)); } catch (Exception) { return(Math.Min((long)value1, (long)value2)); } }
public static Exception DatavalueConvertion( object value, Type type, Exception innerException) { return(InvalidExpressionException.CreateException(innerException, "Cannot convert value '{0}' to Type: {1}.")); }
private static int Compare(object value1, object value2, FunctionContext context, string exp) { Type type1 = value1.GetType(); Type type2 = value2.GetType(); StorageType storageType1 = DataStorageHelper.GetStorageType(type1); StorageType storageType2 = DataStorageHelper.GetStorageType(type2); bool flag1 = DataStorageHelper.IsSqlType(storageType1); bool flag2 = DataStorageHelper.IsSqlType(storageType2); if (flag1 && DataStorageHelper.IsObjectSqlNull(value1) || value1 == DBNull.Value) { return(-1); } if (flag2 && DataStorageHelper.IsObjectSqlNull(value2) || value2 == DBNull.Value) { return(1); } StorageType resultType = flag1 || flag2?DataStorageHelper.ResultSqlType(storageType1, storageType2, false, false, Operator.LessThen) : DataStorageHelper.ResultType(storageType1, storageType2, false, false, Operator.LessThen); if (resultType == StorageType.Empty) { throw InvalidExpressionException.TypeMismatch(exp); } return(Operator.BinaryCompare(value1, value2, resultType, Operator.LessOrEqual, new OperatorContext((object)null, context.FormatProvider, (object)null))); }
public AggregateNode(string name) : base(null, name) { if (null == (this.methodInfo = AggregateNode.LookupFunc(name))) { throw InvalidExpressionException.UndefinedFunction(name); } }
public static bool ToBoolean(object value) { if (IsUnknown(value)) { return(false); } if (value is bool) { return((bool)value); } if (value is SqlBoolean) { SqlBoolean flag2 = (SqlBoolean)value; return(flag2.IsTrue); } if (value is string) { try { return(bool.Parse((string)value)); } catch (Exception exception) { throw InvalidExpressionException.DatavalueConvertion(value, typeof(bool), exception); } } throw InvalidExpressionException.DatavalueConvertion(value, typeof(bool), null); }
public static SqlInt32 ConvertToSqlInt32(object value) { if (value == DBNull.Value) { return(SqlInt32.Null); } Type dataType = value.GetType(); switch (DataStorageHelper.GetStorageType(dataType)) { case StorageType.Byte: return((SqlInt32)((byte)value)); case StorageType.Int16: return((SqlInt32)((short)value)); case StorageType.UInt16: return((SqlInt32)((ushort)value)); case StorageType.Int32: return((int)value); case StorageType.SqlByte: return((SqlByte)value); case StorageType.SqlInt16: return((SqlInt16)value); case StorageType.SqlInt32: return((SqlInt32)value); } throw InvalidExpressionException.SqlConvertFailed(dataType, typeof(SqlInt32)); }
public virtual object Abs(object value) { if (null == value || DBNull.Value == value) { return(null); } StorageType storageType = DataStorageHelper.GetStorageType(value.GetType()); if (!DataStorageHelper.IsInteger(storageType)) { if (!DataStorageHelper.IsNumeric(storageType)) { throw InvalidExpressionException.ArgumentTypeInteger("Abs", 1); } try { return(Math.Abs((decimal)value)); } catch (Exception /*e*/) { return(Math.Abs((double)value)); } } try { return(Math.Abs((int)value)); } catch (Exception /*e*/) { return(Math.Abs((long)value)); } }
public virtual object Min(object value1, object value2) { StorageType storageType1 = DataStorageHelper.GetStorageType(value1.GetType()); StorageType storageType2 = DataStorageHelper.GetStorageType(value2.GetType()); if (DataStorageHelper.IsInteger(storageType1)) { if (DataStorageHelper.IsInteger(storageType2)) { try { return((object)Math.Min((int)value1, (int)value2)); } catch (Exception ex) { return((object)Math.Min((long)value1, (long)value2)); } } } if (DataStorageHelper.IsNumeric(storageType1)) { if (DataStorageHelper.IsNumeric(storageType2)) { try { return((object)Math.Min((Decimal)value1, (Decimal)value2)); } catch (Exception ex) { return((object)Math.Min((double)value1, (double)value2)); } } } throw InvalidExpressionException.ArgumentTypeInteger(nameof(Min), 1); }
public static SqlSingle ConvertToSqlSingle(object value) { if (value == DBNull.Value) { return(SqlSingle.Null); } Type type = value.GetType(); switch (DataStorageHelper.GetStorageType(type)) { case StorageType.Byte: return((SqlSingle)((float)(byte)value)); case StorageType.Int16: return((SqlSingle)((float)(short)value)); case StorageType.UInt16: return((SqlSingle)((float)(ushort)value)); case StorageType.Int32: return((SqlSingle)((float)(int)value)); case StorageType.UInt32: return((SqlSingle)((float)(uint)value)); case StorageType.Int64: return((SqlSingle)((float)(long)value)); case StorageType.UInt64: return((SqlSingle)((float)(ulong)value)); case StorageType.Single: return((SqlSingle)((float)value)); case StorageType.SqlByte: return((SqlSingle)((SqlByte)value)); case StorageType.SqlDecimal: return((SqlSingle)((SqlDecimal)value)); case StorageType.SqlInt16: return((SqlSingle)((SqlInt16)value)); case StorageType.SqlInt32: return((SqlSingle)((SqlInt32)value)); case StorageType.SqlInt64: return((SqlSingle)((SqlInt64)value)); case StorageType.SqlMoney: return((SqlSingle)((SqlMoney)value)); case StorageType.SqlSingle: return((SqlSingle)value); default: throw InvalidExpressionException.SqlConvertFailed(type, typeof(SqlSingle)); } }
public AggregateNode(string name) : base((ExpressionNode)null, name) { if ((object)(this.methodInfo = AggregateNode.LookupFunc(name)) == null) { throw InvalidExpressionException.UndefinedFunction(name); } }
/// <returns> /// -1: value1 < value2 /// 0: value1 = value2 /// 1: value1 > value2 /// </returns> static int Compare(object value1 , object value2 , FunctionContext context , string exp) { Type lType = value1.GetType(); Type rType = value2.GetType(); StorageType lStorageType = DataStorageHelper.GetStorageType(lType); StorageType rStorageType = DataStorageHelper.GetStorageType(rType); bool isSqlTypeL = DataStorageHelper.IsSqlType(lStorageType); bool isSqlTypeR = DataStorageHelper.IsSqlType(rStorageType); if ((isSqlTypeL && DataStorageHelper.IsObjectSqlNull(value1)) || value1 == DBNull.Value) { return(-1); } if ((isSqlTypeR && DataStorageHelper.IsObjectSqlNull(value2)) || value2 == DBNull.Value) { return(1); } StorageType resultType = StorageType.Empty; if (isSqlTypeL || isSqlTypeR) { resultType = DataStorageHelper.ResultSqlType(lStorageType , rStorageType , false , false , Operator.LessThen); // op doesn't matter here } else { resultType = DataStorageHelper.ResultType(lStorageType , rStorageType , false , false , Operator.LessThen); // op doesn't matter here } if (resultType == StorageType.Empty) { throw InvalidExpressionException.TypeMismatch(exp); } return(Operator.BinaryCompare(value1 , value2 , resultType , Operator.LessOrEqual // op doesn't matter here , context.FormatProvider)); }
public override object Eval(object row, object context) { object lValue = base.Left.Eval(row, context); if (!DataStorageHelper.IsObjectNull(lValue)) { if (!(lValue is string || lValue is SqlString)) { throw InvalidExpressionException.TypeMismatchInBinop(Operator.Like, lValue.GetType(), typeof(string)); } object rValue = base.Right.Eval(row, context); if (DataStorageHelper.IsObjectNull(rValue)) { return(DBNull.Value); } if (!(rValue is string || rValue is SqlString)) { throw InvalidExpressionException.TypeMismatchInBinop(Operator.Like, typeof(string), rValue.GetType()); } string text = lValue.ToString(); string pattern = this.AnalizePattern(rValue.ToString()); char[] trimChars = new char[] { ' ', '\u3000' }; text = text.TrimEnd(trimChars); CompareInfo compareInfo = base.Culture.CompareInfo; switch (this.kind) { case match_left: return(0 == compareInfo.IndexOf(text, pattern, this.compareFlags)); case match_right: { string text4 = pattern.TrimEnd(trimChars); return(compareInfo.IsSuffix(text, text4, this.compareFlags)); } case match_middle: return(0 <= compareInfo.IndexOf(text, pattern, this.compareFlags)); case match_exact: return(0 == compareInfo.Compare(text, pattern, this.compareFlags)); case match_all: return(true); } } return(DBNull.Value); }
public static SqlChars ConvertToSqlChars(object value) { if (value == DBNull.Value) { return(SqlChars.Null); } Type dataType = value.GetType(); if (DataStorageHelper.GetStorageType(dataType) != StorageType.SqlChars) { throw InvalidExpressionException.SqlConvertFailed(dataType, typeof(SqlChars)); } return((SqlChars)value); }
public virtual object CBool(object value) { if (DBNull.Value == value) { value = (object)null; } try { return((object)Convert.ToBoolean(value)); } catch (Exception ex) { throw InvalidExpressionException.DatavalueConvertion(value, typeof(bool), ex); } }
public override object Eval(object row, object context) { object obj1 = this.Left.Eval(row, context); if (!DataStorageHelper.IsObjectNull(obj1)) { if (!(obj1 is string) && !(obj1 is SqlString)) { Guid guid = obj1 as Guid; } object obj2 = this.Right.Eval(row, context); if (DataStorageHelper.IsObjectNull(obj2)) { return((object)DBNull.Value); } if (!(obj2 is string) && !(obj2 is SqlString) && !(obj1 is Guid)) { throw InvalidExpressionException.TypeMismatchInBinop(Operator.Like, typeof(string), obj2.GetType()); } string str = obj1.ToString(); string string2 = this.AnalizePattern(obj2.ToString()); char[] chArray = new char[2] { ' ', ' ' }; CompareInfo compareInfo = this.Culture.CompareInfo; switch (this.kind) { case 1: return((object)(0 == compareInfo.IndexOf(str, string2, this.compareFlags))); case 2: string suffix = string2.TrimEnd(chArray); return((object)compareInfo.IsSuffix(str, suffix, this.compareFlags)); case 3: return((object)(0 <= compareInfo.IndexOf(str, string2, this.compareFlags))); case 4: return((object)(0 == compareInfo.Compare(str, string2, this.compareFlags))); case 5: return((object)true); } } return((object)DBNull.Value); }
public static SqlBoolean ConvertToSqlBoolean(object value) { if ((value == DBNull.Value) || (value == null)) { return(SqlBoolean.Null); } Type dataType = value.GetType(); StorageType storageType = DataStorageHelper.GetStorageType(dataType); if (storageType != StorageType.Boolean) { if (storageType != StorageType.SqlBoolean) { throw InvalidExpressionException.SqlConvertFailed(dataType, typeof(SqlBoolean)); } return((SqlBoolean)value); } return((bool)value); }
public static SqlBinary ConvertToSqlBinary(object value) { if (value == DBNull.Value) { return(SqlBinary.Null); } Type dataType = value.GetType(); StorageType storageType = DataStorageHelper.GetStorageType(dataType); if (storageType != StorageType.ByteArray) { if (storageType != StorageType.SqlBinary) { throw InvalidExpressionException.SqlConvertFailed(dataType, typeof(SqlBinary)); } return((SqlBinary)value); } return((byte[])value); }
public static SqlGuid ConvertToSqlGuid(object value) { if (value == DBNull.Value) { return(SqlGuid.Null); } Type dataType = value.GetType(); StorageType storageType = DataStorageHelper.GetStorageType(dataType); if (storageType != StorageType.Guid) { if (storageType != StorageType.SqlGuid) { throw InvalidExpressionException.SqlConvertFailed(dataType, typeof(SqlGuid)); } return((SqlGuid)value); } return((Guid)value); }
public override object Eval(object row, object context) { ArrayList list = new ArrayList(); if (null != this.arguments && this.arguments.Count > 0) { for (int i = 0; i < this.arguments.Count; i++) { list.Add(NormalizeValue(this.arguments[i].Eval(row, context))); } } object[] args = list.ToArray(); object target = context; if (null != this.node) { target = this.node.Eval(row, context); } if (null == this.method) { this.method = this.GetMethod(target, args); } MethodInfo mi = this.method; //this.GetMethod(target, args); if (null == mi) { throw InvalidExpressionException.UndefinedFunction(name); } try { return(mi.Invoke(target, args)); } catch (Exception ex) { throw InvalidExpressionException.ErrorInFunc(mi.Name, ex); } }
public static SqlString ConvertToSqlString(object value) { if (value == DBNull.Value || value == null) { return(SqlString.Null); } Type type = value.GetType(); switch (DataStorageHelper.GetStorageType(type)) { case StorageType.String: return((SqlString)((string)value)); case StorageType.SqlString: return((SqlString)value); default: throw InvalidExpressionException.SqlConvertFailed(type, typeof(SqlString)); } }
public static SqlGuid ConvertToSqlGuid(object value) { if (value == DBNull.Value) { return(SqlGuid.Null); } Type type = value.GetType(); switch (DataStorageHelper.GetStorageType(type)) { case StorageType.Guid: return((SqlGuid)((Guid)value)); case StorageType.SqlGuid: return((SqlGuid)value); default: throw InvalidExpressionException.SqlConvertFailed(type, typeof(SqlGuid)); } }
public static SqlBinary ConvertToSqlBinary(object value) { if (value == DBNull.Value) { return(SqlBinary.Null); } Type type = value.GetType(); switch (DataStorageHelper.GetStorageType(type)) { case StorageType.ByteArray: return((SqlBinary)((byte[])value)); case StorageType.SqlBinary: return((SqlBinary)value); default: throw InvalidExpressionException.SqlConvertFailed(type, typeof(SqlBinary)); } }
public override object Eval(object row, object context) { object component = context; if (this.parent != null) { component = this.parent.Eval(row, context); } else if ("Fields" == this.name) { return(row); } if (component == null) { return((object)null); } IDataItem dataItem = row as IDataItem; if (dataItem != null) { return(dataItem[this.name]); } PropertyDescriptor propertyDescriptor = (PropertyDescriptor)null; Type type = component.GetType(); if (!this.properties.TryGetValue(type, out propertyDescriptor)) { PropertyDescriptorCollection descriptorCollection = !(component is IDataDescriptor) ? TypeDescriptor.GetProperties(component) : ((IDataDescriptor)component).GetProperties(); if (descriptorCollection != null && descriptorCollection.Count > 0) { bool ignoreCase = !(context is ExpressionContext) || !((ExpressionContext)context).CaseSensitive; this.properties[type] = propertyDescriptor = descriptorCollection.Find(this.name, ignoreCase); } } if (propertyDescriptor != null) { return(propertyDescriptor.GetValue(component)); } throw InvalidExpressionException.UndefinedObject(this.name); }
public override object Eval(object row, object context) { ArrayList arrayList = new ArrayList(); if (this.arguments != null && this.arguments.Count > 0) { for (int index = 0; index < this.arguments.Count; ++index) { arrayList.Add(FunctionNode.NormalizeValue(this.arguments[index].Eval(row, context))); } } object[] array = arrayList.ToArray(); object target = context; if (this.node != null) { target = this.node.Eval(row, context); } if ((object)this.method == null) { this.method = this.GetMethod(target, array); } MethodInfo method = this.method; if ((object)method == null) { throw InvalidExpressionException.UndefinedFunction(this.name); } try { return(method.Invoke(target, array)); } catch (Exception ex) { throw InvalidExpressionException.ErrorInFunc(method.Name, ex); } }
public static StorageType ResultType( StorageType left, StorageType right, bool lc, bool rc, Operator op) { if (left == StorageType.Guid && right == StorageType.Guid && Operator.IsRelational(op) || left == StorageType.String && right == StorageType.Guid && Operator.IsRelational(op)) { return(left); } if (left == StorageType.Guid && right == StorageType.String && Operator.IsRelational(op)) { return(right); } int precedence1 = (int)DataStorageHelper.GetPrecedence(left); if (precedence1 == 0) { return(StorageType.Empty); } int precedence2 = (int)DataStorageHelper.GetPrecedence(right); if (precedence2 == 0) { return(StorageType.Empty); } if (Operator.IsLogical(op)) { return(left == StorageType.Boolean && right == StorageType.Boolean ? StorageType.Boolean : StorageType.Empty); } if (op == Operator.Plus && (left == StorageType.String || right == StorageType.String)) { return(StorageType.String); } DataTypePrecedence code = (DataTypePrecedence)Math.Max(precedence1, precedence2); StorageType precedenceType = DataStorageHelper.GetPrecedenceType(code); if (Operator.IsArithmetical(op) && precedenceType != StorageType.String && precedenceType != StorageType.Char && (!DataStorageHelper.IsNumeric(left) || !DataStorageHelper.IsNumeric(right))) { return(StorageType.Empty); } if (op == Operator.Multiply && DataStorageHelper.IsInteger(precedenceType)) { return(StorageType.Double); } if (!DataStorageHelper.IsMixed(left, right)) { return(precedenceType); } if (lc && !rc) { return(right); } if (!lc && rc) { return(left); } if (!DataStorageHelper.IsUnsigned(precedenceType)) { return(precedenceType); } if (code >= DataTypePrecedence.UInt64) { throw InvalidExpressionException.AmbiguousBinop(op, DataStorageHelper.GetTypeStorage(left), DataStorageHelper.GetTypeStorage(right)); } return(DataStorageHelper.GetPrecedenceType(code + 1)); }
public static StorageType ResultSqlType( StorageType left, StorageType right, bool lc, bool rc, Operator op) { int precedence1 = (int)DataStorageHelper.GetPrecedence(left); if (precedence1 == 0) { return(StorageType.Empty); } int precedence2 = (int)DataStorageHelper.GetPrecedence(right); if (precedence2 == 0) { return(StorageType.Empty); } if (Operator.IsLogical(op)) { if (left != StorageType.Boolean && left != StorageType.SqlBoolean || right != StorageType.Boolean && right != StorageType.SqlBoolean) { return(StorageType.Empty); } return(left == StorageType.Boolean && right == StorageType.Boolean ? StorageType.Boolean : StorageType.SqlBoolean); } if (op == Operator.Plus) { if (left == StorageType.SqlString || right == StorageType.SqlString) { return(StorageType.SqlString); } if (left == StorageType.String || right == StorageType.String) { return(StorageType.String); } } if (left == StorageType.SqlBinary && right != StorageType.SqlBinary || left != StorageType.SqlBinary && right == StorageType.SqlBinary || (left == StorageType.SqlGuid && right != StorageType.SqlGuid || left != StorageType.SqlGuid && right == StorageType.SqlGuid) || (precedence1 > 19 && precedence2 < 20 || precedence1 < 20 && precedence2 > 19)) { return(StorageType.Empty); } if (precedence1 > 19) { if (op == Operator.Plus || op == Operator.Minus) { if (left == StorageType.TimeSpan) { return(right); } if (right == StorageType.TimeSpan) { return(left); } return(StorageType.Empty); } if (!Operator.IsRelational(op)) { return(StorageType.Empty); } return(left); } DataTypePrecedence code = (DataTypePrecedence)Math.Max(precedence1, precedence2); DataStorageHelper.GetPrecedenceType(code); StorageType type = DataStorageHelper.GetPrecedenceType((DataTypePrecedence)DataStorageHelper.SqlResultType((int)code)); if (Operator.IsArithmetical(op) && type != StorageType.String && (type != StorageType.Char && type != StorageType.SqlString) && (!DataStorageHelper.IsNumericSql(left) || !DataStorageHelper.IsNumericSql(right))) { return(StorageType.Empty); } if (op == Operator.Multiply && DataStorageHelper.IsIntegerSql(type)) { return(StorageType.SqlDouble); } if (type == StorageType.SqlMoney && left != StorageType.SqlMoney && right != StorageType.SqlMoney) { type = StorageType.SqlDecimal; } if (!DataStorageHelper.IsMixedSql(left, right) || !DataStorageHelper.IsUnsignedSql(type)) { return(type); } if (code >= DataTypePrecedence.UInt64) { throw InvalidExpressionException.AmbiguousBinop(op, DataStorageHelper.GetTypeStorage(left), DataStorageHelper.GetTypeStorage(right)); } return(DataStorageHelper.GetPrecedenceType(code + 1)); }