DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ColumnInfo.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 
23 using Deveel.Data.Types;
24 
25 namespace Deveel.Data.Sql.Tables {
35  [Serializable]
36  public sealed class ColumnInfo : ISerializable {
47  public ColumnInfo(string columnName, SqlType columnType) {
48  if (String.IsNullOrEmpty(columnName))
49  throw new ArgumentNullException("columnName");
50  if (columnType == null)
51  throw new ArgumentNullException("columnType");
52 
53 
54  ColumnType = columnType;
55  ColumnName = columnName;
56  }
57 
58  private ColumnInfo(ObjectData data) {
59  ColumnName = data.GetString("ColumnName");
60  ColumnType = data.GetValue<SqlType>("ColumnType");
61  IsNotNull = data.GetBoolean("IsNotNull");
62  DefaultExpression = data.GetValue<SqlExpression>("Default");
63  IndexType = data.GetString("IndexType");
64  }
65 
74  public TableInfo TableInfo { get; internal set; }
75 
79  public string ColumnName { get; private set; }
80 
90  public ObjectName FullColumnName {
91  get { return TableInfo == null ? new ObjectName(ColumnName) : new ObjectName(TableInfo.TableName, ColumnName); }
92  }
93 
99  public SqlType ColumnType { get; private set; }
100 
105  public int Offset {
106  get { return TableInfo == null ? -1 : TableInfo.IndexOfColumn(ColumnName); }
107  }
108 
109  public bool HasSize {
110  get { return ColumnType is ISizeableType; }
111  }
112 
113  public int Size {
114  get {
115  var sizeable = ColumnType as ISizeableType;
116  return sizeable == null ? -1 : sizeable.Size;
117  }
118  }
119 
120  public bool HasScale {
121  get { return ColumnType is NumericType; }
122  }
123 
124  public int Scale {
125  get {
126  var numericType = ColumnType as NumericType;
127  return numericType == null ? -1 : numericType.Scale;
128  }
129  }
130 
136  public bool IsIndexable {
137  get { return ColumnType.IsIndexable; }
138  }
139 
144  public bool IsNotNull { get; set; }
145 
151  public SqlExpression DefaultExpression { get; set; }
152 
157  public bool HasDefaultExpression {
158  get { return DefaultExpression != null; }
159  }
160 
161  public string IndexType { get; internal set; }
162 
163 
165  data.SetValue("ColumnName", ColumnName);
166  data.SetValue("ColumnType", ColumnType);
167  data.SetValue("IsNotNull", IsNotNull);
168  data.SetValue("Default", DefaultExpression);
169  data.SetValue("IndexType", IndexType);
170  }
171 
172 
173  public static void Serialize(ColumnInfo columnInfo, BinaryWriter writer) {
174  writer.Write(3); // Version
175  writer.Write(columnInfo.ColumnName);
176 
177  TypeSerializer.SerializeTo(writer, columnInfo.ColumnType);
178 
179  writer.Write(columnInfo.IsNotNull ? (byte)1 : (byte)0);
180 
181  if (columnInfo.DefaultExpression != null) {
182  writer.Write((byte)1);
183  SqlExpression.Serialize(columnInfo.DefaultExpression, writer);
184  } else {
185  writer.Write((byte)0);
186  }
187  }
188 
189  public static void Serialize(ColumnInfo columnInfo, Stream stream, Encoding encoding) {
190  var writer = new BinaryWriter(stream, encoding);
191  Serialize(columnInfo, writer);
192  }
193 
194  public static void Serialize(ColumnInfo columnInfo, Stream stream) {
195  Serialize(columnInfo, stream, Encoding.Unicode);
196  }
197 
198  public static ColumnInfo Deserialize(Stream stream, ITypeResolver typeResolver) {
199  var reader = new BinaryReader(stream, Encoding.Unicode);
200  return Deserialize(reader, typeResolver);
201  }
202 
203  public static ColumnInfo Deserialize(BinaryReader reader, ITypeResolver typeResolver) {
204  var version = reader.ReadInt32();
205  if (version != 3)
206  throw new FormatException("Invalid version of the Column-Info");
207 
208  var columnName = reader.ReadString();
209  var columnType = TypeSerializer.Deserialize(reader, typeResolver);
210 
211  var notNull = reader.ReadByte() == 1;
212 
213  var columnInfo = new ColumnInfo(columnName, columnType);
214  columnInfo.IsNotNull = notNull;
215 
216  var hasDefault = reader.ReadByte() == 1;
217  if (hasDefault)
218  columnInfo.DefaultExpression = SqlExpression.Deserialize(reader);
219 
220  return columnInfo;
221  }
222  }
223 }
Defines the metadata properties of a column within a table of a database.
Definition: ColumnInfo.cs:36
static void SerializeTo(BinaryWriter writer, SqlType type)
void GetData(SerializeData data)
A long string in the system.
static void Serialize(ColumnInfo columnInfo, Stream stream, Encoding encoding)
Definition: ColumnInfo.cs:189
bool IsNotNull
Gets or sets a boolean value indicating if the column values are constrained to be ony NOT NULL...
Definition: ColumnInfo.cs:144
static SqlType Deserialize(BinaryReader reader, ITypeResolver resolver)
void SetValue(string key, Type type, object value)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
virtual bool IsIndexable
Indicates if the values handled by the type can be part of an index.
Definition: SqlType.cs:100
static void Serialize(SqlExpression expression, BinaryWriter writer)
ObjectName TableName
Gets the fully qualified name of the table that is ensured to be unique within the system...
Definition: TableInfo.cs:97
static SqlExpression Deserialize(BinaryReader reader)
static void Serialize(ColumnInfo columnInfo, Stream stream)
Definition: ColumnInfo.cs:194
static ColumnInfo Deserialize(Stream stream, ITypeResolver typeResolver)
Definition: ColumnInfo.cs:198
static void Serialize(ColumnInfo columnInfo, BinaryWriter writer)
Definition: ColumnInfo.cs:173
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
string ColumnName
Gets the name of the column.
Definition: ColumnInfo.cs:79
SqlExpression DefaultExpression
Gets or sets a SqlExpression used as a DEFAULT when a constraint for the column is to SET DEFAULT...
Definition: ColumnInfo.cs:151
SqlType ColumnType
Gets the SqlType that cells within a table for this column will handle.
Definition: ColumnInfo.cs:99
int IndexOfColumn(string columnName)
Gets the offset of the column with the given name.
Definition: TableInfo.cs:310
ColumnInfo(string columnName, SqlType columnType)
Constructs a new column with the given name and type.
Definition: ColumnInfo.cs:47
static ColumnInfo Deserialize(BinaryReader reader, ITypeResolver typeResolver)
Definition: ColumnInfo.cs:203
Defines the base class for instances that represent SQL expression tree nodes.
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41