Browse Source

Updtate Demo

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
1d07cb493c
  1. 42
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/1_Query.cs
  2. 13
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/2_Update.cs
  3. 6
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/3_Insert.cs
  4. 3
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/6_ComplexModel.cs
  5. 16
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs
  6. 2
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/D_QueryableView.cs
  7. 43
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/E_Attribute.cs
  8. 119
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/F_VersionValidation.cs
  9. 64
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/G_Mapper.cs
  10. 3
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/Z_DemoBase.cs
  11. 6
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Models/ViewModelStudent.cs
  12. 4
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs

42
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/1_Query.cs

@ -40,8 +40,18 @@ namespace OrmTest.Demo
private static void Subqueryable()
{
var db = GetInstance();
var getAll11 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Max(s=>s.Id)==1).ToList();
var i = 0;
var sumflat2num = db.Queryable<Student, Student>((s1, s2) =>
new object[] { JoinType.Left, s1.Id == s2.Id })
.Select((s1, s2) => new Student
{ Id = SqlFunc.IsNull(SqlFunc.AggregateSum(SqlFunc.IIF(s1.Id ==1, s1.Id, s1.Id * -1)), 0) })
.First();
var getAll11 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Max(s=>s.Id)==i).ToList();
var getAll12 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Max(s => s.Id) == 1).ToList();
var getAll7 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Any()).ToList();
var getAll9 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Count()==1).ToList();
@ -193,6 +203,27 @@ namespace OrmTest.Demo
db.Ado.RollbackTran();
throw;
}
//async tran
var asyncResult = db.Ado.UseTranAsync(() =>
{
var beginCount = db.Queryable<Student>().ToList();
db.Ado.ExecuteCommand("delete student");
var endCount = db.Queryable<Student>().Count();
throw new Exception("error haha");
});
asyncResult.Wait();
var asyncCount = db.Queryable<Student>().Count();
//async
var asyncResult2 = db.Ado.UseTranAsync<List<Student>>(() =>
{
return db.Queryable<Student>().ToList();
});
asyncResult2.Wait();
}
private static void Group()
{
@ -241,7 +272,10 @@ namespace OrmTest.Demo
public static void Easy()
{
var db = GetInstance();
var getAll = db.Queryable<Student>().ToList();
var dbTime = db.GetDate();
var getAll = db.Queryable<Student>().Select<object>("*").ToList();
var getAll2 = db.Queryable<Student>().ToList();
var getRandomList = db.Queryable<Student>().OrderBy(it => SqlFunc.GetRandom()).ToList();
var getAllOrder = db.Queryable<Student>().OrderBy(it => it.Id).OrderBy(it => it.Name, OrderByType.Desc).ToList();
var getId = db.Queryable<Student>().Select(it => it.Id).ToList();
var getNew = db.Queryable<Student>().Where(it => it.Id == 1).Select(it => new { id = SqlFunc.IIF(it.Id == 0, 1, it.Id), it.Name, it.SchoolId }).ToList();
@ -250,6 +284,7 @@ namespace OrmTest.Demo
var getSingleOrDefault = db.Queryable<Student>().Where(it => it.Id == 1).Single();
var getFirstOrDefault = db.Queryable<Student>().First();
var getByWhere = db.Queryable<Student>().Where(it => it.Id == 1 || it.Name == "a").ToList();
var getByWhere2 = db.Queryable<Student>().Where(it => it.Id ==DateTime.Now.Year).ToList();
var getByFuns = db.Queryable<Student>().Where(it => SqlFunc.IsNullOrEmpty(it.Name)).ToList();
var sum = db.Queryable<Student>().Select(it => it.SchoolId).ToList();
var sum2 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Sum((st, sc) => sc.Id);
@ -511,6 +546,9 @@ namespace OrmTest.Demo
var s9 = db.Queryable<Student>().Select(it=>new Student() { Id=it.Id, TestId=1, Name=it.Name, CreateTime=it.CreateTime }).First();
var s10 = db.Queryable<Student>().Select(it => new Student() { Id = it.Id}).First();
//auto fill
var s11 = db.Queryable<Student, School>((st,sc)=>st.SchoolId==sc.Id).Select<ViewModelStudent3>().ToList();
}
private static void Sqlable()
{

13
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/2_Update.cs

@ -59,15 +59,24 @@ namespace OrmTest.Demo
//Column is null no update
db.Updateable(updateObj).Where(true).ExecuteCommand();
//sql
db.Updateable(updateObj).Where("id=@x",new { x="1"}).ExecuteCommand();
db.Updateable(updateObj).Where("id","=",1).ExecuteCommand();
var t12 = db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommandAsync();
t12.Wait();
//update one columns
var count = db.Updateable<Student>().UpdateColumns(it => it.SchoolId == 1).Where(it => it.Id == 1).ExecuteCommand();
var count = db.Updateable<Student>().UpdateColumns(it => it.SchoolId == it.SchoolId).Where(it => it.Id == it.Id+1).ExecuteCommand();
//update one columns
var count2 = db.Updateable<Student>().UpdateColumns(it => it.SchoolId == it.SchoolId+1).Where(it => it.Id == it.Id + 1).ExecuteCommand();
var dt = new Dictionary<string, object>();
dt.Add("id", 1);
dt.Add("name", null);
dt.Add("createTime", DateTime.Now);
var t66 = db.Updateable(dt).AS("student").With(SqlWith.UpdLock).ExecuteCommand();
}
}
}

6
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/3_Insert.cs

@ -59,6 +59,12 @@ namespace OrmTest.Demo
var t12 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").ExecuteReturnIdentityAsync();
t12.Wait();
var dt = new Dictionary<string, object>();
dt.Add("name", "1");
dt.Add("CreateTime", null);
var t66 = db.Insertable(dt).AS("student").ExecuteReturnIdentity();
}
}
}

3
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/6_ComplexModel.cs

@ -12,6 +12,7 @@ namespace OrmTest.Demo
public static void Init()
{
var db = GetInstance();
db.Insertable(new CMStudent() { SchoolId = 1, Name = "xx1" }).ExecuteCommand();
var students = db.Queryable<CMStudent>().ToList();
if (students != null)
{
@ -28,6 +29,8 @@ namespace OrmTest.Demo
}
}
}
db.Insertable(new CMStudent() { Name="xx" }).ExecuteCommand();
}
}

16
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs

@ -38,12 +38,22 @@ namespace OrmTest.Demo
{
var db = GetInstance();
List<IConditionalModel> conModels = new List<IConditionalModel>();
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Like, FieldValue = "1" });
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });//id=1
conModels.Add(new ConditionalModel() { FieldName = "Student.id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });//id=1
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Like, FieldValue = "1" });// id like '%1%'
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNullOrEmpty });
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.In,FieldValue="1,2,3" });
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NotIn, FieldValue = "1,2,3" });
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NoEqual, FieldValue = "1,2,3" });
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNot,FieldValue=null});// id is not null
conModels.Add(new ConditionalCollections() { ConditionalList=new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()// (id=1 or id=2 and id=1)
{
new KeyValuePair<WhereType, ConditionalModel>( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }),
new KeyValuePair<WhereType, ConditionalModel> (WhereType.Or,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
new KeyValuePair<WhereType, ConditionalModel> ( WhereType.And,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" })
}
});
var student = db.Queryable<Student>().Where(conModels).ToList();
}
@ -54,6 +64,8 @@ namespace OrmTest.Demo
var pageJoin = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
})
.Where(st => st.Id==1)
.Where(st => st.Id==2)
.Select((st, sc) => new { id = st.Id, name = sc.Name })
.MergeTable().Where(XXX => XXX.id == 1).OrderBy("name asc").ToList();// Prefix, is, not, necessary, and take the columns in select

2
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/D_QueryableView.cs

@ -7,7 +7,7 @@ using System.Text;
namespace OrmTest.Demo
{
public class D_QueryableViewn : DemoBase
public class QueryableView : DemoBase
{
public static void Init()
{

43
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/E_Attribute.cs

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlSugar;
namespace OrmTest.Demo
{
public class AttributeDemo : DemoBase
{
public static void Init()
{
var db = GetInstance();
AttributeTest a = new AttributeTest()
{
Name = "attr"
};
db.Insertable(a).AS("student").ExecuteCommand();
var list = db.Queryable<AttributeTest>().AS("student").ToList();
var list2 = db.Queryable<AttributeTest>().AS("student").Select(it => new AttributeTest() { Aid = it.Aid + 1,CreateTime=DateTime.Now,Name=it.Name }).ToList();
var s = new AttributeTest2() { Aid = 1,AName="a", CreateTime=DateTime.Now };
var count = db.Updateable(s).UpdateColumns(it=>new { it.CreateTime,it.AName }).Where(it=>it.Aid==100).ExecuteCommand();
}
public class AttributeTest
{
[SugarColumn(ColumnName = "Id")]
public int Aid { get; set; }
public string Name { get; set; }
[SugarColumn(IsOnlyIgnoreInsert = true)]
public DateTime CreateTime { get; set; }
}
[SugarTable("student")]
public class AttributeTest2
{
[SugarColumn(ColumnName = "Id")]
public int Aid { get; set; }
[SugarColumn(ColumnName = "Name")]
public string AName { get; set; }
[SugarColumn(IsOnlyIgnoreInsert = true)]
public DateTime CreateTime { get; set; }
}
}
}

119
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/F_VersionValidation.cs

@ -0,0 +1,119 @@
using OrmTest.Demo;
using OrmTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
public class VersionValidation : DemoBase
{
public static void Init()
{
TimestampDemo();
DateTimeDemo();
}
private static void TimestampDemo()
{
var db = GetInstance();
try
{
var data = new StudentVersion()
{
Id = db.Queryable<Student>().Select(it => it.Id).First(),
CreateTime = DateTime.Now,
Name = "",
};
db.Updateable(data).IgnoreColumns(it => new { it.Timestamp }).ExecuteCommand();
var time = db.Queryable<StudentVersion>().Where(it => it.Id == data.Id).Select(it => it.Timestamp).Single();
data.Timestamp = time;
//is ok
db.Updateable(data).IsEnableUpdateVersionValidation().IgnoreColumns(it => new { it.Timestamp }).ExecuteCommand();
//updated Timestamp change
//is error
db.Updateable(data).IsEnableUpdateVersionValidation().IgnoreColumns(it => new { it.Timestamp }).ExecuteCommand();
//IsEnableUpdateVersionValidation Types of support int or long or byte[](Timestamp) or Datetime
}
catch (Exception ex)
{
if (ex is SqlSugar.VersionExceptions)
{
Console.Write(ex.Message);
}
else
{
}
}
}
private static void DateTimeDemo()
{
var db = GetInstance();
try
{
var data = new StudentVersion2()
{
Id = db.Queryable<Student>().Select(it => it.Id).First(),
CreateTime = DateTime.Now,
Name = "",
};
db.Updateable(data).ExecuteCommand();
var time = db.Queryable<StudentVersion2>().Where(it => it.Id == data.Id).Select(it => it.CreateTime).Single();
data.CreateTime = time;
//is ok
db.Updateable(data).IsEnableUpdateVersionValidation().ExecuteCommand();
data.CreateTime = time.AddMilliseconds(-1);
//is error
db.Updateable(data).IsEnableUpdateVersionValidation().ExecuteCommand();
//IsEnableUpdateVersionValidation Types of support int or long or byte[](Timestamp) or Datetime
}
catch (Exception ex)
{
if (ex is SqlSugar.VersionExceptions)
{
Console.Write(ex.Message);
}
else
{
}
}
}
[SqlSugar.SugarTable("Student")]
public class StudentVersion
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreateTime { get; set; }
[SqlSugar.SugarColumn(IsEnableUpdateVersionValidation = true,IsOnlyIgnoreInsert=true)]
public byte[] Timestamp { get; set; }
}
[SqlSugar.SugarTable("Student")]
public class StudentVersion2
{
public int Id { get; set; }
public string Name { get; set; }
[SqlSugar.SugarColumn(IsEnableUpdateVersionValidation = true, IsOnlyIgnoreInsert = true)]
public DateTime CreateTime { get; set; }
}
}
}

64
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/G_Mapper.cs

@ -0,0 +1,64 @@
using OrmTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
public class Mapper : DemoBase
{
public static void Init()
{
var db = GetInstance();
//auto fill ViewModelStudent3
var s11 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
.Select<ViewModelStudent3>().ToList();
var s12 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select<ViewModelStudent3>()
.Mapper((it, cache) =>
{
var allSchools = cache.GetListByPrimaryKeys<School>(vmodel => vmodel.SchoolId);
//sql select shool where id (in(ViewModelStudent3[0].SchoolId , ViewModelStudent3[1].SchoolId...)
//Equal to allSchools
//var allSchools2= cache.Get(list =>
// {
// var ids=list.Select(i => it.SchoolId).ToList();
// return db.Queryable<School>().In(ids).ToList();
//});Complex writing metho
/*one to one*/
//Good performance
it.School = allSchools.FirstOrDefault(i => i.Id == it.SchoolId);
//Poor performance.
//it.School = db.Queryable<School>().InSingle(it.SchoolId);
/*one to many*/
it.Schools = allSchools.Where(i => i.Id == it.SchoolId).ToList();
/*C# syntax conversion*/
it.Name = it.Name == null ? "null" : it.Name;
}).ToList();
var s13 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select<ViewModelStudent3>()
.Mapper((it, cache) =>
{
it.Schools = db.Queryable<School>().Where(i => i.Id == it.SchoolId).ToList();
}).ToList();
}
}
}

3
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/DemoBase.cs → Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/Z_DemoBase.cs

@ -11,8 +11,7 @@ namespace OrmTest.Demo
public static SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
Console.WriteLine();

6
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Models/ViewModelStudent.cs

@ -17,6 +17,10 @@ namespace OrmTest.Models
}
public class ViewModelStudent3: Student
{
public string SchoolName { get; set; }
public string SchoolName { get; set; }
public string School_Name { get; set; }
public string ScId { get; set; }
public School School { get; set; }
public List<School> Schools { get; set; }
}
}

4
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs

@ -49,7 +49,9 @@ namespace OrmTest
OrmTest.Demo.MasterSlave.Init();
OrmTest.Demo.SharedConnection.Init();
OrmTest.Demo.ExtSqlFun.Init();
OrmTest.Demo.D_QueryableViewn.Init();
OrmTest.Demo.QueryableView.Init();
OrmTest.Demo.Mapper.Init();
//OrmTest.Demo.VersionValidation.Init();
}
}
}

Loading…
Cancel
Save