DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Static Public Member Functions | Properties | Private Member Functions | Private Attributes | List of all members
Deveel.Data.Sql.Query.QueryExpressionFrom Class Reference

Classes

class  FromExpressionPreparer
 

Public Member Functions

 QueryExpressionFrom ()
 
 QueryExpressionFrom (bool ignoreCase)
 
bool CompareStrings (string str1, string str2)
 
void AddTable (IFromTableSource tableSource)
 
void AddExpression (ExpressionReference expressionReference)
 
void ExposeColumn (ObjectName columName)
 
void ExposeColumns (IFromTableSource tableSource)
 
void ExposeColumns (ObjectName tableName)
 
void ExposeAllColumns ()
 
IFromTableSource GetTableSource (int offset)
 
IFromTableSource FindTable (string schema, string name)
 
ObjectName[] GetResolvedColumns ()
 
SqlExpression FindExpression (ObjectName alias)
 
ObjectName ResolveReference (ObjectName refName)
 

Static Public Member Functions

static QueryExpressionFrom Create (IRequest context, SqlQueryExpression expression)
 

Properties

bool IgnoreCase [get, private set]
 
QueryExpressionFrom Parent [get, set]
 
IExpressionPreparer ExpressionPreparer [get, private set]
 
int SourceCount [get]
 

Private Member Functions

ObjectName ResolveColumnReference (ObjectName columnName)
 
ObjectName ResolveAliasReference (ObjectName alias)
 
QueryReference GlobalResolveReference (int level, ObjectName name)
 
object QualifyReference (ObjectName name)
 

Private Attributes

readonly List< IFromTableSourcetableSources
 
readonly List< ExpressionReferenceexpressionReferences
 
readonly List< ObjectNameexposedColumns
 

Detailed Description

Definition at line 27 of file QueryExpressionFrom.cs.

Constructor & Destructor Documentation

Deveel.Data.Sql.Query.QueryExpressionFrom.QueryExpressionFrom ( )
inline

Definition at line 32 of file QueryExpressionFrom.cs.

33  : this(true) {
34  }
Deveel.Data.Sql.Query.QueryExpressionFrom.QueryExpressionFrom ( bool  ignoreCase)
inline

Definition at line 36 of file QueryExpressionFrom.cs.

36  {
37  IgnoreCase = ignoreCase;
38 
39  tableSources = new List<IFromTableSource>();
40  expressionReferences = new List<ExpressionReference>();
41  exposedColumns = new List<ObjectName>();
42 
43  ExpressionPreparer = new FromExpressionPreparer(this);
44  }
readonly List< ExpressionReference > expressionReferences
readonly List< ObjectName > exposedColumns
readonly List< IFromTableSource > tableSources

Member Function Documentation

void Deveel.Data.Sql.Query.QueryExpressionFrom.AddExpression ( ExpressionReference  expressionReference)
inline

Definition at line 65 of file QueryExpressionFrom.cs.

65  {
66  expressionReferences.Add(expressionReference);
67  }
readonly List< ExpressionReference > expressionReferences
void Deveel.Data.Sql.Query.QueryExpressionFrom.AddTable ( IFromTableSource  tableSource)
inline

Definition at line 61 of file QueryExpressionFrom.cs.

61  {
62  tableSources.Add(tableSource);
63  }
readonly List< IFromTableSource > tableSources
bool Deveel.Data.Sql.Query.QueryExpressionFrom.CompareStrings ( string  str1,
string  str2 
)
inline

Definition at line 56 of file QueryExpressionFrom.cs.

56  {
57  var compareType = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
58  return String.Equals(str1, str2, compareType);
59  }
A long string in the system.
static QueryExpressionFrom Deveel.Data.Sql.Query.QueryExpressionFrom.Create ( IRequest  context,
SqlQueryExpression  expression 
)
inlinestatic

Definition at line 227 of file QueryExpressionFrom.cs.

227  {
228  // Get the 'from_clause' from the table expression
229  var fromClause = expression.FromClause;
230  var ignoreCase = context.Query.IgnoreIdentifiersCase();
231 
232  var queryFrom = new QueryExpressionFrom(ignoreCase);
233  foreach (var fromTable in fromClause.AllTables) {
234  var uniqueKey = fromTable.UniqueKey;
235  var alias = fromTable.Alias;
236 
237  if (fromTable.IsSubQuery) {
238  // eg. FROM ( SELECT id FROM Part )
239  var subQuery = fromTable.SubQuery;
240  var subQueryFrom = Create(context, subQuery);
241 
242  // The aliased name of the table
243  ObjectName aliasTableName = null;
244  if (alias != null)
245  aliasTableName = new ObjectName(alias);
246 
247  // Add to list of sub-query tables to add to command,
248  queryFrom.AddTable(new FromTableSubQuerySource(ignoreCase, uniqueKey, subQuery, subQueryFrom, aliasTableName));
249  } else {
250  // Else must be a standard command table,
251  string name = fromTable.Name;
252 
253  // Resolve to full table name
254  var tableName = context.Query.ResolveTableName(name);
255 
256  if (!context.Query.TableExists(tableName))
257  throw new InvalidOperationException(String.Format("Table '{0}' was not found.", tableName));
258 
259  ObjectName givenName = null;
260  if (alias != null)
261  givenName = new ObjectName(alias);
262 
263  // Get the ITableQueryInfo object for this table name (aliased).
264  ITableQueryInfo tableQueryInfo = context.Query.GetTableQueryInfo(tableName, givenName);
265 
266  queryFrom.AddTable(new FromTableDirectSource(ignoreCase, tableQueryInfo, uniqueKey, givenName, tableName));
267  }
268  }
269 
270  // Set up functions, aliases and exposed variables for this from set,
271 
272  foreach (var selectColumn in expression.SelectColumns) {
273  // Is this a glob? (eg. Part.* )
274  if (selectColumn.IsGlob) {
275  // Find the columns globbed and add to the 'selectedColumns' result.
276  if (selectColumn.IsAll) {
277  queryFrom.ExposeAllColumns();
278  } else {
279  // Otherwise the glob must be of the form '[table name].*'
280  queryFrom.ExposeColumns(selectColumn.TableName);
281  }
282  } else {
283  // Otherwise must be a standard column reference. Note that at this
284  // time we aren't sure if a column expression is correlated and is
285  // referencing an outer source. This means we can't verify if the
286  // column expression is valid or not at this point.
287 
288  // If this column is aliased, add it as a function reference to the
289  // select expression
290 
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) {
296  queryFrom.AddExpression(new ExpressionReference(selectColumn.Expression, alias));
297  queryFrom.ExposeColumn(new ObjectName(alias));
298  } else if (v != null) {
299  var resolved = queryFrom.ResolveReference(v);
300  queryFrom.ExposeColumn(resolved ?? v);
301  } else {
302  string funName = selectColumn.Expression.ToString();
303  queryFrom.AddExpression(new ExpressionReference(selectColumn.Expression, funName));
304  queryFrom.ExposeColumn(new ObjectName(funName));
305  }
306  }
307  }
308 
309  return queryFrom;
310  }
A long string in the system.
static QueryExpressionFrom Create(IRequest context, SqlQueryExpression expression)
void Deveel.Data.Sql.Query.QueryExpressionFrom.ExposeAllColumns ( )
inline

Definition at line 88 of file QueryExpressionFrom.cs.

88  {
89  foreach (var source in tableSources) {
90  ExposeColumns(source);
91  }
92  }
readonly List< IFromTableSource > tableSources
void ExposeColumns(IFromTableSource tableSource)
void Deveel.Data.Sql.Query.QueryExpressionFrom.ExposeColumn ( ObjectName  columName)
inline

Definition at line 69 of file QueryExpressionFrom.cs.

69  {
70  exposedColumns.Add(columName);
71  }
readonly List< ObjectName > exposedColumns
void Deveel.Data.Sql.Query.QueryExpressionFrom.ExposeColumns ( IFromTableSource  tableSource)
inline

Definition at line 73 of file QueryExpressionFrom.cs.

73  {
74  foreach (var column in tableSource.ColumnNames) {
75  exposedColumns.Add(column);
76  }
77  }
readonly List< ObjectName > exposedColumns
void Deveel.Data.Sql.Query.QueryExpressionFrom.ExposeColumns ( ObjectName  tableName)
inline

Definition at line 79 of file QueryExpressionFrom.cs.

79  {
80  var schema = tableName.Parent != null ? tableName.Parent.Name : null;
81  IFromTableSource table = FindTable(schema, tableName.Name);
82  if (table == null)
83  throw new InvalidOperationException("Table name found: " + tableName);
84 
85  ExposeColumns(table);
86  }
void ExposeColumns(IFromTableSource tableSource)
IFromTableSource FindTable(string schema, string name)
SqlExpression Deveel.Data.Sql.Query.QueryExpressionFrom.FindExpression ( ObjectName  alias)
inline

Definition at line 108 of file QueryExpressionFrom.cs.

108  {
109  if (alias == null)
110  throw new ArgumentNullException("alias");
111 
112  if (alias.Parent != null)
113  return null;
114 
115  var aliasName = alias.Name;
116 
117  int matchCount = 0;
118  SqlExpression expToRetun = null;
119  foreach (var reference in expressionReferences) {
120  if (matchCount > 1)
121  throw new AmbiguousMatchException(String.Format("Alias '{0}' resolves to multiple expressions in the plan", alias));
122 
123  if (CompareStrings(reference.Alias, aliasName)) {
124  expToRetun = reference.Expression;
125  matchCount++;
126  }
127  }
128 
129  return expToRetun;
130  }
bool CompareStrings(string str1, string str2)
readonly List< ExpressionReference > expressionReferences
A long string in the system.
Defines the base class for instances that represent SQL expression tree nodes.
IFromTableSource Deveel.Data.Sql.Query.QueryExpressionFrom.FindTable ( string  schema,
string  name 
)
inline

Definition at line 98 of file QueryExpressionFrom.cs.

98  {
99  return tableSources.FirstOrDefault(x => x.MatchesReference(null, schema, name));
100  }
readonly List< IFromTableSource > tableSources
ObjectName [] Deveel.Data.Sql.Query.QueryExpressionFrom.GetResolvedColumns ( )
inline

Definition at line 102 of file QueryExpressionFrom.cs.

102  {
103  var columnNames = new ObjectName[exposedColumns.Count];
104  exposedColumns.CopyTo(columnNames, 0);
105  return columnNames;
106  }
readonly List< ObjectName > exposedColumns
IFromTableSource Deveel.Data.Sql.Query.QueryExpressionFrom.GetTableSource ( int  offset)
inline

Definition at line 94 of file QueryExpressionFrom.cs.

94  {
95  return tableSources[offset];
96  }
readonly List< IFromTableSource > tableSources
QueryReference Deveel.Data.Sql.Query.QueryExpressionFrom.GlobalResolveReference ( int  level,
ObjectName  name 
)
inlineprivate

Definition at line 199 of file QueryExpressionFrom.cs.

199  {
200  ObjectName resolvedName = ResolveReference(name);
201  if (resolvedName == null && Parent != null)
202  // If we need to descend to the parent, increment the level.
203  return Parent.GlobalResolveReference(level + 1, name);
204 
205  if (resolvedName != null)
206  return new QueryReference(resolvedName, level);
207 
208  return null;
209  }
QueryReference GlobalResolveReference(int level, ObjectName name)
ObjectName ResolveReference(ObjectName refName)
object Deveel.Data.Sql.Query.QueryExpressionFrom.QualifyReference ( ObjectName  name)
inlineprivate

Definition at line 211 of file QueryExpressionFrom.cs.

211  {
212  var referenceName = ResolveReference(name);
213  if (referenceName == null) {
214  if (Parent == null)
215  throw new InvalidOperationException(String.Format("Reference {0} was not found in context.", name));
216 
217  var queryRef = GlobalResolveReference(1, name);
218  if (queryRef == null)
219  throw new InvalidOperationException(String.Format("Reference {0} was not found in context.", name));
220 
221  return queryRef;
222  }
223 
224  return referenceName;
225  }
A long string in the system.
QueryReference GlobalResolveReference(int level, ObjectName name)
ObjectName ResolveReference(ObjectName refName)
ObjectName Deveel.Data.Sql.Query.QueryExpressionFrom.ResolveAliasReference ( ObjectName  alias)
inlineprivate

Definition at line 158 of file QueryExpressionFrom.cs.

158  {
159  if (alias.Parent != null)
160  return null;
161 
162  var aliasName = alias.Name;
163 
164  int matchCount = 0;
165  ObjectName matched = null;
166  foreach (var reference in expressionReferences) {
167  if (matchCount > 1)
168  throw new AmbiguousMatchException();
169 
170  if (CompareStrings(aliasName, reference.Alias)) {
171  matched = new ObjectName(reference.Alias);
172  matchCount++;
173  }
174  }
175 
176  return matched;
177  }
bool CompareStrings(string str1, string str2)
readonly List< ExpressionReference > expressionReferences
ObjectName Deveel.Data.Sql.Query.QueryExpressionFrom.ResolveColumnReference ( ObjectName  columnName)
inlineprivate

Definition at line 132 of file QueryExpressionFrom.cs.

132  {
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;
140  if (parent != null)
141  schemaName = parent.Name;
142  }
143 
144  ObjectName matchedColumn = null;
145 
146  foreach (var source in tableSources) {
147  int matchCount = source.ResolveColumnCount(null, schemaName, tableName, name);
148  if (matchCount > 1)
149  throw new AmbiguousMatchException();
150 
151  if (matchCount == 1)
152  matchedColumn = source.ResolveColumn(null, schemaName, tableName, name);
153  }
154 
155  return matchedColumn;
156  }
readonly List< IFromTableSource > tableSources
ObjectName Deveel.Data.Sql.Query.QueryExpressionFrom.ResolveReference ( ObjectName  refName)
inline

Definition at line 179 of file QueryExpressionFrom.cs.

179  {
180  var refList = new List<ObjectName>();
181 
182  var resolved = ResolveAliasReference(refName);
183  if (resolved != null)
184  refList.Add(resolved);
185 
186  resolved = ResolveColumnReference(refName);
187  if (resolved != null)
188  refList.Add(resolved);
189 
190  if (refList.Count > 1)
191  throw new AmbiguousMatchException();
192 
193  if (refList.Count == 0)
194  return null;
195 
196  return refList[0];
197  }
ObjectName ResolveColumnReference(ObjectName columnName)
ObjectName ResolveAliasReference(ObjectName alias)

Member Data Documentation

readonly List<ObjectName> Deveel.Data.Sql.Query.QueryExpressionFrom.exposedColumns
private

Definition at line 30 of file QueryExpressionFrom.cs.

readonly List<ExpressionReference> Deveel.Data.Sql.Query.QueryExpressionFrom.expressionReferences
private

Definition at line 29 of file QueryExpressionFrom.cs.

readonly List<IFromTableSource> Deveel.Data.Sql.Query.QueryExpressionFrom.tableSources
private

Definition at line 28 of file QueryExpressionFrom.cs.

Property Documentation

IExpressionPreparer Deveel.Data.Sql.Query.QueryExpressionFrom.ExpressionPreparer
getprivate set

Definition at line 50 of file QueryExpressionFrom.cs.

bool Deveel.Data.Sql.Query.QueryExpressionFrom.IgnoreCase
getprivate set

Definition at line 46 of file QueryExpressionFrom.cs.

QueryExpressionFrom Deveel.Data.Sql.Query.QueryExpressionFrom.Parent
getset

Definition at line 48 of file QueryExpressionFrom.cs.

int Deveel.Data.Sql.Query.QueryExpressionFrom.SourceCount
get

Definition at line 52 of file QueryExpressionFrom.cs.


The documentation for this class was generated from the following file: