DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
CursorCompileTests.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 
5 
6 using NUnit.Framework;
7 
8 namespace Deveel.Data.Sql.Compile {
9  [TestFixture]
11  [Test]
13  const string sql = "DECLARE CURSOR test_cursor (a INT, b VARCHAR) IS SELECT * FROM test_table WHERE test_table.a = a";
14  var result = Compile(sql);
15 
16  Assert.IsNotNull(result);
17  Assert.IsFalse(result.HasErrors);
18 
19  Assert.IsNotEmpty(result.Statements);
20  Assert.AreEqual(1, result.Statements.Count);
21 
22  var statement = result.Statements.FirstOrDefault();
23 
24  Assert.IsNotNull(statement);
25  Assert.IsInstanceOf<DeclareCursorStatement>(statement);
26 
27  var cursorStatement = (DeclareCursorStatement) statement;
28 
29  Assert.AreEqual("test_cursor", cursorStatement.CursorName);
30  Assert.IsNotEmpty(cursorStatement.Parameters);
31  Assert.IsNotNull(cursorStatement.QueryExpression);
32  }
33 
34  [Test]
35  public void ImplicitDeclareCursor() {
36  const string sql = "CURSOR test_cursor (a INT, b VARCHAR) IS SELECT * FROM test_table WHERE test_table.a = a";
37 
38  var result = Compile(sql);
39 
40  Assert.IsNotNull(result);
41  Assert.IsFalse(result.HasErrors);
42 
43  Assert.IsNotEmpty(result.Statements);
44  Assert.AreEqual(1, result.Statements.Count);
45 
46  var statement = result.Statements.FirstOrDefault();
47 
48  Assert.IsNotNull(statement);
49  Assert.IsInstanceOf<DeclareCursorStatement>(statement);
50 
51  var cursorStatement = (DeclareCursorStatement) statement;
52 
53  Assert.AreEqual("test_cursor", cursorStatement.CursorName);
54  Assert.IsNotEmpty(cursorStatement.Parameters);
55  Assert.IsNotNull(cursorStatement.QueryExpression);
56  }
57 
58  [Test]
60  const string sql = "DECLARE CURSOR test_cursor IS SELECT * FROM test_table WHERE test_table.a > 0";
61 
62  var result = Compile(sql);
63 
64  Assert.IsNotNull(result);
65  Assert.IsFalse(result.HasErrors);
66 
67  Assert.IsNotEmpty(result.Statements);
68  Assert.AreEqual(1, result.Statements.Count);
69 
70  var statement = result.Statements.FirstOrDefault();
71 
72  Assert.IsNotNull(statement);
73  Assert.IsInstanceOf<DeclareCursorStatement>(statement);
74 
75  var cursorStatement = (DeclareCursorStatement) statement;
76 
77  Assert.AreEqual("test_cursor", cursorStatement.CursorName);
78  Assert.IsEmpty(cursorStatement.Parameters);
79  Assert.IsNotNull(cursorStatement.QueryExpression);
80  }
81 
82  [Test]
83  public void OpenCursorWithArguments() {
84  const string sql = "OPEN test_cursor (22, b)";
85 
86  var result = Compile(sql);
87 
88  Assert.IsNotNull(result);
89  Assert.IsFalse(result.HasErrors);
90 
91  Assert.IsNotEmpty(result.Statements);
92  Assert.AreEqual(1, result.Statements.Count);
93 
94  var statement = result.Statements.FirstOrDefault();
95 
96  Assert.IsNotNull(statement);
97  Assert.IsInstanceOf<OpenStatement>(statement);
98 
99  var cursorStatement = (OpenStatement) statement;
100  Assert.AreEqual("test_cursor", cursorStatement.CursorName);
101  Assert.IsNotEmpty(cursorStatement.Arguments);
102  }
103 
104  [Test]
105  public void OpenCursorSimple() {
106  const string sql = "OPEN test_cursor";
107 
108  var result = Compile(sql);
109 
110  Assert.IsNotNull(result);
111  Assert.IsFalse(result.HasErrors);
112 
113  Assert.IsNotEmpty(result.Statements);
114  Assert.AreEqual(1, result.Statements.Count);
115 
116  var statement = result.Statements.FirstOrDefault();
117 
118  Assert.IsNotNull(statement);
119  Assert.IsInstanceOf<OpenStatement>(statement);
120 
121  var cursorStatement = (OpenStatement) statement;
122  Assert.AreEqual("test_cursor", cursorStatement.CursorName);
123  Assert.IsEmpty(cursorStatement.Arguments);
124  }
125 
126  [Test]
127  public void CloseCursor() {
128  const string sql = "CLOSE test_cursor";
129 
130  var result = Compile(sql);
131 
132  Assert.IsNotNull(result);
133  Assert.IsFalse(result.HasErrors);
134 
135  Assert.IsNotEmpty(result.Statements);
136  Assert.AreEqual(1, result.Statements.Count);
137 
138  var statement = result.Statements.FirstOrDefault();
139 
140  Assert.IsNotNull(statement);
141  Assert.IsInstanceOf<CloseStatement>(statement);
142 
143  var cursorStatement = (CloseStatement) statement;
144  Assert.AreEqual("test_cursor", cursorStatement.CursorName);
145  }
146 
147  [Test]
148  public void FetchNextFromCursorInto() {
149  const string sql = "FETCH NEXT FROM test_cursor INTO test_table";
150 
151  var result = Compile(sql);
152 
153  Assert.IsNotNull(result);
154  Assert.IsFalse(result.HasErrors);
155 
156  Assert.IsNotEmpty(result.Statements);
157  Assert.AreEqual(1, result.Statements.Count);
158 
159  var statement = result.Statements.FirstOrDefault();
160 
161  Assert.IsNotNull(statement);
162  Assert.IsInstanceOf<FetchStatement>(statement);
163 
164  var cursorStatement = (FetchStatement) statement;
165  Assert.AreEqual("test_cursor", cursorStatement.CursorName);
166  }
167 
168  [Test]
169  public void FetchNextImplicitCursor() {
170  const string sql = "FETCH NEXT";
171 
172  var result = Compile(sql);
173 
174  Assert.IsNotNull(result);
175  Assert.IsFalse(result.HasErrors);
176 
177  Assert.IsNotEmpty(result.Statements);
178  Assert.AreEqual(1, result.Statements.Count);
179 
180  var statement = result.Statements.FirstOrDefault();
181 
182  Assert.IsNotNull(statement);
183  Assert.IsInstanceOf<FetchStatement>(statement);
184 
185  var cursorStatement = (FetchStatement) statement;
186  Assert.IsNullOrEmpty(cursorStatement.CursorName);
187  }
188  }
189 }