From fc4071b73060f240584e9af1ba219324a53d1918 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Tue, 21 Jan 2020 11:36:01 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E6=94=AF=E6=8C=81=20Sqlite=20:memory:=20?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=EF=BC=9B=20#191?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SqlServer/Curd/SqlServerInsertTest.cs | 11 +++++++- FreeSql.Tests/FreeSql.Tests/g.cs | 2 +- .../AdoProvider/DbConnectionPool.cs | 27 +++++++++++++++++++ .../SqliteAdo/SqliteAdo.cs | 7 ++--- .../SqliteAdo/SqliteConnectionPool.cs | 6 +++++ 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerInsertTest.cs b/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerInsertTest.cs index c25d5c68..356f5fcf 100644 --- a/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerInsertTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerInsertTest.cs @@ -25,7 +25,7 @@ namespace FreeSql.Tests.SqlServer { [Column(IsIdentity = true, IsPrimary = true)] public int Id { get; set; } - public int? Clicks { get; set; } + public int Clicks { get; set; } public int TypeGuid { get; set; } public TestTypeInfo Type { get; set; } public string Title { get; set; } @@ -164,6 +164,15 @@ namespace FreeSql.Tests.SqlServer Assert.Equal(items.First().Title, itemsInserted.First().Title); Assert.Equal(items.Last().Title, itemsInserted.Last().Title); } + [Fact] + public void ExecuteSqlBulkCopy() + { + var items = new List(); + for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now }); + + insert.AppendData(items).InsertIdentity().ExecuteSqlBulkCopy(); + // System.NotSupportedException:“DataSet does not support System.Nullable<>.” + } [Fact] public void AsTable() diff --git a/FreeSql.Tests/FreeSql.Tests/g.cs b/FreeSql.Tests/FreeSql.Tests/g.cs index 1f5f3c64..985eae8b 100644 --- a/FreeSql.Tests/FreeSql.Tests/g.cs +++ b/FreeSql.Tests/FreeSql.Tests/g.cs @@ -71,7 +71,7 @@ public class g public static IFreeSql oracle => oracleLazy.Value; static Lazy sqliteLazy = new Lazy(() => new FreeSql.FreeSqlBuilder() - .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Attachs=xxxtb.db;Pooling=true;Max Pool Size=2") + .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Attachs=xxxtb.db;") //.UseConnectionFactory(FreeSql.DataType.Sqlite, () => //{ // var conn = new System.Data.SQLite.SQLiteConnection(@"Data Source=|DataDirectory|\document.db;Pooling=true;"); diff --git a/FreeSql/Internal/CommonProvider/AdoProvider/DbConnectionPool.cs b/FreeSql/Internal/CommonProvider/AdoProvider/DbConnectionPool.cs index a336446b..d3be116e 100644 --- a/FreeSql/Internal/CommonProvider/AdoProvider/DbConnectionPool.cs +++ b/FreeSql/Internal/CommonProvider/AdoProvider/DbConnectionPool.cs @@ -13,9 +13,35 @@ namespace FreeSql.Internal.CommonProvider { internal DataType _dataType; internal Func _connectionFactory; + public DbConnection TestConnection { get; } + public bool IsSingletonConnection { get; } int _id; public DbConnectionPool(DataType dataType, Func connectionFactory) { + #region Test connectionFactory + //情况1:() => new SqlConnection(...) + //情况2:() => conn + DbConnection conn1 = null; + DbConnection conn2 = null; + try + { + conn1 = connectionFactory(); //测试 conn + conn2 = connectionFactory(); + + TestConnection = conn1; //赋值创建 Command,兼容 Mono.Data.Sqlite + IsSingletonConnection = conn1 == conn2; + } + catch { } + finally + { + if (conn1 != conn2) + { + if (conn1?.State == ConnectionState.Open) try { conn1?.Close(); } catch { } + if (conn2?.State == ConnectionState.Open) try { conn2?.Close(); } catch { } + } + } + #endregion + _dataType = dataType; _connectionFactory = connectionFactory; Policy = new DbConnectionPoolPolicy(this); @@ -55,6 +81,7 @@ namespace FreeSql.Internal.CommonProvider public void Return(Object obj, bool isReset = false) { if (obj == null || obj.Value == null) return; + if (IsSingletonConnection) return; if (obj.Value.State != ConnectionState.Closed) obj.Value.Close(); if (_dataType == DataType.Sqlite) diff --git a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs index 5e44f876..09ba12df 100644 --- a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs +++ b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs @@ -3,6 +3,7 @@ using FreeSql.Internal.Model; using SafeObjectPool; using System; using System.Collections; +using System.Data; using System.Data.Common; using System.Data.SQLite; using System.Text; @@ -18,9 +19,9 @@ namespace FreeSql.Sqlite base._util = util; if (connectionFactory != null) { - MasterPool = new FreeSql.Internal.CommonProvider.DbConnectionPool(DataType.Sqlite, connectionFactory); - _CreateCommandConnection = MasterPool.Get().Value; - _CreateCommandConnection.Close(); + var pool = new FreeSql.Internal.CommonProvider.DbConnectionPool(DataType.Sqlite, connectionFactory); + MasterPool = pool; + _CreateCommandConnection = pool.TestConnection; return; } if (!string.IsNullOrEmpty(masterConnectionString)) diff --git a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs index 5257b7e0..fb3ac566 100644 --- a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs +++ b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs @@ -106,6 +106,12 @@ namespace FreeSql.Sqlite _connectionString = string.Concat(att[0], idx == -1 ? "" : att[1].Substring(idx)); } + if (_connectionString.ToLower().Contains(":memory:")) + { + //内存模式 + PoolSize = 1; + } + #if ns20 minPoolSize = 1; #endif