mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 01:05:27 +08:00 
			
		
		
		
	- 支持 Sqlite :memory: 模式; #191
This commit is contained in:
		@@ -25,7 +25,7 @@ namespace FreeSql.Tests.SqlServer
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
            [Column(IsIdentity = true, IsPrimary = true)]
 | 
					            [Column(IsIdentity = true, IsPrimary = true)]
 | 
				
			||||||
            public int Id { get; set; }
 | 
					            public int Id { get; set; }
 | 
				
			||||||
            public int? Clicks { get; set; }
 | 
					            public int Clicks { get; set; }
 | 
				
			||||||
            public int TypeGuid { get; set; }
 | 
					            public int TypeGuid { get; set; }
 | 
				
			||||||
            public TestTypeInfo Type { get; set; }
 | 
					            public TestTypeInfo Type { get; set; }
 | 
				
			||||||
            public string Title { 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.First().Title, itemsInserted.First().Title);
 | 
				
			||||||
            Assert.Equal(items.Last().Title, itemsInserted.Last().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]
 | 
					        [Fact]
 | 
				
			||||||
        public void AsTable()
 | 
					        public void AsTable()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -71,7 +71,7 @@ public class g
 | 
				
			|||||||
    public static IFreeSql oracle => oracleLazy.Value;
 | 
					    public static IFreeSql oracle => oracleLazy.Value;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    static Lazy<IFreeSql> sqliteLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
 | 
					    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, () =>
 | 
					        //.UseConnectionFactory(FreeSql.DataType.Sqlite, () =>
 | 
				
			||||||
        //{
 | 
					        //{
 | 
				
			||||||
        //    var conn = new System.Data.SQLite.SQLiteConnection(@"Data Source=|DataDirectory|\document.db;Pooling=true;");
 | 
					        //    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 DataType _dataType;
 | 
				
			||||||
        internal Func<DbConnection> _connectionFactory;
 | 
					        internal Func<DbConnection> _connectionFactory;
 | 
				
			||||||
 | 
					        public DbConnection TestConnection { get; }
 | 
				
			||||||
 | 
					        public bool IsSingletonConnection { get; }
 | 
				
			||||||
        int _id;
 | 
					        int _id;
 | 
				
			||||||
        public DbConnectionPool(DataType dataType, Func<DbConnection> connectionFactory)
 | 
					        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;
 | 
					            _dataType = dataType;
 | 
				
			||||||
            _connectionFactory = connectionFactory;
 | 
					            _connectionFactory = connectionFactory;
 | 
				
			||||||
            Policy = new DbConnectionPoolPolicy(this);
 | 
					            Policy = new DbConnectionPoolPolicy(this);
 | 
				
			||||||
@@ -55,6 +81,7 @@ namespace FreeSql.Internal.CommonProvider
 | 
				
			|||||||
        public void Return(Object<DbConnection> obj, bool isReset = false)
 | 
					        public void Return(Object<DbConnection> obj, bool isReset = false)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (obj == null || obj.Value == null) return;
 | 
					            if (obj == null || obj.Value == null) return;
 | 
				
			||||||
 | 
					            if (IsSingletonConnection) return;
 | 
				
			||||||
            if (obj.Value.State != ConnectionState.Closed)
 | 
					            if (obj.Value.State != ConnectionState.Closed)
 | 
				
			||||||
                obj.Value.Close();
 | 
					                obj.Value.Close();
 | 
				
			||||||
            if (_dataType == DataType.Sqlite)
 | 
					            if (_dataType == DataType.Sqlite)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,7 @@ using FreeSql.Internal.Model;
 | 
				
			|||||||
using SafeObjectPool;
 | 
					using SafeObjectPool;
 | 
				
			||||||
using System;
 | 
					using System;
 | 
				
			||||||
using System.Collections;
 | 
					using System.Collections;
 | 
				
			||||||
 | 
					using System.Data;
 | 
				
			||||||
using System.Data.Common;
 | 
					using System.Data.Common;
 | 
				
			||||||
using System.Data.SQLite;
 | 
					using System.Data.SQLite;
 | 
				
			||||||
using System.Text;
 | 
					using System.Text;
 | 
				
			||||||
@@ -18,9 +19,9 @@ namespace FreeSql.Sqlite
 | 
				
			|||||||
            base._util = util;
 | 
					            base._util = util;
 | 
				
			||||||
            if (connectionFactory != null)
 | 
					            if (connectionFactory != null)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                MasterPool = new FreeSql.Internal.CommonProvider.DbConnectionPool(DataType.Sqlite, connectionFactory);
 | 
					                var pool = new FreeSql.Internal.CommonProvider.DbConnectionPool(DataType.Sqlite, connectionFactory);
 | 
				
			||||||
                _CreateCommandConnection = MasterPool.Get().Value;
 | 
					                MasterPool = pool;
 | 
				
			||||||
                _CreateCommandConnection.Close();
 | 
					                _CreateCommandConnection = pool.TestConnection;
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if (!string.IsNullOrEmpty(masterConnectionString))
 | 
					            if (!string.IsNullOrEmpty(masterConnectionString))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -106,6 +106,12 @@ namespace FreeSql.Sqlite
 | 
				
			|||||||
                    _connectionString = string.Concat(att[0], idx == -1 ? "" : att[1].Substring(idx));
 | 
					                    _connectionString = string.Concat(att[0], idx == -1 ? "" : att[1].Substring(idx));
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (_connectionString.ToLower().Contains(":memory:"))
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    //内存模式
 | 
				
			||||||
 | 
					                    PoolSize = 1;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if ns20
 | 
					#if ns20
 | 
				
			||||||
                minPoolSize = 1;
 | 
					                minPoolSize = 1;
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user