DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
QueryType.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.Text;
20 
22 using Deveel.Data.Sql.Objects;
23 using Deveel.Data.Sql.Query;
24 
25 namespace Deveel.Data.Types {
26  [Serializable]
27  public sealed class QueryType : SqlType {
28  public QueryType()
29  : base("QUERY", SqlTypeCode.QueryPlan) {
30  }
31 
32  private QueryType(ObjectData data)
33  : base(data) {
34  }
35 
36  public override bool IsIndexable {
37  get { return false; }
38  }
39 
40  public override bool IsStorable {
41  get { return false; }
42  }
43 
44  public override ISqlObject DeserializeObject(Stream stream) {
45  var reader = new BinaryReader(stream, Encoding.Unicode);
46  var isNull = reader.ReadByte() == 0;
47  if (isNull)
48  return SqlQueryObject.Null;
49 
50  var nodeTypeString = reader.ReadString();
51  var nodeType = Type.GetType(nodeTypeString, true);
52  var queryPlan = DeserializePlan(nodeType, reader);
53 
54  return new SqlQueryObject(queryPlan);
55  }
56 
57  private static IQueryPlanNode DeserializePlan(Type nodeType, BinaryReader reader) {
58  var serializer = new BinarySerializer();
59  return (IQueryPlanNode) serializer.Deserialize(reader, nodeType);
60  }
61 
62  public override void SerializeObject(Stream stream, ISqlObject obj) {
63  var writer = new BinaryWriter(stream, Encoding.Unicode);
64 
65  var queryPlanObj = (SqlQueryObject) obj;
66  if (queryPlanObj.IsNull) {
67  writer.Write((byte) 0);
68  } else {
69  writer.Write((byte)1);
70  var nodeTypeString = queryPlanObj.QueryPlan.GetType().AssemblyQualifiedName;
71  if (String.IsNullOrEmpty(nodeTypeString))
72  throw new InvalidOperationException();
73 
74  writer.Write(nodeTypeString);
75  SerializePlan(queryPlanObj.QueryPlan, writer);
76  }
77  }
78 
79  private static void SerializePlan(IQueryPlanNode queryPlan, BinaryWriter writer) {
80  var serializer = new BinarySerializer();
81  serializer.Serialize(writer, queryPlan);
82  }
83  }
84 }
static readonly SqlQueryObject Null
A long string in the system.
QueryType(ObjectData data)
Definition: QueryType.cs:32
static IQueryPlanNode DeserializePlan(Type nodeType, BinaryReader reader)
Definition: QueryType.cs:57
static void SerializePlan(IQueryPlanNode queryPlan, BinaryWriter writer)
Definition: QueryType.cs:79
A node element of a query plan tree. /summary>
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
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 void SerializeObject(Stream stream, ISqlObject obj)
Definition: QueryType.cs:62
override ISqlObject DeserializeObject(Stream stream)
Definition: QueryType.cs:44