DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
CreateViewStatementTests.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 using System;
16 using System.Collections.Generic;
17 using System.Linq;
18 
19 using Deveel.Data;
20 using Deveel.Data.Sql.Tables;
21 using Deveel.Data.Types;
22 
23 using NUnit.Framework;
24 
25 namespace Deveel.Data.Sql.Statements {
26  [TestFixture]
28 
29  protected override ISession CreateAdminSession(IDatabase database) {
30  using (var session = base.CreateAdminSession(database)) {
31  using (var query = session.CreateQuery()) {
32  var tableInfo = new TableInfo(ObjectName.Parse("APP.test_table"));
33  tableInfo.AddColumn("a", PrimitiveTypes.Integer());
34  tableInfo.AddColumn("b", PrimitiveTypes.String(), false);
35 
36  query.CreateTable(tableInfo, false, false);
37  query.Commit();
38  }
39  }
40 
41  return base.CreateAdminSession(database);
42  }
43 
44  [Test]
45  public void ParseSimpleCreateView() {
46  const string sql = "CREATE VIEW text_view1 AS SELECT * FROM test_table WHERE a = 1";
47 
48  var statements = SqlStatement.Parse(sql);
49  Assert.IsNotNull(statements);
50 
51  var statementList = statements.ToList();
52  Assert.IsNotEmpty(statementList);
53  Assert.AreEqual(1, statementList.Count);
54  Assert.IsInstanceOf<CreateViewStatement>(statementList[0]);
55 
56  var createView = (CreateViewStatement) statementList[0];
57  Assert.IsNotNull(createView.SourceQuery);
58  Assert.IsTrue(createView.IsFromQuery);
59 
60  Assert.IsNotNull(createView.ViewName);
61  }
62 
63 
64  [Test]
66  const string sql = "CREATE VIEW text_view1 (a, b, c) AS SELECT * FROM test_table WHERE a = 1";
67 
68  IEnumerable<SqlStatement> statements = null;
69  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
70  Assert.IsNotNull(statements);
71 
72  var statementList = statements.ToList();
73  Assert.IsNotEmpty(statementList);
74  Assert.AreEqual(1, statementList.Count);
75  Assert.IsInstanceOf<CreateViewStatement>(statementList[0]);
76 
77  var createView = (CreateViewStatement)statementList[0];
78  }
79 
80  [Test]
82  const string sql = "CREATE OR REPLACE VIEW text_view1 AS SELECT * FROM test_table WHERE a = 1";
83 
84  IEnumerable<SqlStatement> statements = null;
85  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
86  Assert.IsNotNull(statements);
87 
88  var statementList = statements.ToList();
89  Assert.IsNotEmpty(statementList);
90  Assert.AreEqual(1, statementList.Count);
91  Assert.IsInstanceOf<CreateViewStatement>(statementList[0]);
92 
93  var createView = (CreateViewStatement)statementList[0];
94  }
95 
96  [Test]
97  public void ExecuteSimpleCreateView() {
98  const string sql = "CREATE VIEW text_view1 AS SELECT * FROM test_table WHERE a = 1";
99 
100  IEnumerable<SqlStatement> statements = null;
101  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
102  Assert.IsNotNull(statements);
103 
104  var list = statements.ToList();
105 
106  Assert.AreEqual(1, list.Count);
107 
108  var statement = list[0];
109 
110  Assert.IsNotNull(statement);
111  Assert.IsInstanceOf<CreateViewStatement>(statement);
112 
113  ITable result = null;
114  Assert.DoesNotThrow(() => result = statement.Execute(Query));
115  Assert.IsNotNull(result);
116  Assert.AreEqual(1, result.RowCount);
117  }
118  }
119 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
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
override ISession CreateAdminSession(IDatabase database)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
The representation of a single database in the system.
Definition: IDatabase.cs:40
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
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