DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
CreateTableStatementTests.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.Tables;
22 using Deveel.Data.Types;
23 
24 using NUnit.Framework;
25 
26 namespace Deveel.Data.Sql.Statements {
27  [TestFixture]
29  [Test]
30  public void ParseSimpleCreate() {
31  const string sql = "CREATE TABLE test (id INT, name VARCHAR)";
32 
33  IEnumerable<SqlStatement> statements = null;
34  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
35  Assert.IsNotNull(statements);
36 
37  var list = statements.ToList();
38 
39  Assert.AreEqual(1, list.Count);
40 
41  var statement = list[0];
42 
43  Assert.IsNotNull(statement);
44  Assert.IsInstanceOf<CreateTableStatement>(statement);
45 
46  var createTable = (CreateTableStatement) statement;
47  Assert.AreEqual(2, createTable.Columns.Count);
48 
49  var columns = createTable.Columns;
50 
51  Assert.AreEqual("id", columns[0].ColumnName);
52  Assert.AreEqual(SqlTypeCode.Integer, columns[0].ColumnType.TypeCode);
53 
54  Assert.AreEqual("name", columns[1].ColumnName);
55  Assert.IsInstanceOf<StringType>(columns[1].ColumnType);
56  }
57 
58  [Test]
59  public void SimpleCreate() {
60  const string sql = "CREATE TABLE test (id INT, name VARCHAR)";
61 
62  IEnumerable<SqlStatement> statements = null;
63  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
64  Assert.IsNotNull(statements);
65 
66  var list = statements.ToList();
67 
68  Assert.AreEqual(1, list.Count);
69 
70  var statement = list[0];
71 
72  Assert.IsNotNull(statement);
73  Assert.IsInstanceOf<CreateTableStatement>(statement);
74 
75  ITable result = null;
76  Assert.DoesNotThrow(() => result = statement.Execute(Query));
77  Assert.IsNotNull(result);
78  Assert.AreEqual(1, result.RowCount);
79  }
80 
81  [Test]
82  public void WithColumnDefault() {
83  const string sql = "CREATE TABLE test (id INT, name VARCHAR DEFAULT (67 * 90)+22, date TIMESTAMP DEFAULT GetDate())";
84 
85  IEnumerable<SqlStatement> statements = null;
86  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
87  Assert.IsNotNull(statements);
88 
89  var list = statements.ToList();
90 
91  Assert.AreEqual(1, list.Count);
92 
93  var statement = list[0];
94 
95  Assert.IsNotNull(statement);
96  Assert.IsInstanceOf<CreateTableStatement>(statement);
97 
98  ITable result = null;
99  Assert.DoesNotThrow(() => result = statement.Execute(Query));
100  Assert.IsNotNull(result);
101  Assert.AreEqual(1, result.RowCount);
102  }
103 
104  [Test]
105  public void ParseWithColumnDefault() {
106  const string sql = "CREATE TABLE test (id INT, name VARCHAR DEFAULT (67 * 90)+22, date TIMESTAMP DEFAULT GetDate())";
107 
108  IEnumerable<SqlStatement> statements = null;
109  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
110  Assert.IsNotNull(statements);
111 
112  var list = statements.ToList();
113 
114  Assert.AreEqual(1, list.Count);
115 
116  var statement = list[0];
117 
118  Assert.IsNotNull(statement);
119  Assert.IsInstanceOf<CreateTableStatement>(statement);
120 
121  var createTable = (CreateTableStatement)statement;
122  Assert.AreEqual(3, createTable.Columns.Count);
123 
124  var columns = createTable.Columns;
125 
126  Assert.AreEqual("id", columns[0].ColumnName);
127  Assert.AreEqual(SqlTypeCode.Integer, columns[0].ColumnType.TypeCode);
128 
129  Assert.AreEqual("name", columns[1].ColumnName);
130  Assert.IsInstanceOf<StringType>(columns[1].ColumnType);
131  Assert.IsTrue(columns[1].HasDefaultExpression);
132  Assert.IsInstanceOf<SqlBinaryExpression>(columns[1].DefaultExpression);
133 
134  Assert.AreEqual("date", columns[2].ColumnName);
135  Assert.IsInstanceOf<DateType>(columns[2].ColumnType);
136  Assert.IsTrue(columns[1].HasDefaultExpression);
137  Assert.IsInstanceOf<SqlFunctionCallExpression>(columns[2].DefaultExpression);
138  }
139 
140  [Test]
141  public void ParseWithColumnIdentity() {
142  const string sql = "CREATE TABLE test (id INT IDENTITY, name VARCHAR NOT NULL)";
143 
144  IEnumerable<SqlStatement> statements = null;
145  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
146  Assert.IsNotNull(statements);
147 
148  var list = statements.ToList();
149 
150  Assert.AreEqual(1, list.Count);
151 
152  var statement = list[0];
153 
154  Assert.IsNotNull(statement);
155  Assert.IsInstanceOf<CreateTableStatement>(statement);
156 
157  var createTable = (CreateTableStatement)statement;
158  Assert.AreEqual(2, createTable.Columns.Count);
159 
160  var columns = createTable.Columns;
161 
162  Assert.AreEqual("id", columns[0].ColumnName);
163  Assert.AreEqual(SqlTypeCode.Integer, columns[0].ColumnType.TypeCode);
164  Assert.IsNotNull(columns[0].DefaultExpression);
165  Assert.IsInstanceOf<SqlFunctionCallExpression>(columns[0].DefaultExpression);
166 
167  Assert.AreEqual("name", columns[1].ColumnName);
168  Assert.IsInstanceOf<StringType>(columns[1].ColumnType);
169  Assert.IsTrue(columns[1].IsNotNull);
170  }
171 
172  [Test]
174  const string sql = "CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR NOT NULL)";
175 
176  IEnumerable<SqlStatement> statements = null;
177  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
178  Assert.IsNotNull(statements);
179 
180  var list = statements.ToList();
181 
182  Assert.AreEqual(2, list.Count);
183  }
184 
185  [Test]
187  const string sql = "CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR NOT NULL, CONSTRAINT uk_test UNIQUE(name))";
188 
189  IEnumerable<SqlStatement> statements = null;
190  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
191  Assert.IsNotNull(statements);
192 
193  var list = statements.ToList();
194 
195  Assert.AreEqual(3, list.Count);
196  }
197  }
198 }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
The statement object used to create a table in a database.
Represents the foundation class of SQL statements to be executed.
Definition: SqlStatement.cs:32
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
static IEnumerable< SqlStatement > Parse(string sqlSource)
Parses a given string into one of more statements.