From b9da3f285dd6083b883c5f06da6126157f0c54c7 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 26 Feb 2022 16:18:42 +0800 Subject: [PATCH] Update access --- .../AccessTest/Demo/Demo1_Queryable.cs | 6 ++-- .../Access/SqlBuilder/AccessQueryBuilder.cs | 35 +++++++++++++++---- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Src/Asp.Net/AccessTest/Demo/Demo1_Queryable.cs b/Src/Asp.Net/AccessTest/Demo/Demo1_Queryable.cs index 997d02414..6efdd3730 100644 --- a/Src/Asp.Net/AccessTest/Demo/Demo1_Queryable.cs +++ b/Src/Asp.Net/AccessTest/Demo/Demo1_Queryable.cs @@ -100,7 +100,9 @@ namespace OrmTest Console.WriteLine("#### Examples Start ####"); var db = GetInstance(); var dbTime = db.GetDate(); - var getAll = db.Queryable().Where(it=> SqlFunc.EqualsNull(it.Name,null)).ToList(); + var getAll = db.Queryable().ToList(); + var getTake = db.Queryable().Take(2).ToList(); + var getSkip = db.Queryable().Skip(2).ToList(); var getOrderBy = db.Queryable().OrderBy(it => it.Name,OrderByType.Desc).ToList(); var getOrderBy2 = db.Queryable().OrderBy(it => it.Id).OrderBy(it => it.Name, OrderByType.Desc).ToList(); var getOrderBy3 = db.Queryable().OrderBy(it =>new { it.Name,it.Id}).ToList(); @@ -135,7 +137,7 @@ namespace OrmTest //test09.Wait(); int c = 0; - var test10 = db.Queryable().ToPageList(1, 2, ref c); + var test10 = db.Queryable().OrderBy(it=>it.Id).ToPageList(3,5, ref c); var test11 = db.Queryable().GroupBy(it=>new { it.CreateTime.Year }).Select(it=>it.CreateTime.Year).ToList(); // var test12 = db.Queryable().GroupBy(it => it.CreateTime.Date ).Select(it => it.CreateTime.Date).ToList(); //var test13 = db.Queryable().GroupBy(it => new { it.CreateTime.Date ,it.CreateTime.Year,it.CreateTime.Minute }) diff --git a/Src/Asp.Net/SqlSugar.Access/Access/SqlBuilder/AccessQueryBuilder.cs b/Src/Asp.Net/SqlSugar.Access/Access/SqlBuilder/AccessQueryBuilder.cs index 069ccf6d5..9e9ef4210 100644 --- a/Src/Asp.Net/SqlSugar.Access/Access/SqlBuilder/AccessQueryBuilder.cs +++ b/Src/Asp.Net/SqlSugar.Access/Access/SqlBuilder/AccessQueryBuilder.cs @@ -26,12 +26,14 @@ namespace SqlSugar.Access if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY now() "; if (this.PartitionByValue.HasValue()) { - this.OrderByValue = this.PartitionByValue + this.OrderByValue; + throw new Exception("sqlite no support partition by"); } var isFirst = (Skip == 0 || Skip == null) && Take == 1 && DisableTop == false; var isRowNumber = (Skip != null || Take != null) && !isFirst; var isPage = isRowNumber; isRowNumber = false; + var oldSkip = Skip; + var oldTake = Take; Skip = null; Take = null; var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString); @@ -44,12 +46,7 @@ namespace SqlSugar.Access var result = isFirst ? sql.ToString() : ToPageSql(sql.ToString(), this.Take, this.Skip); if (ExternalPageIndex > 0) { - if (externalOrderBy.IsNullOrEmpty()) - { - externalOrderBy = " ORDER BY now() "; - } - result = string.Format("SELECT *,ROW_NUMBER() OVER({0}) AS RowIndex2 FROM ({1}) ExternalTable ", GetExternalOrderBy(externalOrderBy), result); - result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true); + throw new Exception("sqlite no support partition by"); } this.OrderByValue = oldOrderBy; if (!string.IsNullOrEmpty(this.Offset)) @@ -57,10 +54,34 @@ namespace SqlSugar.Access if (this.OrderByValue.IsNullOrEmpty()) { result += " ORDER BY now() "; + this.OrderByValue = " ORDER BY now() "; } result += this.Offset; } result = GetSqlQuerySql(result); + if (isPage) + { + var colums=this.Context.EntityMaintenance.GetEntityInfo(this.EntityType).Columns; + if (!colums.Any(x => x.IsPrimarykey)) + { + throw new Exception("sqlite page need primary key , entity name: "+ this.EntityName); + } + var pkName =this.Builder.GetTranslationColumnName(colums.Where(x => x.IsPrimarykey).First().DbColumnName); + var takeSql = $@" SELECT { (oldTake == null ? "" : ("Top " + oldTake)) } + *FROM({ result}) ACCTABLE1"; + var skipSql = $@" + WHERE {pkName} NOT IN + (SELECT { (oldSkip == null ? "" : ("Top " + oldSkip))} {pkName} FROM ({result}) ACCTABLE2 { this.OrderByValue })"; + if (oldSkip == 0) + { + result = takeSql; + } + else + { + result = takeSql+skipSql; + } + + } return result; } }