DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
OpenStatement.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 using Deveel.Data.Sql.Cursors;
23 
24 namespace Deveel.Data.Sql.Statements {
25  [Serializable]
27  public OpenStatement(string cursorName)
28  : this(cursorName, new SqlExpression[] {}) {
29  }
30 
31  public OpenStatement(string cursorName, SqlExpression[] arguments) {
32  CursorName = cursorName;
33  Arguments = arguments;
34  }
35 
36  private OpenStatement(ObjectData data) {
37  CursorName = data.GetString("CursorName");
38  Arguments = data.GetValue<SqlExpression[]>("Arguments");
39  }
40 
41  public string CursorName { get; private set; }
42 
43  public SqlExpression[] Arguments { get; set; }
44 
46  SqlExpression[] args = null;
47  if (Arguments != null) {
48  args = (SqlExpression[])Arguments.Clone();
49 
50  for (int i = 0; i < args.Length; i++) {
51  args[i] = args[i].Prepare(preparer);
52  }
53  }
54 
55  return new OpenStatement(CursorName, args);
56  }
57 
59  SqlExpression[] args = Arguments;
60 
61  var cursor = context.Query.FindCursor(CursorName);
62  if (cursor == null)
63  throw new StatementPrepareException(String.Format("Cursor '{0}' was not found.", CursorName));
64 
65  if (args != null) {
66  var orderedParams = cursor.CursorInfo.Parameters.OrderBy(x => x.Offset).ToArray();
67 
68  if (args.Length != orderedParams.Length)
69  throw new StatementPrepareException(String.Format("Invalid number of arguments for cursor '{0}' OPEN statement.", CursorName));
70 
71  // TODO: Cast to the parameter type here?
72  }
73 
74 
75  return new OpenStatement(CursorName, args);
76  }
77 
78  protected override void GetData(SerializeData data) {
79  data.SetValue("CursorName", CursorName);
80  data.SetValue("Arguments", Arguments);
81  }
82 
83  protected override void ExecuteStatement(ExecutionContext context) {
84  var cursor = context.Request.Query.FindCursor(CursorName);
85  if (cursor == null)
86  throw new StatementException(String.Format("Cursor '{0}' was not found in the context.", CursorName));
87 
88  cursor.Open(context.Request, Arguments);
89  }
90  }
91 }
An exception that happens during the SqlStatement.Prepare(IExpressionPreparer, IQueryContext).
OpenStatement(string cursorName, SqlExpression[] arguments)
override void ExecuteStatement(ExecutionContext context)
void SetValue(string key, Type type, object value)
Represents the foundation class of SQL statements to be executed.
Definition: SqlStatement.cs:32
override void GetData(SerializeData data)
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