DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DeclareCursorStatement.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.IO;
20 using System.Linq;
21 
22 using Deveel.Data;
24 using Deveel.Data.Sql.Cursors;
26 using Deveel.Data.Sql.Query;
27 using Deveel.Data.Sql.Tables;
28 
29 namespace Deveel.Data.Sql.Statements {
30  [Serializable]
31  public sealed class DeclareCursorStatement : SqlStatement {
32  public DeclareCursorStatement(string cursorName, SqlQueryExpression queryExpression)
33  : this(cursorName, null, queryExpression) {
34  }
35 
36  public DeclareCursorStatement(string cursorName, IEnumerable<CursorParameter> parameters, SqlQueryExpression queryExpression)
37  : this(cursorName, parameters, CursorFlags.Insensitive, queryExpression) {
38  }
39 
40  public DeclareCursorStatement(string cursorName, CursorFlags flags, SqlQueryExpression queryExpression)
41  : this(cursorName, null, flags, queryExpression) {
42  }
43 
44  public DeclareCursorStatement(string cursorName, IEnumerable<CursorParameter> parameters, CursorFlags flags, SqlQueryExpression queryExpression) {
45  if (queryExpression == null)
46  throw new ArgumentNullException("queryExpression");
47  if (String.IsNullOrEmpty(cursorName))
48  throw new ArgumentNullException("cursorName");
49 
50  CursorName = cursorName;
51  Parameters = parameters;
52  Flags = flags;
53  QueryExpression = queryExpression;
54  }
55 
57  CursorName = data.GetString("CursorName");
58  QueryExpression = data.GetValue<SqlQueryExpression>("QueryExpression");
59  Flags = (CursorFlags) data.GetInt32("Flags");
60 
61  if (data.HasValue("Parameters")) {
62  var parameters = data.GetValue<CursorParameter[]>("Parameters");
63  Parameters = new List<CursorParameter>(parameters);
64  }
65  }
66 
67  public string CursorName { get; private set; }
68 
69  public SqlQueryExpression QueryExpression { get; private set; }
70 
71  public CursorFlags Flags { get; set; }
72 
73  public IEnumerable<CursorParameter> Parameters { get; set; }
74 
75  protected override void GetData(SerializeData data) {
76  data.SetValue("CursorName", CursorName);
77  data.SetValue("QueryExpression", QueryExpression);
78  data.SetValue("Flags", (int)Flags);
79 
80  if (Parameters != null) {
81  var parameters = Parameters.ToArray();
82  data.SetValue("Parameters", parameters);
83  }
84  }
85 
86  protected override void ExecuteStatement(ExecutionContext context) {
87  var cursorInfo = new CursorInfo(CursorName, Flags, QueryExpression);
88  if (Parameters != null) {
89  foreach (var parameter in Parameters) {
90  cursorInfo.Parameters.Add(parameter);
91  }
92  }
93 
94  context.Request.Query.DeclareCursor(cursorInfo);
95  }
96 
97  #region Serializer
98 
99  //internal class Serializer : ObjectBinarySerializer<DeclareCursorStatement> {
100  // public override void Serialize(DeclareCursorStatement obj, BinaryWriter writer) {
101  // writer.Write(obj.CursorName);
102  // writer.Write((byte)obj.Flags);
103 
104  // if (obj.Parameters != null) {
105  // var pars = obj.Parameters.ToArray();
106  // var parLength = pars.Length;
107  // writer.Write(parLength);
108 
109  // for (int i = 0; i < parLength; i++) {
110  // CursorParameter.Serialize(pars[i], writer);
111  // }
112  // } else {
113  // writer.Write(0);
114  // }
115 
116  // SqlExpression.Serialize(obj.QueryExpression, writer);
117  // }
118 
119  // public override DeclareCursorStatement Deserialize(BinaryReader reader) {
120  // var cursorName = reader.ReadString();
121  // var flags = (CursorFlags) reader.ReadByte();
122 
123  // var pars = new List<CursorParameter>();
124  // var parLength = reader.ReadInt32();
125  // for (int i = 0; i < parLength; i++) {
126  // var param = CursorParameter.Deserialize(reader);
127  // pars.Add(param);
128  // }
129 
130  // var queryExpression = SqlExpression.Deserialize(reader) as SqlQueryExpression;
131 
132  // return new DeclareCursorStatement(cursorName, pars.ToArray(), flags, queryExpression);
133  // }
134  //}
135 
136  #endregion
137  }
138 }
DeclareCursorStatement(string cursorName, IEnumerable< CursorParameter > parameters, CursorFlags flags, SqlQueryExpression queryExpression)
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
DeclareCursorStatement(string cursorName, CursorFlags flags, SqlQueryExpression queryExpression)
DeclareCursorStatement(string cursorName, SqlQueryExpression queryExpression)
DeclareCursorStatement(string cursorName, IEnumerable< CursorParameter > parameters, SqlQueryExpression queryExpression)