DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
FromTable.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 
20 
21 namespace Deveel.Data.Sql.Expressions {
26  [Serializable]
27  public sealed class FromTable : IPreparable, ISerializable {
33  public FromTable(string tableName, string tableAlias)
34  : this(tableName, null, tableAlias) {
35  if (String.IsNullOrEmpty(tableName))
36  throw new ArgumentNullException("tableName");
37  }
38 
43  public FromTable(string tableName)
44  : this(tableName, null) {
45  }
46 
52  : this(query, null) {
53  }
54 
60  public FromTable(SqlQueryExpression query, string tableAlias)
61  : this(null, query, tableAlias) {
62  if (query == null)
63  throw new ArgumentNullException("query");
64  }
65 
66  private FromTable(string tableName, SqlQueryExpression query, string alias) {
67  Name = tableName;
68  SubQuery = query;
69  Alias = alias;
70  IsSubQuery = query != null;
71  }
72 
73  private FromTable(ObjectData data) {
74  Name = data.GetString("Name");
75  SubQuery = data.GetValue<SqlQueryExpression>("SubQuery");
76  Alias = data.GetString("Alias");
77  IsSubQuery = data.HasValue("SubQuery");
78  }
79 
83  public string Name { get; private set; }
84 
88  public string Alias { get; private set; }
89 
93  internal string UniqueKey { get; set; }
94 
98  public bool IsSubQuery { get; private set; }
99 
103  public SqlQueryExpression SubQuery { get; private set; }
104 
106  var subQuery = SubQuery;
107  if (subQuery != null)
108  subQuery = (SqlQueryExpression) subQuery.Prepare(preparer);
109 
110  return new FromTable(Name, subQuery, Alias);
111  }
112 
114  data.SetValue("Name", Name);
115  data.SetValue("SubQuery", SubQuery);
116  data.SetValue("Alias", Alias);
117  }
118 
119  //public static void Serialize(FromTable table, BinaryWriter writer) {
120  // if (table.IsSubQuery) {
121  // writer.Write((byte) 2);
122  // SqlExpression.Serialize(table.SubQuery, writer);
123  // } else {
124  // writer.Write((byte)1);
125  // writer.Write(table.Name);
126  // }
127 
128  // bool hasAlias = !String.IsNullOrEmpty(table.Alias);
129  // if (hasAlias) {
130  // writer.Write((byte) 1);
131  // writer.Write(table.Alias);
132  // } else {
133  // writer.Write((byte)0);
134  // }
135 
136  // bool hasUniqueKey = !String.IsNullOrEmpty(table.UniqueKey);
137  // if (hasUniqueKey) {
138  // writer.Write((byte) 1);
139  // writer.Write(table.UniqueKey);
140  // } else {
141  // writer.Write((byte)0);
142  // }
143  //}
144 
145  //public static FromTable Deserialize(BinaryReader reader) {
146  // string name = null;
147  // SqlQueryExpression query = null;
148 
149  // var type = reader.ReadByte();
150  // if (type == 1) {
151  // name = reader.ReadString();
152  // } else if (type == 2) {
153  // query = (SqlQueryExpression) SqlExpression.Deserialize(reader);
154  // } else {
155  // throw new FormatException();
156  // }
157 
158  // string alias = null;
159  // var hasAlias = reader.ReadByte() == 1;
160  // if (hasAlias)
161  // alias = reader.ReadString();
162 
163  // FromTable table;
164  // if (type == 1) {
165  // table = new FromTable(name, alias);
166  // } else {
167  // table = new FromTable(query, alias);
168  // }
169 
170  // var hasUniqueKey = reader.ReadByte() == 1;
171  // if (hasUniqueKey)
172  // table.UniqueKey = reader.ReadString();
173 
174  // return table;
175  //}
176  }
177 }
FromTable(string tableName)
A simple table definition (not aliased).
Definition: FromTable.cs:43
void GetData(SerializeData data)
FromTable(string tableName, string tableAlias)
Constructs a table that is aliased under a different name.
Definition: FromTable.cs:33
void SetValue(string key, Type type, object value)
FromTable(SqlQueryExpression query, string tableAlias)
A table that is a sub-query and given an aliased name.
Definition: FromTable.cs:60
An interface used to prepare a SqlExpression object.
FromTable(string tableName, SqlQueryExpression query, string alias)
Definition: FromTable.cs:66
object Prepare(IExpressionPreparer preparer)
Converts the underlying value of this instance into an object that can be evaluated by an expression...
Describes a single table declaration in the from clause of a table expression (SELECT).
Definition: FromTable.cs:27
FromTable(SqlQueryExpression query)
A table that is a sub-query with no alias set.
Definition: FromTable.cs:51
A contract for objects that participate to a SqlExpression.Prepare phase of an expression evaluation...
Definition: IPreparable.cs:30