DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
InsertIntoStatementTests.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  private void CreateTestTable() {
30  var tableInfo = new TableInfo(ObjectName.Parse("APP.test_table"));
31  var idColumn = tableInfo.AddColumn("id", PrimitiveTypes.Integer());
32  idColumn.DefaultExpression = SqlExpression.FunctionCall("UNIQUE_KEY",
33  new SqlExpression[] { SqlExpression.Reference(tableInfo.TableName) });
34  tableInfo.AddColumn("first_name", PrimitiveTypes.String());
35  tableInfo.AddColumn("last_name", PrimitiveTypes.String());
36  tableInfo.AddColumn("birth_date", PrimitiveTypes.DateTime());
37  tableInfo.AddColumn("active", PrimitiveTypes.Boolean());
38 
39  Query.CreateTable(tableInfo);
40  Query.AddPrimaryKey(tableInfo.TableName, "id", "PK_TEST_TABLE");
41  }
42 
43  [Test]
44  public void ParseValuesInsert_OneRow() {
45  const string sql = "INSERT INTO test_table (first_name, last_name, birth_date) "+
46  "VALUES ('Antonello', 'Provenzano', TOODATE('1980-06-04'))";
47 
48  IEnumerable<SqlStatement> statements = null;
49  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(Query.Context, sql));
50  Assert.IsNotNull(statements);
51 
52  var statementList = statements.ToList();
53  Assert.IsNotEmpty(statementList);
54  Assert.AreEqual(1, statementList.Count);
55 
56  var statement = statementList[0];
57  Assert.IsInstanceOf<InsertStatement>(statement);
58 
59  var insertStatement = (InsertStatement) statement;
60  Assert.AreEqual("test_table", insertStatement.TableName);
61  Assert.AreEqual(3, insertStatement.ColumnNames.Count());
62  Assert.AreEqual(1, insertStatement.Values.Count());
63  }
64 
65  [Test]
67  const string sql = "INSERT INTO test_table (first_name, last_name, birth_date) " +
68  "VALUES ('Antonello', 'Provenzano', TOODATE('1980-06-04')), " +
69  "('Sebastiano', 'Provenzano', TODATE('1981-08-27'))";
70 
71  IEnumerable<SqlStatement> statements = null;
72  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(Query.Context, sql));
73  Assert.IsNotNull(statements);
74 
75  var statementList = statements.ToList();
76  Assert.IsNotEmpty(statementList);
77  Assert.AreEqual(1, statementList.Count);
78 
79  var statement = statementList[0];
80  Assert.IsInstanceOf<InsertStatement>(statement);
81 
82  var insertStatement = (InsertStatement)statement;
83  Assert.AreEqual("test_table", insertStatement.TableName);
84  Assert.AreEqual(3, insertStatement.ColumnNames.Count());
85  Assert.AreEqual(2, insertStatement.Values.Count());
86  }
87 
88  [Test]
89  public void ParserSetInsert() {
90  const string sql =
91  "INSERT INTO test_table SET first_name = 'Antonello', last_name = 'Provenzano', birth_date = TODATE('1980-06-04')";
92 
93  IEnumerable<SqlStatement> statements = null;
94  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(Query.Context, sql));
95  Assert.IsNotNull(statements);
96 
97  var statementList = statements.ToList();
98  Assert.IsNotEmpty(statementList);
99  Assert.AreEqual(1, statementList.Count);
100 
101  var statement = statementList[0];
102  Assert.IsInstanceOf<InsertStatement>(statement);
103 
104  var insertStatement = (InsertStatement)statement;
105  Assert.AreEqual("test_table", insertStatement.TableName);
106  Assert.AreEqual(3, insertStatement.ColumnNames.Count());
107  Assert.AreEqual(1, insertStatement.Values.Count());
108  }
109 
110  [Test]
111  public void ParseInsertSelect() {
112  const string sql = "INSERT INTO test_table FROM (SELECT * FROM table2 WHERE arg1 = 1)";
113 
114  IEnumerable<SqlStatement> statements = null;
115  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(Query.Context, sql));
116  Assert.IsNotNull(statements);
117 
118  var statementList = statements.ToList();
119  Assert.IsNotEmpty(statementList);
120  Assert.AreEqual(1, statementList.Count);
121 
122  var statement = statementList[0];
123  Assert.IsInstanceOf<InsertSelectStatement>(statement);
124  }
125  }
126 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
IQueryContext Context
Definition: Query.cs:68
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