This commit is contained in:
28810
2020-01-20 01:48:41 +08:00
parent c64deb3d20
commit c89e4f9b20
7 changed files with 237 additions and 283 deletions

View File

@ -1,61 +0,0 @@
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<T>(Func<T> systemCreate, Func<T> 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<DbConnection>(
() => new System.Data.SQLite.SQLiteConnection(connectionString),
() => new Microsoft.Data.Sqlite.SqliteConnection(connectionString));
public static DbCommand GetSqliteCommand() => PortableAction<DbCommand>(
() => new System.Data.SQLite.SQLiteCommand(),
() => new Microsoft.Data.Sqlite.SqliteCommand());
public static DbParameter GetSqliteParameter() => PortableAction<DbParameter>(
() => new System.Data.SQLite.SQLiteParameter(),
() => new Microsoft.Data.Sqlite.SqliteParameter());
public static bool IsSqliteException(Exception exception) => PortableAction<bool>(
() => 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
}
}

View File

@ -21,13 +21,10 @@
<ItemGroup>
<None Include="../../logo.png" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<ItemGroup >
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.112" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Data.Sqlite" Version="3.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />

View File

@ -4,6 +4,7 @@ using SafeObjectPool;
using System;
using System.Collections;
using System.Data.Common;
using System.Data.SQLite;
using System.Text;
using System.Threading;
@ -18,6 +19,10 @@ namespace FreeSql.Sqlite
if (connectionFactory != null)
{
MasterPool = new FreeSql.Internal.CommonProvider.DbConnectionPool(DataType.Sqlite, connectionFactory);
using (var conn = MasterPool.Get())
{
_CreateCommandConnection = conn.Value;
}
return;
}
if (!string.IsNullOrEmpty(masterConnectionString))
@ -57,9 +62,11 @@ namespace FreeSql.Sqlite
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
}
DbConnection _CreateCommandConnection;
protected override DbCommand CreateCommand()
{
return AdonetPortable.GetSqliteCommand();
if (_CreateCommandConnection != null) return _CreateCommandConnection.CreateCommand();
return new SQLiteCommand();
}
protected override void ReturnConnection(IObjectPool<DbConnection> pool, Object<DbConnection> conn, Exception ex)

View File

@ -4,6 +4,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
@ -34,7 +35,7 @@ namespace FreeSql.Sqlite
public void Return(Object<DbConnection> obj, Exception exception, bool isRecreate = false)
{
if (exception != null && AdonetPortable.IsSqliteException(exception))
if (exception != null && exception is SQLiteException)
{
try { if (obj.Value.Ping() == false) obj.Value.OpenAndAttach(policy.Attaches); } catch { base.SetUnavailable(exception); }
}
@ -121,7 +122,7 @@ namespace FreeSql.Sqlite
public DbConnection OnCreate()
{
var conn = AdonetPortable.GetSqliteConnection(_connectionString);
var conn = new SQLiteConnection(_connectionString);
return conn;
}

View File

@ -38,6 +38,7 @@ namespace FreeSql.Sqlite
this.Aop = new AopProvider();
this.CodeFirst = new SqliteCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
if (connectionFactory != null) this.CodeFirst.IsNoneCommandParameter = true;
}
internal CommonUtils InternalCommonUtils { get; }

View File

@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
namespace FreeSql.Sqlite
{
@ -31,7 +32,7 @@ namespace FreeSql.Sqlite
dbtype = DbType.Int64;
break;
}
var ret = AdonetPortable.GetSqliteParameter();
var ret = new SQLiteParameter();
ret.ParameterName = QuoteParamterName(parameterName);
ret.DbType = dbtype;
ret.Value = value;
@ -56,7 +57,7 @@ namespace FreeSql.Sqlite
dbtype = DbType.Int64;
break;
}
var ret = AdonetPortable.GetSqliteParameter();
var ret = new SQLiteParameter();
ret.ParameterName = $"@{name}";
ret.DbType = dbtype;
ret.Value = value;