DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
QueryExpressionExtensions.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.Collections.Generic;
19 
21 using Deveel.Data.Sql.Objects;
22 using Deveel.Data.Types;
23 
24 namespace Deveel.Data.Sql.Query {
26  public static void DiscoverTableNames(this SqlExpression expression, IList<ObjectName> tableNames) {
27  var visitor = new TableNamesVisitor();
28  visitor.Visit(expression);
29  }
30 
31  public static IList<QueryReference> DiscoverQueryReferences(this SqlExpression expression, ref int level) {
32  return DiscoverQueryReferences(expression, ref level, new List<QueryReference>());
33  }
34 
35  public static IList<QueryReference> DiscoverQueryReferences(this SqlExpression expression, ref int level, IList<QueryReference> list) {
36  var visitor = new QueryReferencesVisitor(list, level);
37  visitor.Visit(expression);
38  level = visitor.Level;
39  return visitor.References;
40  }
41 
42  public static IEnumerable<ObjectName> DiscoverReferences(this SqlExpression expression) {
43  var discovery = new ReferenceDiscovery();
44  return discovery.Discover(expression);
45  }
46 
47  public static bool HasSubQuery(this SqlExpression expression) {
48  return new SubQueryDiscovery().Verify(expression);
49  }
50 
51  #region ColumnNameDiscovery
52 
54  private readonly List<ObjectName> columnNames;
55 
56  public ReferenceDiscovery() {
57  columnNames = new List<ObjectName>();
58  }
59 
60  public IEnumerable<ObjectName> Discover(SqlExpression expression) {
61  Visit(expression);
62  return columnNames.ToArray();
63  }
64 
66  var value = constant.Value;
67  if (value.Type is ArrayType) {
68  var array = (SqlArray) value.Value;
69  foreach (var element in array) {
70  columnNames.AddRange(element.DiscoverReferences());
71  }
72  }
73 
74  return base.VisitConstant(constant);
75  }
76 
78  var args = expression.Arguments;
79  foreach (var arg in args) {
80  columnNames.AddRange(arg.DiscoverReferences());
81  }
82 
83  return base.VisitFunctionCall(expression);
84  }
85 
87  columnNames.Add(reference.ReferenceName);
88  return base.VisitReference(reference);
89  }
90  }
91 
92  #endregion
93 
94  #region SubQueryDiscovery
95 
97  private bool hasSubQuery;
98 
99  public bool Verify(SqlExpression expression) {
100  Visit(expression);
101  return hasSubQuery;
102  }
103 
105  if (constant.Value.Type is QueryType)
106  hasSubQuery = true;
107 
108  return base.VisitConstant(constant);
109  }
110  }
111 
112  #endregion
113  }
114 }
ObjectName ReferenceName
Gets the name of the object referenced by the expression.
SqlType Type
Gets the SqlType that defines the object properties
Definition: DataObject.cs:78
An expression that references an object within a context.
A query to the database to select data from a set of tables and columns.
static void DiscoverTableNames(this SqlExpression expression, IList< ObjectName > tableNames)
static IEnumerable< ObjectName > DiscoverReferences(this SqlExpression expression)
static IList< QueryReference > DiscoverQueryReferences(this SqlExpression expression, ref int level, IList< QueryReference > list)
override SqlExpression VisitConstant(SqlConstantExpression constant)
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
override SqlExpression VisitFunctionCall(SqlFunctionCallExpression expression)
Visits the expression that calls the function defined.
DataObject Value
Gets the constant value of the expression.
static IList< QueryReference > DiscoverQueryReferences(this SqlExpression expression, ref int level)
IEnumerable< ObjectName > Discover(SqlExpression expression)
override SqlExpression VisitConstant(SqlConstantExpression constant)
static SqlBinaryExpression Add(SqlExpression left, SqlExpression right)
static bool HasSubQuery(this SqlExpression expression)
An expression that holds a constant value.
Defines the base class for instances that represent SQL expression tree nodes.
An object that provides methods for accessing a finite collection of SQL expressions.
Definition: SqlArray.cs:28
override SqlExpression VisitReference(SqlReferenceExpression reference)