DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
BinaryType.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 using System.Linq;
20 using System.Text;
21 
23 using Deveel.Data.Sql.Objects;
24 
25 namespace Deveel.Data.Types {
26  [Serializable]
27  public sealed class BinaryType : SqlType, ISizeableType {
28  public const int DefaultMaxSize = Int16.MaxValue;
29 
30  public BinaryType(SqlTypeCode typeCode)
31  : this(typeCode, DefaultMaxSize) {
32  }
33 
34  public BinaryType(SqlTypeCode typeCode, int maxSize)
35  : base("BINARY", typeCode) {
36  MaxSize = maxSize;
37  AssertIsBinary(typeCode);
38  }
39 
40  private BinaryType(ObjectData data)
41  : base(data) {
42  MaxSize = data.GetInt32("MaxSize");
43  }
44 
46  get { return MaxSize; }
47  }
48 
49  public override bool IsStorable {
50  get { return true; }
51  }
52 
53  public int MaxSize { get; private set; }
54 
55  public override bool IsIndexable {
56  get { return false; }
57  }
58 
59  private static void AssertIsBinary(SqlTypeCode sqlType) {
60  if (!IsBinaryType(sqlType))
61  throw new ArgumentException(String.Format("The SQL type {0} is not a BINARY", sqlType));
62  }
63 
64  protected override void GetData(SerializeData data) {
65  data.SetValue("MaxSize", MaxSize);
66  }
67 
68  public override bool IsCacheable(ISqlObject value) {
69  return value is SqlBinary || value is SqlNull;
70  }
71 
72  public override Type GetObjectType() {
73  return typeof(SqlBinary);
74  }
75 
76  public override Type GetRuntimeType() {
77  return typeof (Stream);
78  }
79 
80  public override string ToString() {
81  var sb = new StringBuilder(Name);
82  if (MaxSize > 0)
83  sb.AppendFormat("({0})", MaxSize);
84 
85  return sb.ToString();
86  }
87 
88  private SqlBoolean ToBoolean(ISqlBinary binary) {
89  if (binary.Length != 1)
90  throw new InvalidCastException();
91 
92  var b = binary.First();
93  if (b != 0 && b != 1)
94  throw new InvalidCastException();
95 
96  return b == 1;
97  }
98 
99  public override DataObject CastTo(DataObject value, SqlType destType) {
100  var sqlType = destType.TypeCode;
101  var binary = ((ISqlBinary) value.Value);
102 
103  ISqlObject casted;
104 
105  switch (sqlType) {
106  case SqlTypeCode.Bit:
107  casted = ToBoolean(binary);
108  break;
109  // TODO: All other casts
110  default:
111  throw new InvalidCastException();
112  }
113 
114  return new DataObject(destType, casted);
115  }
116 
117  internal override int ColumnSizeOf(ISqlObject obj) {
118  if (obj is SqlBinary) {
119  var binary = (SqlBinary) obj;
120  return 1 + 4 + (int) binary.Length;
121  } else if (obj is SqlLongBinary) {
122  throw new NotImplementedException();
123  }
124 
125  throw new NotSupportedException();
126  }
127 
128  public override void SerializeObject(Stream stream, ISqlObject obj) {
129  var writer = new BinaryWriter(stream);
130 
131  if (obj is SqlBinary) {
132  var bin = (SqlBinary) obj;
133  writer.Write((byte)1);
134  writer.Write((int)bin.Length);
135  writer.Write(bin.ToByteArray());
136  } else if (obj is SqlLongBinary) {
137  var lob = (SqlLongBinary) obj;
138 
139  writer.Write((byte) 2);
140 
141  // TODO:
142 
143  throw new NotImplementedException();
144  } else {
145  base.SerializeObject(stream, obj);
146  }
147  }
148 
149  public override ISqlObject DeserializeObject(Stream stream) {
150  var reader = new BinaryReader(stream);
151 
152  var type = reader.ReadByte();
153  if (type == 1) {
154  var length = reader.ReadInt32();
155  var bytes = reader.ReadBytes(length);
156  return new SqlBinary(bytes);
157  }
158  if (type == 2) {
159  // TODO:
160  }
161 
162  throw new FormatException();
163  }
164 
165  internal static bool IsBinaryType(SqlTypeCode sqlType) {
166  return sqlType == SqlTypeCode.Binary ||
167  sqlType == SqlTypeCode.VarBinary ||
168  sqlType == SqlTypeCode.LongVarBinary ||
169  sqlType == SqlTypeCode.Blob;
170  }
171  }
172 }
override int ColumnSizeOf(ISqlObject obj)
Definition: BinaryType.cs:117
override Type GetObjectType()
Definition: BinaryType.cs:72
override string ToString()
Definition: BinaryType.cs:80
A long string in the system.
Implements a BINARY object that handles a limited number of bytes, not exceding MaxLength.
Definition: SqlBinary.cs:27
override DataObject CastTo(DataObject value, SqlType destType)
Converts the given object value to a SqlType specified.
Definition: BinaryType.cs:99
static void AssertIsBinary(SqlTypeCode sqlType)
Definition: BinaryType.cs:59
void SetValue(string key, Type type, object value)
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
long Length
Gets the raw length of the binary object.
Definition: ISqlBinary.cs:29
SqlTypeCode TypeCode
Gets the kind of SQL type this data-type handles.
Definition: SqlType.cs:91
BinaryType(ObjectData data)
Definition: BinaryType.cs:40
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
static bool IsBinaryType(SqlTypeCode sqlType)
Definition: BinaryType.cs:165
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
override Type GetRuntimeType()
Definition: BinaryType.cs:76
BinaryType(SqlTypeCode typeCode)
Definition: BinaryType.cs:30
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
override void GetData(SerializeData data)
Definition: BinaryType.cs:64
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
override void SerializeObject(Stream stream, ISqlObject obj)
Definition: BinaryType.cs:128
override bool IsCacheable(ISqlObject value)
Definition: BinaryType.cs:68
override ISqlObject DeserializeObject(Stream stream)
Definition: BinaryType.cs:149
Defines the required contract of a SQL BINARY object
Definition: ISqlBinary.cs:25
BinaryType(SqlTypeCode typeCode, int maxSize)
Definition: BinaryType.cs:34
SqlBoolean ToBoolean(ISqlBinary binary)
Definition: BinaryType.cs:88