DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
AlterTableStatementTests.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;
21 using Deveel.Data.Sql.Objects;
22 using Deveel.Data.Sql.Tables;
23 using Deveel.Data.Types;
24 
25 using NUnit.Framework;
26 
27 namespace Deveel.Data.Sql.Statements {
28  [TestFixture]
30  protected override void OnSetUp(string testName) {
31  CreateTestTable();
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  [Test]
49  public void AlterTableAddColumn() {
50  const string sql = "ALTER TABLE test_table ADD COLUMN reserved BOOLEAN";
51 
52  IEnumerable<SqlStatement> statements = null;
53  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
54  Assert.IsNotNull(statements);
55 
56  var list = statements.ToList();
57 
58  Assert.AreEqual(1, list.Count);
59 
60  var statement = list[0];
61 
62  Assert.IsNotNull(statement);
63  Assert.IsInstanceOf<AlterTableStatement>(statement);
64 
65  ITable result = null;
66  Assert.DoesNotThrow(() => result = statement.Execute(Query));
67  Assert.IsNotNull(result);
68  Assert.AreEqual(1, result.RowCount);
69  Assert.AreEqual(1, result.TableInfo.ColumnCount);
70  Assert.AreEqual(0, ((SqlNumber) result.GetValue(0,0).Value).ToInt32());
71 
72  var testTable = Query.GetTable(new ObjectName("test_table"));
73 
74  Assert.IsNotNull(testTable);
75  Assert.AreEqual(6, testTable.TableInfo.ColumnCount);
76  }
77  }
78 }
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
static BooleanType Boolean()
Describes the name of an object within a database.
Definition: ObjectName.cs:44
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
static SqlReferenceExpression Reference(ObjectName objectName)
Defines the base class for instances that represent SQL expression tree nodes.
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