DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
StatementExecutor.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 using Deveel.Data;
21 using Deveel.Data.Diagnostics;
22 using Deveel.Data.Sql;
24 using Deveel.Data.Sql.Parser;
25 using Deveel.Data.Sql.Tables;
26 
27 namespace Deveel.Data.Sql.Statements {
32  public static class StatementExecutor {
49  public static ITable[] Execute(IRequest query, SqlQuery sqlQuery) {
50  if (query == null)
51  throw new ArgumentNullException("query");
52 
53  var sqlSouce = sqlQuery.Text;
54 
55  // TODO: find it from the cache...
56 
57  var statements = SqlStatement.Parse(sqlSouce);
58 
59  // TODO: set it in cache ...
60 
61  var preparer = new QueryPreparer(sqlQuery);
62 
63  bool statementSeen = false;
64 
65  var results = new List<ITable>();
66  foreach (var statement in statements) {
67  // TODO: query.RegisterQuery(statement);
68 
69  // TODO: Invoke diagnostics for the preparation...
70 
71  var prepared = statement.Prepare(preparer, query);
72 
73  ITable result;
74 
75  try {
76  var exeContext = new ExecutionContext(query);
77  prepared.Execute(exeContext);
78  if (exeContext.HasResult) {
79  result = exeContext.Result;
80  } else {
81  result = FunctionTable.ResultTable(query, 0);
82  }
83  } catch(StatementException ex) {
84  // TODO: query.RegisterError(ex);
85  throw;
86  } catch (Exception ex) {
87  var sex = new StatementException("An unhanded error occurred while executing the statement.", ex);
88  // TODO: query.RegisterError(sex);
89  throw sex;
90  } finally {
91  statementSeen = true;
92  }
93 
94  results.Add(result);
95  }
96 
97  if (!statementSeen)
98  throw new SqlParseException("The input query was not parsed in any statements that could be executed.");
99 
100  return results.ToArray();
101  }
102 
103  #region QueryPreparer
104 
106  private readonly SqlQuery query;
107 
108  public QueryPreparer(SqlQuery query) {
109  this.query = query;
110  }
111 
112  public bool CanPrepare(SqlExpression expression) {
113  return expression is SqlVariableReferenceExpression;
114  }
115 
116  public SqlExpression Prepare(SqlExpression expression) {
117  var varRef = (SqlVariableReferenceExpression) expression;
118  var varName = varRef.VariableName;
119 
120  var parameter = query.Parameters.FindParameter(varName);
121  if (parameter == null)
122  return expression;
123 
124  var value = parameter.SqlType.CreateFrom(parameter.Value);
125  var obj = new DataObject(parameter.SqlType, value);
126 
127  return SqlExpression.Constant(obj);
128  }
129  }
130 
131  #endregion
132  }
133 }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
static ITable[] Execute(IRequest query, SqlQuery sqlQuery)
This method transforms the input SQL query into a set of statements, prepares and executes them again...
static ITable ResultTable(IRequest context, SqlExpression expression)
Represents the foundation class of SQL statements to be executed.
Definition: SqlStatement.cs:32
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
An interface used to prepare a SqlExpression object.
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
static IEnumerable< SqlStatement > Parse(string sqlSource)
Parses a given string into one of more statements.
SqlExpression Prepare(SqlExpression expression)
Returns the new translated object to be mutated from the given expression.
An error that occurs when compiling a input string into a SQL object.
bool CanPrepare(SqlExpression expression)
Verifies whether the instance of the interface can prepare the given expression.
This class is used to transform an input query to a set of statements and execute them within a given...