DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
FromTableSubQuerySource.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.Linq;
19 
21 
22 namespace Deveel.Data.Sql.Query {
29 
30  internal FromTableSubQuerySource(bool caseInsensitive, string uniqueKey, SqlQueryExpression queryExpression,
31  QueryExpressionFrom fromSet, ObjectName alias) {
32  UniqueName = uniqueKey;
33  QueryExpression = queryExpression;
34  QueryFrom = fromSet;
35  AliasName = alias;
36  IgnoreCase = caseInsensitive;
37  }
38 
39  public bool MatchesReference(string catalog, string schema, string table) {
40  if (schema == null && table == null)
41  return true;
42 
43  if (AliasName != null) {
44  string ts = AliasName.Parent.Name;
45  string tt = AliasName.Name;
46  if (schema == null)
47  return StringCompare(tt, table);
48  if (StringCompare(tt, table) && StringCompare(ts, schema))
49  return true;
50  }
51 
52  // No way to determine if there is a match
53  return false;
54  }
55 
56  public SqlQueryExpression QueryExpression { get; private set; }
57 
58  public QueryExpressionFrom QueryFrom { get; private set; }
59 
60  public ObjectName AliasName { get; private set; }
61 
62  public bool IgnoreCase { get; private set; }
63 
64  public string UniqueName { get; private set; }
65 
66  public ObjectName[] ColumnNames {
67  get {
68  EnsureColumnNames();
69  return columnNames;
70  }
71  }
72 
76  private void EnsureColumnNames() {
77  if (columnNames == null) {
78  columnNames = QueryFrom.GetResolvedColumns();
79 
80  // Are the variables aliased to a table name?
81  if (AliasName != null) {
82  for (int i = 0; i < columnNames.Length; ++i) {
83  columnNames[i] = new ObjectName(AliasName, columnNames[i].Name);
84  }
85  }
86  }
87  }
88 
89  private bool StringCompare(string str1, string str2) {
90  var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
91  return String.Equals(str1, str2, comparison);
92  }
93 
94  private bool Matches(ObjectName name, string catalog, string schema, string table, string column) {
95  var tableName = name.Parent;
96  var schemaName = tableName == null ? null : tableName.Parent;
97  var catalogName = schemaName == null ? null : schemaName.Parent;
98  var columnName = name.Name;
99 
100  if (column == null)
101  return true;
102  if (!StringCompare(columnName, column))
103  return false;
104 
105  if (table == null)
106  return true;
107  if (tableName == null)
108  return false;
109 
110  string tname = tableName.Name;
111  if (tname != null && !StringCompare(tname, table))
112  return false;
113 
114  if (schema == null)
115  return true;
116 
117  string sname = schemaName != null ? schemaName.Name : null;
118  if (sname != null && !StringCompare(sname, schema))
119  return false;
120 
121  string cname = catalogName != null ? catalogName.Name : null;
122  if (cname != null && !StringCompare(cname, catalog))
123  return false;
124 
125  return true;
126  }
127 
128  public int ResolveColumnCount(string catalog, string schema, string table, string column) {
129  EnsureColumnNames();
130 
131  if (String.IsNullOrEmpty(catalog) &&
132  String.IsNullOrEmpty(schema) &&
133  String.IsNullOrEmpty(table) &&
134  String.IsNullOrEmpty(column))
135  return columnNames.Length;
136 
137  return columnNames.Count(v => Matches(v, catalog, schema, table, column));
138 
139  }
140 
141  public ObjectName ResolveColumn(string catalog, string schema, string table, string column) {
142  EnsureColumnNames();
143 
144  var result = columnNames.FirstOrDefault(v => Matches(v, catalog, schema, table, column));
145  if (result == null)
146  throw new InvalidOperationException("Couldn't resolve to a column.");
147 
148  return result;
149  }
150  }
151 }
An implementation of IFromTableSource that wraps around a SqlQueryExpression as a sub-query source...
A query to the database to select data from a set of tables and columns.
A single table resource item in a query which handles the behaviour of resolving references to column...
Describes the name of an object within a database.
Definition: ObjectName.cs:44
void EnsureColumnNames()
Makes sure the columnNames list is created correctly.
bool Matches(ObjectName name, string catalog, string schema, string table, string column)
FromTableSubQuerySource(bool caseInsensitive, string uniqueKey, SqlQueryExpression queryExpression, QueryExpressionFrom fromSet, ObjectName alias)
ObjectName ResolveColumn(string catalog, string schema, string table, string column)
Resolves a variable within the current source.
int ResolveColumnCount(string catalog, string schema, string table, string column)
Returns the number of instances we can resolve the given catalog, schema, table and column name to a ...
ObjectName Parent
Gets the parent reference of the current one, if any or null if none.
Definition: ObjectName.cs:99
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
bool MatchesReference(string catalog, string schema, string table)
Checks if the table matches the given catalog, schema and table.