mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
- 支持 Sqlite :memory: 模式; #191
This commit is contained in:
parent
15d5a59221
commit
fc4071b730
@ -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<Topic>();
|
||||
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()
|
||||
|
@ -71,7 +71,7 @@ public class g
|
||||
public static IFreeSql oracle => oracleLazy.Value;
|
||||
|
||||
static Lazy<IFreeSql> sqliteLazy = new Lazy<IFreeSql>(() => 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;");
|
||||
|
@ -13,9 +13,35 @@ namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
internal DataType _dataType;
|
||||
internal Func<DbConnection> _connectionFactory;
|
||||
public DbConnection TestConnection { get; }
|
||||
public bool IsSingletonConnection { get; }
|
||||
int _id;
|
||||
public DbConnectionPool(DataType dataType, Func<DbConnection> 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<DbConnection> 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)
|
||||
|
@ -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))
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user