DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
UpdateStatementTests.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 IQuery CreateQuery(ISession session) {
31  var query = base.CreateQuery(session);
32  CreateTestTable(query);
33  AddTestData(query);
34  return query;
35  }
36 
37  private void CreateTestTable(IQuery context) {
38  var tableInfo = new TableInfo(ObjectName.Parse("APP.test_table"));
39  var idColumn = tableInfo.AddColumn("id", PrimitiveTypes.Integer());
40  idColumn.DefaultExpression = SqlExpression.FunctionCall("UNIQUE_KEY",
41  new SqlExpression[] { SqlExpression.Reference(tableInfo.TableName) });
42  tableInfo.AddColumn("first_name", PrimitiveTypes.String());
43  tableInfo.AddColumn("last_name", PrimitiveTypes.String());
44  tableInfo.AddColumn("birth_date", PrimitiveTypes.DateTime());
45  tableInfo.AddColumn("active", PrimitiveTypes.Boolean());
46 
47  context.CreateTable(tableInfo);
48  context.AddPrimaryKey(tableInfo.TableName, "id", "PK_TEST_TABLE");
49  }
50 
51  private void AddTestData(IQuery context) {
52  var table = context.GetMutableTable(ObjectName.Parse("APP.test_table"));
53  var row = table.NewRow();
54  row.SetValue("id", DataObject.Integer(0));
55  row.SetValue("first_name", DataObject.String("John"));
56  row.SetValue("last_name", DataObject.String("Doe"));
57  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1977, 01, 01)));
58  row.SetValue("active", DataObject.Boolean(false));
59  table.AddRow(row);
60 
61  row = table.NewRow();
62  row.SetValue("id", DataObject.Integer(1));
63  row.SetValue("first_name", DataObject.String("Jane"));
64  row.SetValue("last_name", DataObject.String("Doe"));
65  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1978, 11, 01)));
66  row.SetValue("active", DataObject.Boolean(true));
67  table.AddRow(row);
68 
69  row = table.NewRow();
70  row.SetValue("id", DataObject.Integer(2));
71  row.SetValue("first_name", DataObject.String("Roger"));
72  row.SetValue("last_name", DataObject.String("Rabbit"));
73  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1985, 05, 05)));
74  row.SetValue("active", DataObject.Boolean(true));
75  table.AddRow(row);
76  }
77 
78  [Test]
79  public void ParseSimpleUpdate() {
80  const string sql = "UPDATE table SET col1 = 'testUpdate', col2 = 22 WHERE id = 1";
81 
82  IEnumerable<SqlStatement> statements = null;
83  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
84  Assert.IsNotNull(statements);
85 
86  var statement = statements.FirstOrDefault();
87 
88  Assert.IsNotNull(statement);
89  Assert.IsInstanceOf<UpdateStatement>(statement);
90  }
91 
92  [Test]
94  const string sql = "UPDATE table SET col1 = 'testUpdate', col2 = 22 WHERE id = 1 LIMIT 20";
95 
96  IEnumerable<SqlStatement> statements = null;
97  Assert.DoesNotThrow(() => statements = SqlStatement.Parse(sql));
98  Assert.IsNotNull(statements);
99 
100  var statement = statements.FirstOrDefault();
101 
102  Assert.IsNotNull(statement);
103  Assert.IsInstanceOf<UpdateStatement>(statement);
104  }
105  }
106 }
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
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
static DataObject String(string s)
Definition: DataObject.cs:592
static DataObject Boolean(SqlBoolean value)
Definition: DataObject.cs:544
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
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
override IQuery CreateQuery(ISession session)
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