diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/BugTest/Bug1.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/BugTest/Bug1.cs new file mode 100644 index 000000000..353d4aa59 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/BugTest/Bug1.cs @@ -0,0 +1,101 @@ +using OrmTest.Demo; +using OrmTest.Models; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest.BugTest +{ + public class Bug1 + { + public static SqlSugarClient GetInstance() + { + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + InitKeyType = InitKeyType.Attribute, + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + IsAutoCloseConnection = true + }); + db.Aop.OnLogExecuting = (sql, pars) => + { + Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); + Console.WriteLine(); + }; + return db; + } + public void Init() + { + var communityId = ""; + var buildId = ""; + var unitId = ""; + var keyword = ""; + GetInstance().CodeFirst.InitTables(typeof(MainTable), typeof(SubTable)); + GetInstance().Queryable().Where(u => +(u.CommunityID == communityId || SqlFunc.IsNullOrEmpty(communityId)) && +(SqlFunc.Contains(u.BuildID, buildId) || SqlFunc.IsNullOrEmpty(buildId)) && +(SqlFunc.Contains(u.UnitID, unitId) || SqlFunc.IsNullOrEmpty(unitId)) && +(SqlFunc.Contains(u.RoomNumber, keyword) || SqlFunc.Contains(u.RoomerName, keyword) || +SqlFunc.Contains(u.HousePlace, keyword) || SqlFunc.Contains(u.UnitName, keyword) || +SqlFunc.Contains(u.BuildName, keyword) || SqlFunc.IsNullOrEmpty(keyword))) +.GroupBy(ru => new { ru.RoomNumber, ru.RoomID }) +.Select(ru => new +{ + RoomNumber = SqlFunc.AggregateMax(ru.RoomNumber), + CountRoomer = SqlFunc.AggregateCount(ru.RoomerName), + RoomID = SqlFunc.AggregateMax(ru.RoomID), + Owner = SqlFunc.Subqueryable().Where(r => r.RoomID == ru.RoomID && SqlFunc.Equals(r.RoomUserType, "业主") && SqlFunc.Equals(r.RoomUserType, "业主")).Select(s => s.RoomerName) +}).OrderBy((r) => r.RoomNumber, type: OrderByType.Desc).ToPageListAsync(1, 2).Wait(); + + GetInstance().Updateable().UpdateColumns(it => + new Student() + { + Name = "a".ToString(), + CreateTime = DateTime.Now.AddDays(-1) + + } + ).Where(it => it.Id == 1).ExecuteCommand(); + + + var list = GetInstance().Queryable((st, sc) => new object[] { + JoinType.Left,st.SchoolId==sc.Id&&st.CreateTime==DateTime.Now.AddDays(-1) + }) + .Where(st => st.Name == "jack").ToList(); + + + GetInstance().Updateable().Where(it => true).UpdateColumns(it => new BugStudent() { Float = 11 }).ExecuteCommand(); + var reslut= GetInstance().Queryable().ToList(); + } + + + [SugarTable("student")] + public class BugStudent + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string name { get; set; } + public float? Float { get; set; } + + } + public class MainTable + { + public string CommunityID { get; internal set; } + public string BuildID { get; internal set; } + public string UnitID { get; internal set; } + public string RoomNumber { get; internal set; } + public string HousePlace { get; internal set; } + public string BuildName { get; internal set; } + public string RoomID { get; internal set; } + public string UnitName { get; internal set; } + public string RoomerName { get; internal set; } + } + public class SubTable + { + public string RoomID { get; internal set; } + public string RoomUserType { get; internal set; } + public string RoomerName { get; internal set; } + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Config.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Config.cs index 7d87d0ee6..43f3df924 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Config.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Config.cs @@ -8,8 +8,8 @@ namespace OrmTest { public class Config { - public static string ConnectionString = "server=.;uid=sa;pwd=sasa;database=SqlSugar4XTest"; - public static string ConnectionString2 = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST"; - public static string ConnectionString3 = "server=.;uid=sa;pwd=sasa;database=sqlsugar4xtest"; + public static string ConnectionString = "server=.;uid=sa;pwd=@jhl85661501;database=SqlSugar4XTest"; + public static string ConnectionString2 = "server=.;uid=sa;pwd=@jhl85661501;database=SQLSUGAR4XTEST"; + public static string ConnectionString3 = "server=.;uid=sa;pwd=@jhl85661501;database=sqlsugar4xtest"; } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/1_Query.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/1_Query.cs index 1985fa315..c0dba7555 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/1_Query.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/1_Query.cs @@ -94,6 +94,27 @@ namespace OrmTest.Demo Id = SqlFunc.Subqueryable().Where(s => s.Id == it.Id).Select(s => s.Id) }).ToList(); + var getAll6 = db.Queryable().Select(it => + new + { + name = it.Name, + id = SqlFunc.Subqueryable().Where(s => s.Id == it.Id).Sum(s => (int)s.SchoolId) + }).ToList(); + + var getAll66 = db.Queryable().Select(it => + new + { + name = it.Name, + id = SqlFunc.Subqueryable().Where(s => s.Id == it.Id).Sum(s =>s.SchoolId.Value) + }).ToList(); + + var getAll666 = db.Queryable().Select(it => + new + { + name = it.Name, + id = SqlFunc.Subqueryable().Where(s => s.Id == it.Id).Min(s => s.Id) + }).ToList(); + } private static void Async() @@ -318,6 +339,12 @@ namespace OrmTest.Demo var getUnionAllList2 = db.UnionAll(db.Queryable(), db.Queryable()).ToList(); + var getUnionAllList3= db.UnionAll(db.Queryable() + .Select(it => new Student { Id =SqlFunc.ToInt32(1) ,Name=SqlFunc.ToString("2"), SchoolId = Convert.ToInt32(3) }) + , db.Queryable() + .Select(it => new Student { Id = SqlFunc.ToInt32(11) , Name = SqlFunc.ToString("22") , SchoolId=Convert.ToInt32(33)})) + .Select(it=>new Student() { Id=SqlFunc.ToInt32(111), Name = SqlFunc.ToString("222") }).ToList(); + var test1 = db.Queryable((st, sc) => st.SchoolId == sc.Id).Where(st=>st.CreateTime>SqlFunc.GetDate()).Select((st, sc) => SqlFunc.ToInt64(sc.Id)).ToList(); var test2 = db.Queryable((st, sc) => st.SchoolId == sc.Id) .Where(st => @@ -473,6 +500,13 @@ namespace OrmTest.Demo .OrderBy(st => st.Id) .Select(st => st) .ToList(); + + var subquery = db.Queryable().Where(it => it.Id == 1); + var subquery2 = db.Queryable(); + db.Queryable(subquery, subquery2, (st1, st2) => st1.Id == st2.Id).Select((st1,st2)=>new { + id=st1.Id, + name=st2.Name + }).ToList(); } public static void Funs() { diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/2_Update.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/2_Update.cs index e8c63fdbe..fe842df47 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/2_Update.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/2_Update.cs @@ -68,6 +68,11 @@ namespace OrmTest.Demo //update one columns var count = db.Updateable().UpdateColumns(it => it.SchoolId == it.SchoolId).Where(it => it.Id == it.Id+1).ExecuteCommand(); + var count1 = db.Updateable() + .UpdateColumnsIF(false,it => it.SchoolId == it.SchoolId)//ignore + .UpdateColumnsIF(true, it => it.SchoolId == 2).//ok + Where(it => it.Id == it.Id + 1).ExecuteCommand(); + //update one columns var count2 = db.Updateable().UpdateColumns(it => it.SchoolId == it.SchoolId+1).Where(it => it.Id == it.Id + 1).ExecuteCommand(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/4_Delete.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/4_Delete.cs index 3258548d1..5ca74d226 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/4_Delete.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/4_Delete.cs @@ -25,6 +25,15 @@ namespace OrmTest.Demo //by primary key array var t4 = db.Deleteable().In(new int[] { 1, 2 }).ExecuteCommand(); + var t41 = db.Deleteable().In(new int[] { 1, 2 }.Select(it=>it)).ExecuteCommand(); + var t42 = db.Deleteable().In(new int[] { 1, 2 }.AsEnumerable()).ExecuteCommand(); + + + //by exp key array + var t44 = db.Deleteable().In(it=>it.SchoolId,new int[] { 1, 2 }).ExecuteCommand(); + var t441 = db.Deleteable().In(it => it.SchoolId,new int[] { 1, 2 }.Select(it => it)).ExecuteCommand(); + var t442 = db.Deleteable().In(it => it.SchoolId,new int[] { 1, 2 }.AsEnumerable()).ExecuteCommand(); + var t443 = db.Deleteable().In(it => it.SchoolId, new int[] { 1, 2 }.ToList()).ExecuteCommand(); //by expression id>1 and id==1 var t5 = db.Deleteable().Where(it => it.Id > 1).Where(it => it.Id == 1).ExecuteCommand(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/5_CodeFirst.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/5_CodeFirst.cs index 965991457..de002e3fe 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/5_CodeFirst.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/5_CodeFirst.cs @@ -6,14 +6,15 @@ using System.Text; namespace OrmTest.Demo { + [SugarTable("CodeTable", " table CodeTable")] public class CodeTable { - [SugarColumn(IsNullable =false ,IsPrimaryKey =true,IsIdentity =true)] + + [SugarColumn(IsNullable =false ,IsPrimaryKey =true,IsIdentity =true,ColumnDescription ="XXhaha primary key!!")] public int Id { get; set; } [SugarColumn(Length = 21,OldColumnName = "Name2")] public string Name{ get; set; } - [SugarColumn(IsNullable = true,Length =10)] public string IsOk { get; set; } public Guid Guid { get; set; } [SugarColumn(ColumnDataType ="int")] @@ -49,7 +50,7 @@ namespace OrmTest.Demo //db.CodeFirst.BackupTable().InitTables(typeof(CodeTable),typeof(CodeTable2)); //No backup table - db.CodeFirst.InitTables(typeof(CodeTable),typeof(CodeTable2)); + db.CodeFirst.SetStringDefaultLength(10).InitTables(typeof(CodeTable),typeof(CodeTable2)); } } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs index 7ffd25e08..8107f72c6 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs @@ -20,6 +20,26 @@ namespace OrmTest.Demo SelectMerge(); ConditionalModel(); JoinExp(); + Clone(); + } + + private static void Clone() + { + var db = GetInstance(); + var qy = db.Queryable().Where(it => 1 == 1); + + var list1 = qy.Clone().Where(it => it.Id == 1).ToList(); + var list2 = qy.Clone().Where(it => it.Id == 2).ToList(); + + + + var qy2 = db.Queryable((st,sc)=>new object[]{ + JoinType.Left,st.SchoolId==sc.Id + }).Where((st,sc)=>st.Id == 1); + + var join0 = qy2.Clone().Where((st, sc) => sc.Id == 222).Select(st=>st.Id).ToList(); + var join1 = qy2.Clone().Where((st,sc) => st.Id== 1111).ToList(); + var join2 = qy2.Clone().Where((st,sc)=>sc.Id==222).ToList(); } private static void JoinExp() diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/9_Aop.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/9_Aop.cs index 7a1dbc3db..d5c6bf377 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/9_Aop.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/9_Aop.cs @@ -1,4 +1,5 @@ -using SqlSugar; +using OrmTest.Models; +using SqlSugar; using System; using System.Collections.Generic; using System.Linq; @@ -12,9 +13,11 @@ namespace OrmTest.Demo public static void Init() { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true }); + + db.Aop.OnLogExecuted = (sql, pars) => { - + Console.Write("time:" + db.Ado.SqlExecutionTime.ToString()); }; db.Aop.OnLogExecuting = (sql, pars) => { @@ -22,11 +25,11 @@ namespace OrmTest.Demo }; db.Aop.OnError = (exp) => { - + }; db.Aop.OnExecutingChangeSql = (sql, pars) => { - return new KeyValuePair(sql,pars); + return new KeyValuePair(sql, pars); }; db.Queryable().ToList(); @@ -38,9 +41,47 @@ namespace OrmTest.Demo catch (Exception) { - + } - } -} + + //diff log demo + + db.Aop.OnDiffLogEvent = it => + { + var editBeforeData = it.BeforeData; + var editAfterData = it.AfterDate; + var sql = it.Sql; + var parameter = it.Parameters; + var data = it.BusinessData; + }; + + + var id = db.Insertable(new Student() { Name = "beforeName" }) + .EnableDiffLogEvent(new { title="add student"}) + .ExecuteReturnIdentity(); + + + db.Updateable(new Student() + { + Id = id, + CreateTime = DateTime.Now, + Name = "afterName", + SchoolId = 2 + }) + .EnableDiffLogEvent(new { title = "update Student", Modular = 1, Operator = "admin" }) + .ExecuteCommand(); + + + db.Deleteable(id) + .EnableDiffLogEvent(new { title = "delete student" }) + .ExecuteCommand(); + + + //primary key guid + db.Insertable(new DataTestInfo2() { Bool1=true, Bool2=false, PK=Guid.NewGuid(), Text1="a" }) + .EnableDiffLogEvent(new { title = "add DataTestInfo2" }) + .ExecuteReturnIdentity(); + } + } } \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/H_ExtEntity.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/H_ExtEntity.cs new file mode 100644 index 000000000..f50d5698b --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/H_ExtEntity.cs @@ -0,0 +1,58 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; + +namespace OrmTest.Demo +{ + /// + /// mapping ef attribute + /// + public class ExtEntity: DemoBase + { + public static void Init() + { + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + IsAutoCloseConnection = true, + ConfigureExternalServices=new ConfigureExternalServices() { + EntityService = (property, column) => { + if (property.Name == "xxx") {// by name ignore column + column.IsIgnore = true; + } + var attributes = property.GetCustomAttributes(true);//get all attributes + + if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey + { + column.IsPrimarykey = true; + } + }, + EntityNameService = (type,entity) => { + var attributes = type.GetCustomAttributes(true); + if (attributes.Any(it => it is TableAttribute)) + { + entity.DbTableName = (attributes.First(it => it is TableAttribute)as TableAttribute).Name; + } + } + } + }); + + var sql=db.Queryable().ToList(); + var sql2 = db.Insertable(new StudentTest()).ExecuteCommand(); + } + + } + + [Table("student")]//default + public class StudentTest { + + [Key] + public string Id { get; set; } + public string xxx { get; set; } + public string Name { get; set; } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs index c8c7421a7..a4176b2d2 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs @@ -17,6 +17,7 @@ namespace OrmTest { static void Main(string[] args) { + // /***Unit Test***/ new Select(1).Init(); new Field(1).Init(); @@ -36,22 +37,26 @@ namespace OrmTest new SqlSugarPerformance(100).Select(); /***Demo***/ - OrmTest.Demo.Query.Init(); - OrmTest.Demo.Insert.Init(); - OrmTest.Demo.Delete.Init(); - OrmTest.Demo.Update.Init(); - OrmTest.Demo.DbFirst.Init(); - OrmTest.Demo.JoinSql.Init(); - OrmTest.Demo.Filter.Init(); - OrmTest.Demo.ComplexModel.Init(); - OrmTest.Demo.CodeFirst.Init(); - OrmTest.Demo.Aop.Init(); - OrmTest.Demo.MasterSlave.Init(); - OrmTest.Demo.SharedConnection.Init(); - OrmTest.Demo.ExtSqlFun.Init(); - OrmTest.Demo.QueryableView.Init(); - OrmTest.Demo.Mapper.Init(); - //OrmTest.Demo.VersionValidation.Init(); + Demo.Query.Init(); + Demo.Insert.Init(); + Demo.Delete.Init(); + Demo.Update.Init(); + Demo.DbFirst.Init(); + Demo.JoinSql.Init(); + Demo.Filter.Init(); + Demo.ComplexModel.Init(); + Demo.CodeFirst.Init(); + Demo.Aop.Init(); + Demo.MasterSlave.Init(); + Demo.SharedConnection.Init(); + Demo.ExtSqlFun.Init(); + Demo.QueryableView.Init(); + Demo.AttributeDemo.Init(); + Demo.Mapper.Init(); + Demo.ExtEntity.Init(); + + /***BUG repair test***/ + new BugTest.Bug1().Init(); } } }