Browse Source

Update Demo

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
171ab25db5
  1. 94
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/1_Query.cs
  2. 50
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/2_Update.cs
  3. 2
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/3_Insert.cs
  4. 3
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/4_Delete.cs
  5. 12
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/5_CodeFirst.cs
  6. 4
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/6_ComplexModel.cs
  7. 2
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/9_Aop.cs
  8. 11
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/G_Mapper.cs
  9. 40
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/I_InsertOrUpdate.cs
  10. 61
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/J_Debugger.cs
  11. 2
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/Z_DemoBase.cs
  12. 1
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs

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

@ -114,6 +114,31 @@ namespace OrmTest.Demo
name = it.Name,
id = SqlFunc.Subqueryable<Student>().Where(s => s.Id == it.Id).Min(s => s.Id)
}).ToList();
string name = "a";
var getAll6666 = db.Queryable<Student>().Select(it =>
new
{
name = it.Name,
id = SqlFunc.Subqueryable<Student>().WhereIF(!string.IsNullOrEmpty(name), s=>s.Id==1).Min(s => s.Id)
}).ToList();
name = null;
var getAll66666 = db.Queryable<Student>().Select(it =>
new
{
name = it.Name,
id = SqlFunc.Subqueryable<Student>().WhereIF(!string.IsNullOrEmpty(name), s => s.Id == 1).Min(s => s.Id)
}).ToList();
var getAll666666 = db.Queryable<Student>()
.Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Any())
.Select(it =>
new
{
name = it.Name,
id = SqlFunc.Subqueryable<Student>().Where(s=>s.Id==SqlFunc.Subqueryable<School>().Where(y=>y.Id==s.SchoolId).Select(y=>y.Id)).Min(s => s.Id),
id2 = SqlFunc.Subqueryable<Student>().Where(s => s.Id == SqlFunc.Subqueryable<School>().Where(y => y.Id == s.SchoolId).Select(y => y.Id)).Min(s => s.Id)
}).ToList();
}
@ -140,7 +165,7 @@ namespace OrmTest.Demo
var student1 = db.Queryable<Student>().InSingle(1);
//get SimpleClient
var sdb = db.SimpleClient;
var sdb = db.GetSimpleClient();
var student2 = sdb.GetById<Student>(1);
sdb.DeleteById<Student>(1);
sdb.Insert(new Student() { Name = "xx" });
@ -285,6 +310,9 @@ namespace OrmTest.Demo
var t1 = db.Ado.SqlQuery<string>("select 'a'");
var t2 = db.Ado.GetInt("select 1");
var t3 = db.Ado.GetDataTable("select 1 as id");
var t4 = db.Ado.GetScalar("select * from student where id in (@id) ", new { id = new List<int>() { 1, 2, 3 } });
var t5 = db.Ado.GetScalar("select * from student where id in (@id) ", new { id = new int [] { 1, 2, 3 } });
var t6= db.Ado.GetScalar("select * from student where id in (@id) ", new SugarParameter("@id", new int[] { 1, 2, 3 }));
db.Ado.CommitTran();
var t11 = db.Ado.SqlQuery<Student>("select * from student");
//more
@ -295,7 +323,11 @@ namespace OrmTest.Demo
var db = GetInstance();
var dbTime = db.GetDate();
var getAll = db.Queryable<Student>().Select<object>("*").ToList();
var getAll2 = db.Queryable<Student>().ToList();
var getAll2 = db.Queryable<Student>().Select(it=>it.Name.Substring(0,4)).ToList();
var getAll22 = db.Queryable<Student>().ToDataTable();
var getAll222 = db.Queryable<Student>().ToJson();
var getAll2222 = db.Queryable<Student>().OrderBy(it=>it.Name.Length).ToJson();
var getAll3 = db.Queryable<Student>().OrderBy(it => new { it.Id, it.Name }).GroupBy(it => new { it.Id, it.Name }).Select<object>("id").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();
@ -356,6 +388,52 @@ namespace OrmTest.Demo
var test4 = db.Queryable<DataTestInfo2>().Select(it => new { b=it.Bool1 }).ToSql();
DateTime? result = DateTime.Now;
var test5 = db.Queryable<Student>().Where(it=>it.CreateTime> result.Value.Date).ToList();
var test6 = db.Queryable<DataTestInfo2>().Where(it => SqlFunc.HasValue(it.Bool2)==true && SqlFunc.HasValue(it.Bool2)==true).ToList();
var test7 = db.Queryable<DataTestInfo2>().Where(it => SqlFunc.HasValue(it.Bool1) && SqlFunc.HasValue(it.Bool1)).ToList();
var test8 = db.Queryable<Student>().Where(it => SqlFunc.HasValue(it.SchoolId) && SqlFunc.HasValue(it.SchoolId)).ToList();
bool? b = false;
var test9 = db.Queryable<DataTestInfo2>().Where(it => it.Bool1 == b).ToList();
var test10 = db.Queryable<Student>(db.Queryable<Student>().Select(it => new Student() { Name = it.Name.Substring(0, 1) })).GroupBy(it => it.Name).ToList(); ;
var test11 = db.Queryable<Student>().Distinct().ToList();
var test12 = db.Queryable<Student>().Distinct().Select(it=>new Student{ Name=it.Name }).ToList();
var test13 = db.Queryable<Student>().Where(it=>DateTime.Parse("2014-1-1")==DateTime.Now).Where(it => Boolean.Parse("true") ==true).ToList();
var test14 = db.Queryable<DataTestInfo2>().Where(it =>Convert.ToBoolean(it.Bool1)).ToList();
var test15 = db.Queryable<DataTestInfo2>().Where(it => it.Bool2.Value&&it.Bool1).ToList();
var test16 = db.Queryable<DataTestInfo2>().Where(it => !it.Bool2.Value && !it.Bool1).ToList();
var test17 = db.Queryable<DataTestInfo2>().Where(it => it.Bool1 && it.Bool1).ToList();
var test18 = db.Queryable<Student>().Where(it => it.SchoolId.HasValue&&it.SchoolId.HasValue).ToList();
var test19 = db.Queryable<Student>().Where(it => it.SchoolId.HasValue && it.SchoolId.HasValue&&it.SchoolId.HasValue).ToList();
var test20 = db.Queryable<Student>().Where(it => it.SchoolId.HasValue && SqlFunc.IsNullOrEmpty(it.Name)).ToList();
var test21 = db.Queryable<Student>().Where(it => !it.SchoolId.HasValue && it.Name == "").ToList();
var test22 = db.Queryable<Student>().Where(it => !it.SchoolId.HasValue && it.SchoolId.HasValue).ToList();
var test23 = db.Queryable<Student>().Where(it => !(it.Id==1) && it.Name=="").ToList();
var test24 = db.Queryable<Student>().Where(it => string.IsNullOrEmpty("a")).Where(it=>string.IsNullOrEmpty(it.Name)).ToList();
var test25 = db.Queryable<Student>().Where(it => SqlFunc.IIF(it.Id==0,1,2)==1).ToList();
var test26 = db.Queryable<Student>().Where(it => (it.Name==null?2:3)==1 )
.ToList();
var test27 = db.Queryable<Student>().Select(x => new {
name=x.Name==null?"1":"2"
}).ToList();
var test28 = db.Queryable<Student>().Select(x => new Student{
Name = x.Name == null ? "1" : "2"
}).ToList();
var test29 = db.Queryable<Student>().Where(it=>it.Id%1==0).ToList();
var test30 = db.Queryable<Student>().Select(x => new Student
{
Name = x.Name ?? "a"
}).ToList();
var test31 = db.Queryable<Student>().Where(it=>(it.Name??"a")=="a").ToList();
var test32 = db.Queryable<Student>().Where(it => it.Name == null ? true : false).ToList();
var test33 = db.Queryable<Student>().Where(it => SqlFunc.IIF(it.Name==null,true ,false)).ToList();
var test34 = db.Queryable<Student>().Where(it => SqlFunc.IIF(it.Name == null||1==1, true, false)).ToList();
var test35 = db.Queryable<Student>().Where(it =>it.Id==1&&SqlFunc.IF(it.Id==1).Return(true).End(false)).ToList();
var test36 = db.Queryable<Student>().Where(it => it.Id == 1 &&it.SchoolId.HasValue).ToList();
var test37 = db.Queryable<Student>().Where(it => it.Id == 1 && SqlFunc.IIF(it.Id == 1, true, false)).ToList();
var test38 = db.Queryable<Student>().Where(it => it.Id == 1 && SqlFunc.IIF(it.Id == 1, true, false)==true).ToList();
var test39 = db.Queryable<Student>().Where(it => it.Id == 1 && (it.Id==1?true:false)).ToList();
var test40 = db.Queryable<Student>().Where(it => it.Id==1&&Convert.ToBoolean("true")).ToList();
var test41 = db.Queryable<Student>().Where(it => it.Id==((it.Id==1?2:3)==2?1:2)).ToList();
}
public static void Page()
{
@ -507,6 +585,18 @@ namespace OrmTest.Demo
id=st1.Id,
name=st2.Name
}).ToList();
var q1 = db.Queryable<Student>().Select(it => new Student()
{
Id = it.Id,
Name = "a"
});
var q2 = db.Queryable<Student>().Select(it => new Student()
{
Id = it.Id,
Name = "b"
});
var unionAllList = db.Union(q1, q2).ToList();
}
public static void Funs()
{

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

@ -57,7 +57,7 @@ namespace OrmTest.Demo
//Update Student set Name='jack' Where Id=1
//Column is null no update
db.Updateable(updateObj).Where(true).ExecuteCommand();
db.Updateable(updateObj).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommand();
//sql
db.Updateable(updateObj).Where("id=@x",new { x="1"}).ExecuteCommand();
@ -81,7 +81,53 @@ namespace OrmTest.Demo
dt.Add("id", 1);
dt.Add("name", null);
dt.Add("createTime", DateTime.Now);
var t66 = db.Updateable(dt).AS("student").With(SqlWith.UpdLock).ExecuteCommand();
var t66 = db.Updateable(dt).AS("student").WhereColumns("id").With(SqlWith.UpdLock).ExecuteCommand();
var dt2 = new Dictionary<string, object>();
dt2.Add("id", 2);
dt2.Add("name", null);
dt2.Add("createTime", DateTime.Now);
var dtList = new List<Dictionary<string, object>>();
dtList.Add(dt);
dtList.Add(dt2);
var t666 = db.Updateable(dtList).AS("student").WhereColumns("id").With(SqlWith.UpdLock).ExecuteCommand();
var t20 = db.Updateable<Student>().UpdateColumns(p => new Student()
{
SchoolId = SqlFunc.IIF(p.Id == 1, 2, 3)
}).Where(p => p.Id == 10000).ExecuteCommand();
var t21= db.Updateable<Student>().UpdateColumns(p => new Student()
{
SchoolId = SqlFunc.IF(p.Id==1).Return(1).End(p.Id)
}).Where(p => p.Id == 10000).ExecuteCommand();
var t22= db.Updateable<Student>().UpdateColumns(p => new Student()
{
SchoolId = SqlFunc.Subqueryable<Student>().Where(s=>s.SchoolId==p.Id).Select(s=>s.Id)
}).Where(p => p.Id == 10000).ExecuteCommand();
var t23= db.Updateable<Student>(new Student() { })
.Where(p => p.SchoolId == SqlFunc.Subqueryable<Student>().Where(s => s.SchoolId == p.Id).Select(s => s.Id)).ExecuteCommand();
var t24 = db.Updateable(new Student() { }).WhereColumns(it=>it.CreateTime).ExecuteCommand();
var t25 = db.Updateable(new Student() { }).UpdateColumns(it=> new { it.Name,it.CreateTime}).WhereColumns(it => it.CreateTime).ExecuteCommand();
var t26 = db.Updateable(new List<Student>() { new Student() { }, new Student() { } }).UpdateColumns(it => new { it.Name, it.CreateTime }).WhereColumns(it => it.CreateTime).ExecuteCommand();
db.Updateable<Student>().UpdateColumns(it => new Student { SchoolId = GeneratePassword(2, 1), Name =SqlFunc.ToString(it.Name), CreateTime = DateTime.Now.AddDays(1) }).Where(it => it.Id == 1).ExecuteCommand();
}
private static int GeneratePassword(int v1, int v2)
{
return 1;
}
}
}

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

@ -48,7 +48,7 @@ namespace OrmTest.Demo
//Insert List<T>
var insertObjs = new List<Student>();
for (int i = 0; i < 1000; i++)
for (int i = 0; i < 10; i++)
{
insertObjs.Add(new Student() { Name = "name" + i });
}

3
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/4_Delete.cs

@ -39,6 +39,9 @@ namespace OrmTest.Demo
var t5 = db.Deleteable<Student>().Where(it => it.Id > 1).Where(it => it.Id == 1).ExecuteCommand();
var t6 = db.Deleteable<Student>().AS("student").Where(it => it.Id > 1).Where(it => it.Id == 1).ExecuteCommandAsync();
var t7 = db.Deleteable<Student>().Where(p => p.SchoolId == SqlFunc.Subqueryable<School>().Where(s => s.Id == p.SchoolId).Select(s => s.Id)).ExecuteCommand();
t6.Wait();
}
}

12
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/5_CodeFirst.cs

@ -34,6 +34,15 @@ namespace OrmTest.Demo
[SugarColumn(IsIgnore =true)]
public string TestId { get; set; }
}
public class CodeTable3
{
[SugarColumn(IsPrimaryKey =true)]
public int Id { get; set; }
[SugarColumn(IsPrimaryKey = true)]
public string Name { get; set; }
[SugarColumn(IsIgnore = true)]
public string TestId { get; set; }
}
public class CodeFirst : DemoBase
{
public static void Init()
@ -51,6 +60,9 @@ namespace OrmTest.Demo
//No backup table
db.CodeFirst.SetStringDefaultLength(10).InitTables(typeof(CodeTable),typeof(CodeTable2));
db.CodeFirst.InitTables(typeof(CodeTable3));
}
}
}

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

@ -13,7 +13,7 @@ namespace OrmTest.Demo
{
var db = GetInstance();
db.Insertable(new CMStudent() { SchoolId = 1, Name = "xx1" }).ExecuteCommand();
var students = db.Queryable<CMStudent>().ToList();
var students = db.Queryable<CMStudent>().Take(10).ToList();
if (students != null)
{
foreach (var item in students)
@ -67,7 +67,7 @@ namespace OrmTest.Demo
{
get
{
return base.CreateMapping<CMSchool>().Where(it => it.Id == this.SchoolId).ToList();
return base.CreateMapping<CMSchool>().Where(it => it.Id == this.SchoolId).Take(2).ToList();
}
}
}

2
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/9_Aop.cs

@ -54,6 +54,8 @@ namespace OrmTest.Demo
var sql = it.Sql;
var parameter = it.Parameters;
var data = it.BusinessData;
var type = it.DiffType;
var time = it.Time;
};

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

@ -46,6 +46,17 @@ namespace OrmTest.Demo
})
.ToList();
var list2 = db.Queryable<MyOrder>()
.Mapper(it => it.masterPerson, it => it.masterPersonId)
.Mapper(it => it.Persons, it => it.orgId)
.Mapper(it => it.OrderItems, it => it.OrderItems.First().masterOrderId)
.Mapper(it => it.OrderItemSignle, it => it.OrderItemSignle.masterOrderId)
.Mapper(it => {
it.orderName = it.orderName + "aa";//
})
.ToListAsync();
list2.Wait();
}
public class MyOrder
{

40
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/I_InsertOrUpdate.cs

@ -0,0 +1,40 @@
using OrmTest.Demo;
using OrmTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
public class InsertOrUpdate : DemoBase
{
public static void Init()
{
var db = GetInstance();
var entity= db.Insertable<Student>(new Student() { Name = "abc" }).ExecuteReturnEntity();
db.Saveable<Student>(entity).ExecuteReturnEntity();
//UPDATE [STudent] SET
//[SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE[Id] = @Id
db.Saveable<Student>(new Student() { Name="" }).ExecuteReturnEntity();
// INSERT INTO[STudent]
//([SchoolId],[Name],[CreateTime])
// VALUES
//(@SchoolId, @Name, @CreateTime); SELECT SCOPE_IDENTITY();
db.Saveable<Student>(new Student() { Name = "" }).InsertColumns(it=>it.Name).ExecuteReturnEntity();
db.Saveable<Student>(new Student() { Name = "" }).InsertIgnoreColumns(it => it.SchoolId).ExecuteReturnEntity();
db.Saveable<Student>(entity).UpdateIgnoreColumns(it=>it.SchoolId).ExecuteReturnEntity();
db.Saveable<Student>(entity).UpdateColumns(it=>new { it.Name,it.CreateTime }).ExecuteReturnEntity();
db.Saveable<Student>(new List<Student>() {
entity,
new Student() { Name = "" }
}).ExecuteCommand();
}
}
}

61
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/J_Debugger.cs

@ -0,0 +1,61 @@
using OrmTest.Demo;
using OrmTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest.Demo
{
public class Debugger : DemoBase
{
public static void Init()
{
var db = GetInstance();
db.CurrentConnectionConfig.Debugger = new SqlSugar.SugarDebugger() { EnableThreadSecurityValidation = true };
db.Queryable<Student>().ToList();
db.Queryable<Student>().ToListAsync().Wait();
db.Insertable<Student>(new Student() { Name = "a" }).ExecuteCommandAsync().Wait();
db.Updateable<Student>(new Student() { Name = "a" }).ExecuteCommandAsync().Wait();
db.Deleteable<Student>(1111).ExecuteCommandAsync().Wait();
var task = new Task(() =>
{
try
{
//is error
Console.WriteLine("is error");
db.Queryable<Student>().ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
});
task.Start();
task.Wait();
for (int i = 0; i < 10; i++)
{
var task2 = new Task(() =>
{
//is ok
Console.WriteLine("is ok");
var db2 = GetInstance();
db2.CurrentConnectionConfig.Debugger = new SqlSugar.SugarDebugger() { EnableThreadSecurityValidation = true };
db2.Queryable<Student>().ToList();
db2.Queryable<Student>().ToList();
});
task2.Start();
task2.Wait();
}
}
}
}

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

@ -13,7 +13,7 @@ namespace OrmTest.Demo
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { 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(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value==DBNull.Value?null:it.Value)));
Console.WriteLine();
};
return db;

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

@ -56,6 +56,7 @@ namespace OrmTest
Demo.ExtEntity.Init();
Demo.Mapper.Init();
Demo.IInsertOrUpdate.Init();
Demo.Debugger.Init();
/***BUG repair test***/
new BugTest.Bug1().Init();
}

Loading…
Cancel
Save