DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlQueryExpressionTests.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.Linq;
18 
19 using Deveel.Data;
20 using Deveel.Data.Sql.Objects;
21 using Deveel.Data.Sql.Tables;
22 using Deveel.Data.Types;
23 
24 using NUnit.Framework;
25 
26 namespace Deveel.Data.Sql.Expressions {
27  [TestFixture]
28  public sealed class SqlQueryExpressionTests : ContextBasedTest {
29  protected override void OnSetUp(string testName) {
30  CreateTestTable();
31  AddTestData();
32  }
33 
34  private void CreateTestTable() {
35  var tableInfo = new TableInfo(ObjectName.Parse("APP.test_table"));
36  var idColumn = tableInfo.AddColumn("id", PrimitiveTypes.Integer());
37  idColumn.DefaultExpression = SqlExpression.FunctionCall("UNIQUE_KEY",
38  new SqlExpression[] {SqlExpression.Reference(tableInfo.TableName)});
39  tableInfo.AddColumn("first_name", PrimitiveTypes.String());
40  tableInfo.AddColumn("last_name", PrimitiveTypes.String());
41  tableInfo.AddColumn("birth_date", PrimitiveTypes.DateTime());
42  tableInfo.AddColumn("active", PrimitiveTypes.Boolean());
43 
44  Query.CreateTable(tableInfo);
45  Query.AddPrimaryKey(tableInfo.TableName, "id", "PK_TEST_TABLE");
46  }
47 
48  private void AddTestData() {
49  var table = Query.GetMutableTable(ObjectName.Parse("APP.test_table"));
50  var row = table.NewRow();
51  row.SetValue("first_name", DataObject.String("John"));
52  row.SetValue("last_name", DataObject.String("Doe"));
53  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1977, 01, 01)));
54  row.SetValue("active", DataObject.Boolean(false));
55  table.AddRow(row);
56 
57  row = table.NewRow();
58  row.SetValue("first_name", DataObject.String("Jane"));
59  row.SetValue("last_name", DataObject.String("Doe"));
60  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1978, 11, 01)));
61  row.SetValue("active", DataObject.Boolean(true));
62  table.AddRow(row);
63 
64  row = table.NewRow();
65  row.SetValue("first_name", DataObject.String("Roger"));
66  row.SetValue("last_name", DataObject.String("Rabbit"));
67  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1985, 05, 05)));
68  row.SetValue("active", DataObject.Boolean(true));
69  table.AddRow(row);
70  }
71 
72  [Test]
73  [Category("System")]
74  public void ExecuteSelectAll() {
75  var expression =
76  new SqlQueryExpression(new[] {new SelectColumn(SqlExpression.Reference(new ObjectName("first_name")))});
77  expression.FromClause.AddTable("test_table");
78 
79  DataObject result = null;
80  Assert.DoesNotThrow(() => result = expression.EvaluateToConstant(Query, null));
81  Assert.IsNotNull(result);
82  Assert.IsInstanceOf<QueryType>(result.Type);
83  Assert.IsNotNull(result.Value);
84  Assert.IsInstanceOf<SqlQueryObject>(result.Value);
85 
86  ITable queryResult = null;
87 
88  Assert.DoesNotThrow(() => queryResult = ((SqlQueryObject) result.Value).QueryPlan.Evaluate(Query));
89  Assert.IsNotNull(queryResult);
90  Assert.AreEqual(3, queryResult.RowCount);
91  }
92 
93  [Test]
94  [Category("SQL Parse")]
95  public void ParseSelectWithFromClause() {
96  const string sql = "SELECT col1 AS a FROM table";
97 
98  var expression = SqlExpression.Parse(sql);
99  Assert.IsNotNull(expression);
100  Assert.IsInstanceOf<SqlQueryExpression>(expression);
101 
102  var queryExpression = (SqlQueryExpression) expression;
103  Assert.IsNotEmpty(queryExpression.SelectColumns);
104  Assert.IsInstanceOf<SqlReferenceExpression>(queryExpression.SelectColumns.First().Expression);
105  Assert.AreEqual("a", queryExpression.SelectColumns.First().Alias);
106  Assert.IsNotNull(queryExpression.FromClause);
107  Assert.AreEqual(1, queryExpression.FromClause.AllTables.Count());
108  Assert.AreEqual("table", queryExpression.FromClause.AllTables.First().Name);
109  }
110 
111  [Test]
112  [Category("SQL Parse")]
114  const string sql = "SELECT a.col1, b.col2 FROM table1 a, table2 b";
115 
116  SqlExpression expression = null;
117  Assert.DoesNotThrow(() => expression = SqlExpression.Parse(sql));
118  Assert.IsNotNull(expression);
119  Assert.IsInstanceOf<SqlQueryExpression>(expression);
120 
121  var queryExpression = (SqlQueryExpression) expression;
122  Assert.IsNotEmpty(queryExpression.SelectColumns);
123  Assert.IsInstanceOf<SqlReferenceExpression>(queryExpression.SelectColumns.First().Expression);
124  Assert.IsInstanceOf<SqlReferenceExpression>(queryExpression.SelectColumns.Skip(1).First().Expression);
125  Assert.IsNotNull(queryExpression.FromClause);
126  Assert.IsNotEmpty(queryExpression.FromClause.AllTables);
127  Assert.AreEqual(2, queryExpression.FromClause.AllTables.Count());
128  Assert.AreEqual(1, queryExpression.FromClause.JoinPartCount);
129  Assert.IsNotNull(queryExpression.FromClause.GetJoinPart(0));
130  Assert.AreEqual(JoinType.Inner, queryExpression.FromClause.GetJoinPart(0).JoinType);
131  }
132 
133  [Test]
134  [Category("SQL Parse")]
135  public void ParseSelectWithInnerJoin() {
136  const string sql = "SELECT a.col1, b.col2 FROM table1 AS a INNER JOIN table2 b ON a.id = b.id";
137 
138  SqlExpression expression = null;
139  Assert.DoesNotThrow(() => expression = SqlExpression.Parse(sql));
140  Assert.IsNotNull(expression);
141  Assert.IsInstanceOf<SqlQueryExpression>(expression);
142 
143  var queryExpression = (SqlQueryExpression) expression;
144  Assert.IsNotEmpty(queryExpression.SelectColumns);
145  Assert.IsInstanceOf<SqlReferenceExpression>(queryExpression.SelectColumns.First().Expression);
146  Assert.IsInstanceOf<SqlReferenceExpression>(queryExpression.SelectColumns.Skip(1).First().Expression);
147  Assert.IsNotNull(queryExpression.FromClause);
148  Assert.IsNotEmpty(queryExpression.FromClause.AllTables);
149  Assert.AreEqual(2, queryExpression.FromClause.AllTables.Count());
150  Assert.AreEqual(1, queryExpression.FromClause.JoinPartCount);
151  Assert.IsNotNull(queryExpression.FromClause.GetJoinPart(0));
152  Assert.AreEqual(JoinType.Inner, queryExpression.FromClause.GetJoinPart(0).JoinType);
153  Assert.IsNotNull(queryExpression.FromClause.GetJoinPart(0).OnExpression);
154  Assert.IsInstanceOf<SqlBinaryExpression>(queryExpression.FromClause.GetJoinPart(0).OnExpression);
155  }
156 
157  [Test]
158  [Category("SQL Parse")]
159  public void ParseSelectFunction() {
160  const string sql = "SELECT user()";
161 
162  SqlExpression expression = null;
163  Assert.DoesNotThrow(() => expression = SqlExpression.Parse(sql));
164  Assert.IsNotNull(expression);
165  Assert.IsInstanceOf<SqlQueryExpression>(expression);
166 
167  var queryExpression = (SqlQueryExpression) expression;
168  Assert.IsNotEmpty(queryExpression.SelectColumns);
169  Assert.IsInstanceOf<SqlFunctionCallExpression>(queryExpression.SelectColumns.First().Expression);
170  Assert.AreEqual("user", ((SqlFunctionCallExpression) queryExpression.SelectColumns.First().Expression).FunctioName.FullName);
171  }
172 
173  [Test]
174  [Category("SQL Parse")]
175  public void ParseSelectSubQuery() {
176  const string sql = "SELECT * FROM (SELECT a, b FROM table1)";
177 
178  SqlExpression expression = null;
179  Assert.DoesNotThrow(() => expression = SqlExpression.Parse(sql));
180  Assert.IsNotNull(expression);
181  Assert.IsInstanceOf<SqlQueryExpression>(expression);
182 
183  var queryExpression = (SqlQueryExpression) expression;
184  Assert.IsNotEmpty(queryExpression.SelectColumns);
185  Assert.IsNotEmpty(queryExpression.FromClause.AllTables);
186  Assert.AreEqual(1, queryExpression.FromClause.AllTables.Count());
187  Assert.IsTrue(queryExpression.FromClause.AllTables.First().IsSubQuery);
188  }
189 
190  //[Test]
191  //public void FluidSelectWithClause() {
192  // var expression = SqlQueryBuilder.Configure().Items(list => list.Column("col1", "a")).From("table").AsExpression();
193  //}
194 
195  [Test]
196  [Category("SQL Parse")]
197  public void ParseSimpleQuery() {
198  const string sql = "SELECT col1 AS a FROM table";
199 
200  SqlExpression expression = null;
201  Assert.DoesNotThrow(() => expression = SqlExpression.Parse(sql));
202  Assert.IsNotNull(expression);
203  Assert.IsInstanceOf<SqlQueryExpression>(expression);
204 
205  var queryExpression = (SqlQueryExpression)expression;
206  Assert.IsNotEmpty(queryExpression.SelectColumns);
207  Assert.IsInstanceOf<SqlReferenceExpression>(queryExpression.SelectColumns.First().Expression);
208  Assert.AreEqual("a", queryExpression.SelectColumns.First().Alias);
209  Assert.IsNotNull(queryExpression.FromClause);
210  Assert.AreEqual(1, queryExpression.FromClause.AllTables.Count());
211  Assert.AreEqual("table", queryExpression.FromClause.AllTables.First().Name);
212  }
213 
214  [Test]
215  [Category("SQL Parse")]
216  public void ParseSelectGroupBy() {
217  const string sql = "SELECT col1 AS a, AVG(col2) b FROM table WHERE b > 2 GROUP BY a";
218 
219  SqlExpression expression = null;
220  Assert.DoesNotThrow(() => expression = SqlExpression.Parse(sql));
221  Assert.IsNotNull(expression);
222  Assert.IsInstanceOf<SqlQueryExpression>(expression);
223 
224  var queryExpression = (SqlQueryExpression)expression;
225  Assert.IsNotEmpty(queryExpression.SelectColumns);
226 
227  var groupBy = queryExpression.GroupBy;
228  Assert.IsNotNull(groupBy);
229  Assert.IsNotEmpty(groupBy);
230  Assert.AreEqual(1, groupBy.Count());
231  }
232  }
233 }
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
SqlType Type
Gets the SqlType that defines the object properties
Definition: DataObject.cs:78
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
static SqlExpression Parse(string s)
Parses the given SQL string to an expression that can be evaluated.
An expression that references an object within a context.
JoinType
Enumerates the kind of group join in a selection query.
Definition: JoinType.cs:23
static BooleanType Boolean()
Describes the name of an object within a database.
Definition: ObjectName.cs:44
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
static DataObject String(string s)
Definition: DataObject.cs:592
static DataObject Boolean(SqlBoolean value)
Definition: DataObject.cs:544
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
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
Represents a column selected to be in the output of a select statement.
Definition: SelectColumn.cs:31
static SqlReferenceExpression Reference(ObjectName objectName)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlFunctionCallExpression FunctionCall(ObjectName functionName)
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41