DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
PreparerVisitor.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 
20 namespace Deveel.Data.Sql.Expressions {
22  private readonly IExpressionPreparer preparer;
23 
25  this.preparer = preparer;
26  }
27 
28  public override SqlExpression Visit(SqlExpression expression) {
29  if (preparer.CanPrepare(expression))
30  expression = preparer.Prepare(expression);
31 
32  return base.Visit(expression);
33  }
34 
35  public override SqlExpression VisitAssign(SqlAssignExpression assign) {
36  var reference = assign.ReferenceExpression;
37  if (reference != null)
38  reference = reference.Prepare(preparer);
39 
40  var valueExpression = assign.ValueExpression;
41  if (valueExpression != null)
42  valueExpression = valueExpression.Prepare(preparer);
43 
44  return SqlExpression.Assign(reference, valueExpression);
45  }
46 
47  private static IEnumerable<T> Prepare<T>(IEnumerable<T> list, IExpressionPreparer preparer)
48  where T : IPreparable {
49  var newList = new List<T>();
50  foreach (var item in list) {
51  var newItem = item;
52  if (newItem != null) {
53  newItem = (T)newItem.Prepare(preparer);
54  }
55 
56  newList.Add(newItem);
57  }
58 
59  return newList.ToArray();
60  }
61 
62  public override SqlExpression VisitQuery(SqlQueryExpression query) {
63  var selectColumns = Prepare(query.SelectColumns, preparer);
64  var newExpression = new SqlQueryExpression(selectColumns);
65 
66  var where = query.WhereExpression;
67  if (where != null)
68  where = where.Prepare(preparer);
69 
70  newExpression.WhereExpression = where;
71 
72  var having = query.HavingExpression;
73  if (having != null)
74  having = having.Prepare(preparer);
75 
76  newExpression.HavingExpression = having;
77 
78  var from = query.FromClause;
79  if (from != null)
80  from = (FromClause) ((IPreparable)from).Prepare(preparer);
81 
82  query.FromClause = from;
83 
84  var nextComposite = query.NextComposite;
85  if (nextComposite != null)
86  nextComposite = (SqlQueryExpression) nextComposite.Prepare(preparer);
87 
88  query.NextComposite = nextComposite;
89 
90  return newExpression;
91  }
92  }
93 }
override SqlExpression VisitQuery(SqlQueryExpression query)
override SqlExpression VisitAssign(SqlAssignExpression assign)
PreparerVisitor(IExpressionPreparer preparer)
static SqlAssignExpression Assign(SqlExpression reference, SqlExpression valueExpression)
override SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
A container for the FROM clause of a select statement.
Definition: FromClause.cs:32
readonly IExpressionPreparer preparer
An interface used to prepare a SqlExpression object.
virtual SqlExpression Prepare(IExpressionPreparer preparer)
object Prepare(IExpressionPreparer preparer)
Converts the underlying value of this instance into an object that can be evaluated by an expression...
Defines the base class for instances that represent SQL expression tree nodes.
A contract for objects that participate to a SqlExpression.Prepare phase of an expression evaluation...
Definition: IPreparable.cs:30