DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DateType.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 DateType : SqlType {
24  public DateType(SqlTypeCode typeCode)
25  : base("DATE", typeCode) {
26  AssertDateType(typeCode);
27  }
28 
29  public static readonly string[] DateFormatSql = {
30  "yyyy-MM-dd",
31  "yyyy MM dd"
32  };
33 
34  public static readonly string[] TimeFormatSql = {
35  "HH:mm:ss.fff z",
36  "HH:mm:ss.fff zz",
37  "HH:mm:ss.fff zzz",
38  "HH:mm:ss.fff",
39  "HH:mm:ss z",
40  "HH:mm:ss zz",
41  "HH:mm:ss zzz",
42  "HH:mm:ss"
43  };
44 
45  public static readonly string[] TsFormatSql = {
46  "yyyy-MM-dd HH:mm:ss.fff",
47  "yyyy-MM-dd HH:mm:ss.fff z",
48  "yyyy-MM-dd HH:mm:ss.fff zz",
49  "yyyy-MM-dd HH:mm:ss.fff zzz",
50  "yyyy-MM-dd HH:mm:ss",
51  "yyyy-MM-dd HH:mm:ss z",
52  "yyyy-MM-dd HH:mm:ss zz",
53  "yyyy-MM-dd HH:mm:ss zzz",
54 
55  "yyyy-MM-ddTHH:mm:ss.fff",
56  "yyyy-MM-ddTHH:mm:ss.fff z",
57  "yyyy-MM-ddTHH:mm:ss.fff zz",
58  "yyyy-MM-ddTHH:mm:ss.fff zzz",
59  "yyyy-MM-ddTHH:mm:ss",
60  "yyyy-MM-ddTHH:mm:ss z",
61  "yyyy-MM-ddTHH:mm:ss zz",
62  "yyyy-MM-ddTHH:mm:ss zzz",
63  };
64 
65 
66  private static void AssertDateType(SqlTypeCode sqlType) {
67  if (!IsDateType(sqlType))
68  throw new ArgumentException(String.Format("The SQL type {0} is not a valid DATE", sqlType), "sqlType");
69  }
70 
71  public override bool IsStorable {
72  get { return true; }
73  }
74 
75  public override bool IsCacheable(ISqlObject value) {
76  return value is SqlDateTime || value is SqlNull;
77  }
78 
79  public override Type GetObjectType() {
80  return typeof (SqlDateTime);
81  }
82 
83  public override Type GetRuntimeType() {
84  return typeof (DateTimeOffset);
85  }
86 
87  public override bool Equals(object obj) {
88  var other = obj as SqlType;
89  if (other == null)
90  return false;
91 
92  return TypeCode == other.TypeCode;
93  }
94 
95  public override int GetHashCode() {
96  return TypeCode.GetHashCode();
97  }
98 
99  public override bool IsComparable(SqlType type) {
100  return type is DateType;
101  }
102 
103  public override bool CanCastTo(SqlType destType) {
104  return destType is StringType || destType is DateType;
105  }
106 
107  private SqlString ToString(SqlDateTime dateTime) {
108  if (dateTime.IsNull)
109  return SqlString.Null;
110 
111  if (TypeCode == SqlTypeCode.Date)
112  return dateTime.ToDateString();
113  if (TypeCode == SqlTypeCode.Time)
114  return dateTime.ToTimeString();
115  if (TypeCode == SqlTypeCode.TimeStamp)
116  return dateTime.ToTimeStampString();
117 
118  return SqlString.Null;
119  }
120 
121  private static SqlDateTime ToTime(SqlDateTime dateTime) {
122  return new SqlDateTime(0, 0, 0, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond, dateTime.Offset);
123  }
124 
125  private static SqlDateTime ToDate(SqlDateTime dateTime) {
126  return new SqlDateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0, dateTime.Offset);
127  }
128 
129  public override DataObject CastTo(DataObject value, SqlType destType) {
130  if (destType == null)
131  throw new ArgumentNullException("destType");
132 
133  var date = (SqlDateTime) value.Value;
134  var sqlType = destType.TypeCode;
135 
136  ISqlObject casted;
137 
138  switch (sqlType) {
139  case SqlTypeCode.String:
140  case SqlTypeCode.VarChar:
141  casted = ToString(date);
142  break;
143  case SqlTypeCode.Date:
144  casted = ToDate(date);
145  break;
146  case SqlTypeCode.Time:
147  casted = ToTime(date);
148  break;
149  case SqlTypeCode.TimeStamp:
150  case SqlTypeCode.DateTime:
151  // if it is not a TimeStamp already, there's not much we can do
152  casted = date;
153  break;
154  default:
155  throw new InvalidCastException(String.Format("Cannot cast type '{0}' to '{1}'.",
156  sqlType.ToString().ToUpperInvariant(), TypeCode.ToString().ToUpperInvariant()));
157  }
158 
159  return new DataObject(destType, casted);
160  }
161 
162  public override void SerializeObject(Stream stream, ISqlObject obj) {
163  var writer = new BinaryWriter(stream);
164 
165  if (obj is SqlNull) {
166  writer.Write((byte) 0);
167  } else {
168  var date = (SqlDateTime) obj;
169 
170  if (date.IsNull) {
171  writer.Write((byte)0);
172  } else {
173  var bytes = date.ToByteArray(true);
174 
175  writer.Write((byte)1);
176  writer.Write(bytes.Length);
177  writer.Write(bytes);
178  }
179  }
180  }
181 
182  internal override int ColumnSizeOf(ISqlObject obj) {
183  if (obj is SqlNull)
184  return 1;
185 
186  if (!(obj is SqlDateTime))
187  throw new ArgumentException(String.Format("Cannot determine the size of an object of type '{0}'", obj.GetType()));
188 
189  if (obj.IsNull)
190  return 1;
191 
192  // Type + Length + Bytes
193  return 1 + 4 + 13;
194  }
195 
196  public override ISqlObject DeserializeObject(Stream stream) {
197  var reader = new BinaryReader(stream);
198 
199  var type = reader.ReadByte();
200  if (type == 0)
201  return SqlDateTime.Null;
202 
203  var length = reader.ReadInt32();
204  var bytes = reader.ReadBytes(length);
205  return new SqlDateTime(bytes);
206  }
207 
208  internal static bool IsDateType(SqlTypeCode sqlType) {
209  return sqlType == SqlTypeCode.Date ||
210  sqlType == SqlTypeCode.Time ||
211  sqlType == SqlTypeCode.TimeStamp ||
212  sqlType == SqlTypeCode.DateTime;
213  }
214  }
215 }
override bool CanCastTo(SqlType destType)
Verifies if this type can cast any value to the given SqlType.
Definition: DateType.cs:103
SqlDayToSecond Offset
Gets the offset between the date-time instance and the UTC time.
Definition: SqlDateTime.cs:214
override Type GetRuntimeType()
Definition: DateType.cs:83
A long string in the system.
override int GetHashCode()
Definition: DateType.cs:95
override bool IsCacheable(ISqlObject value)
Definition: DateType.cs:75
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
SqlTypeCode TypeCode
Gets the kind of SQL type this data-type handles.
Definition: SqlType.cs:91
override void SerializeObject(Stream stream, ISqlObject obj)
Definition: DateType.cs:162
override Type GetObjectType()
Definition: DateType.cs:79
SqlString ToString(SqlDateTime dateTime)
Definition: DateType.cs:107
bool IsNull
Gets a boolean value indicating if the object is NULL.
Definition: ISqlObject.cs:28
static SqlDateTime ToDate(SqlDateTime dateTime)
Definition: DateType.cs:125
static bool IsDateType(SqlTypeCode sqlType)
Definition: DateType.cs:208
static SqlDateTime ToTime(SqlDateTime dateTime)
Definition: DateType.cs:121
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
DateType(SqlTypeCode typeCode)
Definition: DateType.cs:24
override ISqlObject DeserializeObject(Stream stream)
Definition: DateType.cs:196
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
override bool Equals(object obj)
Definition: DateType.cs:87
static readonly SqlDateTime Null
Definition: SqlDateTime.cs:24
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
override DataObject CastTo(DataObject value, SqlType destType)
Converts the given object value to a SqlType specified.
Definition: DateType.cs:129
override bool IsComparable(SqlType type)
Verifies if a given SqlType is comparable to this data-type.
Definition: DateType.cs:99
override int ColumnSizeOf(ISqlObject obj)
Definition: DateType.cs:182
static void AssertDateType(SqlTypeCode sqlType)
Definition: DateType.cs:66