25 namespace Deveel.Data.Types {
29 : base(
"NUMERIC", typeCode) {
30 AssertIsNumeric(typeCode);
36 : this(typeCode, -1) {
40 : this(typeCode, size, 0) {
49 public int Size {
get;
private set; }
51 public byte Scale {
get;
private set; }
53 public override bool IsStorable {
62 public override bool Equals(
object obj) {
89 return typeof(
double);
92 return typeof(BigDecimal);
94 return base.GetRuntimeType();
102 return (TypeCode.GetHashCode() * Scale) + Size.GetHashCode();
106 if (!IsNumericType(typeCode))
107 throw new ArgumentException(
String.Format(
"The type '{0}' is not a valid NUMERIC type.", typeCode));
161 int t1IntSize = GetIntSize(t1SqlType);
162 int t2IntSize = GetIntSize(t2SqlType);
163 if (t1IntSize > 0 && t2IntSize > 0) {
165 return (t1IntSize > t2IntSize) ?
this : otherType;
168 int t1FloatSize = GetFloatSize(t1SqlType);
169 int t2FloatSize = GetFloatSize(t2SqlType);
170 if (t1FloatSize > 0 && t2FloatSize > 0) {
172 return (t1FloatSize > t2FloatSize) ?
this : otherType;
175 if (t1FloatSize > t2IntSize) {
178 if (t2FloatSize > t1IntSize) {
181 if (t1IntSize >= t2FloatSize || t2IntSize >= t1FloatSize) {
188 throw new InvalidOperationException(
"Widest type error.");
202 n2 = (
SqlBoolean) y ? SqlNumber.One : SqlNumber.Zero;
204 throw new NotSupportedException();
207 return n1.CompareTo(n2);
269 casted = ToDate(n.ToInt64());
281 throw new InvalidCastException();
288 var sb =
new StringBuilder(Name);
299 return sb.ToString();
304 throw new ArgumentException();
306 var number = (SqlNumber)obj;
310 if (destType == typeof(byte))
311 return number.ToByte();
312 if (destType == typeof(
short))
313 return number.ToInt16();
314 if (destType == typeof(
int))
315 return number.ToInt32();
316 if (destType == typeof(
long))
317 return number.ToInt64();
318 if (destType == typeof(
float))
319 return number.ToSingle();
320 if (destType == typeof(
double))
321 return number.ToDouble();
323 if (destType == typeof(
bool))
324 return number.ToBoolean();
326 if (destType == typeof(
string))
327 return number.ToString();
329 return base.ConvertTo(obj, destType);
334 throw new ArgumentException();
336 return SqlNumber.Null;
338 var num1 = (SqlNumber) a;
342 if ((SqlBoolean) b) {
343 num2 = SqlNumber.
One;
344 }
else if (!(SqlBoolean) b) {
345 num2 = SqlNumber.
Zero;
347 num2 = SqlNumber.
Null;
349 }
else if (b is SqlNumber) {
350 num2 = (SqlNumber) b;
352 throw new ArgumentException();
355 return num1.Add(num2);
360 throw new ArgumentException();
362 return SqlNumber.Null;
364 var num1 = (SqlNumber) a;
368 if ((SqlBoolean) b) {
369 num2 = SqlNumber.
One;
370 }
else if (!(SqlBoolean) b) {
371 num2 = SqlNumber.
Zero;
373 num2 = SqlNumber.
Null;
375 }
else if (b is SqlNumber) {
376 num2 = (SqlNumber) b;
378 throw new ArgumentException();
381 return num1.Subtract(num2);
387 throw new ArgumentException();
389 return SqlNumber.Null;
394 var num1 = (SqlNumber) a;
395 var num2 = (SqlNumber) b;
397 return num1.Multiply(num2);
403 throw new ArgumentException();
406 return SqlNumber.Null;
411 var num1 = (SqlNumber)a;
412 var num2 = (SqlNumber)b;
414 return num1.Divide(num2);
420 throw new ArgumentException();
423 return SqlNumber.Null;
428 var num1 = (SqlNumber) a;
429 var num2 = (SqlNumber) b;
431 return num1.Modulo(num2);
437 throw new ArgumentException();
445 var num1 = (SqlNumber) a;
446 var num2 = (SqlNumber) b;
454 throw new ArgumentException();
462 var num1 = (SqlNumber) a;
463 var num2 = (SqlNumber) b;
471 throw new ArgumentException();
479 var num1 = (SqlNumber) a;
480 var num2 = (SqlNumber) b;
488 throw new ArgumentException();
496 var num1 = (SqlNumber) a;
497 var num2 = (SqlNumber) b;
505 throw new ArgumentException();
513 var num1 = (SqlNumber) a;
514 var num2 = (SqlNumber) b;
516 return num1.Equals(num2);
520 return !IsEqualTo(a, b);
525 throw new ArgumentException();
527 var num = (SqlNumber) value;
537 throw new ArgumentException();
539 var num = (SqlNumber)value;
549 throw new ArgumentException();
551 var num = (SqlNumber)value;
562 throw new ArgumentException();
565 return SqlNumber.Null;
570 var num1 = (SqlNumber) a;
571 var num2 = (SqlNumber) b;
573 return num1.XOr(num2);
579 throw new ArgumentException();
582 return SqlNumber.Null;
587 var num1 = (SqlNumber) a;
588 var num2 = (SqlNumber) b;
590 return num1.And(num2);
596 throw new ArgumentException();
599 return SqlNumber.Null;
604 var num1 = (SqlNumber) a;
605 var num2 = (SqlNumber) b;
607 return num1.Or(num2);
611 var writer =
new BinaryWriter(stream);
614 writer.Write((byte)0);
618 writer.Write((byte) 0);
619 }
else if (number.CanBeInt32) {
620 writer.Write((byte) 1);
621 writer.Write(number.ToInt32());
622 }
else if (number.CanBeInt64) {
623 writer.Write((byte) 2);
624 writer.Write(number.ToInt64());
626 var bytes = number.ToByteArray();
627 writer.Write((byte) 3);
628 writer.Write(number.Precision);
629 writer.Write(number.Scale);
630 writer.Write(bytes.Length);
637 var reader =
new BinaryReader(stream);
639 var type = reader.ReadByte();
644 var value = reader.ReadInt32();
648 var value = reader.ReadInt64();
652 var precision = reader.ReadInt32();
653 var scale = reader.ReadInt32();
654 var length = reader.ReadInt32();
655 var bytes = reader.ReadBytes(length);
656 return new SqlNumber(bytes, scale, precision);
659 throw new FormatException();
667 throw new ArgumentException(
String.Format(
"Cannot determine the size of an object of type '{0}'.", obj.GetType()));
669 var number = (SqlNumber) obj;
674 if (number.CanBeInt32)
676 if (number.CanBeInt64)
680 var length = number.ToByteArray().Length;
681 return 1 + 4 + 4 + 4 + length;
override string ToString()
NumericType(SqlTypeCode typeCode, int size)
static void AssertIsNumeric(SqlTypeCode typeCode)
NumericState
Lists all the possible special states of a number.
override bool IsCacheable(ISqlObject value)
override int Compare(ISqlObject x, ISqlObject y)
override ISqlObject DeserializeObject(Stream stream)
A long string in the system.
override void SerializeObject(Stream stream, ISqlObject obj)
override int GetHashCode()
override DataObject CastTo(DataObject value, SqlType destType)
Converts the given object value to a SqlType specified.
Implements a BINARY object that handles a limited number of bytes, not exceding MaxLength.
override SqlBoolean IsSmallerThan(ISqlObject a, ISqlObject b)
static bool IsNumericType(SqlTypeCode typeCode)
override object ConvertTo(ISqlObject obj, Type destType)
void SetValue(string key, Type type, object value)
NumericType(SqlTypeCode typeCode, int size, byte scale)
ISqlObject Value
Gets the underlined value that is handled.
SqlTypeCode TypeCode
Gets the kind of SQL type this data-type handles.
bool IsNull
Gets a boolean value indicating if the object is NULL.
override ISqlObject Subtract(ISqlObject a, ISqlObject b)
override SqlType Wider(SqlType otherType)
Gets the one data-type between this and the other one given that handles the wider range of values...
override SqlBoolean IsNotEqualTo(ISqlObject a, ISqlObject b)
static readonly SqlNull Value
Defines the contract for a valid SQL Object
static SqlDateTime ToDate(long time)
override bool IsComparable(SqlType type)
Verifies if a given SqlType is comparable to this data-type.
override ISqlObject Divide(ISqlObject a, ISqlObject b)
override ISqlObject Negate(ISqlObject value)
override ISqlObject Reverse(ISqlObject value)
static readonly SqlNumber Zero
NumericType(ObjectData data)
override ISqlObject XOr(ISqlObject a, ISqlObject b)
override Type GetRuntimeType()
override ISqlObject Modulus(ISqlObject a, ISqlObject b)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
override bool CanCastTo(SqlType destType)
Verifies if this type can cast any value to the given SqlType.
override int ColumnSizeOf(ISqlObject obj)
Defines the properties of a specific SQL Type and handles the values compatible.
override Type GetObjectType()
override SqlBoolean IsGreaterOrEqualThan(ISqlObject a, ISqlObject b)
override bool Equals(object obj)
static readonly SqlNumber Null
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
static readonly SqlNumber One
static int GetIntSize(SqlTypeCode sqlType)
override void GetData(SerializeData data)
override ISqlObject And(ISqlObject a, ISqlObject b)
Deveel.Data.Sql.Objects.SqlBoolean SqlBoolean
override ISqlObject Multiply(ISqlObject a, ISqlObject b)
override SqlBoolean IsSmallerOrEqualThan(ISqlObject a, ISqlObject b)
override ISqlObject Or(ISqlObject a, ISqlObject b)
override SqlBoolean IsGreatherThan(ISqlObject a, ISqlObject b)
NumericType(SqlTypeCode typeCode)
static int GetFloatSize(SqlTypeCode sqlType)
override SqlBoolean IsEqualTo(ISqlObject a, ISqlObject b)
Deveel.Data.Sql.Objects.SqlString SqlString
override ISqlObject UnaryPlus(ISqlObject value)
override ISqlObject Add(ISqlObject a, ISqlObject b)