diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 3f9fb047..2711e35d 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -99,6 +99,13 @@ 清空状态数据 + + + 根据 lambda 条件删除数据 + + + + 添加 diff --git a/Providers/FreeSql.Provider.Sqlite/AdonetPortable.cs b/Providers/FreeSql.Provider.Sqlite/AdonetPortable.cs new file mode 100644 index 00000000..6d8dcbf4 --- /dev/null +++ b/Providers/FreeSql.Provider.Sqlite/AdonetPortable.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Data.Common; +using System.Reflection; +using System.Text; + +namespace FreeSql.Sqlite +{ + internal class AdonetPortable + { + +#if ns20 + static bool _IsMicrosoft_Data_Sqlite; + static object _IsMicrosoft_Data_SqliteLock = new object(); + + static T PortableAction(Func systemCreate, Func microsoftCreate) + { + if (_IsMicrosoft_Data_Sqlite == false) + { + try + { + return systemCreate(); + } + catch + { + lock (_IsMicrosoft_Data_SqliteLock) + { + _IsMicrosoft_Data_Sqlite = true; + } + } + } + return microsoftCreate(); + } + + public static DbConnection GetSqliteConnection(string connectionString) => PortableAction( + () => new System.Data.SQLite.SQLiteConnection(connectionString), + () => new Microsoft.Data.Sqlite.SqliteConnection(connectionString)); + + public static DbCommand GetSqliteCommand() => PortableAction( + () => new System.Data.SQLite.SQLiteCommand(), + () => new Microsoft.Data.Sqlite.SqliteCommand()); + + public static DbParameter GetSqliteParameter() => PortableAction( + () => new System.Data.SQLite.SQLiteParameter(), + () => new Microsoft.Data.Sqlite.SqliteParameter()); + + public static bool IsSqliteException(Exception exception) => PortableAction( + () => exception is System.Data.SQLite.SQLiteException, + () => exception is Microsoft.Data.Sqlite.SqliteException); +#else + + public static DbConnection GetSqliteConnection(string connectionString) => new System.Data.SQLite.SQLiteConnection(connectionString); + + public static DbCommand GetSqliteCommand() => new System.Data.SQLite.SQLiteCommand(); + + public static DbParameter GetSqliteParameter() => new System.Data.SQLite.SQLiteParameter(); + + public static bool IsSqliteException(Exception exception) => exception is System.Data.SQLite.SQLiteException; +#endif + } +} diff --git a/Providers/FreeSql.Provider.Sqlite/FreeSql.Provider.Sqlite.csproj b/Providers/FreeSql.Provider.Sqlite/FreeSql.Provider.Sqlite.csproj index f1d19624..640876ae 100644 --- a/Providers/FreeSql.Provider.Sqlite/FreeSql.Provider.Sqlite.csproj +++ b/Providers/FreeSql.Provider.Sqlite/FreeSql.Provider.Sqlite.csproj @@ -2,7 +2,7 @@ netstandard2.0;net45;net40 - 0.11.9 + 0.11.9.2 true YeXiangQin FreeSql 数据库实现,基于 Sqlite 3.0 @@ -25,11 +25,17 @@ + + + + + ns20;netstandard20 + net40 diff --git a/Providers/FreeSql.Provider.Sqlite/MonoAdapter.cs b/Providers/FreeSql.Provider.Sqlite/MonoAdapter.cs deleted file mode 100644 index ae762497..00000000 --- a/Providers/FreeSql.Provider.Sqlite/MonoAdapter.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Data.Common; -using System.Reflection; -using System.Text; - -namespace FreeSql.Sqlite -{ - internal class MonoAdapter - { - - static bool? _isMono; - static object _isMonoLock = new object(); - static Assembly _monoAssemly; - static Type _monoSqliteConnectionType; - static Type _monoSqliteCommandType; - static Type _monoSqliteParameterType; - static Type _monoSqliteExceptionType; - - static bool IsMono - { - get - { - if (_isMono != null) return _isMono == true; - lock (_isMonoLock) - { - Assembly ass = null; - try - { - ass = Assembly.Load("Mono.Data.Sqlite"); - } - catch { } - _isMono = ass != null; - if (_isMono == false) return false; - - _monoAssemly = ass; - _monoSqliteConnectionType = _monoAssemly.GetType("Mono.Data.Sqlite.SqliteConnection"); - _monoSqliteCommandType = _monoAssemly.GetType("Mono.Data.Sqlite.SqliteCommand"); - _monoSqliteParameterType = _monoAssemly.GetType("Mono.Data.Sqlite.SqliteParameter"); - _monoSqliteExceptionType = _monoAssemly.GetType("Mono.Data.Sqlite.SqliteException"); - } - return true; - } - } - - public static DbConnection GetSqliteConnection(string connectionString) - { - if (IsMono == false) return new System.Data.SQLite.SQLiteConnection(connectionString); - return Activator.CreateInstance(_monoSqliteConnectionType, new object[] { connectionString }) as DbConnection; - } - - public static DbCommand GetSqliteCommand() - { - if (IsMono == false) return new System.Data.SQLite.SQLiteCommand(); - return Activator.CreateInstance(_monoSqliteCommandType, new object[0]) as DbCommand; - } - - public static DbParameter GetSqliteParameter() - { - if (IsMono == false) return new System.Data.SQLite.SQLiteParameter(); - return Activator.CreateInstance(_monoSqliteParameterType, new object[0]) as DbParameter; - } - - public static bool IsSqliteException(Exception exception) - { - if (exception == null) return false; - if (IsMono == false) return exception is System.Data.SQLite.SQLiteException; - return exception.GetType() == _monoSqliteExceptionType; - } - - } -} diff --git a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs index f1217038..d8df365d 100644 --- a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs +++ b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs @@ -55,7 +55,7 @@ namespace FreeSql.Sqlite protected override DbCommand CreateCommand() { - return MonoAdapter.GetSqliteCommand(); + return AdonetPortable.GetSqliteCommand(); } protected override void ReturnConnection(ObjectPool pool, Object conn, Exception ex) diff --git a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs index 0aac8b70..a6805118 100644 --- a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs +++ b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs @@ -34,7 +34,7 @@ namespace FreeSql.Sqlite public void Return(Object obj, Exception exception, bool isRecreate = false) { - if (exception != null && MonoAdapter.IsSqliteException(exception)) + if (exception != null && AdonetPortable.IsSqliteException(exception)) { try { if (obj.Value.Ping() == false) obj.Value.OpenAndAttach(policy.Attaches); } catch { base.SetUnavailable(exception); } } @@ -57,7 +57,6 @@ namespace FreeSql.Sqlite public int CheckAvailableInterval { get; set; } = 5; public string[] Attaches = new string[0]; - static ConcurrentDictionary dicConnStrIncr = new ConcurrentDictionary(StringComparer.CurrentCultureIgnoreCase); private string _connectionString; public string ConnectionString { @@ -68,12 +67,11 @@ namespace FreeSql.Sqlite var pattern = @"Max\s*pool\s*size\s*=\s*(\d+)"; Match m = Regex.Match(_connectionString, pattern, RegexOptions.IgnoreCase); - if (m.Success == false || int.TryParse(m.Groups[1].Value, out var poolsize) == false || poolsize <= 0) poolsize = 100; - var connStrIncr = dicConnStrIncr.AddOrUpdate(_connectionString, 1, (oldkey, oldval) => oldval + 1); - PoolSize = poolsize + connStrIncr; - _connectionString = m.Success ? - Regex.Replace(_connectionString, pattern, $"Max pool size={PoolSize}", RegexOptions.IgnoreCase) : - $"{_connectionString};Max pool size={PoolSize}"; + if (m.Success) + { + PoolSize = int.Parse(m.Groups[1].Value); + _connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase); + } pattern = @"Connection\s*LifeTime\s*=\s*(\d+)"; m = Regex.Match(_connectionString, pattern, RegexOptions.IgnoreCase); @@ -92,14 +90,26 @@ namespace FreeSql.Sqlite _connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase); } - var att = Regex.Split(_connectionString, @"Attachs\s*=\s*", RegexOptions.IgnoreCase); + var att = Regex.Split(_connectionString, @"Pooling\s*=\s*", RegexOptions.IgnoreCase); + if (att.Length == 2) + { + var idx = att[1].IndexOf(';'); + _connectionString = string.Concat(att[0], idx == -1 ? "" : att[1].Substring(idx)); + } + + att = Regex.Split(_connectionString, @"Attachs\s*=\s*", RegexOptions.IgnoreCase); if (att.Length == 2) { var idx = att[1].IndexOf(';'); Attaches = (idx == -1 ? att[1] : att[1].Substring(0, idx)).Split(','); + _connectionString = string.Concat(att[0], idx == -1 ? "" : att[1].Substring(idx)); } +#if ns20 + minPoolSize = 1; +#endif FreeSql.Internal.CommonUtils.PrevReheatConnectionPool(_pool, minPoolSize); + } } @@ -111,7 +121,7 @@ namespace FreeSql.Sqlite public DbConnection OnCreate() { - var conn = MonoAdapter.GetSqliteConnection(_connectionString); + var conn = AdonetPortable.GetSqliteConnection(_connectionString); return conn; } diff --git a/Providers/FreeSql.Provider.Sqlite/SqliteUtils.cs b/Providers/FreeSql.Provider.Sqlite/SqliteUtils.cs index e3c7b46e..9f862c74 100644 --- a/Providers/FreeSql.Provider.Sqlite/SqliteUtils.cs +++ b/Providers/FreeSql.Provider.Sqlite/SqliteUtils.cs @@ -33,7 +33,7 @@ namespace FreeSql.Sqlite dbtype = DbType.Int64; break; } - var ret = MonoAdapter.GetSqliteParameter(); + var ret = AdonetPortable.GetSqliteParameter(); ret.ParameterName = QuoteParamterName(parameterName); ret.DbType = dbtype; ret.Value = value; @@ -58,7 +58,7 @@ namespace FreeSql.Sqlite dbtype = DbType.Int64; break; } - var ret = MonoAdapter.GetSqliteParameter(); + var ret = AdonetPortable.GetSqliteParameter(); ret.ParameterName = $"@{name}"; ret.DbType = dbtype; ret.Value = value;