DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SelectStatementTests.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 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 using System;
17 using System.Collections.Generic;
18 using System.Linq;
19 
20 using Deveel.Data;
22 using Deveel.Data.Sql.Objects;
23 using Deveel.Data.Sql.Tables;
24 using Deveel.Data.Types;
25 
26 using NUnit.Framework;
27 
28 namespace Deveel.Data.Sql.Statements {
29  [TestFixture]
31  protected override ISession CreateAdminSession(IDatabase database) {
32  using (var session = base.CreateAdminSession(database)) {
33  using (var query = session.CreateQuery()) {
34  CreateTestTable(query);
35  AddTestData(query);
36 
37  query.Commit();
38  }
39  }
40 
41  return base.CreateAdminSession(database);
42  }
43 
44  private void CreateTestTable(IQuery context) {
45  var tableInfo = new TableInfo(ObjectName.Parse("APP.test_table"));
46  var idColumn = tableInfo.AddColumn("id", PrimitiveTypes.Integer());
47  idColumn.DefaultExpression = SqlExpression.FunctionCall("UNIQUEKEY",
48  new SqlExpression[] { SqlExpression.Constant(tableInfo.TableName.FullName) });
49  tableInfo.AddColumn("first_name", PrimitiveTypes.String());
50  tableInfo.AddColumn("last_name", PrimitiveTypes.String());
51  tableInfo.AddColumn("birth_date", PrimitiveTypes.DateTime());
52  tableInfo.AddColumn("active", PrimitiveTypes.Boolean());
53 
54  context.CreateTable(tableInfo);
55  context.AddPrimaryKey(tableInfo.TableName, "id", "PK_TEST_TABLE");
56  }
57 
58  private void AddTestData(IQuery context) {
59  var table = context.GetMutableTable(ObjectName.Parse("APP.test_table"));
60  var row = table.NewRow();
61 
62  // row.SetValue("id", DataObject.Integer(0));
63  row.SetDefault(0, context);
64  row.SetValue("first_name", DataObject.String("John"));
65  row.SetValue("last_name", DataObject.String("Doe"));
66  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1977, 01, 01)));
67  row.SetValue("active", DataObject.Boolean(false));
68  table.AddRow(row);
69 
70  row = table.NewRow();
71 
72  // row.SetValue("id", DataObject.Integer(1));
73  row.SetDefault(0, context);
74  row.SetValue("first_name", DataObject.String("Jane"));
75  row.SetValue("last_name", DataObject.String("Doe"));
76  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1978, 11, 01)));
77  row.SetValue("active", DataObject.Boolean(true));
78  table.AddRow(row);
79 
80  row = table.NewRow();
81 
82  // row.SetValue("id", DataObject.Integer(2));
83  row.SetDefault(0, context);
84  row.SetValue("first_name", DataObject.String("Roger"));
85  row.SetValue("last_name", DataObject.String("Rabbit"));
86  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1985, 05, 05)));
87  row.SetValue("active", DataObject.Boolean(true));
88  table.AddRow(row);
89 
90  context.Commit();
91  }
92 
93  [Test]
94  public void ParseWithFromClause() {
95  const string sql = "SELECT col1 AS a FROM table";
96 
97  IEnumerable<SqlStatement> statements = null;
98  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
99  Assert.IsNotNull(statements);
100 
101  var statement = statements.FirstOrDefault();
102 
103  Assert.IsNotNull(statement);
104  Assert.IsInstanceOf<SelectStatement>(statement);
105 
106  var selectStatement = (SelectStatement) statement;
107  Assert.IsNotNull(selectStatement.QueryExpression);
108  Assert.IsNull(selectStatement.OrderBy);
109  }
110 
111  [Test]
112  public void ParseWithVariable() {
113  const string sql = "SELECT :a";
114 
115  IEnumerable<SqlStatement> statements = null;
116  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
117  Assert.IsNotNull(statements);
118 
119  var statement = statements.FirstOrDefault();
120 
121  Assert.IsNotNull(statement);
122  Assert.IsInstanceOf<SelectStatement>(statement);
123 
124  var selectStatement = (SelectStatement)statement;
125  Assert.IsNotNull(selectStatement.QueryExpression);
126  Assert.IsNull(selectStatement.OrderBy);
127 
128  Assert.IsNotNull(selectStatement.QueryExpression.SelectColumns);
129 
130  var selectCols = selectStatement.QueryExpression.SelectColumns.ToList();
131  Assert.AreEqual(1, selectCols.Count);
132  Assert.IsInstanceOf<SqlVariableReferenceExpression>(selectCols[0].Expression);
133  }
134 
135  [Test]
136  public void ParseWithFunction() {
137  const string sql = "SELECT user()";
138 
139  IEnumerable<SqlStatement> statements = null;
140  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
141  Assert.IsNotNull(statements);
142 
143  var statement = statements.FirstOrDefault();
144 
145  Assert.IsNotNull(statement);
146  Assert.IsInstanceOf<SelectStatement>(statement);
147 
148  var selectStatement = (SelectStatement)statement;
149  Assert.IsNotNull(selectStatement.QueryExpression);
150  Assert.IsNull(selectStatement.OrderBy);
151 
152  Assert.IsNotNull(selectStatement.QueryExpression.SelectColumns);
153 
154  var selectCols = selectStatement.QueryExpression.SelectColumns.ToList();
155  Assert.AreEqual(1, selectCols.Count);
156  Assert.IsInstanceOf<SqlFunctionCallExpression>(selectCols[0].Expression);
157  }
158 
159  [Test]
160  public void ParseWithOrderByClause() {
161  const string sql = "SELECT col1 AS a FROM table ORDER BY a ASC";
162 
163  IEnumerable<SqlStatement> statements = null;
164  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
165  Assert.IsNotNull(statements);
166 
167  var statement = statements.FirstOrDefault();
168 
169  Assert.IsNotNull(statement);
170  Assert.IsInstanceOf<SelectStatement>(statement);
171 
172  var selectStatement = (SelectStatement)statement;
173  Assert.IsNotNull(selectStatement.QueryExpression);
174  Assert.IsNotNull(selectStatement.OrderBy);
175  }
176 
177  [Test]
178  public void ExecuteSimpleSelect() {
179  const string sql = "SELECT * FROM test_table";
180 
181  IEnumerable<SqlStatement> statements = null;
182  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
183  Assert.IsNotNull(statements);
184 
185  var statement = statements.FirstOrDefault();
186 
187  Assert.IsNotNull(statement);
188  Assert.IsInstanceOf<SelectStatement>(statement);
189 
190  ITable result = null;
191  Assert.DoesNotThrow(() => result = statement.Execute(Query));
192  Assert.IsNotNull(result);
193  Assert.AreEqual(3, result.RowCount);
194  }
195 
196  [Test]
198  const string sql = "SELECT * FROM test_table ORDER BY birth_date DESC";
199 
200  IEnumerable<SqlStatement> statements = null;
201  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
202  Assert.IsNotNull(statements);
203 
204  var statement = statements.FirstOrDefault();
205 
206  Assert.IsNotNull(statement);
207  Assert.IsInstanceOf<SelectStatement>(statement);
208 
209  ITable result = null;
210  Assert.DoesNotThrow(() => result = statement.Execute(Query));
211  Assert.IsNotNull(result);
212  Assert.AreEqual(3, result.RowCount);
213 
214  var firstName = result.GetValue(0, 1);
215 
216  Assert.AreEqual("Roger", firstName.Value.ToString());
217  }
218 
219  [Test]
221  const string sql = "SELECT * FROM test_table t0 WHERE (t0.id = 1 AND t0.id <> 0)";
222 
223  IEnumerable<SqlStatement> statements = null;
224  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
225  Assert.IsNotNull(statements);
226 
227  var statement = statements.FirstOrDefault();
228 
229  Assert.IsNotNull(statement);
230  Assert.IsInstanceOf<SelectStatement>(statement);
231 
232  ITable result = null;
233  Assert.DoesNotThrow(() => result = statement.Execute(Query));
234  Assert.IsNotNull(result);
235  Assert.AreEqual(1, result.RowCount);
236  }
237 
238  [Test]
239  public void SelectFromAliased() {
240  const string sql = "SELECT * FROM test_table t0 WHERE t0.id = 1";
241 
242  IEnumerable<SqlStatement> statements = null;
243  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
244  Assert.IsNotNull(statements);
245 
246  var statement = statements.FirstOrDefault();
247 
248  Assert.IsNotNull(statement);
249  Assert.IsInstanceOf<SelectStatement>(statement);
250 
251  ITable result = null;
252  Assert.DoesNotThrow(() => result = statement.Execute(Query));
253  Assert.IsNotNull(result);
254  Assert.AreEqual(1, result.RowCount);
255  }
256  }
257 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static DataObject Date(DateTimeOffset value)
Definition: DataObject.cs:600
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
static BooleanType Boolean()
override ISession CreateAdminSession(IDatabase database)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
static DataObject String(string s)
Definition: DataObject.cs:592
static DataObject Boolean(SqlBoolean value)
Definition: DataObject.cs:544
The representation of a single database in the system.
Definition: IDatabase.cs:40
DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
Represents the foundation class of SQL statements to be executed.
Definition: SqlStatement.cs:32
void AddColumn(ColumnInfo column)
Adds a new column to the table at the last position of the columns list in the table metadata...
Definition: TableInfo.cs:230
An isolated session to a given database for a given user, encapsulating the transaction for operation...
Definition: ISession.cs:30
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
static SqlFunctionCallExpression FunctionCall(ObjectName functionName)
static IEnumerable< SqlStatement > Parse(string sqlSource)
Parses a given string into one of more statements.
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41