SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.6 KiB

using SqlSugar;
2 years ago
using SqlSugar.Extensions;
2 years ago
using SqlSugarSelect;
using System;
6 years ago
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public partial class NewUnitTest
{
public static void Json()
{
6 years ago
Db.CodeFirst.InitTables<UnitJsonTest>();
Db.CodeFirst.InitTables<UnitJsonTest2>();
6 years ago
Db.DbMaintenance.TruncateTable<UnitJsonTest>();
Db.Insertable(new UnitJsonTest() { Order = new Order { Id = 1, Name = "order1" } }).ExecuteCommand();
var list = Db.Queryable<UnitJsonTest>().ToList();
var json = Db.Queryable<UnitJsonTest>().Select(it => SqlFunc.JsonField(it.Order, "Name")).ToList();
6 years ago
UValidate.Check("order1", list.First().Order.Name, "Json");
6 years ago
Db.Updateable(new UnitJsonTest() { Id = 1, Order = new Order { Id = 2, Name = "order2" } }).ExecuteCommand();
3 years ago
list = Db.Queryable<UnitJsonTest>().ToList();
6 years ago
UValidate.Check("order2", list.First().Order.Name, "Json");
6 years ago
var list2 = Db.Queryable<UnitJsonTest>().ToList();
3 years ago
var x = new Order() { Name = "a" };
Db.Updateable<UnitJsonTest2>()
3 years ago
.SetColumns(it => it.Name == "a")
.Where(it => it.Id == 1)
.ExecuteCommand();
3 years ago
var list3 = Db.Queryable<UnitJsonTest>().Select(it => new
3 years ago
{
x = it
}).ToList();
3 years ago
if (list3[0].x == null)
3 years ago
{
throw new Exception("unit error");
}
var db = Db;
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(SqlSugarSelect.TestModel1));
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(SqlSugarSelect.TestModel2));
3 years ago
#region 加入数据
2 years ago
var isadd = !db.Queryable<SqlSugarSelect.TestModel1>().Any();
if (isadd)
{
db.Insertable(new SqlSugarSelect.TestModel1
{
3 years ago
Ids = new Guid[] { Guid.NewGuid() },
Titlt = "123"
}).ExecuteCommand();
db.Insertable(new SqlSugarSelect.TestModel2
{
Pid = 1
}).ExecuteCommand();
}
#endregion
#region 实际搜索代码,Bug所在处
var rv = db.Queryable<SqlSugarSelect.TestModel2>()
.LeftJoin<SqlSugarSelect.TestModel1>((a, b) => a.Pid == b.Id)
.Select((a, b) => new { a, b }).ToList();
#endregion
3 years ago
db.CodeFirst.SetStringDefaultLength(2000).InitTables<UnitJsonTestadsga1>();
2 years ago
db.DbMaintenance.TruncateTable<UnitJsonTestadsga1>();
3 years ago
db.Insertable(new UnitJsonTestadsga1() { os = new List<Order>() }).ExecuteCommand();
db.Insertable(new UnitJsonTestadsga1() { os = new List<Order>() { new Order() { CreateTime = DateTime.Now } } }).ExecuteCommand();
var list10 = db.Queryable<UnitJsonTestadsga1>().Select(it => new { it }).ToList();
var jsonDb = Db;
jsonDb.CurrentConnectionConfig.ConfigureExternalServices = new SqlSugar.ConfigureExternalServices()
{
EntityService = (c, p) =>
{
p.IsJson = true;
}
};
var list11 = jsonDb.Queryable<UNITJSONTESTADSGA1>().Select(it => new { it }).ToList();
2 years ago
if (list11.FirstOrDefault().it.os == null)
3 years ago
{
throw new Exception("unit test");
}
2 years ago
2 years ago
var list12 = jsonDb.Queryable<UNITJSONTESTADSGA1>().Select(it => it).ToList();
if (!list12.Any(z => z.os.Count > 0))
2 years ago
{
throw new Exception("unit test");
}
2 years ago
2 years ago
var list13 = db.Queryable<object>().AS("UnitTestModel1").Select<ViewTestModel1>().ToList();
if (list13.First().Ids.Count() == 0)
2 years ago
{
throw new Exception("unit test");
}
2 years ago
var order = new List<Order>() { new Order() { Id = 1 } };
2 years ago
db.
Updateable<UnitJsonTestadsga1>()
.SetColumns(it => it.os == order)
2 years ago
.Where(it => true)
2 years ago
.ExecuteCommand();
2 years ago
db.
Updateable<UnitJsonTestadsga1>()
.SetColumns(it => new UnitJsonTestadsga1() { os = order })
.Where(it => true)
.ExecuteCommand();
2 years ago
var list14=db.Queryable<UnitJsonTestadsga1>().ToList();
2 years ago
var list150 = db.Queryable<UnitJsonTest>().ToDictionary(it => it.Id, it => it.Order);
var list15 = db.Queryable<UnitJsonTest>().ToDictionaryAsync(it=>it.Id,it=>it.Order).GetAwaiter().GetResult();
6 years ago
}
}
3 years ago
public class UnitJsonTestadsga1
{
[SqlSugar.SugarColumn(Length =2000,IsJson =true)]
public List<Order> os{get;set;}
}
3 years ago
public class UNITJSONTESTADSGA1
{
[SqlSugar.SugarColumn(Length = 2000)]
public List<Order> os { get; set; }
}
public class UnitJsonTest2
{
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
[SqlSugar.SugarColumn(IsJson = true)]
public Order Order { get; set; }
[SqlSugar.SugarColumn(IsNullable =true)]
public string Name { get; set; }
}
6 years ago
public class UnitJsonTest
6 years ago
{
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
[SqlSugar.SugarColumn(ColumnDataType = "varchar(max)", IsJson = true)]
public Order Order { get; set; }
}
}