DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlStatement.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 using System.Linq;
20 
22 using Deveel.Data.Sql.Compile;
23 using Deveel.Data.Sql.Parser;
25 using Deveel.Data.Sql.Tables;
26 
27 namespace Deveel.Data.Sql.Statements {
31  [Serializable]
32  public abstract class SqlStatement : IStatement, ISerializable {
33  protected SqlStatement() {
34 
35  }
36 
37  protected SqlStatement(ObjectData data) {
38  SourceQuery = data.GetValue<SqlQuery>("SourceQuery");
39  IsFromQuery = data.GetBoolean("IsFromQuery");
40  }
41 
49  public SqlQuery SourceQuery { get; set; }
50 
60  public bool IsFromQuery { get; private set; }
61 
63  ExecuteStatement(context);
64  }
65 
66  protected virtual void ExecuteStatement(ExecutionContext context) {
67  throw new NotSupportedException(String.Format("The statement '{0}' does not support execution", GetType().Name));
68  }
69 
70  internal void SetSource(SqlQuery query) {
71  SourceQuery = query;
72  IsFromQuery = true;
73  }
74 
76  data.SetValue("SourceQuery", SourceQuery);
77  data.SetValue("IsFromQuery", IsFromQuery);
78 
79  GetData(data);
80  }
81 
82  protected virtual void GetData(SerializeData data) {
83 
84  }
85 
97  public ITable Execute(IRequest context) {
98  return PrepareAndExecute(null, context);
99  }
100 
102  IExecutable prepared;
103 
104  try {
105  prepared = this.Prepare(preparer, context);
106  } catch (Exception ex) {
107  throw new InvalidOperationException("Unable to prepare the statement for execution.", ex);
108  }
109 
110  if (prepared == null)
111  throw new InvalidOperationException();
112 
113  var exeContext = new ExecutionContext(context);
114  prepared.Execute(exeContext);
115  if (!exeContext.HasResult)
116  return FunctionTable.ResultTable(context, 0);
117 
118  return exeContext.Result;
119  }
120 
133  public static IEnumerable<SqlStatement> Parse(string sqlSource) {
134  return Parse(null, sqlSource);
135  }
136 
150  public static IEnumerable<SqlStatement> Parse(IContext context, string sqlSource) {
151  return Parse(context, new SqlQuery(sqlSource));
152  }
153 
154  private static readonly ISqlCompiler DefaultCompiler = new SqlDefaultCompiler();
155 
156 
157 
158  public static IEnumerable<SqlStatement> Parse(IContext context, SqlQuery query) {
159  if (query == null)
160  throw new ArgumentNullException("query");
161 
162  var compiler = DefaultCompiler;
163 
164  if (context != null) {
165  compiler = context.ResolveService<ISqlCompiler>();
166  }
167 
168  try {
169  var compileContext = new SqlCompileContext(context, query.Text);
170  var result = compiler.Compile(compileContext);
171  if (result.HasErrors)
172  throw new SqlParseException();
173 
174  var statements = result.Statements.Cast<SqlStatement>();
175 
176  foreach (var statement in statements) {
177  if (statement != null)
178  statement.SetSource(query);
179  }
180 
181  return statements;
182  } catch (SqlParseException) {
183  throw;
184  } catch (Exception ex) {
185  throw new SqlParseException("The input string cannot be parsed into SQL Statements", ex);
186  }
187  }
188  }
189 }
void GetData(SerializeData data)
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
static ITable ResultTable(IRequest context, SqlExpression expression)
static IEnumerable< SqlStatement > Parse(IContext context, string sqlSource)
Parses a given string into one of more statements.
void SetValue(string key, Type type, object value)
ITable PrepareAndExecute(IExpressionPreparer preparer, IRequest context)
virtual void GetData(SerializeData data)
Definition: SqlStatement.cs:82
Represents the foundation class of SQL statements to be executed.
Definition: SqlStatement.cs:32
ITable Execute(IRequest context)
Prepares and evaluates this statement into a tabular result.
Definition: SqlStatement.cs:97
void Execute(ExecutionContext context)
An interface used to prepare a SqlExpression object.
static IEnumerable< SqlStatement > Parse(IContext context, SqlQuery query)
virtual void ExecuteStatement(ExecutionContext context)
Definition: SqlStatement.cs:66
static IEnumerable< SqlStatement > Parse(string sqlSource)
Parses a given string into one of more statements.
An error that occurs when compiling a input string into a SQL object.