- 修复 SqlServer2005/2008 Skip 问题(未设置 Take 时);

This commit is contained in:
2881099
2022-07-14 14:01:40 +08:00
parent 9088da180c
commit 50b7d5307d
6 changed files with 103 additions and 26 deletions

View File

@ -29,21 +29,21 @@ namespace FreeSql.Tests.DataContext.SqlServer
private void ClearDataBase()
{
var dataTables = SqlServer.DbFirst.GetTablesByDatabase();
if (dataTables.Any(item => item.Name == "TopicAddField" && item.Schema == "dbo2"))
{
SqlServer.Ado.ExecuteNonQuery("TRUNCATE TABLE dbo2.TopicAddField ");
SqlServer.Ado.ExecuteNonQuery("DROP TABLE dbo2.TopicAddField");
SqlServer.Ado.ExecuteNonQuery("DROP SCHEMA dbo2");
}
//var dataTables = SqlServer.DbFirst.GetTablesByDatabase();
//if (dataTables.Any(item => item.Name == "TopicAddField" && item.Schema == "dbo2"))
//{
// SqlServer.Ado.ExecuteNonQuery("TRUNCATE TABLE dbo2.TopicAddField ");
// SqlServer.Ado.ExecuteNonQuery("DROP TABLE dbo2.TopicAddField");
// SqlServer.Ado.ExecuteNonQuery("DROP SCHEMA dbo2");
//}
var tempTables = new string[] { "cccccdddwww", "song", "tag", "Song_tag", "tb_alltype", "tb_topic", "tb_topic22",
"tb_topic22211", "tb_topic111333", "TestTypeInfo", "TestTypeInfo333", "TestTypeParentInfo",
"TestTypeParentInfo23123", "xxdkdkdk1222", "xxx"};
foreach (var tempTable in tempTables)
{
//DeleteTmpTable(dataTables, tempTable);
}
//var tempTables = new string[] { "cccccdddwww", "song", "tag", "Song_tag", "tb_alltype", "tb_topic", "tb_topic22",
// "tb_topic22211", "tb_topic111333", "TestTypeInfo", "TestTypeInfo333", "TestTypeParentInfo",
// "TestTypeParentInfo23123", "xxdkdkdk1222", "xxx"};
//foreach (var tempTable in tempTables)
//{
// //DeleteTmpTable(dataTables, tempTable);
//}
}
private void DeleteTmpTable(List<DatabaseModel.DbTableInfo> dbTables, string deleteTableName, string schemaName = "dbo")

View File

@ -1,4 +1,4 @@
using FreeSql.DataAnnotations;
using FreeSql.DataAnnotations;
using FreeSql.Tests.DataContext.SqlServer;
using SaleIDO.Entity.Storeage;
using System;
@ -79,6 +79,7 @@ namespace FreeSql.Tests.SqlServer
dicRet = fsql.InsertDict(diclist).AsTable("table1dict").NoneParameter().ExecuteInserted();
dicRet = fsql.DeleteDict(diclist).AsTable("table1dict").ExecuteDeleted();
var sss = fsql.InsertOrUpdateDict(dic).AsTable("table1");
sql1 = fsql.InsertOrUpdateDict(dic).AsTable("table1").WherePrimary("id").ToSql();
sql2 = fsql.InsertOrUpdateDict(diclist).AsTable("table1").WherePrimary("id").ToSql();

View File

@ -1,9 +1,13 @@
using FreeSql.DataAnnotations;
using FreeSql.Internal.CommonProvider;
using FreeSql.SqlServer;
using FreeSql.Tests.DataContext.SqlServer;
using NetTaste;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Xunit;
namespace FreeSql.Tests.SqlServer
@ -967,8 +971,61 @@ ORDER BY newid()", t1);
[Fact]
public void Skip_Offset()
{
var sql = select.Offset(10).Limit(10).ToList();
var list = select.Offset(10).Limit(10).ToList();
var sqlSkip = select.Skip(10).ToSql();
Assert.Equal(@"SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime]
FROM [tb_topic22] a
ORDER BY a.[Id]
OFFSET 10 ROW", sqlSkip);
list = select.Skip(10).ToList();
using (var fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
.UseAutoSyncStructure(true)
.UseMonitorCommand(
cmd => Trace.WriteLine("\r\n线程" + Thread.CurrentThread.ManagedThreadId + ": " + cmd.CommandText) //监听SQL命令对象在执行前
//, (cmd, traceLog) => Console.WriteLine(traceLog)
)
.Build())
{
var commUtils = (fsql.Select<object>() as Select0Provider)._commonUtils as SqlServerUtils;
commUtils.ServerVersion = 9;
sqlSkip = fsql.Select<Topic>().Skip(10).ToSql();
Assert.Equal(@"WITH t AS ( SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime], ROW_NUMBER() OVER(ORDER BY a.[Id]) AS __rownum__
FROM [tb_topic22] a ) SELECT t.* FROM t where __rownum__ > 10", sqlSkip);
list = fsql.Select<Topic>().Skip(10).ToList();
sqlSkip = fsql.Select<Topic>().Limit(10).ToSql();
Assert.Equal(@"SELECT TOP 10 a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime]
FROM [tb_topic22] a
ORDER BY a.[Id]", sqlSkip);
list = fsql.Select<Topic>().Limit(10).ToList();
sqlSkip = fsql.Select<Topic>().Skip(10).Limit(10).ToSql();
Assert.Equal(@"WITH t AS ( SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime], ROW_NUMBER() OVER(ORDER BY a.[Id]) AS __rownum__
FROM [tb_topic22] a ) SELECT t.* FROM t where __rownum__ between 11 and 20", sqlSkip);
list = fsql.Select<Topic>().Skip(10).Limit(10).ToList();
sqlSkip = fsql.Select<Topic>().Skip(10).OrderBy(a => a.Title).ToSql();
Assert.Equal(@"WITH t AS ( SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime], ROW_NUMBER() OVER(ORDER BY a.[Title]) AS __rownum__
FROM [tb_topic22] a ) SELECT t.* FROM t where __rownum__ > 10", sqlSkip);
list = fsql.Select<Topic>().Skip(10).OrderBy(a => a.Title).ToList();
sqlSkip = fsql.Select<Topic>().Limit(10).OrderBy(a => a.Title).ToSql();
Assert.Equal(@"SELECT TOP 10 a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime]
FROM [tb_topic22] a
ORDER BY a.[Title]", sqlSkip);
list = fsql.Select<Topic>().Limit(10).OrderBy(a => a.Title).ToList();
sqlSkip = fsql.Select<Topic>().Skip(10).Limit(10).OrderBy(a => a.Title).ToSql();
Assert.Equal(@"WITH t AS ( SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime], ROW_NUMBER() OVER(ORDER BY a.[Title]) AS __rownum__
FROM [tb_topic22] a ) SELECT t.* FROM t where __rownum__ between 11 and 20", sqlSkip);
list = fsql.Select<Topic>().Skip(10).Limit(10).OrderBy(a => a.Title).ToList();
}
}
[Fact]
public void Take_Limit()
{