DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
IntervalType.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.IO;
19 
20 using Deveel.Data.Sql.Objects;
21 
22 namespace Deveel.Data.Types {
23  public sealed class IntervalType : SqlType {
24  public IntervalType(SqlTypeCode typeCode)
25  : base(GetTypeString(typeCode), typeCode) {
26  AssertIsInterval(typeCode);
27  }
28 
29  public override bool IsIndexable {
30  get { return true; }
31  }
32 
33  private static string GetTypeString(SqlTypeCode sqlType) {
34  if (sqlType == SqlTypeCode.DayToSecond)
35  return "DAY TO SECOND";
36  if (sqlType == SqlTypeCode.YearToMonth)
37  return "YEAR TO MONTH";
38 
39  return "INTERVAL";
40  }
41 
42  private static void AssertIsInterval(SqlTypeCode sqlType) {
43  if (!IsIntervalType(sqlType))
44  throw new ArgumentException(String.Format("SQL Type {0} is not a valid INTERVAL.", sqlType.ToString().ToUpperInvariant()));
45  }
46 
47  public override bool IsCacheable(ISqlObject value) {
48  return value is SqlNumber || value is SqlNull;
49  }
50 
51  public override Type GetRuntimeType() {
52  if (TypeCode == SqlTypeCode.DayToSecond)
53  return typeof (TimeSpan);
54 
55  return base.GetRuntimeType();
56  }
57 
58  public override Type GetObjectType() {
59  if (TypeCode == SqlTypeCode.YearToMonth)
60  return typeof (SqlYearToMonth);
61  if (TypeCode == SqlTypeCode.DayToSecond)
62  return typeof (SqlDayToSecond);
63 
64  return base.GetObjectType();
65  }
66 
68  public override bool IsComparable(SqlType type) {
69  if (!(type is IntervalType))
70  return false;
71 
72  // TODO: better check ...
73  return TypeCode.Equals(type.TypeCode);
74  }
75 
76  public override void SerializeObject(Stream stream, ISqlObject obj) {
77  var writer = new BinaryWriter(stream);
78 
79  if (obj is SqlDayToSecond) {
80  var interval = (SqlDayToSecond) obj;
81  var bytes = interval.ToByArray();
82 
83  writer.Write((byte)1);
84  writer.Write(bytes.Length);
85  writer.Write(bytes);
86  } else if (obj is SqlYearToMonth) {
87  var interval = (SqlYearToMonth) obj;
88  var months = interval.TotalMonths;
89 
90  writer.Write((byte)2);
91  writer.Write(months);
92  }
93 
94  throw new FormatException();
95  }
96 
97  public override ISqlObject DeserializeObject(Stream stream) {
98  var reader = new BinaryReader(stream);
99 
100  var type = reader.ReadByte();
101 
102  if (type == 1) {
103  var length = reader.ReadInt32();
104  var bytes = reader.ReadBytes(length);
105 
106  // TODO: implement the constructor from bytes
107  throw new NotImplementedException();
108  }
109  if (type == 2) {
110  var months = reader.ReadInt32();
111  return new SqlYearToMonth(months);
112  }
113 
114  return base.DeserializeObject(stream);
115  }
116 
117  internal static bool IsIntervalType(SqlTypeCode sqlType) {
118  return sqlType == SqlTypeCode.YearToMonth ||
119  sqlType == SqlTypeCode.DayToSecond;
120  }
121  }
122 }
override void SerializeObject(Stream stream, ISqlObject obj)
Definition: IntervalType.cs:76
A long string in the system.
override Type GetRuntimeType()
Definition: IntervalType.cs:51
SqlTypeCode TypeCode
Gets the kind of SQL type this data-type handles.
Definition: SqlType.cs:91
IntervalType(SqlTypeCode typeCode)
Definition: IntervalType.cs:24
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
override bool IsComparable(SqlType type)
Verifies if a given SqlType is comparable to this data-type.
Definition: IntervalType.cs:68
override ISqlObject DeserializeObject(Stream stream)
Definition: IntervalType.cs:97
static string GetTypeString(SqlTypeCode sqlType)
Definition: IntervalType.cs:33
override bool IsCacheable(ISqlObject value)
Definition: IntervalType.cs:47
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
static void AssertIsInterval(SqlTypeCode sqlType)
Definition: IntervalType.cs:42
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
A month span representation of time.
static bool IsIntervalType(SqlTypeCode sqlType)