mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
Db.Fastest support sqlite
This commit is contained in:
parent
6134010f97
commit
7da71b94da
@ -133,7 +133,10 @@ namespace SqlSugar
|
||||
await buider.ExecuteBulkCopyAsync(dt);
|
||||
//var queryTemp = this.context.Queryable<T>().AS(dt.TableName).ToList();//test
|
||||
var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns);
|
||||
if (this.context.CurrentConnectionConfig.DbType != DbType.Sqlite)
|
||||
{
|
||||
this.context.DbMaintenance.DropTable(dt.TableName);
|
||||
}
|
||||
this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto;
|
||||
buider.CloseDb();
|
||||
End(datas, false);
|
||||
|
@ -18,7 +18,7 @@ namespace SqlSugar
|
||||
case DbType.SqlServer:
|
||||
return new SqlServerFastBuilder();
|
||||
case DbType.Sqlite:
|
||||
break;
|
||||
return new SqliteFastBuilder(this.entityInfo);
|
||||
case DbType.Oracle:
|
||||
return new OracleFastBuilder(this.entityInfo);
|
||||
case DbType.PostgreSQL:
|
||||
|
@ -0,0 +1,140 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SQLite;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqliteFastBuilder : IFastBuilder
|
||||
{
|
||||
private EntityInfo entityInfo;
|
||||
private bool IsUpdate = false;
|
||||
private DataTable UpdateDataTable { get; set; }
|
||||
public SqliteFastBuilder(EntityInfo entityInfo)
|
||||
{
|
||||
this.entityInfo = entityInfo;
|
||||
}
|
||||
|
||||
public SqlSugarProvider Context { get; set; }
|
||||
|
||||
public void CloseDb()
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection)
|
||||
{
|
||||
this.Context.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CreateTempAsync<T>(DataTable dt) where T : class, new()
|
||||
{
|
||||
await Task.Delay(0);
|
||||
IsUpdate = true;
|
||||
}
|
||||
|
||||
|
||||
public async Task<int> ExecuteBulkCopyAsync(DataTable dt)
|
||||
{
|
||||
if (dt.Rows.Count == 0||IsUpdate)
|
||||
{
|
||||
this.UpdateDataTable = dt;
|
||||
return 0;
|
||||
}
|
||||
foreach (var item in this.entityInfo.Columns)
|
||||
{
|
||||
if (item.IsIdentity && dt.Columns.Contains(item.DbColumnName))
|
||||
{
|
||||
dt.Columns.Remove(item.DbColumnName);
|
||||
}
|
||||
}
|
||||
var dictionary = this.Context.Utilities.DataTableToDictionaryList(dt.Rows.Cast<DataRow>().Take(1).CopyToDataTable());
|
||||
int result = 0;
|
||||
var cn = this.Context.Ado.Connection as SQLiteConnection;
|
||||
Open(cn);
|
||||
if (this.Context.Ado.Transaction == null)
|
||||
{
|
||||
using (var transaction = cn.BeginTransaction())
|
||||
{
|
||||
result = await _BulkCopy(dt, dictionary, result, cn);
|
||||
transaction.Commit();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await _BulkCopy(dt, dictionary, result, cn);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<int> _BulkCopy(DataTable dt, List<Dictionary<string, object>> dictionary, int i, SQLiteConnection cn)
|
||||
{
|
||||
using (var cmd = cn.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = this.Context.Insertable(dictionary.First()).AS(dt.TableName).ToSql().Key.Replace(";SELECT LAST_INSERT_ROWID();","");
|
||||
|
||||
foreach (DataRow dataRow in dt.Rows)
|
||||
{
|
||||
foreach (DataColumn item in dt.Columns)
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@" + item.ColumnName, dataRow[item.ColumnName]);
|
||||
}
|
||||
i += await cmd.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
private async Task<int> _BulkUpdate(DataTable dt, List<Dictionary<string, object>> dictionary, int i,string [] whereColums,string [] updateColums, SQLiteConnection cn)
|
||||
{
|
||||
using (var cmd = cn.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = this.Context.Updateable(dictionary.First())
|
||||
.WhereColumns(whereColums)
|
||||
.UpdateColumns(updateColums)
|
||||
.AS(dt.TableName).ToSql().Key;
|
||||
|
||||
foreach (DataRow dataRow in dt.Rows)
|
||||
{
|
||||
foreach (DataColumn item in dt.Columns)
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@" + item.ColumnName, dataRow[item.ColumnName]);
|
||||
}
|
||||
i += await cmd.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
private static void Open(SQLiteConnection cn)
|
||||
{
|
||||
if (cn.State != ConnectionState.Open)
|
||||
cn.Open();
|
||||
}
|
||||
|
||||
public async Task<int> UpdateByTempAsync(string tableName, string tempName, string[] updateColumns, string[] whereColumns)
|
||||
{
|
||||
var dt = UpdateDataTable;
|
||||
if (dt.Rows.Count == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
var dictionary = this.Context.Utilities.DataTableToDictionaryList(dt.Rows.Cast<DataRow>().Take(1).CopyToDataTable());
|
||||
int result = 0;
|
||||
var cn = this.Context.Ado.Connection as SQLiteConnection;
|
||||
Open(cn);
|
||||
if (this.Context.Ado.Transaction == null)
|
||||
{
|
||||
using (var transaction = cn.BeginTransaction())
|
||||
{
|
||||
result = await _BulkUpdate(dt, dictionary, result,whereColumns,updateColumns, cn);
|
||||
transaction.Commit();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await _BulkUpdate(dt, dictionary, result, whereColumns, updateColumns, cn);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -104,6 +104,7 @@
|
||||
<Compile Include="OnlyNet\OracleFastBuilder.cs" />
|
||||
<Compile Include="Realization\MySql\SqlBuilder\MySqlFastBuilder.cs" />
|
||||
<Compile Include="Realization\PostgreSQL\SqlBuilder\PostgreSQLFastBuilder.cs" />
|
||||
<Compile Include="Realization\Sqlite\SqlBuilder\SqliteFastBuilder.cs" />
|
||||
<Compile Include="Realization\SqlServer\SqlBuilder\SqlServerFastBuilder.cs" />
|
||||
<Compile Include="SpliteTable\SplitTableAttribute.cs" />
|
||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||
|
@ -82,6 +82,7 @@
|
||||
<Compile Include="UnitTest\Models\ExToCon.cs" />
|
||||
<Compile Include="UnitTest\Models\LoadCon.cs" />
|
||||
<Compile Include="UnitTest\UAdo.cs" />
|
||||
<Compile Include="UnitTest\UBulkCopy.cs" />
|
||||
<Compile Include="UnitTest\UCodeFirst.cs" />
|
||||
<Compile Include="UnitTest\UCustom01.cs" />
|
||||
<Compile Include="UnitTest\UCustom02.cs" />
|
||||
|
@ -31,8 +31,8 @@ namespace OrmTest
|
||||
}
|
||||
public static void Init()
|
||||
{
|
||||
UCustom01.Init();
|
||||
UCustom02.Init();
|
||||
|
||||
UBulkCopy.Init();
|
||||
Insert();
|
||||
CodeFirst();
|
||||
Updateable();
|
||||
|
43
Src/Asp.Net/SqliteTest/UnitTest/UBulkCopy.cs
Normal file
43
Src/Asp.Net/SqliteTest/UnitTest/UBulkCopy.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using F9.DataEntity.Entity;
|
||||
using SqlSugar;
|
||||
namespace OrmTest
|
||||
{
|
||||
public class UBulkCopy
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
var db = NewUnitTest.Db;
|
||||
db.DbMaintenance.CreateDatabase();
|
||||
db.DbMaintenance.TruncateTable<UBulkCopydsafad1>();
|
||||
db.Aop.OnLogExecuting = null;
|
||||
db.CodeFirst.InitTables<UBulkCopydsafad1>();
|
||||
var list = new List<UBulkCopydsafad1>();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
list.Add(new UBulkCopydsafad1() { name = "a", name1 = "x" + i, name2 = "a" });
|
||||
}
|
||||
// db.BeginTran();
|
||||
// db.Insertable(list).ExecuteCommand();
|
||||
var list2 = db.Queryable<UBulkCopydsafad1>().ToList();
|
||||
db.Fastest<UBulkCopydsafad1>().BulkCopy(list);
|
||||
var list3 = db.Queryable<UBulkCopydsafad1>().ToList();
|
||||
var updateRows= db.Fastest<UBulkCopydsafad1>().BulkUpdate(list3);
|
||||
//db.CommitTran();
|
||||
}
|
||||
public class UBulkCopydsafad1
|
||||
{
|
||||
[SugarColumn(IsIdentity =true,IsPrimaryKey =true)]
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
public string name1 { get; set; }
|
||||
public string name2 { get; set; }
|
||||
public int id2 { get; set; }
|
||||
public int id3 { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user