DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TypeSerializer.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.Globalization;
19 using System.IO;
20 using System.Text;
21 
23 
24 namespace Deveel.Data.Types {
25  static class TypeSerializer {
26  public static void SerializeTo(BinaryWriter writer, SqlType type) {
27  writer.Write((byte) type.TypeCode);
28 
29  if (type.IsPrimitive) {
30  if (type is NumericType) {
31  var numericType = (NumericType) type;
32  writer.Write(numericType.Size);
33  writer.Write(numericType.Scale);
34  } else if (type is StringType) {
35  var stringType = (StringType) type;
36  writer.Write(stringType.MaxSize);
37 
38  if (stringType.Locale != null) {
39  writer.Write((byte) 1);
40  writer.Write(stringType.Locale.Name);
41  } else {
42  writer.Write((byte) 0);
43  }
44  } else if (type is BinaryType) {
45  var binaryType = (BinaryType) type;
46 
47  writer.Write(binaryType.MaxSize);
48  } else if (type is BooleanType ||
49  type is IntervalType ||
50  type is DateType ||
51  type is NullType) {
52  // nothing to add to the SQL Type Code
53  } else {
54  throw new NotSupportedException(String.Format("The data type '{0}' cannot be serialized.", type.GetType().FullName));
55  }
56  } else if (type is UserType) {
57  var userType = (UserType) type;
58  writer.Write((byte) 1); // The code of custom type
59  writer.Write(userType.FullName.FullName);
60  } else if (type is QueryType) {
61  // nothing to do for the Query Type here
62  } else if (type is ArrayType) {
63  var arrayType = (ArrayType) type;
64  writer.Write(arrayType.Length);
65  } else {
66  throw new NotSupportedException();
67  }
68  }
69 
70  public static void SerializeTo(Stream stream, SqlType type) {
71  var writer = new BinaryWriter(stream, Encoding.Unicode);
72  SerializeTo(writer, type);
73  }
74 
75  public static SqlType Deserialize(BinaryReader reader, ITypeResolver resolver) {
76 
77  var typeCode = (SqlTypeCode)reader.ReadByte();
78 
79  if (BooleanType.IsBooleanType(typeCode))
80  return PrimitiveTypes.Boolean(typeCode);
81  if (IntervalType.IsIntervalType(typeCode))
82  return PrimitiveTypes.Interval(typeCode);
83  if (DateType.IsDateType(typeCode))
84  return PrimitiveTypes.DateTime(typeCode);
85 
86  if (StringType.IsStringType(typeCode)) {
87  var maxSize = reader.ReadInt32();
88 
89  CultureInfo locale = null;
90  var hasLocale = reader.ReadByte() == 1;
91  if (hasLocale) {
92  var name = reader.ReadString();
93  locale = new CultureInfo(name);
94  }
95 
96  // TODO: Get the encoding from the serialization...
97  return PrimitiveTypes.String(typeCode, maxSize, Encoding.Unicode, locale);
98  }
99 
100  if (NumericType.IsNumericType(typeCode)) {
101  var size = reader.ReadInt32();
102  var scale = reader.ReadByte();
103 
104  return PrimitiveTypes.Numeric(typeCode, size, scale);
105  }
106 
107  if (BinaryType.IsBinaryType(typeCode)) {
108  var size = reader.ReadInt32();
109  return PrimitiveTypes.Binary(typeCode, size);
110  }
111 
112  if (typeCode == SqlTypeCode.Type) {
113  // TODO:
114  }
115 
116  if (typeCode == SqlTypeCode.QueryPlan)
117  return new QueryType();
118 
119  if (typeCode == SqlTypeCode.Array) {
120  var size = reader.ReadInt32();
121  return new ArrayType(size);
122  }
123 
124  if (typeCode == SqlTypeCode.Null)
125  return PrimitiveTypes.Null();
126 
127  throw new NotSupportedException();
128  }
129 
130  public static SqlType Deserialize(Stream stream, ITypeResolver typeResolver) {
131  var reader = new BinaryReader(stream, Encoding.Unicode);
132  return Deserialize(reader, typeResolver);
133  }
134  }
135 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static void SerializeTo(BinaryWriter writer, SqlType type)
static bool IsBooleanType(SqlTypeCode sqlType)
Definition: BooleanType.cs:41
A long string in the system.
static BooleanType Boolean()
static SqlType Deserialize(BinaryReader reader, ITypeResolver resolver)
static void SerializeTo(Stream stream, SqlType type)
static BinaryType Binary(int maxSize)
static bool IsNumericType(SqlTypeCode typeCode)
Definition: NumericType.cs:684
SqlTypeCode TypeCode
Gets the kind of SQL type this data-type handles.
Definition: SqlType.cs:91
static bool IsDateType(SqlTypeCode sqlType)
Definition: DateType.cs:208
static bool IsStringType(SqlTypeCode typeCode)
Definition: StringType.cs:566
static bool IsBinaryType(SqlTypeCode sqlType)
Definition: BinaryType.cs:165
static SqlType Deserialize(Stream stream, ITypeResolver typeResolver)
static NumericType Numeric()
static IntervalType Interval(SqlTypeCode sqlType)
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
bool IsPrimitive
Gets a value indicating if this data-type is primitive.
Definition: SqlType.cs:107
A data type that represents the NULL value of a given SQL data type.
Definition: NullType.cs:29
static bool IsIntervalType(SqlTypeCode sqlType)