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

153 lines
5.7 KiB

8 years ago
using OrmTest.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest.UnitTest
{
8 years ago
public class Insert : UnitTestBase
8 years ago
{
private Insert() { }
public Insert(int eachCount)
{
this.Count = eachCount;
}
8 years ago
public void Init()
{
8 years ago
var db = GetInstance();
8 years ago
var insertObj = new Student() { Name = "jack", CreateTime = Convert.ToDateTime("2010-1-1"), SchoolId=0 };
8 years ago
db.IgnoreColumns.Add("TestId", "Student");
8 years ago
//db.MappingColumns.Add("id","dbid", "Student");
8 years ago
8 years ago
var t1 = db.Insertable(insertObj).ToSql();
8 years ago
base.Check(@"INSERT INTO [STudent]
8 years ago
([SchoolId],[Name],[CreateTime])
VALUES
(@SchoolId,@Name,@CreateTime) ;SELECT SCOPE_IDENTITY();",
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"
);
8 years ago
8 years ago
8 years ago
//Insert reutrn Command Count
8 years ago
var t2 = db.Insertable(insertObj).ExecuteCommand();
8 years ago
8 years ago
db.IgnoreColumns = null;
8 years ago
//Only insert Name
8 years ago
var t3 = db.Insertable(insertObj).InsertColumns(it => new { it.Name }).ToSql();
8 years ago
base.Check(@"INSERT INTO [STudent]
8 years ago
([Name])
VALUES
(@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
new SugarParameter("@Name","jack")
}, t3.Key, t3.Value, "Insert t3 error");
8 years ago
//Ignore Name and TestId
8 years ago
var t4 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ToSql();
8 years ago
base.Check(@"INSERT INTO [STudent]
8 years ago
([SchoolId],[CreateTime])
VALUES
(@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();",
new List<SugarParameter>() {
new SugarParameter("@SchoolId",0),
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
}, t4.Key, t4.Value, "Insert t4 error"
);
8 years ago
8 years ago
//Ignore Name and TestId
8 years ago
var t5 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").With(SqlWith.UpdLock).ToSql();
8 years ago
base.Check(@"INSERT INTO [STudent] WITH(UPDLOCK)
8 years ago
([SchoolId],[CreateTime])
VALUES
(@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();",
new List<SugarParameter>() {
new SugarParameter("@SchoolId",0),
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
}, t5.Key, t5.Value, "Insert t5 error"
);
8 years ago
//Use Lock
8 years ago
var t6 = db.Insertable(insertObj).With(SqlWith.UpdLock).ToSql();
8 years ago
base.Check(@"INSERT INTO [STudent] WITH(UPDLOCK)
([SchoolId],[Name],[CreateTime])
8 years ago
VALUES
8 years ago
(@SchoolId,@Name,@CreateTime) ;SELECT SCOPE_IDENTITY();",
8 years ago
new List<SugarParameter>() {
new SugarParameter("@SchoolId",0),
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
8 years ago
new SugarParameter("@Name","jack")
8 years ago
}, t6.Key, t6.Value, "Insert t6 error"
);
8 years ago
8 years ago
var insertObj2 = new Student() { Name = null,SchoolId=0, CreateTime = Convert.ToDateTime("2010-1-1") };
8 years ago
var t8 = db.Insertable(insertObj2).Where(true/* Is insert null */, true/*off identity*/).ToSql();
8 years ago
base.Check(@"INSERT INTO [STudent]
([ID],[SchoolId],[CreateTime])
8 years ago
VALUES
(@ID,@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();",
8 years ago
new List<SugarParameter>() {
new SugarParameter("@SchoolId", 0),
new SugarParameter("@ID", 0),
8 years ago
new SugarParameter("@CreateTime", Convert.ToDateTime("2010-1-1"))
8 years ago
},
t8.Key,
t8.Value,
"Insert t8 error"
);
8 years ago
7 years ago
db.IgnoreColumns = new IgnoreColumnList();
8 years ago
db.IgnoreColumns.Add("TestId", "Student");
8 years ago
8 years ago
//Insert List<T>
8 years ago
var insertObjs = new List<Student>();
for (int i = 0; i < 1000; i++)
{
8 years ago
insertObjs.Add(new Student() { Name = "name" + i });
8 years ago
}
8 years ago
var s9 = db.Insertable(insertObjs.ToArray()).InsertColumns(it => new { it.Name }).With(SqlWith.UpdLock).ToSql();
8 years ago
insertObj.Name = null;
var t10 = db.Insertable(insertObj).ExecuteCommand();
7 years ago
var t11 = db.Insertable(new MyStudent() { Id = 1, Name = "张三" }).AS("Student").ToSql();
7 years ago
base.Check(@"INSERT INTO [Student]
([Name])
VALUES
(@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
new SugarParameter("@Name","张三")
}, t11.Key, t11.Value, "Insert t11 error");
7 years ago
var t12 = db.Insertable<Student>(new { Name = "a" }).ToSql();
base.Check(@"INSERT INTO [STudent]
([Name])
VALUES
(@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
new SugarParameter("@Name","a")
}, t12.Key, t12.Value, "Insert t12 error");
var t13 = db.Insertable<Student>(new Dictionary<string, object>() { {"id",0 },{ "name","2"} }).ToSql();
base.Check(@"INSERT INTO [STudent]
([Name])
VALUES
(@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
new SugarParameter("@Name","2")
}, t13.Key, t13.Value, "Insert t13 error");
8 years ago
}
}
7 years ago
public class MyStudent {
public int Id { get; set; }
public string Name { get; set; }
}
8 years ago
}