DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
NullType.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 
21 using Deveel.Data.Sql.Objects;
22 
23 namespace Deveel.Data.Types {
28  [Serializable]
29  public sealed class NullType : SqlType {
34  public NullType(SqlTypeCode typeCode)
35  : base("NULL", typeCode) {
36  }
37 
38  private NullType(ObjectData data)
39  : base(data) {
40  }
41 
42  public override void SerializeObject(Stream stream, ISqlObject obj) {
43  var writer = new BinaryWriter(stream);
44 
45  if (obj is SqlNull) {
46  writer.Write((byte)1);
47  } else if (obj == null || obj.IsNull) {
48  writer.Write((byte) 2);
49  } else {
50  throw new FormatException();
51  }
52  }
53 
54  public override ISqlObject DeserializeObject(Stream stream) {
55  var reader = new BinaryReader(stream);
56  var type = reader.ReadByte();
57 
58  if (type == 1)
59  return SqlNull.Value;
60  if (type == 2) {
61  // TODO: check the SQL Type Code of the type and construct the
62  // NULL value specific for the type.
63  throw new NotImplementedException();
64  }
65 
66  throw new FormatException();
67  }
68 
69  public override object ConvertTo(ISqlObject obj, Type destType) {
70  if (obj == null || obj.IsNull)
71  return null;
72 
73  throw new InvalidCastException();
74  }
75  }
76 }
NullType(SqlTypeCode typeCode)
Constructs the type with the given SqlTypeCode.
Definition: NullType.cs:34
NullType(ObjectData data)
Definition: NullType.cs:38
bool IsNull
Gets a boolean value indicating if the object is NULL.
Definition: ISqlObject.cs:28
static readonly SqlNull Value
Definition: SqlNull.cs:24
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
override ISqlObject DeserializeObject(Stream stream)
Definition: NullType.cs:54
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 object ConvertTo(ISqlObject obj, Type destType)
Definition: NullType.cs:69
A data type that represents the NULL value of a given SQL data type.
Definition: NullType.cs:29
override void SerializeObject(Stream stream, ISqlObject obj)
Definition: NullType.cs:42