public void Test_Limit_Offset_Start() { var sql = Db <DB> .Sql(db => Select(Asterisk(db.tbl_remuneration)). From(db.tbl_remuneration). OrderBy(Asc(db.tbl_remuneration.id)) + Limit(1) + Offset(3) ); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT * FROM tbl_remuneration ORDER BY tbl_remuneration.id ASC LIMIT @p_0 OFFSET @p_1", 1, 3); }
public void Test_While_1() { var sql = Db <DB> .Sql(db => While(Select(Avg(db.tbl_remuneration.money)).From(db.tbl_remuneration) < 2000). Begin(). Select(Asterisk(db.tbl_remuneration)). From(db.tbl_remuneration). End()); _connection.Query(sql).ToList(); AssertEx.AreEqual(sql, _connection, @"WHILE (SELECT AVG(tbl_remuneration.money) FROM tbl_remuneration) < @p_0 BEGIN SELECT tbl_remuneration.* FROM tbl_remuneration END", (double)2000); }
public void Test_Having_Vanish() { var empty = new Sql <bool>(); var sql = Db <DB> .Sql(db => Select(new SelectedData2 { Id = db.tbl_remuneration.staff_id }). From(db.tbl_remuneration). GroupBy(db.tbl_remuneration.staff_id). Having(empty)); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT tbl_remuneration.staff_id AS Id FROM tbl_remuneration GROUP BY tbl_remuneration.staff_id"); }
public void Test_DateAdd() { var sql = Db <DB> .Sql(db => Select(new { Val1 = DateAdd(DateAddElement.Year, 1, Current_TimeStamp()), Val2 = DateAdd(DateAddElement.Quarter, 2, Current_TimeStamp()), Val3 = DateAdd(DateAddElement.Month, 3, Current_TimeStamp()), Val4 = DateAdd(DateAddElement.Dayofyear, 4, Current_TimeStamp()), Val5 = DateAdd(DateAddElement.Day, 5, Current_TimeStamp()), Val6 = DateAdd(DateAddElement.Week, 6, Current_TimeStamp()), Val7 = DateAdd(DateAddElement.Weekday, 7, Current_TimeStamp()), Val8 = DateAdd(DateAddElement.Hour, 8, Current_TimeStamp()), Val9 = DateAdd(DateAddElement.Minute, 9, Current_TimeStamp()), Val10 = DateAdd(DateAddElement.Second, 10, Current_TimeStamp()), Val11 = DateAdd(DateAddElement.Millisecond, -11, SysDateTime()), Val12 = DateAdd(DateAddElement.Microsecond, 12, SysDateTime()), Val13 = DateAdd(DateAddElement.Nanosecond, -13, SysDateTime()) })); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT DATEADD(YEAR, @p_0, CURRENT_TIMESTAMP) AS Val1, DATEADD(QUARTER, @p_1, CURRENT_TIMESTAMP) AS Val2, DATEADD(MONTH, @p_2, CURRENT_TIMESTAMP) AS Val3, DATEADD(DAYOFYEAR, @p_3, CURRENT_TIMESTAMP) AS Val4, DATEADD(DAY, @p_4, CURRENT_TIMESTAMP) AS Val5, DATEADD(WEEK, @p_5, CURRENT_TIMESTAMP) AS Val6, DATEADD(WEEKDAY, @p_6, CURRENT_TIMESTAMP) AS Val7, DATEADD(HOUR, @p_7, CURRENT_TIMESTAMP) AS Val8, DATEADD(MINUTE, @p_8, CURRENT_TIMESTAMP) AS Val9, DATEADD(SECOND, @p_9, CURRENT_TIMESTAMP) AS Val10, DATEADD(MILLISECOND, @p_10, SYSDATETIME()) AS Val11, DATEADD(MICROSECOND, @p_11, SYSDATETIME()) AS Val12, DATEADD(NANOSECOND, @p_12, SYSDATETIME()) AS Val13" , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, 12, -13); }
public void Test_InsertInto_Values_Select() { Test_InsertInto_Values(); var sql = Db <DB> .Sql(db => InsertInto(db.tbl_data). Select(new { id = db.tbl_data.id + 10, val1 = db.tbl_data.val1, val2 = db.tbl_data.val2 }). From(db.tbl_data)); Assert.AreEqual(1, _connection.Execute(sql)); AssertEx.AreEqual(sql, _connection, @"INSERT INTO tbl_data SELECT tbl_data.id + @p_0 AS id, tbl_data.val1 AS val1, tbl_data.val2 AS val2 FROM tbl_data", 10); }
public void Test_Round() { var sql = Db<DB>.Sql(db => Select(new { Val1 = Round((long)2, 3), Val2 = Round(2, 3), Val3 = Round((decimal)2, 3), Val4 = Round((double)2, 3) }). From(db.tbl_staff)); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT ROUND(@p_0, @p_1) AS Val1, ROUND(@p_2, @p_3) AS Val2, ROUND(@p_4, @p_5) AS Val3, ROUND(@p_6, @p_7) AS Val4 FROM tbl_staff", (long)2, 3, 2, 3, (decimal)2, 3, (double)2, 3); }
public void Test_Having_Start() { var sql = Db <DB> .Sql(db => Select(new SelectedData2 { Id = db.tbl_remuneration.staff_id }). From(db.tbl_remuneration). GroupBy(db.tbl_remuneration.staff_id) + Having(100 < Sum(db.tbl_remuneration.money))); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT tbl_remuneration.staff_id AS Id FROM tbl_remuneration GROUP BY tbl_remuneration.staff_id HAVING @p_0 < SUM(tbl_remuneration.money)", (decimal)100); }
public void Test_Sign() { var sql = Db<DB>.Sql(db => Select(new { Val1 = Sign((long)1), Val2 = Sign(1), Val3 = Sign((decimal)1), Val4 = Sign((double)1), }). From(db.tbl_staff)); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT SIGN(@p_0) AS Val1, SIGN(@p_1) AS Val2, SIGN(@p_2) AS Val3, SIGN(@p_3) AS Val4 FROM tbl_staff", (long)1, 1, (decimal)1, (double)1); }
public void Test_Rows_2() { var sql = Db <DB> .Sql(db => Select(new SelectData() { Val = Count(db.tbl_remuneration.money). Over(OrderBy(Asc(db.tbl_remuneration.money)), Rows(1, 5)) }). From(db.tbl_remuneration)); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT COUNT(tbl_remuneration.money) OVER( ORDER BY tbl_remuneration.money ASC ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING) AS Val FROM tbl_remuneration"); }
public void Test_OffsetRows_FetchNext_Start() { var sql = Db <DB> .Sql(db => Select(Asterisk(db.tbl_remuneration)). From(db.tbl_remuneration). OrderBy(Asc(db.tbl_remuneration.id)) + OffsetRows(1) + FetchNextRowsOnly(3) ); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT tbl_remuneration.* FROM tbl_remuneration ORDER BY tbl_remuneration.id ASC OFFSET @p_0 ROWS FETCH NEXT @p_1 ROWS ONLY", 1, 3); }
public void Test_Recursive_1() { var rec = Db <DB> .Sql(db => new { val = 0 }); var select = Db <DB> .Sql(db => Select(new object[] { 1 }). Union(All()). Select(new object[] { rec.Body.val + 1 }). From(rec). Where(rec.Body.val + 1 <= 5) ); var sql = Db <DB> .Sql(db => With(rec.ExpandArguments().As(select)). Select(rec.Body.val). From(rec) ); sql.Gen(_connection); var datas = _connection.Query <SelectedData>(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"WITH rec(val) AS (SELECT @p_0 UNION ALL SELECT rec.val + @p_1 FROM rec WHERE rec.val + @p_2 <= @p_3) SELECT rec.val FROM rec", 1, 1, 1, 5); }
public void Test_Unicode() { var sql = Db <DB> .Sql(db => Select(new { Val = Unicode(new DbParam <string> { Value = "𠮟", DbType = DbType.String }) })); var data = _connection.Query(sql).ToList(); Assert.IsTrue(data.Count > 0); AssertEx.AreEqual(sql, _connection, @"SELECT UNICODE(@p_0) AS Val" , new DbParams { { "@p_0", new DbParam { Value = "𠮟", DbType = DbType.String } } }); }
public void Test_Try_Catch_throw_2() { var sql = Db <DB> .Sql(db => Begin() + Try() + Throw(1, "a", 1) + End() + Try() + Begin() + Catch() + End() + Catch() ); _connection.Query <object>(sql).ToList(); AssertEx.AreEqual(sql, _connection, @"BEGIN TRY THROW @p_0, @p_1, @p_2 END TRY BEGIN CATCH END CATCH", 1, "a", (byte)1); }
public void Test_From_Multi() { var sql = Db <DB> .Sql(db => Select(new SelectData { Name = db.tbl_staff.name, PaymentDate = db.tbl_remuneration.payment_date, Money = db.tbl_remuneration.money, }). From(db.tbl_remuneration, db.tbl_staff). Where(db.tbl_remuneration.staff_id == db.tbl_staff.id)); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT tbl_staff.name AS Name, tbl_remuneration.payment_date AS PaymentDate, tbl_remuneration.money AS Money FROM tbl_remuneration, tbl_staff WHERE tbl_remuneration.staff_id = tbl_staff.id"); }
public void Test_Normal() { var sql = Db <DB> .Sql(db => Select(new SelecteData { name = db.tbl_staff.name, payment_date = db.tbl_remuneration.payment_date, money = db.tbl_remuneration.money }). From(db.tbl_remuneration). Join(db.tbl_staff, db.tbl_staff.id == db.tbl_remuneration.staff_id)); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT tbl_staff.name AS name, tbl_remuneration.payment_date AS payment_date, tbl_remuneration.money AS money FROM tbl_remuneration JOIN tbl_staff ON tbl_staff.id = tbl_remuneration.staff_id" ); }
public void Test_SysTable_2() { var sql1 = Db <DB> .Sql(db => Sys2.X); AssertEx.AreEqual(sql1, _connection, @"X"); var sql2 = Db <DB> .Sql(db => Sys2.X.id); AssertEx.AreEqual(sql2, _connection, @"X.id"); var sql3 = Db <DB> .Sql(db => Sys2.X2); AssertEx.AreEqual(sql3, _connection, @"System"); var sql4 = Db <DB> .Sql(db => Sys2.X2.id); AssertEx.AreEqual(sql4, _connection, @"System.id"); var sql = Db <DB> .Sql(db => Select(sql3.Body.id).From(sql3)); AssertEx.AreEqual(sql, _connection, @"SELECT sql3.id FROM System sql3"); }
public void Test_Text_3() { var exp1 = Db <DB> .Sql(db => db.tbl_staff.id); var exp2 = Db <DB> .Sql(db => db.tbl_staff.name); var sql = Db <DB> .Sql(db => Select(new Staff { id = (int)"{0}".ToSqlObject(exp1), name = (string)"{0}".ToSqlObject(exp2), }). From(db.tbl_staff)); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT tbl_staff.id AS id, tbl_staff.name AS name FROM tbl_staff"); }
public void Test1() { var exps = new Expressions(); var query = Db <DB> .Sql(db => Select(Asterisk(db.tbl_staff)). From(db.tbl_staff). Where(exps.Condition1 || exps.Condition2)); var datas = _connection.Query(query).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(query, _connection, @"SELECT * FROM tbl_staff WHERE (@val1 = @p_0) OR (@val1 = @p_1)", new Params() { { "@val1", 1 }, { "@p_0", 1 }, { "@p_1", 2 }, }); }
public void Test_IsDate() { var sql = Db <DB> .Sql(db => Select(new { Val1 = IsDate("2017-04-01"), Val2 = IsDate(null), Val3 = IsDate(20170401), Val4 = IsDate(Current_TimeStamp()), Val5 = IsDate("2017-04-01 12:34:56.000") })); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT ISDATE(@p_0) AS Val1, ISDATE(@p_1) AS Val2, ISDATE(@p_2) AS Val3, ISDATE(CURRENT_TIMESTAMP) AS Val4, ISDATE(@p_3) AS Val5" , "2017-04-01", null, 20170401, "2017-04-01 12:34:56.000"); }
public void Test_AdditionalOperator_SubQuery_5() { var empty = new Sql <decimal>(); var sql = Db <DB> .Sql(db => Select(new SelectData { Money = empty + Select(Sum(db.tbl_remuneration.money)) + From(db.tbl_remuneration) }). From(db.tbl_remuneration)); sql.Gen(_connection); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT (SELECT SUM(tbl_remuneration.money) FROM tbl_remuneration) AS Money FROM tbl_remuneration"); }
public void Test_SubQuery_1() { var sub = Db <DB> .Sql(db => Select(Sum(db.tbl_remuneration.money)). From(db.tbl_remuneration)); var sql = Db <DB> .Sql(db => Select(new SelectData { Money = sub }). From(db.tbl_remuneration)); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT (SELECT SUM(tbl_remuneration.money) FROM tbl_remuneration) AS Money FROM tbl_remuneration"); }
public void Test_In() { var sql = Db <DB> .Sql(db => Select(new SelectData { Id = db.tbl_staff.id }). From(db.tbl_staff). Where(In(db.tbl_staff.id, 1, 2, 3, 4, 5))); sql.Gen(_connection); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT tbl_staff.id AS Id FROM tbl_staff WHERE tbl_staff.id IN(@p_0, @p_1, @p_2, @p_3, @p_4)", 1, 2, 3, 4, 5); }
public void Test_trace_xe_action_map() { var sql = Db <DB> .Sql(db => Select(new { Sys.Trace_xe_action_map.Trace_column_id, Sys.Trace_xe_action_map.Package_name, Sys.Trace_xe_action_map.Xe_action_name, }). From(Sys.Trace_xe_action_map)); sql.Gen(_connection); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT sys.trace_xe_action_map.Trace_column_id AS Trace_column_id, sys.trace_xe_action_map.Package_name AS Package_name, sys.trace_xe_action_map.Xe_action_name AS Xe_action_name FROM sys.trace_xe_action_map"); }
public void Test_Sum() { var sql = Db <DB> .Sql(db => Select(new SelectData { Val1 = Sum(db.tbl_remuneration.money), Val2 = Sum(All(), db.tbl_remuneration.money) }). From(db.tbl_remuneration). Join(db.tbl_staff, db.tbl_remuneration.staff_id == db.tbl_staff.id). GroupBy(db.tbl_staff.id, db.tbl_staff.name)); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT SUM(tbl_remuneration.money) AS Val1, SUM(ALL tbl_remuneration.money) AS Val2 FROM tbl_remuneration JOIN tbl_staff ON tbl_remuneration.staff_id = tbl_staff.id GROUP BY tbl_staff.id, tbl_staff.name"); }
public void Test_IsNotNull_3() { string val = ""; var sql = Db <DB> .Sql(db => Select(new SelectData { Id = db.tbl_staff.id }). From(db.tbl_staff). Where(val != null)); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT tbl_staff.id AS Id FROM tbl_staff WHERE @val IS NOT NULL", new Params() { { "@val", "" } }); }
public void Test_ForeignKey_References_1() { CleanUpCreateDropTestTable(); var tbl1 = Db <DBForCreateTest> .Sql(db => CreateTable(db.table1, new Column(db.table1.id, DataType.Int(), NotNull(), PrimaryKey()) )); _connection.Execute(tbl1); var sql = Db <DBForCreateTest> .Sql(db => CreateTable(db.table2, new Column(db.table2.table1Id, DataType.Int()), ForeignKey(db.table2.table1Id).References(db.table1, db.table1.id) )); _connection.Execute(sql); AssertEx.AreEqual(sql, _connection, @"CREATE TABLE table2( table1Id INT, FOREIGN KEY(table1Id) REFERENCES table1(id))" ); }
public void Test_DateDiff() { var sql = Db <DB> .Sql(db => Select(new { Val1 = DateDiff(DateDiffElement.Year, Current_TimeStamp(), Current_TimeStamp()), Val2 = DateDiff(DateDiffElement.Quarter, Current_TimeStamp(), Current_TimeStamp()), Val3 = DateDiff(DateDiffElement.Month, Current_TimeStamp(), Current_TimeStamp()), Val4 = DateDiff(DateDiffElement.Dayofyear, Current_TimeStamp(), SysDateTimeOffset()), Val5 = DateDiff(DateDiffElement.Day, Current_TimeStamp(), SysDateTimeOffset()), Val6 = DateDiff(DateDiffElement.Week, Current_TimeStamp(), SysDateTimeOffset()), Val7 = DateDiff(DateDiffElement.Hour, SysDateTimeOffset(), Current_TimeStamp()), Val8 = DateDiff(DateDiffElement.Minute, SysDateTimeOffset(), Current_TimeStamp()), Val9 = DateDiff(DateDiffElement.Second, SysDateTimeOffset(), Current_TimeStamp()), Val10 = DateDiff(DateDiffElement.Millisecond, SysDateTimeOffset(), SysDateTimeOffset()), Val11 = DateDiff(DateDiffElement.Microsecond, SysDateTimeOffset(), SysDateTimeOffset()), Val12 = DateDiff(DateDiffElement.Nanosecond, SysDateTimeOffset(), SysDateTimeOffset()) })); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT DATEDIFF(YEAR, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) AS Val1, DATEDIFF(QUARTER, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) AS Val2, DATEDIFF(MONTH, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) AS Val3, DATEDIFF(DAYOFYEAR, CURRENT_TIMESTAMP, SYSDATETIMEOFFSET()) AS Val4, DATEDIFF(DAY, CURRENT_TIMESTAMP, SYSDATETIMEOFFSET()) AS Val5, DATEDIFF(WEEK, CURRENT_TIMESTAMP, SYSDATETIMEOFFSET()) AS Val6, DATEDIFF(HOUR, SYSDATETIMEOFFSET(), CURRENT_TIMESTAMP) AS Val7, DATEDIFF(MINUTE, SYSDATETIMEOFFSET(), CURRENT_TIMESTAMP) AS Val8, DATEDIFF(SECOND, SYSDATETIMEOFFSET(), CURRENT_TIMESTAMP) AS Val9, DATEDIFF(MILLISECOND, SYSDATETIMEOFFSET(), SYSDATETIMEOFFSET()) AS Val10, DATEDIFF(MICROSECOND, SYSDATETIMEOFFSET(), SYSDATETIMEOFFSET()) AS Val11, DATEDIFF(NANOSECOND, SYSDATETIMEOFFSET(), SYSDATETIMEOFFSET()) AS Val12" ); }
public void Test_PartitionBy() { var sql = Db <DB> .Sql(db => Select(new SelectData() { Val = Count(db.tbl_remuneration.money). Over(PartitionBy(db.tbl_remuneration.payment_date), OrderBy(Asc(db.tbl_remuneration.money))) }). From(db.tbl_remuneration)); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT COUNT(tbl_remuneration.money) OVER( PARTITION BY tbl_remuneration.payment_date ORDER BY tbl_remuneration.money ASC) AS Val FROM tbl_remuneration"); }
public void Test_OrderBy_Element_Vanish_3() { var asc1 = new Sql <OrderByElement>(); var asc2 = new Sql <OrderByElement>(); var sql = Db <DB> .Sql(db => Select(new SelectedData2 { Id = db.tbl_remuneration.staff_id }). From(db.tbl_remuneration) + OrderBy(asc1, Desc(db.tbl_remuneration.staff_id), asc2)); sql.Gen(_connection); var datas = _connection.Query(sql).ToList(); Assert.IsTrue(0 < datas.Count); AssertEx.AreEqual(sql, _connection, @"SELECT tbl_remuneration.staff_id AS Id FROM tbl_remuneration ORDER BY tbl_remuneration.staff_id DESC" ); }
public void Test_All_1() { var sql = Db <DB> .Sql(db => Select(new SelectData { Id = db.tbl_staff.id }). From(db.tbl_staff). Where(db.tbl_staff.id < All(Select(db.tbl_staff.id).From(db.tbl_staff)))); var datas = _connection.Query(sql).ToList(); AssertEx.AreEqual(sql, _connection, @"SELECT tbl_staff.id AS Id FROM tbl_staff WHERE tbl_staff.id < ALL( (SELECT tbl_staff.id FROM tbl_staff))" ); }