18 using System.Collections.Generic;
19 using System.Globalization;
24 namespace Deveel.Data.Sql.Expressions {
34 fromTables =
new List<FromTable>();
35 joinParts =
new List<JoinPart>();
36 tableNames =
new List<string>();
40 var tableNames = data.
GetValue<
string[]>(
"TableNames");
44 this.tableNames =
new List<string>();
45 this.fromTables =
new List<FromTable>();
46 this.joinParts =
new List<JoinPart>();
48 if (tableNames != null)
49 this.tableNames.AddRange(tableNames);
50 if (fromTables != null)
51 this.fromTables.AddRange(fromTables);
52 if (joinParts != null)
53 this.joinParts.AddRange(joinParts);
68 public IEnumerable<FromTable> AllTables {
69 get {
return fromTables.ToArray(); }
75 public int JoinPartCount {
76 get {
return joinParts.Count; }
80 get {
return fromTables.Count == 0; }
85 return tableKey.ToString(CultureInfo.InvariantCulture);
95 throw new ArgumentNullException(
"table");
97 if (!String.IsNullOrEmpty(alias)) {
98 if (tableNames.Contains(alias))
99 throw new ArgumentException(String.Format(
"Duplicated table name {0} is FROM clause.", alias));
101 tableNames.Add(alias);
105 string key = CreateNewKey();
107 fromTables.Add(table);
116 public void AddTable(
string alias,
string tableName) {
117 AddTable(alias,
new FromTable(tableName, alias));
126 AddTable(null, tableName);
135 AddSubQuery(null, subQuery);
146 AddTable(alias,
new FromTable(subQuery, alias));
155 var lastTable = fromTables[fromTables.Count - 1];
156 if (lastTable.IsSubQuery) {
157 var subQuery = lastTable.SubQuery;
158 joinParts.Add(
new JoinPart(joinType, subQuery, onExpression));
161 joinParts.Add(
new JoinPart(joinType, tableName, onExpression));
175 return joinParts[offset];
182 int size = joinParts.Count;
183 for (
int i = 0; i < size; ++i) {
184 var part = joinParts[i];
185 var exp = part.OnExpression;
187 exp = exp.Prepare(preparer);
188 if (part.SubQuery != null) {
189 part =
new JoinPart(part.JoinType, part.SubQuery, exp);
191 part =
new JoinPart(part.JoinType, part.TableName, exp);
195 clause.joinParts.Add(part);
199 for (
int i = 0; i < fromTables.Count; i++) {
200 var table = fromTables[i];
202 var tableAlias = tableNames[i];
203 clause.tableNames.Insert(i, tableAlias);
204 clause.fromTables.Insert(i, preparedTable);
211 if (tableNames != null)
212 data.
SetValue(
"TableNames", tableNames.ToArray());
213 if (fromTables != null)
214 data.
SetValue(
"FromTables", fromTables.ToArray());
215 if (joinParts != null)
216 data.
SetValue(
"JoinParts", joinParts.ToArray());
void GetData(SerializeData data)
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
JoinType
Enumerates the kind of group join in a selection query.
void AddTable(string alias, FromTable table)
Adds a table as source to the query with a given alias.
readonly List< JoinPart > joinParts
void Join(JoinType joinType, SqlExpression onExpression)
Sets a join between the last added table and the one that preceeds it.
void SetValue(string key, Type type, object value)
Describes the name of an object within a database.
JoinPart GetJoinPart(int offset)
Gets the descriptor of the join at the given offset.
readonly List< string > tableNames
int tableKey
An id used for making unique names for anonymous inner selects.
void AddSubQuery(SqlQueryExpression subQuery)
Adds a sub-query expression as source of the query.
A container for the FROM clause of a select statement.
An interface used to prepare a SqlExpression object.
string UniqueKey
Gets or sets the unique key.
FromClause(ObjectData data)
readonly List< FromTable > fromTables
object Prepare(IExpressionPreparer preparer)
Converts the underlying value of this instance into an object that can be evaluated by an expression...
void AddTable(string alias, string tableName)
Adds a simple table reference as the source of the query with a given alias.
void AddTable(string tableName)
Adds a simple table reference as the source of the query.
object GetValue(string key)
Describes a single table declaration in the from clause of a table expression (SELECT).
Defines the base class for instances that represent SQL expression tree nodes.
void AddSubQuery(string alias, SqlQueryExpression subQuery)
Adds a sub-query expression as source of the query.
A contract for objects that participate to a SqlExpression.Prepare phase of an expression evaluation...