diff --git a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/FastestProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/FastestProvider.cs index 6305ea98d..e8fa15590 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/FastestProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/FastestProvider.cs @@ -133,7 +133,10 @@ namespace SqlSugar await buider.ExecuteBulkCopyAsync(dt); //var queryTemp = this.context.Queryable().AS(dt.TableName).ToList();//test var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns); - this.context.DbMaintenance.DropTable(dt.TableName); + if (this.context.CurrentConnectionConfig.DbType != DbType.Sqlite) + { + this.context.DbMaintenance.DropTable(dt.TableName); + } this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto; buider.CloseDb(); End(datas, false); diff --git a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs index e85aa43d3..3e928bc8d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs @@ -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: diff --git a/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteFastBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteFastBuilder.cs new file mode 100644 index 000000000..6e5e4da3e --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteFastBuilder.cs @@ -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(DataTable dt) where T : class, new() + { + await Task.Delay(0); + IsUpdate = true; + } + + + public async Task 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().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 _BulkCopy(DataTable dt, List> 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 _BulkUpdate(DataTable dt, List> 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 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().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; + } + } +} diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index 58d3a0451..8786263da 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -104,6 +104,7 @@ + diff --git a/Src/Asp.Net/SqliteTest/SqliteTest.csproj b/Src/Asp.Net/SqliteTest/SqliteTest.csproj index d8f59f483..3ad26700d 100644 --- a/Src/Asp.Net/SqliteTest/SqliteTest.csproj +++ b/Src/Asp.Net/SqliteTest/SqliteTest.csproj @@ -82,6 +82,7 @@ + diff --git a/Src/Asp.Net/SqliteTest/UnitTest/Main.cs b/Src/Asp.Net/SqliteTest/UnitTest/Main.cs index cf9508ef4..f416bee38 100644 --- a/Src/Asp.Net/SqliteTest/UnitTest/Main.cs +++ b/Src/Asp.Net/SqliteTest/UnitTest/Main.cs @@ -31,8 +31,8 @@ namespace OrmTest } public static void Init() { - UCustom01.Init(); - UCustom02.Init(); + + UBulkCopy.Init(); Insert(); CodeFirst(); Updateable(); diff --git a/Src/Asp.Net/SqliteTest/UnitTest/UBulkCopy.cs b/Src/Asp.Net/SqliteTest/UnitTest/UBulkCopy.cs new file mode 100644 index 000000000..93c5f7640 --- /dev/null +++ b/Src/Asp.Net/SqliteTest/UnitTest/UBulkCopy.cs @@ -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(); + db.Aop.OnLogExecuting = null; + db.CodeFirst.InitTables(); + var list = new List(); + 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().ToList(); + db.Fastest().BulkCopy(list); + var list3 = db.Queryable().ToList(); + var updateRows= db.Fastest().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; } + } + } +}