mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-23 04:23:47 +08:00
Update demo
This commit is contained in:
@@ -1,84 +0,0 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class DataTest : UnitTestBase
|
||||
{
|
||||
private DataTest() { }
|
||||
public DataTest(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var guid = Guid.Parse("d8268a7e-a2d1-4329-9990-9bd801292415");
|
||||
var db = GetInstance();
|
||||
db.DbMaintenance.TruncateTable("DataTestInfo");
|
||||
var insertObject = new DataTestInfo()
|
||||
{
|
||||
Datetime1 = DateTime.Now,
|
||||
Datetime2 = DateTime.Now,
|
||||
Decimal1 = 1,
|
||||
Decimal2 = 2,
|
||||
Float1 = 3,
|
||||
Float2 = 4,
|
||||
Guid1 = guid,
|
||||
Guid2 = guid,
|
||||
Image1 = new byte[] { 1, 2 },
|
||||
Image2 = new byte[] { 2, 3 },
|
||||
Int2 = 6,
|
||||
Money1 = Convert.ToDecimal(7.1),
|
||||
Money2 = 8,
|
||||
Varbinary1 = new byte[] { 4, 5 },
|
||||
Varbinary2 = null,
|
||||
String = "string",
|
||||
Long1=100
|
||||
};
|
||||
|
||||
var id = db.Insertable<DataTestInfo>(insertObject).ExecuteReturnIdentity();
|
||||
var x = db.Queryable<DataTestInfo2>().Select(it => it.PK).ToList();
|
||||
var x2 = db.Queryable<DataTestInfo>().Select(it => it.Money1).ToList();
|
||||
var data = db.Queryable<DataTestInfo>().InSingle(id);
|
||||
if (
|
||||
insertObject.Datetime1.ToString("yyyy-MM-dd") != data.Datetime1.ToString("yyyy-MM-dd") ||
|
||||
insertObject.Decimal1 != data.Decimal1 ||
|
||||
insertObject.Float1 != data.Float1 ||
|
||||
insertObject.Float2 != data.Float2 ||
|
||||
insertObject.Int2 != data.Int2 ||
|
||||
insertObject.Money1 != data.Money1 ||
|
||||
insertObject.Long1!=data.Long1||
|
||||
insertObject.Guid2!=data.Guid2||
|
||||
string.Join(",", insertObject.Varbinary1) != string.Join(",", data.Varbinary1) ||
|
||||
insertObject.String != data.String)
|
||||
{
|
||||
throw new Exception("DataTest Error");
|
||||
}
|
||||
data.Float1= data.Float1+1;
|
||||
db.Updateable(data).ExecuteCommand();
|
||||
data = db.Queryable<DataTestInfo>().InSingle(id);
|
||||
if (
|
||||
insertObject.Datetime1.ToString("yyyy-MM-dd") != data.Datetime1.ToString("yyyy-MM-dd") ||
|
||||
insertObject.Decimal1 != data.Decimal1 ||
|
||||
(insertObject.Float1+1) != data.Float1 ||
|
||||
insertObject.Float2 != data.Float2 ||
|
||||
insertObject.Int2 != data.Int2 ||
|
||||
insertObject.Money1 != data.Money1 ||
|
||||
string.Join(",", insertObject.Varbinary1) != string.Join(",", data.Varbinary1) ||
|
||||
insertObject.String != data.String)
|
||||
{
|
||||
throw new Exception("DataTest Error");
|
||||
}
|
||||
}
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.Sqlite, IsAutoCloseConnection = true });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
using OrmTest.Models;
|
||||
using OrmTest.UnitTest;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Delete : UnitTestBase
|
||||
{
|
||||
private Delete() { }
|
||||
public Delete(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var db = GetInstance();
|
||||
//by entity
|
||||
var t1= db.Deleteable<Student>().Where(new Student() { Id = 1 }).ToSql();
|
||||
base.Check(@"DELETE FROM `STudent` WHERE `Id` IN ('1') ",
|
||||
null,
|
||||
t1.Key,
|
||||
null, "Delte t1 error"
|
||||
);
|
||||
//use lock
|
||||
var t2 = db.Deleteable<Student>().With(SqlWith.RowLock).ToSql();
|
||||
base.Check(@"DELETE FROM `STudent`",
|
||||
null,
|
||||
t2.Key,
|
||||
null, "Delte t2 error"
|
||||
);
|
||||
|
||||
//by primary key
|
||||
var t3 = db.Deleteable<Student>().In(1).ToSql();
|
||||
base.Check(@"DELETE FROM `STudent` WHERE `Id` IN ('1') ",
|
||||
null,
|
||||
t3.Key,
|
||||
null, "Delte tt error"
|
||||
);
|
||||
//by primary key array
|
||||
var t4 = db.Deleteable<Student>().In(new int[] { 1,2}).ToSql();
|
||||
base.Check(@"DELETE FROM `STudent` WHERE `Id` IN ('1','2') ", null, t4.Key, null, "Update t4 error");
|
||||
|
||||
//by expression
|
||||
var t5 = db.Deleteable<Student>().Where(it=>it.Id==1).ToSql();
|
||||
base.Check(@"DELETE FROM `STudent` WHERE ( `ID` = @Id0 ) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1)
|
||||
}, t5.Key, t5.Value, "Delte t5 error");
|
||||
|
||||
var t6 = db.Deleteable<Student>().Where("id=@id",new { id=1}).ToSql();
|
||||
base.Check(@"DELETE FROM `STudent` WHERE id=@id", new List<SugarParameter>() {
|
||||
new SugarParameter("@id",1)
|
||||
}, t6.Key, t6.Value, "Delte t6 error");
|
||||
}
|
||||
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.Sqlite, IsAutoCloseConnection = true });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,51 +0,0 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class Field : UnitTestBase
|
||||
{
|
||||
private Field() { }
|
||||
public Field(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
internal void Init()
|
||||
{
|
||||
base.Begin();
|
||||
for (int i = 0; i < base.Count; i++)
|
||||
{
|
||||
FieldSingle();
|
||||
FieldMultiple();
|
||||
}
|
||||
base.End("Filed Test");
|
||||
}
|
||||
private void FieldSingle()
|
||||
{
|
||||
Expression<Func<Student, object>> exp = it => it.Name;
|
||||
ExpressionContext expContext = GetContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.FieldSingle);
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
Check(selectorValue, null, expContext.GetTranslationColumnName("Name"), null, "FieldSingle error");
|
||||
}
|
||||
private void FieldMultiple()
|
||||
{
|
||||
Expression<Func<Student, object>> exp = it => it.Name;
|
||||
ExpressionContext expContext = GetContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.FieldMultiple);
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
Check(selectorValue, null, expContext.GetTranslationColumnName("it.Name"), null, "FieldMultiple error");
|
||||
}
|
||||
|
||||
public ExpressionContext GetContext()
|
||||
{
|
||||
return new MySqlExpressionContext();//可以更换
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,610 +0,0 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class Method : UnitTestBase
|
||||
{
|
||||
private Method() { }
|
||||
public Method(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
internal void Init()
|
||||
{
|
||||
base.Begin();
|
||||
for (int i = 0; i < base.Count; i++)
|
||||
{
|
||||
|
||||
//Native methods
|
||||
ExtendContainsArray();
|
||||
ConvetToString();
|
||||
ExtendToString();
|
||||
ExtendSubstring();
|
||||
ExtendDate();
|
||||
|
||||
//SqlFun methods
|
||||
IIF();
|
||||
IIF2();
|
||||
#region StringIsNullOrEmpty
|
||||
HasValue();
|
||||
HasNumber();
|
||||
StringIsNullOrEmpty();
|
||||
StringIsNullOrEmpty2();
|
||||
StringIsNullOrEmpty3();
|
||||
StringIsNullOrEmpty4();
|
||||
StringIsNullOrEmpty5();
|
||||
#endregion
|
||||
ToUpper();
|
||||
ToLower();
|
||||
Trim();
|
||||
Contains();
|
||||
Contains2();
|
||||
ContainsArray();
|
||||
StartsWith();
|
||||
EndsWith();
|
||||
Between();
|
||||
Equals();
|
||||
Equals_2();
|
||||
DateIsSameByDay();
|
||||
DateIsSameByType();
|
||||
DateAddDay();
|
||||
DateAddByType();
|
||||
DateValue();
|
||||
ToInt32();
|
||||
ToInt64();
|
||||
ToDate();
|
||||
Tostring();
|
||||
ToDecimal();
|
||||
ToGuid();
|
||||
ToDouble();
|
||||
ToBool();
|
||||
Substring();
|
||||
Replace();
|
||||
Length();
|
||||
}
|
||||
base.End("Method Test");
|
||||
}
|
||||
|
||||
private void ExtendToString()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id.ToString() == "a";
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(CAST(`Id` AS TEXT) = @Const0 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Const0","a")
|
||||
}, "ExtendToString error");
|
||||
}
|
||||
|
||||
private void ConvetToString()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => Convert.ToString(it.Id) == "a";
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(CAST(`Id` AS TEXT) = @Const0 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Const0","a")
|
||||
}, "ConvetToString error");
|
||||
}
|
||||
|
||||
|
||||
private void Length()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.Length("aaaa") > 1;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(LENGTH(@MethodConst0) > @Const1 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","aaaa"),new SugarParameter("@Const1",1)
|
||||
}, "Length error");
|
||||
}
|
||||
|
||||
private void Replace()
|
||||
{
|
||||
var x2 = Guid.NewGuid();
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.Replace("aaaa", "a", "1") == "a";
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(REPLACE(@MethodConst0,@MethodConst1,@MethodConst2) = @Const3 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","aaaa"), new SugarParameter("@MethodConst1","a") , new SugarParameter("@MethodConst2","1"),new SugarParameter("@Const3","a")
|
||||
}, "Replace error");
|
||||
}
|
||||
|
||||
private void Substring()
|
||||
{
|
||||
var x2 = Guid.NewGuid();
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.Substring("aaaa", 0, 2) == "a";
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(SUBSTR(@MethodConst0,1 + @MethodConst1,@MethodConst2) = @Const3 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","aaaa"), new SugarParameter("@MethodConst1",0) , new SugarParameter("@MethodConst2",2),new SugarParameter("@Const3","a")
|
||||
}, "Substring error");
|
||||
}
|
||||
private void ExtendSubstring()
|
||||
{
|
||||
var x2 = Guid.NewGuid();
|
||||
Expression<Func<Student, bool>> exp = it =>"aaaa".Substring(0, 2)== "a";
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(SUBSTR(@MethodConst0,1 + @MethodConst1,@MethodConst2) = @Const3 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","aaaa"), new SugarParameter("@MethodConst1",0) , new SugarParameter("@MethodConst2",2),new SugarParameter("@Const3","a")
|
||||
}, "Substring error");
|
||||
}
|
||||
|
||||
|
||||
private void ToBool()
|
||||
{
|
||||
var x2 = Guid.NewGuid();
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.ToBool("true") == true;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(CAST(@MethodConst0 AS SIGNED) = @Const1 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","true"),new SugarParameter("@Const1",(bool)true)
|
||||
}, "ToBool error");
|
||||
}
|
||||
|
||||
private void ToDouble()
|
||||
{
|
||||
var x2 = Guid.NewGuid();
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.ToDouble("2") == 2;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(CAST(@MethodConst0 AS DECIMAL(18,4)) = @Const1 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","2"),new SugarParameter("@Const1",(Double)2)
|
||||
}, "ToDouble error");
|
||||
}
|
||||
|
||||
private void ToGuid()
|
||||
{
|
||||
var x2 = Guid.NewGuid();
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.ToGuid("A94027A3-476E-478D-8228-F4054394B874") == x2;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(CAST(@MethodConst0 AS TEXT) = @Const1 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","A94027A3-476E-478D-8228-F4054394B874"),new SugarParameter("@Const1",x2)
|
||||
}, "ToGuid error");
|
||||
}
|
||||
|
||||
private void ToDecimal()
|
||||
{
|
||||
var x2 = DateTime.Now;
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.ToDecimal("22") == 1;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(CAST(@MethodConst0 AS DECIMAL(18,4)) = @Const1 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","22"),new SugarParameter("@Const1",(decimal)1)
|
||||
}, "ToDecimal error");
|
||||
}
|
||||
|
||||
private void Tostring()
|
||||
{
|
||||
var x2 = DateTime.Now;
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.ToString("2015-1-1") == "a";
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(CAST(@MethodConst0 AS TEXT) = @Const1 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","2015-1-1"),new SugarParameter("@Const1","a")
|
||||
}, "Tostring error");
|
||||
}
|
||||
|
||||
private void ToDate()
|
||||
{
|
||||
var x2 = DateTime.Now;
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.ToDate("2015-1-1") == x2;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(DATETIME(@MethodConst0) = @Const1 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","2015-1-1"),new SugarParameter("@Const1",x2)
|
||||
}, "ToDate error");
|
||||
}
|
||||
private void ExtendDate()
|
||||
{
|
||||
var x2 = DateTime.Now;
|
||||
Expression<Func<Student, bool>> exp = it => Convert.ToDateTime("2015-1-1") == x2;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(DATETIME(@MethodConst0) = @Const1 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","2015-1-1"),new SugarParameter("@Const1",x2)
|
||||
}, "ExtendDate error");
|
||||
}
|
||||
|
||||
private void ToInt64()
|
||||
{
|
||||
var x2 = DateTime.Now;
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.ToInt64("3") == 1;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(CAST(@MethodConst0 AS INTEGER) = @Const1 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","3"),new SugarParameter("@Const1",(Int64)1)
|
||||
}, "ToInt64 error");
|
||||
}
|
||||
|
||||
private void ToInt32()
|
||||
{
|
||||
var x2 = DateTime.Now;
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.ToInt32("3") == 1;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(CAST(@MethodConst0 AS INTEGER) = @Const1 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","3"),new SugarParameter("@Const1",1)
|
||||
}, "ToInt32 error");
|
||||
}
|
||||
|
||||
private void DateValue()
|
||||
{
|
||||
var x2 = DateTime.Now;
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.DateValue(x2, DateType.Year) == 1;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(CAST(STRFTIME('%Y', DATETIME(DATETIME(@MethodConst0), 'LOCALTIME')) AS INTEGER) = @Const2 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0",x2),new SugarParameter("@Const2",1)
|
||||
}, "DateValue error");
|
||||
}
|
||||
|
||||
private void StartsWith()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.StartsWith(it.Name, "a");
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " (`Name` like @MethodConst0||'%') ", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","a")
|
||||
}, "StartsWith error");
|
||||
}
|
||||
private void EndsWith()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.EndsWith(it.Name, "a");
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " (`Name` like '%'||@MethodConst0) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","a")
|
||||
}, "EndsWith");
|
||||
}
|
||||
private void Between()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.Between(it.Name, 1, 2);
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " (`Name` BETWEEN @MethodConst0 AND @MethodConst1) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0",1),new SugarParameter("@MethodConst1",2),
|
||||
}, "Between error");
|
||||
}
|
||||
|
||||
private void DateAddByType()
|
||||
{
|
||||
var x2 = DateTime.Now;
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.DateAdd(x2, 11, DateType.Minute) == x2;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(DATETIME(DATETIME(@MethodConst0), '+11 Minutes') = @Const3 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0",x2),new SugarParameter("@MethodConst1",11),new SugarParameter("@Const3",x2)
|
||||
}, "DateAddByType error");
|
||||
}
|
||||
private void DateAddDay()
|
||||
{
|
||||
var x2 = DateTime.Now;
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.DateAdd(x2, 1) == x2;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(DATE(DATETIME(@MethodConst0), '+1 days') = @Const2 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0",x2),new SugarParameter("@MethodConst1",1),new SugarParameter("@Const2",x2)
|
||||
}, "DateAddDay error");
|
||||
}
|
||||
|
||||
private void DateIsSameByType()
|
||||
{
|
||||
var x2 = DateTime.Now;
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.DateIsSame(x2, x2, DateType.Millisecond);
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " Cast((JulianDay(@MethodConst0) - JulianDay(@MethodConst1)) *86400000 As INTEGER)", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0",x2),new SugarParameter("@MethodConst1",x2)
|
||||
}, "DateIsSameByType error");
|
||||
}
|
||||
private void DateIsSameByDay()
|
||||
{
|
||||
var x2 = DateTime.Now;
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.DateIsSame(x2, x2);
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " Cast((JulianDay(@MethodConst0) - JulianDay(@MethodConst1)) *1 As INTEGER)=0 ", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0",x2),new SugarParameter("@MethodConst1",x2)
|
||||
}, "DateIsSameDay error");
|
||||
}
|
||||
|
||||
private void Equals()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.Equals(it.Name, "a");
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " (`Name` = @MethodConst0) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","a")
|
||||
}, "Equals1 error");
|
||||
|
||||
|
||||
Expression<Func<Student, bool>> exp2 = it => SqlFunc.Equals("a", it.Name);
|
||||
SqliteExpressionContext expContext2 = new SqliteExpressionContext();
|
||||
expContext2.Resolve(exp2, ResolveExpressType.WhereSingle);
|
||||
var value2 = expContext2.Result.GetString();
|
||||
var pars2 = expContext2.Parameters;
|
||||
base.Check(value2, pars2, " (@MethodConst0 = `Name`) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","a")
|
||||
}, "Equals2 error");
|
||||
}
|
||||
private void Equals_2()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.Equals(it.Name, it.Name);
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " (`Name` = `Name`) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","a")
|
||||
}, "Equals1 error");
|
||||
|
||||
|
||||
Expression<Func<Student, bool>> exp2 = it => SqlFunc.Equals("a", "a2");
|
||||
SqliteExpressionContext expContext2 = new SqliteExpressionContext();
|
||||
expContext2.Resolve(exp2, ResolveExpressType.WhereSingle);
|
||||
var value2 = expContext2.Result.GetString();
|
||||
var pars2 = expContext2.Parameters;
|
||||
base.Check(value2, pars2, " (@MethodConst0 = @MethodConst1) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","a"),new SugarParameter("@MethodConst1","a2")
|
||||
}, "Equals2 error");
|
||||
}
|
||||
|
||||
private void Contains()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.Contains(it.Name, "a");
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " (`Name` like '%'||@MethodConst0||'%') ", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","a")
|
||||
}, "Contains error");
|
||||
}
|
||||
private void Contains2(string name="a")
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.Contains(it.Name, name);
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " (`Name` like '%'||@MethodConst0||'%') ", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","a")
|
||||
}, "Contains2 error");
|
||||
}
|
||||
|
||||
private void ExtendContainsArray() {
|
||||
var array = new string[] { "1", "2" }.ToList();
|
||||
Expression<Func<Student, bool>> exp = it => array.Contains(it.Name);
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, null, " (`Name` IN ('1','2')) ", null, "Contains2 error");
|
||||
}
|
||||
|
||||
private void ContainsArray()
|
||||
{
|
||||
string[] array = new string[] { "1", "2" };
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.ContainsArray(array, it.Name);
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, null, " (`Name` IN ('1','2')) ", null, "Contains2 error");
|
||||
}
|
||||
|
||||
private void Trim()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.Trim(" a") == it.Name;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "((rtrim(ltrim(@MethodConst0))) = `Name` )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0"," a")
|
||||
}, "Trim error");
|
||||
}
|
||||
|
||||
private void ToUpper()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => "a" == SqlFunc.ToUpper(it.Id);
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( @Const0 = (UPPER(`Id`)) )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Const0","a")
|
||||
}, "ToUpper error");
|
||||
}
|
||||
private void ToLower()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => "a" == SqlFunc.ToLower(it.Id);
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( @Const0 = (LOWER(`Id`)) )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Const0","a")
|
||||
}, "ToLower error");
|
||||
}
|
||||
|
||||
#region StringIsNullOrEmpty
|
||||
private void StringIsNullOrEmpty()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id > 2 || SqlFunc.IsNullOrEmpty(it.Id); ;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(( `Id` > @Id0 ) OR ( `Id`='' OR `Id` IS NULL ))", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",2)
|
||||
}, "StringIsNullOrEmpty error");
|
||||
}
|
||||
private void StringIsNullOrEmpty2()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => 2 == it.Id || SqlFunc.IsNullOrEmpty(true); ;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(( @Id0 = `Id` ) OR ( @MethodConst1='' OR @MethodConst1 IS NULL ))", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst1",true),
|
||||
new SugarParameter("@Id0",2)
|
||||
}, "StringIsNullOrEmpty2 error");
|
||||
}
|
||||
private void StringIsNullOrEmpty3()
|
||||
{
|
||||
int a = 1;
|
||||
Expression<Func<Student, bool>> exp = it => 2 == it.Id || SqlFunc.IsNullOrEmpty(a); ;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(( @Id0 = `Id` ) OR ( @MethodConst1='' OR @MethodConst1 IS NULL ))", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst1",1),
|
||||
new SugarParameter("@Id0",2)
|
||||
}, "StringIsNullOrEmpty3 error");
|
||||
}
|
||||
private void StringIsNullOrEmpty4()
|
||||
{
|
||||
WhereConst.name = "xx";
|
||||
Expression<Func<Student, bool>> exp = it => 2 == it.Id || SqlFunc.IsNullOrEmpty(WhereConst.name); ;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(( @Id0 = `Id` ) OR ( @MethodConst1='' OR @MethodConst1 IS NULL ))", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst1","xx"),
|
||||
new SugarParameter("@Id0",2)
|
||||
}, "StringIsNullOrEmpty4 error");
|
||||
}
|
||||
private void StringIsNullOrEmpty5()
|
||||
{
|
||||
WhereConst.name = "xx";
|
||||
Expression<Func<Student, bool>> exp = it => !SqlFunc.IsNullOrEmpty(WhereConst.name); ;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "NOT( @MethodConst0='' OR @MethodConst0 IS NULL )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","xx")
|
||||
}, "StringIsNullOrEmpty5 error");
|
||||
}
|
||||
#endregion
|
||||
|
||||
private void HasValue()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.HasValue(it.Name);
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( `Name`<>'' AND `Name` IS NOT NULL )", new List<SugarParameter>()
|
||||
{
|
||||
|
||||
}, "HasValue error");
|
||||
}
|
||||
|
||||
private void HasNumber()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.HasNumber(it.Id);
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( `Id`>0 AND `Id` IS NOT NULL )", new List<SugarParameter>()
|
||||
{
|
||||
|
||||
|
||||
}, "HasNumber error");
|
||||
}
|
||||
|
||||
private void IIF()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.IIF(it.Id == 1, 1, 2)==1;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(( CASE WHEN ( `Id` = @Id0 ) THEN @MethodConst1 ELSE @MethodConst2 END ) = @Const3 )", new List<SugarParameter>()
|
||||
{
|
||||
new SugarParameter("@Id0",1),
|
||||
new SugarParameter("@MethodConst1",1),
|
||||
new SugarParameter("@MethodConst2",2),
|
||||
new SugarParameter("@Const3",1)
|
||||
}, "IIF error");
|
||||
}
|
||||
|
||||
private void IIF2()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => SqlFunc.IIF(SqlFunc.Contains(it.Name,"a"), 1, 2)==1;
|
||||
SqliteExpressionContext expContext = new SqliteExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(( CASE WHEN (`Name` like '%'||@MethodConst0||'%') THEN @MethodConst1 ELSE @MethodConst2 END ) = @Const3 )", new List<SugarParameter>()
|
||||
{
|
||||
new SugarParameter("@MethodConst0","a"),
|
||||
new SugarParameter("@MethodConst1",1),
|
||||
new SugarParameter("@MethodConst2",2),
|
||||
new SugarParameter("@Const3",1)
|
||||
}, "IIF2 error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,99 +0,0 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class Select : UnitTestBase
|
||||
{
|
||||
private Select() { }
|
||||
public Select(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
internal void Init()
|
||||
{
|
||||
base.Begin();
|
||||
for (int i = 0; i < base.Count; i++)
|
||||
{
|
||||
single();
|
||||
Multiple();
|
||||
singleDynamic();
|
||||
MultipleDynamic();
|
||||
}
|
||||
base.End("Select Test");
|
||||
}
|
||||
|
||||
private void Multiple()
|
||||
{
|
||||
Expression<Func<Student, School, object>> exp = (it, school) => new Student() { Name = "a", Id = it.Id, SchoolId = school.Id, TestId = it.Id + 1 };
|
||||
ExpressionContext expContext = new ExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(
|
||||
selectorValue,
|
||||
pars,
|
||||
@" @constant1 AS Name , it.Id AS Id , school.Id AS SchoolId , ( it.Id + 1 ) AS TestId ",
|
||||
new List<SugarParameter>(){
|
||||
new SugarParameter("@constant1","a")},
|
||||
"Select.Multiple Error");
|
||||
}
|
||||
|
||||
private void MultipleDynamic()
|
||||
{
|
||||
Expression<Func<Student, School, object>> exp = (it, school) => new { Name = "a", Id = it.Id / 2, SchoolId = school.Id };
|
||||
ExpressionContext expContext = new ExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(
|
||||
selectorValue,
|
||||
pars,
|
||||
@" @constant1 AS Name , ( it.Id / 2 ) AS Id , school.Id AS SchoolId ",
|
||||
new List<SugarParameter>(){
|
||||
new SugarParameter("@constant1","a")},
|
||||
"Select.MultipleDynamic Error");
|
||||
}
|
||||
private void single()
|
||||
{
|
||||
int p = 1;
|
||||
Expression<Func<Student, object>> exp = it => new Student() { Name = "a", Id = it.Id, SchoolId = p,TestId=it.Id+1 };
|
||||
ExpressionContext expContext = new ExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(
|
||||
selectorValue,
|
||||
pars,
|
||||
@" @constant1 AS Name , Id AS Id , @constant3 AS SchoolId , ( Id + 1 ) AS TestId ",
|
||||
new List<SugarParameter>(){
|
||||
new SugarParameter("@constant1","a"),
|
||||
new SugarParameter("@constant3",1)},
|
||||
"Select.single Error");
|
||||
}
|
||||
|
||||
private void singleDynamic()
|
||||
{
|
||||
string a = "a";
|
||||
Expression<Func<Student, object>> exp = it => new { x = it.Id, shoolid = 1, name = a,p=it.Id*1 };
|
||||
ExpressionContext expContext = new ExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(
|
||||
selectorValue,
|
||||
pars,
|
||||
@" Id AS x , @constant2 AS shoolid , @constant3 AS name , ( Id * 1 ) AS p ",
|
||||
new List<SugarParameter>(){
|
||||
new SugarParameter("@constant2",1),
|
||||
new SugarParameter("@constant3","a")},
|
||||
"Select.single Error");
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,258 +0,0 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class Where : UnitTestBase
|
||||
{
|
||||
private Where() { }
|
||||
public Where(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
internal void Init()
|
||||
{
|
||||
base.Begin();
|
||||
for (int i = 0; i < base.Count; i++)
|
||||
{
|
||||
whereSingle1();
|
||||
whereSingle2();
|
||||
whereSingle3();
|
||||
whereSingle4();
|
||||
whereSingle5();
|
||||
whereSingle6();
|
||||
whereSingle7(new Student() { Id = 1 });
|
||||
whereSingle8(new Student() { Id = 1 });
|
||||
whereSingle9(new Student() { Id = 1 });
|
||||
whereSingle10();
|
||||
whereSingle11();
|
||||
whereSingle12();
|
||||
whereSingle13();
|
||||
whereSingle14();
|
||||
WhereMultiple1();
|
||||
WhereMultiple2();
|
||||
|
||||
}
|
||||
base.End("Where Test");
|
||||
}
|
||||
private void WhereMultiple1()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id > 1;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereMultiple);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( `it`.`Id` > @Id0 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1)
|
||||
}, "WhereMultiple1");
|
||||
}
|
||||
private void WhereMultiple2()
|
||||
{
|
||||
string name = "a";
|
||||
WhereConst.name = "a1";
|
||||
Expression<Func<Student, bool>> exp = it => (it.Id > 1 && it.Name != name || it.Id == 1) || it.Name == WhereConst.name;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereMultiple);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " (((( `it`.`Id` > @Id0 ) AND ( `it`.`Name` <> @Name1 )) OR ( `it`.`Id` = @Id2 )) OR ( `it`.`Name` = @Name3 ))", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1),
|
||||
new SugarParameter("@Name1","a"),
|
||||
new SugarParameter("@Id2",1),
|
||||
new SugarParameter("@Name3","a1")
|
||||
}, "WhereMultiple2");
|
||||
}
|
||||
private void whereSingle1()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id > 1;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( `Id` > @Id0 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1)
|
||||
}, "whereSingle1");
|
||||
}
|
||||
private void whereSingle2()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => 1 > it.Id;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( @Id0 > `Id` )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1)
|
||||
}, "whereSingle2");
|
||||
}
|
||||
private void whereSingle3()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id > 1 || it.Name == "a";
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " (( `Id` > @Id0 ) OR ( `Name` = @Name1 ))", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1),
|
||||
new SugarParameter("@Name1","a")
|
||||
}, "whereSingle3");
|
||||
}
|
||||
private void whereSingle4()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => (it.Id > 1 && it.Name != "a") || it.Name == "a1";
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " ((( `Id` > @Id0 ) AND ( `Name` <> @Name1 )) OR ( `Name` = @Name2 )) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1),
|
||||
new SugarParameter("@Name1","a"),
|
||||
new SugarParameter("@Name2","a1")
|
||||
}, "whereSingle4");
|
||||
}
|
||||
private void whereSingle5()
|
||||
{
|
||||
string name = "a";
|
||||
WhereConst.name = "a1";
|
||||
Expression<Func<Student, bool>> exp = it => (it.Id > 1 && it.Name != name) || it.Name == WhereConst.name;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " ((( `Id` > @Id0 ) AND ( `Name` <> @Name1 )) OR ( `Name` = @Name2 )) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1),
|
||||
new SugarParameter("@Name1","a"),
|
||||
new SugarParameter("@Name2","a1")
|
||||
}, "whereSingle5");
|
||||
}
|
||||
private void whereSingle6()
|
||||
{
|
||||
string name = "a";
|
||||
WhereConst.name = "a1";
|
||||
Expression<Func<Student, bool>> exp = it => (it.Id > 1 && it.Name != name||it.Id==1) || it.Name == WhereConst.name;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " (((( `Id` > @Id0 ) AND ( `Name` <> @Name1 )) OR ( `Id` = @Id2 )) OR ( `Name` = @Name3 ))", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1),
|
||||
new SugarParameter("@Name1","a"),
|
||||
new SugarParameter("@Id2",1),
|
||||
new SugarParameter("@Name3","a1")
|
||||
}, "whereSingle6");
|
||||
}
|
||||
private void whereSingle7(Student st)
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id > st.Id;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( `Id` > @Id0 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1)
|
||||
}, "whereSingle7");
|
||||
}
|
||||
|
||||
private void whereSingle8(Student st)
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Name == null;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( `Name` IS NULL )", new List<SugarParameter>() {
|
||||
|
||||
}, "whereSingle8");
|
||||
}
|
||||
|
||||
private void whereSingle9(Student st)
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Name == st.Name;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( `Name` = @Name0 )", new List<SugarParameter>()
|
||||
{
|
||||
new SugarParameter("@Name0",null)
|
||||
}, "whereSingle9");
|
||||
}
|
||||
|
||||
|
||||
private void whereSingle10()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => true;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( 1 = 1 )", new List<SugarParameter>()
|
||||
{
|
||||
|
||||
}, "whereSingle10");
|
||||
}
|
||||
|
||||
|
||||
private void whereSingle11()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => !true;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( 1 = 2 )", new List<SugarParameter>()
|
||||
{
|
||||
|
||||
}, "whereSingle11");
|
||||
}
|
||||
|
||||
private void whereSingle12()
|
||||
{
|
||||
Expression<Func<DataTestInfo2, bool>> exp = it => it.Bool1==true;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( `Bool1` = @Bool10 )", new List<SugarParameter>()
|
||||
{
|
||||
new SugarParameter("@Bool10",true)
|
||||
}, "whereSingle12");
|
||||
}
|
||||
|
||||
private void whereSingle13()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Name!=null;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "( `Name` IS NOT NULL )", new List<SugarParameter>()
|
||||
{
|
||||
|
||||
}, "whereSingle13");
|
||||
}
|
||||
|
||||
private void whereSingle14()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it =>true&& it.Name != null;
|
||||
ExpressionContext expContext = new MySqlExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(( 1 = 1 ) AND( `Name` IS NOT NULL ))", new List<SugarParameter>()
|
||||
{
|
||||
|
||||
}, "whereSingle14");
|
||||
}
|
||||
}
|
||||
|
||||
public class WhereConst
|
||||
{
|
||||
public static string name { get; set; }
|
||||
}
|
||||
}
|
@@ -1,127 +0,0 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class Insert : UnitTestBase
|
||||
{
|
||||
private Insert() { }
|
||||
public Insert(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var db = GetInstance();
|
||||
var insertObj = new Student() { Name = "jack", CreateTime = Convert.ToDateTime("2010-1-1"), SchoolId=0 };
|
||||
db.IgnoreColumns.Add("TestId", "Student");
|
||||
//db.MappingColumns.Add("id","dbid", "Student");
|
||||
|
||||
var t1 = db.Insertable(insertObj).ToSql();
|
||||
base.Check(@"INSERT INTO `STudent`
|
||||
(`SchoolId`,`Name`,`CreateTime`)
|
||||
VALUES
|
||||
(@SchoolId,@Name,@CreateTime) ;SELECT LAST_INSERT_ROWID();",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@SchoolId",0),
|
||||
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
|
||||
new SugarParameter("@Name","jack")
|
||||
}, t1.Key, t1.Value, "Insert t1 error"
|
||||
);
|
||||
|
||||
|
||||
//Insert reutrn Command Count
|
||||
var t2 = db.Insertable(insertObj).ExecuteCommand();
|
||||
|
||||
db.IgnoreColumns = null;
|
||||
//Only insert Name
|
||||
var t3 = db.Insertable(insertObj).InsertColumns(it => new { it.Name }).ToSql();
|
||||
base.Check(@"INSERT INTO `STudent`
|
||||
(`Name`)
|
||||
VALUES
|
||||
(@Name) ;SELECT LAST_INSERT_ROWID();", new List<SugarParameter>() {
|
||||
new SugarParameter("@Name","jack")
|
||||
}, t3.Key, t3.Value, "Insert t3 error");
|
||||
|
||||
|
||||
//Ignore Name and TestId
|
||||
var t4 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ToSql();
|
||||
base.Check(@"INSERT INTO `STudent`
|
||||
(`SchoolId`,`CreateTime`)
|
||||
VALUES
|
||||
(@SchoolId,@CreateTime) ;SELECT LAST_INSERT_ROWID();",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@SchoolId",0),
|
||||
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
|
||||
}, t4.Key, t4.Value, "Insert t4 error"
|
||||
);
|
||||
|
||||
//Ignore Name and TestId
|
||||
var t5 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").With(SqlWith.UpdLock).ToSql();
|
||||
base.Check(@"INSERT INTO `STudent`
|
||||
(`SchoolId`,`CreateTime`)
|
||||
VALUES
|
||||
(@SchoolId,@CreateTime) ;SELECT LAST_INSERT_ROWID();",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@SchoolId",0),
|
||||
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
|
||||
}, t5.Key, t5.Value, "Insert t5 error"
|
||||
);
|
||||
//Use Lock
|
||||
var t6 = db.Insertable(insertObj).With(SqlWith.UpdLock).ToSql();
|
||||
base.Check(@"INSERT INTO `STudent`
|
||||
(`SchoolId`,`Name`,`CreateTime`)
|
||||
VALUES
|
||||
(@SchoolId,@Name,@CreateTime) ;SELECT LAST_INSERT_ROWID();",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@SchoolId",0),
|
||||
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
|
||||
new SugarParameter("@Name","jack")
|
||||
}, t6.Key, t6.Value, "Insert t6 error"
|
||||
);
|
||||
|
||||
var insertObj2 = new Student() { Name = null,SchoolId=0, CreateTime = Convert.ToDateTime("2010-1-1") };
|
||||
var t8 = db.Insertable(insertObj2).Where(true/* Is insert null */, true/*off identity*/).ToSql();
|
||||
base.Check(@"INSERT INTO `STudent`
|
||||
(`ID`,`SchoolId`,`CreateTime`)
|
||||
VALUES
|
||||
(@ID,@SchoolId,@CreateTime) ;SELECT LAST_INSERT_ROWID();",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@SchoolId", 0),
|
||||
new SugarParameter("@ID", 0),
|
||||
new SugarParameter("@CreateTime", Convert.ToDateTime("2010-1-1"))
|
||||
},
|
||||
t8.Key,
|
||||
t8.Value,
|
||||
"Insert t8 error"
|
||||
);
|
||||
|
||||
|
||||
db.IgnoreColumns = new IgnoreColumnList();
|
||||
db.IgnoreColumns.Add("TestId", "Student");
|
||||
|
||||
//Insert List<T>
|
||||
var insertObjs = new List<Student>();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
insertObjs.Add(new Student() { Name = "name" + i });
|
||||
}
|
||||
var s9 = db.Insertable(insertObjs.ToArray()).InsertColumns(it => new { it.Name }).With(SqlWith.UpdLock).ToSql();
|
||||
|
||||
insertObj.Name = null;
|
||||
var t10 = db.Insertable(insertObj).ExecuteCommand();
|
||||
}
|
||||
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.Sqlite, IsAutoCloseConnection = true });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
45
Src/Asp.Net/SqliteTest/UnitTest/Main.cs
Normal file
45
Src/Asp.Net/SqliteTest/UnitTest/Main.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
public static SqlSugarClient Db=> new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.MySql,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public static void RestData()
|
||||
{
|
||||
Db.DbMaintenance.TruncateTable<Order>();
|
||||
Db.DbMaintenance.TruncateTable<OrderItem>();
|
||||
}
|
||||
public static void Init()
|
||||
{
|
||||
CodeFirst();
|
||||
Updateable();
|
||||
Json();
|
||||
Ado();
|
||||
Queryable();
|
||||
QueryableAsync();
|
||||
Thread();
|
||||
Thread2();
|
||||
Thread3();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class Mapping:UnitTestBase
|
||||
{
|
||||
private Mapping() { }
|
||||
public Mapping(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
|
||||
public void Init() {
|
||||
|
||||
var db = GetInstance();
|
||||
var t1= db.Queryable<Student>().Where(it=>it.Id==1).ToSql();
|
||||
base.Check("SELECT `ID`,`SchoolId`,`Name`,`CreateTime` FROM `STudent` WHERE ( `ID` = @Id0 ) ", null, t1.Key, null,"Mapping t1 error");
|
||||
|
||||
db.MappingColumns.Add("Id", "id", "School");
|
||||
var t2 = db.Queryable<Student, School>((st, sc) => new object[] {
|
||||
JoinType.Left,st.SchoolId==sc.Id
|
||||
})
|
||||
.Where(st => st.Id == 1)
|
||||
.Where((st,sc) => sc.Id == 1)
|
||||
.Where((st,sc) => sc.Id == st.Id)
|
||||
.GroupBy(st => st.Id)
|
||||
.GroupBy((st,sc) => sc.Id).OrderBy(st => st.Id,OrderByType.Asc)
|
||||
.Select((st,sc)=> new { stid=st.Id,scid=sc.Id}).ToSql();
|
||||
base.Check(@"SELECT `st`.`ID` AS `stid` , `sc`.`id` AS `scid` FROM `STudent` st Left JOIN `School` sc ON ( `st`.`SchoolId` = `sc`.`id` ) WHERE ( `st`.`ID` = @Id0 ) AND ( `sc`.`id` = @Id1 ) AND ( `sc`.`id` = `st`.`ID` )GROUP BY `st`.`ID`,`sc`.`id` ORDER BY `st`.`ID` ASC ",
|
||||
null, t2.Key, null, " Mapping t2 error");
|
||||
var x2 = GetInstance();
|
||||
}
|
||||
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() {InitKeyType=InitKeyType.Attribute, ConnectionString = Config.ConnectionString, DbType = DbType.Sqlite, IsAutoCloseConnection = true });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,82 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SqlSugar;
|
||||
using System.Linq.Expressions;
|
||||
using OrmTest.Models;
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class JoinQuery : UnitTestBase
|
||||
{
|
||||
private JoinQuery() { }
|
||||
public JoinQuery(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
internal void Init()
|
||||
{
|
||||
base.Begin();
|
||||
for (int i = 0; i < base.Count; i++)
|
||||
{
|
||||
Q1();
|
||||
Q2();
|
||||
Q3();
|
||||
}
|
||||
base.End("Method Test");
|
||||
}
|
||||
|
||||
private void Q3()
|
||||
{
|
||||
using (var db = GetInstance())
|
||||
{
|
||||
var join3 = db.Queryable("Student", "st")
|
||||
.AddJoinInfo("School", "sh", "sh.id=st.schoolid")
|
||||
.Where("st.id>@id")
|
||||
.AddParameters(new { id = 1 })
|
||||
.Select("st.*").ToSql();
|
||||
string sql = @"SELECT st.* FROM `Student` st Left JOIN `School` sh ON sh.id=st.schoolid WHERE st.id>@id ";
|
||||
base.Check(sql,new List<SugarParameter>() {new SugarParameter("@id",1)}, join3.Key, join3.Value, "join 3 Error");
|
||||
}
|
||||
}
|
||||
|
||||
public void Q1()
|
||||
{
|
||||
using (var db = GetInstance())
|
||||
{
|
||||
var join1 = db.Queryable<Student, School>((st, sc) => new object[] {
|
||||
JoinType.Left,st.SchoolId==sc.Id
|
||||
}).Where(st => st.Id > 0).Select<Student>("*").ToSql();
|
||||
base.Check(@"SELECT * FROM `STudent` st Left JOIN `School` sc ON ( `st`.`SchoolId` = `sc`.`Id` ) WHERE ( `st`.`ID` > @Id0 ) ",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",0)
|
||||
}, join1.Key, join1.Value, "join 1 Error");
|
||||
}
|
||||
}
|
||||
public void Q2()
|
||||
{
|
||||
using (var db = GetInstance())
|
||||
{
|
||||
var join2 = db.Queryable<Student, School>((st, sc) => new object[] {
|
||||
JoinType.Left,st.SchoolId==sc.Id
|
||||
}).Where(st => st.Id > 2).Select<Student>("*").ToSql();
|
||||
base.Check(@"SELECT * FROM `STudent` st Left JOIN `School` sc ON ( `st`.`SchoolId` = `sc`.`Id` ) WHERE ( `st`.`ID` > @Id0 ) ",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",2)
|
||||
}, join2.Key, join2.Value, "join 2 Error");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.Sqlite });
|
||||
db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
Console.WriteLine(sql + " " + pars);
|
||||
};
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,94 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SqlSugar;
|
||||
using System.Linq.Expressions;
|
||||
using OrmTest.Models;
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class SelectQuery : UnitTestBase
|
||||
{
|
||||
private SelectQuery() { }
|
||||
public SelectQuery(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
internal void Init()
|
||||
{
|
||||
base.Begin();
|
||||
for (int i = 0; i < base.Count; i++)
|
||||
{
|
||||
Q2();
|
||||
}
|
||||
base.End("Method Test");
|
||||
}
|
||||
|
||||
public void Q2()
|
||||
{
|
||||
using (var db = GetInstance())
|
||||
{
|
||||
//db.Database.IsEnableLogEvent = true;
|
||||
db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
Console.WriteLine(sql + " " + pars);
|
||||
};
|
||||
|
||||
|
||||
#region dr ot entity
|
||||
db.IgnoreColumns.Add("TestId", "Student");
|
||||
var s1 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Name = it.Name, Student = it }).ToList();
|
||||
var s2 = db.Queryable<Student>().Select(it => new { id = it.Id, w = new { x = it } }).ToList();
|
||||
var s3 = db.Queryable<Student>().Select(it => new { newid = it.Id }).ToList();
|
||||
var s4 = db.Queryable<Student>().Select(it => new { newid = it.Id, obj = it }).ToList();
|
||||
var s5 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Student = it, Name = it.Name }).ToList();
|
||||
#endregion
|
||||
|
||||
|
||||
#region sql and parameters validate
|
||||
var t1 = db.Queryable<Student, School>((st, sc) => new object[] {
|
||||
JoinType.Inner,st.Id==sc.Id
|
||||
}).GroupBy(st => st.Id).Having(st => SqlFunc.AggregateAvg(st.Id) == 1).Select(st => new { avgId = SqlFunc.AggregateAvg(st.Id) }).ToSql();
|
||||
base.Check("SELECT AVG(`st`.`ID`) AS `avgId` FROM `STudent` st Inner JOIN `School` sc ON ( `st`.`ID` = `sc`.`Id` ) GROUP BY `st`.`ID` HAVING (AVG(`st`.`ID`) = @Const0 ) ",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@Const0",1)
|
||||
}
|
||||
,
|
||||
t1.Key, t1.Value, " select t1 Error");
|
||||
|
||||
|
||||
var t2 = db.Queryable<School, School>((st, st2) => new object[] {
|
||||
JoinType.Left,st.Id==st2.Id
|
||||
})
|
||||
.Where(st => st.Id > 0)
|
||||
.Select((st, st2) => new { stid = st.Id, scId = st2.Id, xx = st }).ToSql();
|
||||
|
||||
base.Check("SELECT `st`.`Id` AS `stid` , `st2`.`Id` AS `scId` , `st`.`Id` AS `School.Id` , `st`.`Name` AS `School.Name` FROM `School` st Left JOIN `School` st2 ON ( `st`.`Id` = `st2`.`Id` ) WHERE ( `st`.`Id` > @Id0 ) "
|
||||
, new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",0)
|
||||
}, t2.Key, t2.Value, "select t2 Error");
|
||||
|
||||
|
||||
var t3 = db.Queryable<Student, School, School>((st, sc, sc2) => new object[] {
|
||||
JoinType.Left,st.SchoolId==sc.Id,
|
||||
JoinType.Left,sc2.Id==sc.Id
|
||||
}).Where(st => st.Id > 0)
|
||||
.Select<School>((st) => new School() { Id = st.Id }).ToSql();
|
||||
base.Check("SELECT `st`.`ID` AS `Id` FROM `STudent` st Left JOIN `School` sc ON ( `st`.`SchoolId` = `sc`.`Id` ) Left JOIN `School` sc2 ON ( `sc2`.`Id` = `sc`.`Id` ) WHERE ( `st`.`ID` > @Id0 ) ",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",0)
|
||||
}, t3.Key, t3.Value, "select t3 Error");
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.Sqlite });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,119 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SqlSugar;
|
||||
using System.Linq.Expressions;
|
||||
using OrmTest.Models;
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class SingleQuery : UnitTestBase
|
||||
{
|
||||
private SingleQuery() { }
|
||||
public SingleQuery(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
internal void Init()
|
||||
{
|
||||
base.Begin();
|
||||
for (int i = 0; i < base.Count; i++)
|
||||
{
|
||||
Q2();
|
||||
}
|
||||
base.End("Method Test");
|
||||
}
|
||||
|
||||
public void Q2()
|
||||
{
|
||||
using (var db = GetInstance())
|
||||
{
|
||||
var t1 = db.Queryable<Student>().ToSql();
|
||||
base.Check("SELECT `ID`,`SchoolId`,`Name`,`CreateTime` FROM `STudent`", null, t1.Key, null, "single t1 Error");
|
||||
|
||||
var t2 = db.Queryable<Student>().With(SqlWith.NoLock).ToSql();
|
||||
base.Check("SELECT `ID`,`SchoolId`,`Name`,`CreateTime` FROM `STudent` ", null, t2.Key, null, "single t2 Error");
|
||||
|
||||
var t3 = db.Queryable<Student>().OrderBy(it=>it.Id).ToSql();
|
||||
base.Check("SELECT `ID`,`SchoolId`,`Name`,`CreateTime` FROM `STudent` ORDER BY `ID` ASC", null, t3.Key, null, "single t3 Error");
|
||||
|
||||
var t4 = db.Queryable<Student>().OrderBy(it => it.Id).Take(3).ToSql();
|
||||
base.Check(@"SELECT `ID`,`SchoolId`,`Name`,`CreateTime` FROM `STudent` ORDER BY `ID` ASC LIMIT 0,3", null, t4.Key, null, "single t4 Error");
|
||||
|
||||
var t5 = db.Queryable<Student>().OrderBy(it => it.Id).Skip(3).ToSql();
|
||||
base.Check(@"SELECT `ID`,`SchoolId`,`Name`,`CreateTime` FROM `STudent` LIMIT 3,9223372036854775807", null, t5.Key,null, "single t5 Error");
|
||||
|
||||
int pageIndex = 2;
|
||||
int pageSize = 10;
|
||||
var t6 = db.Queryable<Student>().OrderBy(it => it.Id,OrderByType.Desc).Skip((pageIndex-1)*pageSize).Take(pageSize).ToSql();
|
||||
base.Check(@"SELECT `ID`,`SchoolId`,`Name`,`CreateTime` FROM `STudent` ORDER BY `ID` DESC LIMIT 10,10", null, t6.Key, null, "single t6 Error");
|
||||
|
||||
|
||||
int studentCount=db.Ado.GetInt("select count(1) from Student");
|
||||
var countIsSuccess=db.Queryable<Student>().Count()== studentCount;
|
||||
if (!countIsSuccess) {
|
||||
throw new Exception(" single countIsSuccess Error");
|
||||
}
|
||||
|
||||
var t7 = db.Queryable<Student>().OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToPageList(pageIndex,pageSize,ref studentCount);
|
||||
countIsSuccess = studentCount == db.Queryable<Student>().OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize * pageIndex).Count();
|
||||
if (!countIsSuccess)
|
||||
{
|
||||
throw new Exception("single t7 Error");
|
||||
}
|
||||
|
||||
int studentMin = db.Ado.GetInt("select min(id) from Student");
|
||||
var minIsSuccess = db.Queryable<Student>().Min(it=>it.Id) == studentMin;
|
||||
if (!minIsSuccess)
|
||||
{
|
||||
throw new Exception("single minIsSuccess Error");
|
||||
}
|
||||
|
||||
int studentMax = db.Ado.GetInt("select max(id) from Student");
|
||||
var maxIsSuccess = db.Queryable<Student>().Max(it => it.Id) == studentMax;
|
||||
if (!maxIsSuccess)
|
||||
{
|
||||
throw new Exception("single maxIsSuccess Error");
|
||||
}
|
||||
|
||||
int studentAvg = db.Ado.GetInt("select avg(id) from Student");
|
||||
var avgIsSuccess = db.Queryable<Student>().Avg(it => it.Id) == studentAvg;
|
||||
if (!maxIsSuccess)
|
||||
{
|
||||
throw new Exception(" single avgIsSuccess Error");
|
||||
}
|
||||
|
||||
int studentSum = db.Ado.GetInt("select sum(id) from Student");
|
||||
var sumIsSuccess = db.Queryable<Student>().Sum(it => it.Id) == studentSum;
|
||||
if (!sumIsSuccess)
|
||||
{
|
||||
throw new Exception("single sumIsSuccess Error");
|
||||
}
|
||||
|
||||
var t8 = db.Queryable<Student>()
|
||||
.Where(it=>it.Id==1)
|
||||
.WhereIF(true,it=> SqlFunc.Contains(it.Name,"a"))
|
||||
.OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize ).With(SqlWith.NoLock).ToSql();
|
||||
base.Check(@"SELECT `ID`,`SchoolId`,`Name`,`CreateTime` FROM `STudent` WHERE ( `ID` = @Id0 ) AND (`Name` like '%'||@MethodConst1||'%') ORDER BY `ID` DESC LIMIT 10,10", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1),new SugarParameter("@MethodConst1","a")
|
||||
}, t8.Key, t8.Value,"single t8 Error");
|
||||
|
||||
|
||||
|
||||
var t9 = db.Queryable<Student>()
|
||||
.In(1)
|
||||
.Select(it => new { it.Id, it.Name,x=it.Id }).ToSql();
|
||||
base.Check("SELECT `ID` AS `Id` , `Name` AS `Name` , `ID` AS `x` FROM `STudent` WHERE `Id` IN (@InPara0) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@InPara0",1) },t9.Key,t9.Value, "single t9 error");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.Sqlite });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class Attribute : UnitTestBase
|
||||
{
|
||||
public Attribute(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
public void Init()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,32 +0,0 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class AutoClose : UnitTestBase
|
||||
{
|
||||
public AutoClose(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
public void Init()
|
||||
{
|
||||
//IsAutoCloseConnection
|
||||
for (int i = 0; i < this.Count; i++)
|
||||
{
|
||||
var db = GetInstance();
|
||||
var x = db.Queryable<Student>().ToList();
|
||||
}
|
||||
}
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.Sqlite, IsAutoCloseConnection = true });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class MapColumn : UnitTestBase
|
||||
{
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class MapTable : UnitTestBase
|
||||
{
|
||||
public void Init()
|
||||
{
|
||||
//IsAutoCloseConnection
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
var db = GetInstance();
|
||||
var x = db.Queryable<Student>().ToList();
|
||||
}
|
||||
}
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
57
Src/Asp.Net/SqliteTest/UnitTest/UAdo.cs
Normal file
57
Src/Asp.Net/SqliteTest/UnitTest/UAdo.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
public static void Ado()
|
||||
{
|
||||
|
||||
var task1 = Db.Ado.GetScalarAsync("select 1");
|
||||
task1.Wait();
|
||||
UValidate.Check(1, task1.Result, "ado");
|
||||
|
||||
var task2 = Db.Ado.GetIntAsync("select 2");
|
||||
task2.Wait();
|
||||
UValidate.Check(2, task2.Result, "ado");
|
||||
|
||||
|
||||
var task3 = Db.Ado.GetLongAsync("select 3");
|
||||
task3.Wait();
|
||||
UValidate.Check(3, task3.Result, "ado");
|
||||
|
||||
|
||||
var task4 = Db.Ado.GetDataTableAsync("select 4 as id");
|
||||
task4.Wait();
|
||||
UValidate.Check(4, task4.Result.Rows[0]["id"], "ado");
|
||||
|
||||
|
||||
var task5 = Db.Ado.GetInt("select @id as id",new { id=5});
|
||||
UValidate.Check(5, task5, "ado");
|
||||
|
||||
|
||||
|
||||
var task6 = Db.Ado.SqlQuery<dynamic>("select @id as id", new { id = 5 });
|
||||
UValidate.Check(5, task6[0].id, "ado");
|
||||
|
||||
|
||||
var task7 = Db.Ado.SqlQueryAsync<dynamic>("select @id as id", new { id = 7 });
|
||||
task7.Wait();
|
||||
UValidate.Check(7, task7.Result[0].id, "ado");
|
||||
|
||||
|
||||
var task8 = Db.Ado.SqlQueryAsync<dynamic>("select 8 as id");
|
||||
task8.Wait();
|
||||
UValidate.Check(8, task8.Result[0].id, "ado");
|
||||
|
||||
var task9=Db.Ado.SqlQuery<Order, OrderItem>("select * from `order`;select * from OrderDetail");
|
||||
|
||||
var task10 = Db.Ado.SqlQueryAsync<Order, OrderItem>("select * from `order`;select * from OrderDetail");
|
||||
task10.Wait();
|
||||
}
|
||||
}
|
||||
}
|
25
Src/Asp.Net/SqliteTest/UnitTest/UCodeFirst.cs
Normal file
25
Src/Asp.Net/SqliteTest/UnitTest/UCodeFirst.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
public static void CodeFirst()
|
||||
{
|
||||
if (Db.DbMaintenance.IsAnyTable("UnitCodeTest1", false))
|
||||
Db.DbMaintenance.DropTable("UnitCodeTest1");
|
||||
Db.CodeFirst.InitTables<UnitCodeTest1>();
|
||||
}
|
||||
public class UnitCodeTest1
|
||||
{
|
||||
[SqlSugar.SugarColumn(IndexGroupNameList = new string[] { "group1" })]
|
||||
public int Id { get; set; }
|
||||
[SqlSugar.SugarColumn(DefaultValue="getdate()", IndexGroupNameList =new string[] {"group1" } )]
|
||||
public DateTime? CreateDate { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
34
Src/Asp.Net/SqliteTest/UnitTest/UJson.cs
Normal file
34
Src/Asp.Net/SqliteTest/UnitTest/UJson.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
|
||||
public static void Json()
|
||||
{
|
||||
Db.CodeFirst.InitTables<UnitJsonTest>();
|
||||
Db.DbMaintenance.TruncateTable<UnitJsonTest>();
|
||||
Db.Insertable(new UnitJsonTest() { Order = new Order { Id = 1, Name = "order1" } }).ExecuteCommand();
|
||||
var list = Db.Queryable<UnitJsonTest>().ToList();
|
||||
UValidate.Check("order1", list.First().Order.Name, "Json");
|
||||
Db.Updateable(new UnitJsonTest() { Id = 1, Order = new Order { Id = 2, Name = "order2" } }).ExecuteCommand();
|
||||
list= Db.Queryable<UnitJsonTest>().ToList();
|
||||
UValidate.Check("order2", list.First().Order.Name, "Json");
|
||||
var list2 = Db.Queryable<UnitJsonTest>().ToList();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class UnitJsonTest
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
[SqlSugar.SugarColumn(ColumnDataType = "varchar(max)", IsJson = true)]
|
||||
public Order Order { get; set; }
|
||||
}
|
||||
}
|
323
Src/Asp.Net/SqliteTest/UnitTest/UQueryable.cs
Normal file
323
Src/Asp.Net/SqliteTest/UnitTest/UQueryable.cs
Normal file
@@ -0,0 +1,323 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
public static void Queryable()
|
||||
{
|
||||
|
||||
var pageindex = 1;
|
||||
var pagesize = 10;
|
||||
var total = 0;
|
||||
var totalPage = 0;
|
||||
var list = Db.Queryable<Order>().ToPageList(pageindex, pagesize, ref total, ref totalPage);
|
||||
|
||||
//Db.CodeFirst.InitTables(typeof(CarType));
|
||||
//Db.Updateable<CarType>()
|
||||
// .SetColumns(it => new CarType { State = SqlSugar.SqlFunc.IIF(it.State == true, false, true) }).Where(it => true)
|
||||
// .ExecuteCommand();
|
||||
|
||||
//Db.CodeFirst.InitTables(typeof(TestTree));
|
||||
//Db.DbMaintenance.TruncateTable<TestTree>();
|
||||
//Db.Ado.ExecuteCommand("insert testtree values(hierarchyid::GetRoot(),geography :: STGeomFromText ('POINT(55.9271035250276 -3.29431266523898)',4326),'name')");
|
||||
//var list2 = Db.Queryable<TestTree>().ToList();
|
||||
|
||||
Db.CodeFirst.InitTables<UnitGuidTable>();
|
||||
Db.Queryable<UnitGuidTable>().Where(it => it.Id.HasValue).ToList();
|
||||
|
||||
Db.Queryable<Order>().Where(it => SqlSugar.SqlFunc.Equals(it.CreateTime.Date, it.CreateTime.Date)).ToList();
|
||||
|
||||
var sql = Db.Queryable<UnitSelectTest>().Select(it => new UnitSelectTest()
|
||||
{
|
||||
|
||||
DcNull = it.Dc,
|
||||
Dc = it.Int
|
||||
}).ToSql().Key;
|
||||
UValidate.Check(sql, "SELECT [Dc] AS [DcNull] , [Int] AS [Dc] FROM [UnitSelectTest]", "Queryable");
|
||||
|
||||
sql = Db.Updateable<UnitSelectTest2>(new UnitSelectTest2()).ToSql().Key;
|
||||
UValidate.Check(sql, @"UPDATE [UnitSelectTest2] SET
|
||||
[Dc]=@Dc,[IntNull]=@IntNull WHERE [Int]=@Int", "Queryable");
|
||||
|
||||
sql = Db.Queryable<Order>().IgnoreColumns(it => it.CreateTime).ToSql().Key;
|
||||
UValidate.Check(sql, "SELECT [Id],[Name],[Price],[CustomId] FROM `order` ", "Queryable");
|
||||
sql = Db.Queryable<Order>().IgnoreColumns(it => new { it.Id, it.Name }).ToSql().Key;
|
||||
UValidate.Check(sql, "SELECT [Price],[CreateTime],[CustomId] FROM `order` ", "Queryable");
|
||||
sql = Db.Queryable<Order>().IgnoreColumns("id").ToSql().Key;
|
||||
UValidate.Check(sql, "SELECT [Name],[Price],[CreateTime],[CustomId] FROM `order` ", "Queryable");
|
||||
|
||||
var cts = IEnumerbleContains.Data();
|
||||
var list2=Db.Queryable<Order>()
|
||||
.Where(p => /*ids.*/cts.Select(c => c.Id).Contains(p.Id)).ToList();
|
||||
|
||||
var cts2 = IEnumerbleContains.Data().ToList(); ;
|
||||
var list3 = Db.Queryable<Order>()
|
||||
.Where(p => /*ids.*/cts2.Select(c => c.Id).Contains(p.Id)).ToList();
|
||||
|
||||
|
||||
var list4 = Db.Queryable<Order>()
|
||||
.Where(p => new List<int> { 1, 2, 3 }.Where(b => b > 1).Contains(p.Id)).ToList();
|
||||
|
||||
Db.CodeFirst.InitTables<UnitTest3>();
|
||||
var list5 = Db.Queryable<UnitTest3>().Where(it => SqlSugar.SqlFunc.ToString(it.Date.Value.Year) == "1").ToList();
|
||||
var list6 = Db.Queryable<UnitTest3>().Where(it => it.Date.Value.Year == 1).ToList();
|
||||
var list7 = Db.Queryable<UnitTest3>().Where(it => it.Date.Value.Date == DateTime.Now.Date).ToList();
|
||||
|
||||
|
||||
SaleOrder saleOrderInfo = new SaleOrder();
|
||||
Db.CodeFirst.InitTables<SaleOrder>();
|
||||
var result = Db.GetSimpleClient<SaleOrder>().Update(o => new SaleOrder()
|
||||
{
|
||||
OrderStatus = 1,
|
||||
CheckMan = saleOrderInfo.CheckMan,
|
||||
CheckTime = DateTime.Now
|
||||
}, o => o.OrderSn == saleOrderInfo.OrderSn && o.OrderStatus != 1);
|
||||
}
|
||||
|
||||
public static class IEnumerbleContains
|
||||
{
|
||||
public static IEnumerable<Order> Data()
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
yield return new Order
|
||||
{
|
||||
Id = i,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
[SugarTable("UnitSaleOrder")]
|
||||
public class SaleOrder
|
||||
{
|
||||
public SaleOrder()
|
||||
{
|
||||
SaleDate = DateTime.Now;
|
||||
Team = 1;
|
||||
AddTime = DateTime.Now;
|
||||
OrderStatus = 0;
|
||||
Points = 0;
|
||||
PayPoints = 0;
|
||||
PointsExchangeMoney = decimal.Zero;
|
||||
IsPushMessage = false;
|
||||
CostAmount = decimal.Zero;
|
||||
OrderAmount = decimal.Zero;
|
||||
RealOrderAmount = decimal.Zero;
|
||||
AccountsDueAmount = decimal.Zero;
|
||||
SettleType = 0;
|
||||
IsPushMessage = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订单号
|
||||
/// </summary>
|
||||
public string OrderSn { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 客户编号
|
||||
/// </summary>
|
||||
public string CustomerNo { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 收货人姓名
|
||||
/// </summary>
|
||||
public string CustomerName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 成本总金额
|
||||
/// </summary>
|
||||
public decimal CostAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 订单总金额
|
||||
/// </summary>
|
||||
public decimal OrderAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实收金额(整单优惠后)
|
||||
/// </summary>
|
||||
public decimal RealOrderAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 销货日期
|
||||
/// </summary>
|
||||
public DateTime SaleDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 下单时间
|
||||
/// </summary>
|
||||
public DateTime AddTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 媒体资源投放ID
|
||||
/// </summary>
|
||||
public string IndustryCode { get; set; }
|
||||
|
||||
public string IndustryName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string Remark { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 班组
|
||||
/// </summary>
|
||||
public int Team { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 销售员编号
|
||||
/// </summary>
|
||||
public string SellerNo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 销售员姓名
|
||||
/// </summary>
|
||||
public string SellerName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 操作人ID
|
||||
/// </summary>
|
||||
public virtual string HandlerCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 操作者
|
||||
/// </summary>
|
||||
public string Handler { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 发货仓库代号
|
||||
/// </summary>
|
||||
public string StoreCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 发货仓库名称
|
||||
/// </summary>
|
||||
public string StoreName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 销货店铺渠道代号
|
||||
/// </summary>
|
||||
public string ShopChannelCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 销货店铺渠道名称
|
||||
/// </summary>
|
||||
public string ShopChannelName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 订单产品数
|
||||
/// </summary>
|
||||
public int GoodsNum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 礼品数量
|
||||
/// </summary>
|
||||
public int GiftNum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 对应预订单号
|
||||
/// </summary>
|
||||
public string CustomerOrderSn { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 订单赠送积分
|
||||
/// </summary>
|
||||
public int Points { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 应收款金额
|
||||
/// </summary>
|
||||
public decimal AccountsDueAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 来自预约单号
|
||||
/// </summary>
|
||||
public string ReserationOrderSn { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 订单状态 0为未审核 1为已审核
|
||||
/// </summary>
|
||||
public int OrderStatus { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 审核人
|
||||
/// </summary>
|
||||
public string CheckMan { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 审核时间
|
||||
/// </summary>
|
||||
public DateTime? CheckTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 结算类型 0为非金工石(零售) 1为金工石
|
||||
/// </summary>
|
||||
public int SettleType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 使用积分
|
||||
/// </summary>
|
||||
public int PayPoints { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 积分抵现金额
|
||||
/// </summary>
|
||||
public decimal PointsExchangeMoney { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已推送微信消息
|
||||
/// </summary>
|
||||
public bool IsPushMessage { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class SaleOrderBaseInfo
|
||||
{
|
||||
public int GoodsNum { get; set; }
|
||||
|
||||
public int GiftNum { get; set; }
|
||||
|
||||
public decimal OrderAmount { get; set; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
public class UnitTest3
|
||||
{
|
||||
public DateTime? Date { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public class UnitSelectTest2
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsOnlyIgnoreUpdate = true)]
|
||||
public decimal? DcNull { get; set; }
|
||||
public decimal Dc { get; set; }
|
||||
public int? IntNull { get; set; }
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
|
||||
public decimal Int { get; set; }
|
||||
}
|
||||
|
||||
public class UnitSelectTest
|
||||
{
|
||||
public decimal? DcNull { get; set; }
|
||||
public decimal Dc { get; set; }
|
||||
public int? IntNull { get; set; }
|
||||
public decimal Int { get; set; }
|
||||
}
|
||||
|
||||
public class UnitGuidTable
|
||||
{
|
||||
public Guid? Id { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
43
Src/Asp.Net/SqliteTest/UnitTest/UQueryableAsync.cs
Normal file
43
Src/Asp.Net/SqliteTest/UnitTest/UQueryableAsync.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SqlSugar;
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
public static void QueryableAsync()
|
||||
{
|
||||
q1();
|
||||
q2();
|
||||
q3();
|
||||
}
|
||||
|
||||
private static void q1()
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
var count = Db.Queryable<Order>().Count();
|
||||
Task t = Db.Queryable<Order>().ToPageListAsync(1, 2, total);
|
||||
t.Wait();
|
||||
UValidate.Check(count, total.Value, "QueryableAsync");
|
||||
}
|
||||
private static void q2()
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
var count = Db.Queryable<Order>().Count();
|
||||
Task t = Db.Queryable<Order>().ToDataTablePageAsync(1, 2, total);
|
||||
t.Wait();
|
||||
UValidate.Check(count, total.Value, "QueryableAsync");
|
||||
}
|
||||
private static void q3()
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
var count = Db.Queryable<Order>().Count();
|
||||
Task t = Db.Queryable<Order>().ToJsonPageAsync(1, 2, total);
|
||||
t.Wait();
|
||||
UValidate.Check(count, total.Value, "QueryableAsync");
|
||||
}
|
||||
}
|
||||
}
|
378
Src/Asp.Net/SqliteTest/UnitTest/UThread.cs
Normal file
378
Src/Asp.Net/SqliteTest/UnitTest/UThread.cs
Normal file
@@ -0,0 +1,378 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
|
||||
public static SqlSugarClient simpleDb => new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.MySql,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
public static SqlSugarClient ssDb => new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.MySql,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
IsShardSameThread = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
public static SqlSugarClient singleDb = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.MySql,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
public static SqlSugarClient singleAndSsDb = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.MySql,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
IsShardSameThread = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
public static void Thread()
|
||||
{
|
||||
Simple();
|
||||
IsShardSameThread();
|
||||
Single();
|
||||
SingleAndIsShardSameThread();
|
||||
SimpleAsync();
|
||||
IsShardSameThreadAsync();
|
||||
SingleAsync();
|
||||
SingleAndIsShardSameThreadAsync();
|
||||
|
||||
}
|
||||
|
||||
private static void Simple()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAndIsShardSameThread()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void Single()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void IsShardSameThread()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static void SimpleAsync()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); ;
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAndIsShardSameThreadAsync()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAsync()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void IsShardSameThreadAsync()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
}
|
||||
}
|
316
Src/Asp.Net/SqliteTest/UnitTest/UThread2.cs
Normal file
316
Src/Asp.Net/SqliteTest/UnitTest/UThread2.cs
Normal file
@@ -0,0 +1,316 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
|
||||
public static void Thread2()
|
||||
{
|
||||
Simple2();
|
||||
IsShardSameThread2();
|
||||
Single2();
|
||||
SingleAndIsShardSameThread2();
|
||||
SimpleAsync2();
|
||||
IsShardSameThreadAsync2();
|
||||
SingleAsync2();
|
||||
SingleAndIsShardSameThreadAsync2();
|
||||
|
||||
}
|
||||
|
||||
private static void Simple2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAndIsShardSameThread2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void Single2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void IsShardSameThread2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static void SimpleAsync2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait(); ;
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAndIsShardSameThreadAsync2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAsync2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void IsShardSameThreadAsync2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
}
|
||||
}
|
117
Src/Asp.Net/SqliteTest/UnitTest/UThread3.cs
Normal file
117
Src/Asp.Net/SqliteTest/UnitTest/UThread3.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
|
||||
public static void Thread3()
|
||||
{
|
||||
Console.WriteLine("Thread3");
|
||||
SimpleAsync3().Wait();
|
||||
IsShardSameThreadAsync3().Wait();
|
||||
SingleAsync3().Wait();
|
||||
SingleAndIsShardSameThreadAsync3().Wait();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static async Task SimpleAsync3()
|
||||
{
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await simpleDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await simpleDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
List<Order> orders = new List<Order>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
orders = await simpleDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
if (orders.Count > 0)
|
||||
{
|
||||
Console.WriteLine("async is ok");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task SingleAndIsShardSameThreadAsync3()
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await singleAndSsDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await singleAndSsDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync();
|
||||
}
|
||||
List<Order> orders = new List<Order>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
orders = await singleAndSsDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
if (orders.Count > 0)
|
||||
{
|
||||
Console.WriteLine("async is ok");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task SingleAsync3()
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await singleDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await singleDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
List<Order> orders = new List<Order>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
orders = await singleDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
if (orders.Count > 0)
|
||||
{
|
||||
Console.WriteLine("async is ok");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task IsShardSameThreadAsync3()
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await ssDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await ssDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
List<Order> orders = new List<Order>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
orders = await ssDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
if (orders.Count > 0)
|
||||
{
|
||||
Console.WriteLine("async is ok");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
19
Src/Asp.Net/SqliteTest/UnitTest/UValidate.cs
Normal file
19
Src/Asp.Net/SqliteTest/UnitTest/UValidate.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class UValidate
|
||||
{
|
||||
public static void Check(object a, object b, object name)
|
||||
{
|
||||
if (a?.ToString()?.Trim() != b?.ToString()?.Trim())
|
||||
{
|
||||
throw new Exception(name + " error");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,49 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using SqlSugar;
|
||||
using System.Linq;
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class UnitTestBase
|
||||
{
|
||||
public int Count { get; set; }
|
||||
private DateTime BeginTime { get; set; }
|
||||
private DateTime EndTime { get; set; }
|
||||
|
||||
public void Begin()
|
||||
{
|
||||
this.BeginTime = DateTime.Now;
|
||||
}
|
||||
|
||||
public void End(string title)
|
||||
{
|
||||
this.EndTime = DateTime.Now;
|
||||
Console.WriteLine(title + " \r\nCount: " + this.Count + "\r\nTime: " + (this.EndTime - this.BeginTime).TotalSeconds + " Seconds \r\n");
|
||||
}
|
||||
|
||||
internal void Check(string value, List<SugarParameter> pars, string validValue, List<SugarParameter> validPars, string errorMessage)
|
||||
{
|
||||
if (value.Trim() != validValue.Trim())
|
||||
{
|
||||
throw new Exception(errorMessage);
|
||||
}
|
||||
if (pars != null && pars.Count > 0)
|
||||
{
|
||||
if (pars.Count != validPars.Count)
|
||||
{
|
||||
throw new Exception(errorMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var item in pars)
|
||||
{
|
||||
if (!validPars.Any(it => it.ParameterName.Equals(item.ParameterName) && it.Value.ObjToString().Equals(item.Value.ObjToString())))
|
||||
{
|
||||
throw new Exception(errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,135 +0,0 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class Update : UnitTestBase
|
||||
{
|
||||
private Update() { }
|
||||
public Update(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var db = GetInstance();
|
||||
var updateObj = new Student() { Id = 1, Name = "jack",SchoolId=0, CreateTime = Convert.ToDateTime("2017-05-21 09:56:12.610") };
|
||||
var updateObjs = new List<Student>() { updateObj,new Student() { Id=2,Name="sun",SchoolId=0 } }.ToArray();
|
||||
db.IgnoreColumns.Add("TestId", "Student");
|
||||
//db.MappingColumns.Add("id","dbid", "Student");
|
||||
|
||||
var t1 = db.Updateable(updateObj).ToSql();
|
||||
base.Check(@"UPDATE `STudent` SET
|
||||
`SchoolId`=@SchoolId,`Name`=@Name,`CreateTime`=@CreateTime WHERE `Id`=@Id", new List<SugarParameter>() {
|
||||
new SugarParameter("@SchoolId",0),
|
||||
new SugarParameter("@ID",1),
|
||||
new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
|
||||
new SugarParameter("@Name", "jack")
|
||||
}, t1.Key, t1.Value,"Update t1 error");
|
||||
|
||||
//update reutrn Command Count
|
||||
var t2 = db.Updateable(updateObj).ExecuteCommand();
|
||||
|
||||
db.IgnoreColumns = null;
|
||||
//Only update Name
|
||||
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ToSql();
|
||||
base.Check(@"UPDATE `STudent` SET
|
||||
`Name`=@Name WHERE `Id`=@Id", new List<SugarParameter>() {
|
||||
new SugarParameter("@ID",1),
|
||||
new SugarParameter("@Name", "jack")
|
||||
}, t3.Key, t3.Value, "Update t3 error");
|
||||
|
||||
//Ignore Name and TestId
|
||||
var t4 = db.Updateable(updateObj).IgnoreColumns(it => new { it.Name, it.TestId }).ToSql();
|
||||
base.Check(@"UPDATE `STudent` SET
|
||||
`SchoolId`=@SchoolId,`CreateTime`=@CreateTime WHERE `Id`=@Id", new List<SugarParameter>() {
|
||||
new SugarParameter("@CreateTime",Convert.ToDateTime("2017-05-21 09:56:12.610")),
|
||||
new SugarParameter("@SchoolId", 0),
|
||||
new SugarParameter("@ID",1),
|
||||
}, t4.Key, t4.Value, "Update t4 error");
|
||||
|
||||
//Ignore Name and TestId
|
||||
var t5 = db.Updateable(updateObj).IgnoreColumns(it => it == "Name" || it == "TestId").With(SqlWith.UpdLock).ToSql();
|
||||
base.Check(@"UPDATE `STudent` SET
|
||||
`SchoolId`=@SchoolId,`CreateTime`=@CreateTime WHERE `Id`=@Id", new List<SugarParameter>() {
|
||||
new SugarParameter("@CreateTime",Convert.ToDateTime("2017-05-21 09:56:12.610")),
|
||||
new SugarParameter("@SchoolId", 0),
|
||||
new SugarParameter("@ID",1),
|
||||
}, t5.Key, t5.Value, "Update t5 error");
|
||||
|
||||
|
||||
//Use Lock
|
||||
var t6 = db.Updateable(updateObj).With(SqlWith.UpdLock).ToSql();
|
||||
base.Check(@"UPDATE `STudent` SET
|
||||
`SchoolId`=@SchoolId,`Name`=@Name,`CreateTime`=@CreateTime WHERE `Id`=@Id", new List<SugarParameter>() {
|
||||
new SugarParameter("@SchoolId",0),
|
||||
new SugarParameter("@ID",1),
|
||||
new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
|
||||
new SugarParameter("@Name", "jack")
|
||||
}, t6.Key, t6.Value, "Update t6 error");
|
||||
|
||||
|
||||
// //update List<T>
|
||||
// var t7 = db.Updateable(updateObjs).With(SqlWith.UpdLock).ToSql();
|
||||
// base.Check(@"UPDATE S SET S.`SchoolId`=T.`SchoolId`,S.`Name`=T.`Name`,S.`CreateTime`=T.`CreateTime` FROM `STudent` S WITH(UPDLOCK) INNER JOIN (
|
||||
|
||||
// SELECT N'1' AS ID,N'0' AS SchoolId,N'jack' AS Name,'2017-05-21 09:56:12.610' AS CreateTime
|
||||
//UNION ALL
|
||||
// SELECT N'2' AS ID,N'0' AS SchoolId,N'sun' AS Name,NULL AS CreateTime
|
||||
|
||||
|
||||
// ) T ON S.`Id`=T.`Id`
|
||||
// ; ", null, t7.Key, null,"Update t7 error");
|
||||
|
||||
//Re Set Value
|
||||
var t8 = db.Updateable(updateObj)
|
||||
.ReSetValue(it=>it.Name==(it.Name+1)).ToSql();
|
||||
base.Check(@"UPDATE `STudent` SET
|
||||
`SchoolId`=@SchoolId, `Name` =( `Name` + @Const0 ),`CreateTime`=@CreateTime WHERE `Id`=@Id",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@SchoolId",0),
|
||||
new SugarParameter("@ID",1),
|
||||
new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
|
||||
new SugarParameter("@Const0", 1)
|
||||
}, t8.Key, t8.Value, "Update t8 error"
|
||||
);
|
||||
|
||||
//Where By Expression
|
||||
var t9 = db.Updateable(updateObj)
|
||||
.Where(it => it.Id==1).ToSql();
|
||||
base.Check(@"UPDATE `STudent` SET
|
||||
`SchoolId`=@SchoolId,`Name`=@Name,`CreateTime`=@CreateTime WHERE ( `ID` = @Id0 )",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@SchoolId",0),
|
||||
new SugarParameter("@ID",1),
|
||||
new SugarParameter("@Id0",1),
|
||||
new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
|
||||
new SugarParameter("@Name", "jack") },t9.Key,t9.Value,"Upate t9 error"
|
||||
);
|
||||
updateObj.SchoolId = 18;
|
||||
string name = "x";
|
||||
var t10 = db.Updateable<Student>().UpdateColumns(it => new Student() { Name =name, SchoolId=updateObj.SchoolId }).Where(it=>it.Id==11).ToSql();
|
||||
base.Check(@"UPDATE `STudent` SET
|
||||
`SchoolId` = @Const1 , `Name` = @Const0 WHERE ( `ID` = @Id2 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Const1",18),
|
||||
new SugarParameter("@Const0","x"),
|
||||
new SugarParameter("@Id2",11)},
|
||||
t10.Key,
|
||||
t10.Value,
|
||||
"Update 10 error"
|
||||
);
|
||||
}
|
||||
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.Sqlite, IsAutoCloseConnection = true });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
211
Src/Asp.Net/SqliteTest/UnitTest/Updateable.cs
Normal file
211
Src/Asp.Net/SqliteTest/UnitTest/Updateable.cs
Normal file
@@ -0,0 +1,211 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
public static void Updateable()
|
||||
{
|
||||
Db.CodeFirst.InitTables(typeof(UnitUser));
|
||||
Db.DbMaintenance.TruncateTable<UnitUser>();
|
||||
Db.Insertable(new UnitUser() { USER_ID=1,USER_ACCOUNT = "a", USER_PWD = "b", USER_NAME = "c", PWD_LASTCHTIME = DateTime.Now, PWD_ERRORCOUNT = 1, PWD_LASTERRTIME = DateTime.Now }).ExecuteCommand();
|
||||
Db.Updateable(new UnitUser() { USER_ID=1, PWD_LASTERRTIME = null }).WhereColumns(it=> new{ it.PWD_ERRORCOUNT, it.PWD_LASTERRTIME }).ExecuteCommand();
|
||||
Db.CodeFirst.InitTables(typeof(UnitBoolTest));
|
||||
var x = new UnitBoolTest();
|
||||
Db.Updateable<UnitBoolTest>().SetColumns(it => new UnitBoolTest() { BoolValue = !it.BoolValue }).Where(it=>it.Id==1).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>().SetColumns(it => it.BoolValue == !it.BoolValue ).Where(it=>it.Id==1).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>().SetColumns(it => new UnitBoolTest() { BoolValue = x.BoolValue }).Where(it => it.Id == 1).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>().SetColumns(it => it.BoolValue == x.BoolValue).Where(it => it.Id == 1).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>().SetColumns(it => new UnitBoolTest() { BoolValue = !x.BoolValue }).Where(it => it.Id == 1).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>().SetColumns(it => it.BoolValue == !x.BoolValue).Where(it => it.Id == 1).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>(x).ReSetValue(it => it.BoolValue == it.BoolValue).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>(x).ReSetValue(it => it.BoolValue == true).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>(x).ReSetValue(it => it.BoolValue == !it.BoolValue).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>(x).UpdateColumns(it =>new { it.BoolValue }) .ExecuteCommand();
|
||||
|
||||
|
||||
|
||||
UnitSaveDiary saveDiary = new UnitSaveDiary();
|
||||
saveDiary.ID = 2;
|
||||
saveDiary.TypeID = 10;
|
||||
saveDiary.TypeName = "类型100";
|
||||
saveDiary.Title = "标题1000";
|
||||
saveDiary.Content = "内容";
|
||||
saveDiary.Time = DateTime.Now;
|
||||
saveDiary.IsRemind = false;//无论传false/true 最终执行的结果都是以true执行的
|
||||
|
||||
var sql = Db.Updateable<UnitDiary>().SetColumns(it => new UnitDiary()
|
||||
{
|
||||
IsRemind = saveDiary.IsRemind,
|
||||
}).Where(it => it.ID == saveDiary.ID).ToSql();
|
||||
UValidate.Check(sql.Key, @"UPDATE [Diary] SET
|
||||
[IsRemind] = @Const0 WHERE ( [ID] = @ID1 )", "Updateable");
|
||||
|
||||
|
||||
sql = Db.Updateable<UnitDiary>().SetColumns(it => new UnitDiary()
|
||||
{
|
||||
TypeID = saveDiary.TypeID,
|
||||
}).Where(it => it.ID == saveDiary.ID).ToSql();
|
||||
UValidate.Check(sql.Key, @"UPDATE [Diary] SET
|
||||
[TypeID] = @Const0 WHERE ( [ID] = @ID1 )", "Updateable");
|
||||
|
||||
}
|
||||
}
|
||||
public class UnitSaveDiary
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public int TypeID { get; set; }
|
||||
public string TypeName { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Content { get; set; }
|
||||
public DateTime? Time { get; set; }
|
||||
public bool IsRemind { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 日记表
|
||||
/// </summary>
|
||||
[SugarTable("Diary")]
|
||||
public class UnitDiary
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int ID { get; set; }
|
||||
/// <summary>
|
||||
/// 用户ID
|
||||
/// </summary>
|
||||
public int? UserID { get; set; }
|
||||
/// <summary>
|
||||
/// 日记类型ID
|
||||
/// </summary>
|
||||
public int? TypeID { get; set; }
|
||||
/// <summary>
|
||||
/// 日记类型名称
|
||||
/// </summary>
|
||||
public string TypeName { get; set; }
|
||||
/// <summary>
|
||||
/// 标题
|
||||
/// </summary>
|
||||
public string Title { get; set; }
|
||||
/// <summary>
|
||||
/// 内容
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
/// <summary>
|
||||
/// 时间
|
||||
/// </summary>
|
||||
public DateTime? Time { get; set; }
|
||||
/// <summary>
|
||||
/// 是否提醒
|
||||
/// </summary>
|
||||
public bool? IsRemind { get; set; }
|
||||
/// <summary>
|
||||
/// 封面图
|
||||
/// </summary>
|
||||
public string Cover { get; set; }
|
||||
/// <summary>
|
||||
/// 是否为系统日记 1:系统日记 0:用户日记
|
||||
/// </summary>
|
||||
public bool? IsSystem { get; set; }
|
||||
/// <summary>
|
||||
/// 权重(排序)
|
||||
/// </summary>
|
||||
public int? Sequence { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string IP { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public DateTime? CreateTime { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public DateTime? UpdateTime { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public bool? IsDelete { get; set; }
|
||||
}
|
||||
|
||||
public class UnitBoolTest
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey =true)]
|
||||
public int Id { get; set; }
|
||||
public bool BoolValue { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 普通用户表
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class UnitUser
|
||||
{
|
||||
private System.Int64? _USER_ID;
|
||||
/// <summary>
|
||||
/// GUID主键
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = false)]
|
||||
public System.Int64? USER_ID { get { return this._USER_ID; } set { this._USER_ID = value; } }
|
||||
|
||||
private System.String _USER_ACCOUNT;
|
||||
/// <summary>
|
||||
/// 用户账号,不可重名,即使是假删除了,亦不可重复
|
||||
/// </summary>
|
||||
public System.String USER_ACCOUNT { get { return this._USER_ACCOUNT; } set { this._USER_ACCOUNT = value; } }
|
||||
|
||||
private System.String _USER_PWD;
|
||||
/// <summary>
|
||||
/// 用户密码
|
||||
/// </summary>
|
||||
public System.String USER_PWD
|
||||
{
|
||||
get { return this._USER_PWD; }
|
||||
set { this._USER_PWD = value; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 不允许用户密码序列化,可以反序列化
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool ShouldSerializeUSER_PWD()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
private System.String _USER_NAME;
|
||||
/// <summary>
|
||||
/// 用户姓名
|
||||
/// </summary>
|
||||
|
||||
public System.String USER_NAME { get { return this._USER_NAME; } set { this._USER_NAME = value; } }
|
||||
|
||||
private System.Int32 _USER_STATUS;
|
||||
/// <summary>
|
||||
/// 用户状体:10正常;20锁定;99已删除
|
||||
/// </summary>
|
||||
public System.Int32 USER_STATUS { get { return this._USER_STATUS; } set { this._USER_STATUS = value; } }
|
||||
|
||||
private System.DateTime _PWD_LASTCHTIME;
|
||||
/// <summary>
|
||||
/// 最后一次密码更新时间
|
||||
/// </summary>
|
||||
public System.DateTime PWD_LASTCHTIME { get { return this._PWD_LASTCHTIME; } set { this._PWD_LASTCHTIME = value; } }
|
||||
|
||||
private System.Int32? _PWD_ERRORCOUNT;
|
||||
/// <summary>
|
||||
/// 密码错误次数,达到定义的次数就锁定
|
||||
/// </summary>
|
||||
public System.Int32? PWD_ERRORCOUNT { get { return this._PWD_ERRORCOUNT; } set { this._PWD_ERRORCOUNT = value ?? 0; } }
|
||||
|
||||
private System.DateTime? _PWD_LASTERRTIME = null;
|
||||
/// <summary>
|
||||
/// 密码最后一次错误时间,满足定义的时间差之后,可自动解锁,如20分钟后自动解锁,亦作为累次错误次数的时间差比对基础
|
||||
/// 允许为空
|
||||
/// </summary>
|
||||
public System.DateTime? PWD_LASTERRTIME { get { return this._PWD_LASTERRTIME; } set { this._PWD_LASTERRTIME = value; } }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user