DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
JoinTableTests.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 
18 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 {
27  [TestFixture]
29  protected override void OnSetUp(string testName) {
30  CreateTestTables();
31  AddTestData();
32  }
33 
34  private void AddTestData() {
35  var table = Query.GetMutableTable(new ObjectName(new ObjectName("APP"), "persons"));
36 
37  var row = table.NewRow();
38  row["person_id"] = DataObject.Integer(1);
39  row["name"] = DataObject.String("Antonello Provenzano");
40  row["age"] = DataObject.Integer(34);
41  table.AddRow(row);
42 
43  row = table.NewRow();
44  row["person_id"] = DataObject.Integer(3);
45  row["name"] = DataObject.String("John Doe");
46  row["age"] = DataObject.Integer(56);
47  table.AddRow(row);
48 
49  table = Query.GetMutableTable(new ObjectName(new ObjectName("APP"), "codes"));
50  row = table.NewRow();
51  row["person_id"] = DataObject.Integer(1);
52  row["code"] = DataObject.String("123456");
53  row["registered"] = DataObject.Date(new SqlDateTime(2014, 01, 12));
54  table.AddRow(row);
55  }
56 
57  private void CreateTestTables() {
58  var tableInfo = CreateFirstTable();
59  Query.CreateTable(tableInfo);
60  Query.AddPrimaryKey(tableInfo.TableName, "person_id");
61 
62  tableInfo = CreateSecondTable();
63  Query.CreateTable(tableInfo);
64  }
65 
67  var tableInfo = new TableInfo(new ObjectName(new ObjectName("APP"), "codes"));
68  tableInfo.AddColumn("person_id", PrimitiveTypes.Integer());
69  tableInfo.AddColumn("code", PrimitiveTypes.String());
70  tableInfo.AddColumn("registered", PrimitiveTypes.DateTime());
71 
72  return tableInfo;
73  }
74 
76  var tableInfo = new TableInfo(new ObjectName(new ObjectName("APP"), "persons"));
77  tableInfo.AddColumn("person_id", PrimitiveTypes.Integer());
78  tableInfo.AddColumn("name", PrimitiveTypes.String());
79  tableInfo.AddColumn("age", PrimitiveTypes.Integer());
80 
81  return tableInfo;
82  }
83 
84  [Test]
85  public void NaturalInnerJoin() {
86  SqlExpression expression = null;
87  Assert.DoesNotThrow(() => expression = SqlExpression.Parse("SELECT * FROM persons, codes"));
88  Assert.IsNotNull(expression);
89  Assert.IsInstanceOf<SqlQueryExpression>(expression);
90 
91  SqlExpression resultExpression = null;
92  Assert.DoesNotThrow(() => resultExpression = expression.Evaluate(Query, null));
93  Assert.IsNotNull(resultExpression);
94  Assert.IsInstanceOf<SqlConstantExpression>(resultExpression);
95 
96  var constantExpression = (SqlConstantExpression) resultExpression;
97  Assert.IsInstanceOf<QueryType>(constantExpression.Value.Type);
98 
99  var queryPlan = ((SqlQueryObject) constantExpression.Value.Value);
100  ITable result = null;
101 
102  Assert.DoesNotThrow(() => result = queryPlan.QueryPlan.Evaluate(Query));
103 
104  Assert.IsNotNull(result);
105  }
106  }
107 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static DataObject Date(DateTimeOffset value)
Definition: DataObject.cs:600
static DataObject Integer(int value)
Definition: DataObject.cs:576
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
static SqlExpression Parse(string s)
Parses the given SQL string to an expression that can be evaluated.
Describes the name of an object within a database.
Definition: ObjectName.cs:44
static DataObject String(string s)
Definition: DataObject.cs:592
virtual SqlExpression Evaluate(EvaluateContext context)
When overridden by a derived class, this method evaluates the expression within the provided context...
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
An expression that holds a constant value.
Defines the base class for instances that represent SQL expression tree nodes.
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41
override void OnSetUp(string testName)