mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Add db.Saveable
This commit is contained in:
parent
a21a54098c
commit
afdec7845d
40
Src/Asp.Net/SqlServerTest/Demos/IInsertOrUpdate.cs
Normal file
40
Src/Asp.Net/SqlServerTest/Demos/IInsertOrUpdate.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using OrmTest.Demo;
|
||||
using OrmTest.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class IInsertOrUpdate : DemoBase
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
var db = GetInstance();
|
||||
var entity= db.Insertable<Student>(new Student() { Name = "abc" }).ExecuteReturnEntity();
|
||||
db.Saveable<Student>(entity).ExecuteReturnEntity();
|
||||
//UPDATE [STudent] SET
|
||||
//[SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE[Id] = @Id
|
||||
db.Saveable<Student>(new Student() { Name="" }).ExecuteReturnEntity();
|
||||
// INSERT INTO[STudent]
|
||||
//([SchoolId],[Name],[CreateTime])
|
||||
// VALUES
|
||||
//(@SchoolId, @Name, @CreateTime); SELECT SCOPE_IDENTITY();
|
||||
|
||||
|
||||
db.Saveable<Student>(new Student() { Name = "" }).InsertColumns(it=>it.Name).ExecuteReturnEntity();
|
||||
db.Saveable<Student>(new Student() { Name = "" }).InsertIgnoreColumns(it => it.SchoolId).ExecuteReturnEntity();
|
||||
db.Saveable<Student>(entity).UpdateIgnoreColumns(it=>it.SchoolId).ExecuteReturnEntity();
|
||||
db.Saveable<Student>(entity).UpdateColumns(it=>new { it.Name,it.CreateTime }).ExecuteReturnEntity();
|
||||
|
||||
|
||||
|
||||
db.Saveable<Student>(new List<Student>() {
|
||||
entity,
|
||||
new Student() { Name = "" }
|
||||
}).ExecuteCommand();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -60,6 +60,7 @@ namespace OrmTest
|
||||
Demo.ExtEntity.Init();
|
||||
Demo.VersionValidation.Init();
|
||||
Demo.Delete.Init();
|
||||
Demo.IInsertOrUpdate.Init();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -58,6 +58,7 @@
|
||||
<Compile Include="BugTest\BugModels\tLogonHistoryModel.cs" />
|
||||
<Compile Include="BugTest\BugModels\VipAccountsModel.cs" />
|
||||
<Compile Include="BugTest\BugModels\VipBenefitsModel.cs" />
|
||||
<Compile Include="Demos\IInsertOrUpdate.cs" />
|
||||
<Compile Include="Models\Brand.cs" />
|
||||
<Compile Include="BugTest\Bug1.cs" />
|
||||
<Compile Include="Models\VendorAndBrand.cs" />
|
||||
|
@ -271,7 +271,7 @@ namespace SqlSugar
|
||||
Where(SqlBuilder.SqlFalse);
|
||||
return this;
|
||||
}
|
||||
if (pkValues.Length == 1 && pkValues.First().GetType().FullName.IsCollectionsList())
|
||||
if (pkValues.Length == 1 && pkValues.First().GetType().FullName.IsCollectionsList()|| pkValues.First() is IEnumerable)
|
||||
{
|
||||
var newValues = new List<object>();
|
||||
foreach (var item in pkValues.First() as IEnumerable)
|
||||
|
@ -0,0 +1,178 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class SaveableProvider<T> : ISaveable<T> where T : class, new()
|
||||
{
|
||||
internal SaveableProvider(SqlSugarClient context,List<T> saveObjects)
|
||||
{
|
||||
this.saveObjects = saveObjects;
|
||||
this.Context = context;
|
||||
}
|
||||
internal SaveableProvider(SqlSugarClient context, T saveObject)
|
||||
{
|
||||
this.saveObjects = new List<T>() { saveObject };
|
||||
this.Context = context;
|
||||
}
|
||||
public SqlSugarClient Context { get; set; }
|
||||
public List<T> saveObjects = new List<T>();
|
||||
public List<T> existsObjects = null;
|
||||
public List<T> insertObjects
|
||||
{
|
||||
get
|
||||
{
|
||||
List<T> result = new List<T>();
|
||||
var pks = GetPrimaryKeys();
|
||||
Check.Exception(pks.IsNullOrEmpty(), "Need primary key");
|
||||
Check.Exception(pks.Count() > 1, "Multiple primary keys are not supported");
|
||||
var pkInfo = this.EntityInfo.Columns.Where(it => it.DbColumnName.Equals(pks.First(), StringComparison.CurrentCultureIgnoreCase)).First();
|
||||
var pkValues = saveObjects.Select(it=>it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it,null));
|
||||
if(existsObjects==null)
|
||||
existsObjects=this.Context.Queryable<T>().In(pkValues).ToList();
|
||||
return saveObjects.Where(it=>!
|
||||
existsObjects.Any(e=>
|
||||
e.GetType().GetProperty(pkInfo.PropertyName).GetValue(e,null).ObjToString()
|
||||
==
|
||||
it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it, null).ObjToString())).ToList();
|
||||
}
|
||||
}
|
||||
public List<T> updatObjects
|
||||
{
|
||||
get
|
||||
{
|
||||
List<T> result = new List<T>();
|
||||
var pks = GetPrimaryKeys();
|
||||
Check.Exception(pks.IsNullOrEmpty(), "Need primary key");
|
||||
Check.Exception(pks.Count() > 1, "Multiple primary keys are not supported");
|
||||
var pkInfo = this.EntityInfo.Columns.Where(it => it.DbColumnName.Equals(pks.First(), StringComparison.CurrentCultureIgnoreCase)).First();
|
||||
var pkValues = saveObjects.Select(it => it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it, null));
|
||||
if (existsObjects == null)
|
||||
existsObjects = this.Context.Queryable<T>().In(pkValues).ToList();
|
||||
return saveObjects.Where(it =>
|
||||
existsObjects.Any(e =>
|
||||
e.GetType().GetProperty(pkInfo.PropertyName).GetValue(e, null).ObjToString()
|
||||
==
|
||||
it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it, null).ObjToString())).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public IInsertable<T> insertable { get; set; }
|
||||
public IUpdateable<T> updateable { get; set; }
|
||||
|
||||
public EntityInfo EntityInfo
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
}
|
||||
}
|
||||
|
||||
public int ExecuteCommand()
|
||||
{
|
||||
LoadInsertable();
|
||||
LoadUpdateable();
|
||||
var insertCount = 0;
|
||||
var updateCount = 0;
|
||||
if (insertable != null)
|
||||
{
|
||||
insertCount = insertable.ExecuteCommand();
|
||||
}
|
||||
if (updateable != null)
|
||||
{
|
||||
updateCount = updateable.ExecuteCommand();
|
||||
}
|
||||
return updateCount + insertCount;
|
||||
}
|
||||
|
||||
public T ExecuteReturnEntity()
|
||||
{
|
||||
LoadInsertable();
|
||||
LoadUpdateable();
|
||||
if (insertable != null)
|
||||
insertable.ExecuteCommandIdentityIntoEntity();
|
||||
if (updateable != null)
|
||||
updateable.ExecuteCommand();
|
||||
return saveObjects.First();
|
||||
}
|
||||
|
||||
public List<T> ExecuteReturnList()
|
||||
{
|
||||
LoadInsertable();
|
||||
LoadUpdateable();
|
||||
if (insertable != null)
|
||||
insertable.ExecuteCommand();
|
||||
if (updateable != null)
|
||||
updateable.ExecuteCommand();
|
||||
return saveObjects;
|
||||
}
|
||||
public ISaveable<T> InsertColumns(Expression<Func<T, object>> columns)
|
||||
{
|
||||
LoadInsertable();
|
||||
if (this.insertable != null)
|
||||
{
|
||||
this.insertable.InsertColumns(columns);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISaveable<T> InsertIgnoreColumns(Expression<Func<T, object>> columns)
|
||||
{
|
||||
LoadInsertable();
|
||||
if (this.insertable != null)
|
||||
{
|
||||
this.insertable.IgnoreColumns(columns);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISaveable<T> UpdateColumns(Expression<Func<T, object>> columns)
|
||||
{
|
||||
LoadUpdateable();
|
||||
if (this.updateable != null)
|
||||
{
|
||||
this.updateable.UpdateColumns(columns);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISaveable<T> UpdateIgnoreColumns(Expression<Func<T, object>> columns)
|
||||
{
|
||||
LoadUpdateable();
|
||||
if (this.updateable != null)
|
||||
{
|
||||
this.updateable.IgnoreColumns(columns);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
protected virtual List<string> GetPrimaryKeys()
|
||||
{
|
||||
if (this.Context.IsSystemTablesConfig)
|
||||
{
|
||||
return this.Context.DbMaintenance.GetPrimaries(this.Context.EntityMaintenance.GetTableName(this.EntityInfo.EntityName));
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.EntityInfo.Columns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName).ToList();
|
||||
}
|
||||
}
|
||||
private void LoadInsertable()
|
||||
{
|
||||
var temp = insertObjects;
|
||||
if (insertable == null && temp.HasValue())
|
||||
insertable = this.Context.Insertable<T>(temp);
|
||||
}
|
||||
private void LoadUpdateable()
|
||||
{
|
||||
var temp = updatObjects;
|
||||
if (updateable == null && temp.HasValue())
|
||||
updateable = this.Context.Updateable<T>(temp);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
19
Src/Asp.Net/SqlSugar/Interface/ISaveable.cs
Normal file
19
Src/Asp.Net/SqlSugar/Interface/ISaveable.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial interface ISaveable<T> where T : class, new()
|
||||
{
|
||||
int ExecuteCommand();
|
||||
T ExecuteReturnEntity();
|
||||
List<T> ExecuteReturnList();
|
||||
ISaveable<T> InsertColumns(Expression<Func<T, object>> columns);
|
||||
ISaveable<T> InsertIgnoreColumns(Expression<Func<T, object>> columns);
|
||||
ISaveable<T> UpdateColumns(Expression<Func<T, object>> columns);
|
||||
ISaveable<T> UpdateIgnoreColumns(Expression<Func<T, object>> columns);
|
||||
}
|
||||
}
|
@ -73,6 +73,7 @@
|
||||
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
|
||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||
<Compile Include="Abstract\SaveableProvider\SaveableProvider.cs" />
|
||||
<Compile Include="CacheScheme\CacheKeyBuider.cs" />
|
||||
<Compile Include="CacheScheme\CacheSchemeMain.cs" />
|
||||
<Compile Include="Entities\CacheKey.cs" />
|
||||
@ -116,6 +117,7 @@
|
||||
<Compile Include="ExternalServiceInterface\ISerializeService.cs" />
|
||||
<Compile Include="Entities\DefaultServices.cs" />
|
||||
<Compile Include="Infrastructure\Mapper.cs" />
|
||||
<Compile Include="Interface\ISaveable.cs" />
|
||||
<Compile Include="OnlyNet\PostgreSQLExpressionContext.cs" />
|
||||
<Compile Include="Realization\Oracle\Deleteable\OracleDeleteable.cs" />
|
||||
<Compile Include="Realization\Oracle\Insertable\OracleInsertable.cs" />
|
||||
|
@ -561,6 +561,17 @@ namespace SqlSugar
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Saveable
|
||||
public ISaveable<T> Saveable<T>(List<T> saveObjects)where T:class,new()
|
||||
{
|
||||
return new SaveableProvider<T>(this,saveObjects);
|
||||
}
|
||||
public ISaveable<T> Saveable<T>(T saveObject) where T : class, new()
|
||||
{
|
||||
return new SaveableProvider<T>(this,saveObject);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DbFirst
|
||||
public virtual IDbFirst DbFirst
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user