18 using System.Collections.Generic;
37 IgnoreCase = ignoreCase;
39 tableSources =
new List<IFromTableSource>();
40 expressionReferences =
new List<ExpressionReference>();
41 exposedColumns =
new List<ObjectName>();
46 public bool IgnoreCase {
get;
private set; }
52 public int SourceCount {
53 get {
return tableSources.Count; }
57 var compareType = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
58 return String.Equals(str1, str2, compareType);
62 tableSources.Add(tableSource);
66 expressionReferences.Add(expressionReference);
70 exposedColumns.Add(columName);
74 foreach (var column
in tableSource.ColumnNames) {
75 exposedColumns.Add(column);
83 throw new InvalidOperationException(
"Table name found: " + tableName);
89 foreach (var source
in tableSources) {
90 ExposeColumns(source);
95 return tableSources[offset];
99 return tableSources.FirstOrDefault(x => x.MatchesReference(null, schema, name));
103 var columnNames =
new ObjectName[exposedColumns.Count];
104 exposedColumns.CopyTo(columnNames, 0);
110 throw new ArgumentNullException(
"alias");
115 var aliasName = alias.
Name;
119 foreach (var reference
in expressionReferences) {
121 throw new AmbiguousMatchException(String.Format(
"Alias '{0}' resolves to multiple expressions in the plan", alias));
123 if (CompareStrings(reference.Alias, aliasName)) {
124 expToRetun = reference.Expression;
133 var parent = columnName.
Parent;
134 var name = columnName.
Name;
135 string schemaName = null;
136 string tableName = null;
137 if (parent != null) {
138 tableName = parent.Name;
139 parent = parent.Parent;
141 schemaName = parent.Name;
146 foreach (var source
in tableSources) {
147 int matchCount = source.ResolveColumnCount(null, schemaName, tableName, name);
149 throw new AmbiguousMatchException();
152 matchedColumn = source.ResolveColumn(null, schemaName, tableName, name);
155 return matchedColumn;
162 var aliasName = alias.
Name;
166 foreach (var reference
in expressionReferences) {
168 throw new AmbiguousMatchException();
170 if (CompareStrings(aliasName, reference.Alias)) {
180 var refList =
new List<ObjectName>();
182 var resolved = ResolveAliasReference(refName);
183 if (resolved != null)
184 refList.Add(resolved);
186 resolved = ResolveColumnReference(refName);
187 if (resolved != null)
188 refList.Add(resolved);
190 if (refList.Count > 1)
191 throw new AmbiguousMatchException();
193 if (refList.Count == 0)
200 ObjectName resolvedName = ResolveReference(name);
201 if (resolvedName == null && Parent != null)
203 return Parent.GlobalResolveReference(level + 1, name);
205 if (resolvedName != null)
212 var referenceName = ResolveReference(name);
213 if (referenceName == null) {
215 throw new InvalidOperationException(String.Format(
"Reference {0} was not found in context.", name));
217 var queryRef = GlobalResolveReference(1, name);
218 if (queryRef == null)
219 throw new InvalidOperationException(String.Format(
"Reference {0} was not found in context.", name));
224 return referenceName;
230 var ignoreCase = context.
Query.IgnoreIdentifiersCase();
233 foreach (var fromTable
in fromClause.AllTables) {
234 var uniqueKey = fromTable.UniqueKey;
235 var alias = fromTable.Alias;
237 if (fromTable.IsSubQuery) {
239 var subQuery = fromTable.SubQuery;
240 var subQueryFrom = Create(context, subQuery);
248 queryFrom.AddTable(
new FromTableSubQuerySource(ignoreCase, uniqueKey, subQuery, subQueryFrom, aliasTableName));
251 string name = fromTable.Name;
254 var tableName = context.Query.ResolveTableName(name);
256 if (!context.Query.TableExists(tableName))
257 throw new InvalidOperationException(String.Format(
"Table '{0}' was not found.", tableName));
264 ITableQueryInfo tableQueryInfo = context.Query.GetTableQueryInfo(tableName, givenName);
266 queryFrom.AddTable(
new FromTableDirectSource(ignoreCase, tableQueryInfo, uniqueKey, givenName, tableName));
272 foreach (var selectColumn
in expression.SelectColumns) {
274 if (selectColumn.IsGlob) {
276 if (selectColumn.IsAll) {
277 queryFrom.ExposeAllColumns();
280 queryFrom.ExposeColumns(selectColumn.TableName);
291 string alias = selectColumn.Alias;
292 var v = selectColumn.Expression.AsReferenceName();
293 bool aliasMatchV = (v != null && alias != null &&
294 queryFrom.CompareStrings(v.Name, alias));
295 if (alias != null && !aliasMatchV) {
297 queryFrom.ExposeColumn(
new ObjectName(alias));
298 }
else if (v != null) {
299 var resolved = queryFrom.ResolveReference(v);
300 queryFrom.ExposeColumn(resolved ?? v);
302 string funName = selectColumn.Expression.ToString();
304 queryFrom.ExposeColumn(
new ObjectName(funName));
312 #region FromExpressionPreparer
318 this.fromSet = fromSet;
328 var reference = fromSet.QualifyReference(refName);
334 throw new InvalidOperationException();
bool CompareStrings(string str1, string str2)
readonly QueryExpressionFrom fromSet
void ExposeColumn(ObjectName columName)
readonly List< ExpressionReference > expressionReferences
An implementation of IFromTableSource that wraps around a SqlQueryExpression as a sub-query source...
SqlExpression FindExpression(ObjectName alias)
An expression that references an object within a context.
readonly List< ObjectName > exposedColumns
readonly List< IFromTableSource > tableSources
QueryReference GlobalResolveReference(int level, ObjectName name)
IFromTableSource GetTableSource(int offset)
A single table resource item in a query which handles the behaviour of resolving references to column...
ObjectName ResolveReference(ObjectName refName)
void ExposeColumns(IFromTableSource tableSource)
Describes the name of an object within a database.
void AddTable(IFromTableSource tableSource)
ObjectName ResolveColumnReference(ObjectName columnName)
FromExpressionPreparer(QueryExpressionFrom fromSet)
static QueryExpressionFrom Create(IRequest context, SqlQueryExpression expression)
bool CanPrepare(SqlExpression expression)
Verifies whether the instance of the interface can prepare the given expression.
An interface used to prepare a SqlExpression object.
SqlExpression Prepare(SqlExpression expression)
Returns the new translated object to be mutated from the given expression.
QueryExpressionFrom(bool ignoreCase)
ObjectName Parent
Gets the parent reference of the current one, if any or null if none.
void ExposeColumns(ObjectName tableName)
string Name
Gets the name of the object being referenced.
static SqlReferenceExpression Reference(ObjectName objectName)
object QualifyReference(ObjectName name)
void AddExpression(ExpressionReference expressionReference)
Defines the base class for instances that represent SQL expression tree nodes.
ObjectName ResolveAliasReference(ObjectName alias)
ObjectName[] GetResolvedColumns()
An implementation of IFromTableSource that wraps around a ObjectName/ITable object.
IFromTableSource FindTable(string schema, string name)