Skip to content
Permalink
Browse files

- 支持 Sqlite :memory: 模式; #191

  • Loading branch information
28810 28810
28810 authored and 28810 committed Jan 21, 2020
1 parent 15d5a59 commit fc4071b73060f240584e9af1ba219324a53d1918
@@ -25,7 +25,7 @@ class Topic
{
[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 @@ public void ExecuteInserted()
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 @@ public class DbConnectionPool : IObjectPool<DbConnection>
{
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 @@ async public Task<Object<DbConnection>> GetAsync()
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 SafeObjectPool;
using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Text;
@@ -18,9 +19,9 @@ public SqliteAdo(CommonUtils util, string masterConnectionString, string[] slave
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 @@ public string ConnectionString
_connectionString = string.Concat(att[0], idx == -1 ? "" : att[1].Substring(idx));
}

if (_connectionString.ToLower().Contains(":memory:"))
{
//内存模式
PoolSize = 1;
}

#if ns20
minPoolSize = 1;
#endif

0 comments on commit fc4071b

Please sign in to comment.
You can’t perform that action at this time.