public override SqlBoolean NotEqual(ISqlValue a, ISqlValue b) { var b1 = (SqlBoolean)a; var b2 = (SqlBoolean)b; return(!b1.Equals(b2)); }
public override ISqlValue Cast(ISqlValue value, SqlType destType) { if (!(value is ISqlBinary)) { throw new ArgumentException(); } var binary = ((ISqlBinary)value); if (destType is SqlBooleanType) { return(ToBoolean(binary)); } if (destType is SqlCharacterType) { return(ToString(binary, (SqlCharacterType)destType)); } if (destType is SqlNumericType) { return(ToNumber(binary, (SqlNumericType)destType)); } if (destType is SqlDateTimeType) { return(ToDate(binary)); } return(base.Cast(value, destType)); }
public override bool IsInstanceOf(ISqlValue value) { if (value is SqlNumber) { var number = (SqlNumber)value; switch (TypeCode) { case SqlTypeCode.Integer: case SqlTypeCode.TinyInt: case SqlTypeCode.SmallInt: case SqlTypeCode.BigInt: return(number.Scale == 0 && number.Precision <= Precision); case SqlTypeCode.Double: case SqlTypeCode.Float: return(number.Precision <= Precision); case SqlTypeCode.Numeric: return(number.Precision <= Precision && (Scale < 0 || number.Scale <= Scale)); case SqlTypeCode.VarNumeric: return(number.Precision > 0 && number.Scale >= 0); } } return(value is SqlNull); }
public override bool CanCastTo(ISqlValue value, SqlType destType) { return(destType is SqlBooleanType || destType is SqlNumericType || destType is SqlCharacterType || destType is SqlDateTimeType); }
public override ISqlValue NormalizeValue(ISqlValue value) { if (value is SqlNull) { return(value); } if (!(value is SqlDateTime)) { throw new ArgumentException(); } var date = (SqlDateTime)value; switch (TypeCode) { case SqlTypeCode.Time: return(date.TimePart); case SqlTypeCode.Date: return(date.DatePart); } return(base.NormalizeValue(value)); }
public override ISqlValue Cast(ISqlValue value, SqlType destType) { if (!(value is SqlBoolean)) { throw new ArgumentException($"Cannot cast from {ToString()} to {destType}"); } var b = (SqlBoolean)value; if (destType is SqlBooleanType) { return(b); } if (destType is SqlNumericType) { return(ToNumber(b)); } if (destType is SqlBinaryType) { return(ToBinary(b)); } if (destType is SqlCharacterType) { return(ToString(b, (SqlCharacterType)destType)); } return(base.Cast(value, destType)); }
public override ISqlValue NormalizeValue(ISqlValue value) { if (value is SqlNull) { return(value); } if (!(value is SqlNumber)) { throw new ArgumentException(); } var number = (SqlNumber)value; switch (TypeCode) { case SqlTypeCode.TinyInt: case SqlTypeCode.SmallInt: case SqlTypeCode.Integer: case SqlTypeCode.BigInt: return(ToInteger(number)); case SqlTypeCode.Real: case SqlTypeCode.Float: case SqlTypeCode.Double: return(ToFloatingPoint(number)); case SqlTypeCode.Numeric: return(ToDecimal(number)); } return(base.NormalizeValue(value)); }
public override SqlBoolean GreaterOrEqual(ISqlValue a, ISqlValue b) { var b1 = (SqlBoolean)a; var b2 = (SqlBoolean)b; return(b1 >= b2); }
/// <inheritdoc/> public int CompareTo(ISqlValue other) { SqlBoolean otherBoolean; if (other is SqlNumber) { var num = (SqlNumber)other; if (num == SqlNumber.One) { otherBoolean = True; } else if (num == SqlNumber.Zero) { otherBoolean = False; } else { throw new ArgumentOutOfRangeException("other", "The given numeric value is out of range for a comparison with SQL BOOLEAN."); } } else if (other is SqlBoolean) { otherBoolean = (SqlBoolean)other; } else { throw new ArgumentException(String.Format("Object of type {0} cannot be compared to SQL BOOLEAN", other.GetType().FullName)); } return(CompareTo(otherBoolean)); }
public override SqlBoolean LessOrEqual(ISqlValue a, ISqlValue b) { var b1 = (SqlBoolean)a; var b2 = (SqlBoolean)b; return(b1 <= b2); }
public override ISqlValue XOr(ISqlValue a, ISqlValue b) { var b1 = (SqlBoolean)a; var b2 = (SqlBoolean)b; return(b1 ^ b2); }
/// <summary> /// Constructs a named parameter of the given type /// and with a provided value. /// </summary> /// <param name="name">The name of the parameter. This can be /// either a <see cref="Marker"/> value or a variable name.</param> /// <param name="sqlType">The SQL type of the parameter</param> /// <param name="value">The value of the parameter</param> /// <exception cref="ArgumentNullException">If the specified /// <paramref name="sqlType"/> is <c>null</c> or the <paramref name="name"/> /// is <c>null</c> or empty.</exception> /// <exception cref="ArgumentException">If the specified <paramref name="name"/> /// is one character long and the single character is not the <see cref="Marker"/> /// or if the single character is the <see cref="NamePrefix"/>.</exception> public SqlQueryParameter(string name, SqlType sqlType, ISqlValue value) { if (sqlType == null) { throw new ArgumentNullException(nameof(sqlType)); } if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } if (!String.Equals(name, Marker, StringComparison.Ordinal) && name[0] == NamePrefix) { name = name.Substring(1); if (String.IsNullOrEmpty(name)) { throw new ArgumentException("Cannot specify only the variable bind prefix as parameter."); } } Name = name; SqlType = sqlType; Value = value; Direction = SqlParameterDirection.In; }
public void PopRow() { ISqlValue[] newValues = new ISqlValue[_columnCount]; Array.Copy(_curRow, newValues, _columnCount); _rows.Add(newValues); _curRow = null; }
/// <summary> /// Converts the given <see cref="ISqlValue">object value</see> to a /// <see cref="SqlType"/> specified. /// </summary> /// <param name="value">The value to convert.</param> /// <param name="destType">The destination type of the conversion.</param> /// <remarks> /// If the given <paramref name="destType">destination type</paramref> is equivalent /// to this type, it will return the <paramref name="value"/> provided, otherwise /// it will throw an exception by default. /// <para> /// Casting values to specific types is specific to each data-type: override this /// method to support type-specific conversions. /// </para> /// <para> /// When overriding this method, <see cref="CanCastTo"/> should be overridden accordingly /// to indicate the type supports casting. /// </para> /// </remarks> /// <returns> /// Returns an instance of <see cref="ISqlValue"/> that is the result /// of the conversion from this data-type to the other type given. /// </returns> public virtual ISqlValue Cast(ISqlValue value, SqlType destType) { if (Equals(destType)) { return(value); } return(SqlNull.Value); }
int IComparable <ISqlValue> .CompareTo(ISqlValue other) { if (other == null || other is SqlNull) { return(0); } return(-1); }
public override ISqlValue Negate(ISqlValue value) { if (!(value is SqlNumber)) { return(SqlNull.Value); } return(-(SqlNumber)value); }
public override ISqlValue UnaryPlus(ISqlValue value) { if (!(value is SqlNumber)) { return(SqlNull.Value); } return(+(SqlNumber)value); }
int IComparable <ISqlValue> .CompareTo(ISqlValue other) { if (!(other is SqlDateTime)) { throw new ArgumentException(); } return(CompareTo((SqlDateTime)other)); }
public override bool CanCastTo(ISqlValue value, SqlType destType) { return(destType is SqlCharacterType || destType is SqlBinaryType || destType is SqlBooleanType || destType is SqlNumericType || destType is SqlDateTimeType || destType is SqlYearToMonthType || destType is SqlDayToSecondType); }
public void AddValue(object value) { if (_rowIndex < _columnCount) { var dlgt = _columnTypes[_rowIndex].Delegate; ISqlValue sqlvalue = dlgt.Invoke(value); _curRow[_rowIndex] = sqlvalue; _rowIndex++; } }
public override SqlBoolean Greater(ISqlValue a, ISqlValue b) { if (a is SqlDateTime && b is SqlDateTime) { var x = (SqlDateTime)a; var y = (SqlDateTime)b; return(x > y); } return(base.Greater(a, b)); }
public override SqlBoolean LessOrEqual(ISqlValue a, ISqlValue b) { if (a is SqlDateTime && b is SqlDateTime) { var x = (SqlDateTime)a; var y = (SqlDateTime)b; return(x <= y); } return(base.LessOrEqual(a, b)); }
private void AssertComparable(ISqlValue a, ISqlValue b) { if (a == null || b == null) { return; } if (!a.IsComparableTo(b)) { throw new ArgumentException("Values are not comparable"); } }
public override ISqlValue XOr(ISqlValue a, ISqlValue b) { if (!(a is SqlNumber) || !(b is SqlNumber)) { return(SqlNull.Value); } var x = (SqlNumber)a; var y = (SqlNumber)b; return(x ^ y); }
public override ISqlValue Modulo(ISqlValue a, ISqlValue b) { if (!(a is SqlNumber) || !(b is SqlNumber)) { return(SqlNull.Value); } var x = (SqlNumber)a; var y = (SqlNumber)b; return(SqlMath.Remainder(x, y)); }
public override ISqlValue Divide(ISqlValue a, ISqlValue b) { if (!(a is SqlNumber) || !(b is SqlNumber)) { return(SqlNull.Value); } var x = (SqlNumber)a; var y = (SqlNumber)b; return(SqlMath.Divide(x, y, Precision)); }
public override ISqlValue Multiply(ISqlValue a, ISqlValue b) { if (!(a is SqlNumber) || !(b is SqlNumber)) { return(SqlNull.Value); } var x = (SqlNumber)a; var y = (SqlNumber)b; return(SqlMath.Multiply(x, y)); }
public override string ToSqlString(ISqlValue obj) { var b = (SqlBoolean)obj; switch (TypeCode) { case SqlTypeCode.Bit: return(b == true ? "1" : "0"); case SqlTypeCode.Boolean: default: return(b == true ? "TRUE" : "FALSE"); } }
public void SerializeValue(bool?value) { var type = PrimitiveTypes.Boolean(); ISqlValue b = value == null ? (ISqlValue)SqlNull.Value : (SqlBoolean)value.Value; var stream = new MemoryStream(); type.Serialize(stream, b); stream.Seek(0, SeekOrigin.Begin); var result = type.Deserialize(stream); Assert.Equal(b, result); }
public static void SerializeValue(string s) { ISqlValue value = String.IsNullOrEmpty(s) ? (ISqlValue)SqlNull.Value : new SqlString(s); var type = PrimitiveTypes.String(); var stream = new MemoryStream(); type.Serialize(stream, value); stream.Seek(0, SeekOrigin.Begin); var result = type.Deserialize(stream); Assert.Equal(value, result); }