mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
- 增加 FreeSql.Provider.Sqlite 对 Xamarin 环境下的适配;
This commit is contained in:
parent
13810c4422
commit
8ec8daa6be
@ -1996,6 +1996,137 @@
|
|||||||
<param name="parms"></param>
|
<param name="parms"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteReaderAsync(System.Func{System.Data.Common.DbDataReader,System.Threading.Tasks.Task},System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||||
|
<summary>
|
||||||
|
查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】
|
||||||
|
</summary>
|
||||||
|
<param name="readerHander"></param>
|
||||||
|
<param name="cmdType"></param>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="cmdParms"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteReaderAsync(System.Func{System.Data.Common.DbDataReader,System.Threading.Tasks.Task},System.String,System.Object)">
|
||||||
|
<summary>
|
||||||
|
查询,ExecuteReaderAsync(dr => {}, "select * from user where age > @age", new { age = 25 })
|
||||||
|
</summary>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="parms"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteArrayAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||||
|
<summary>
|
||||||
|
查询
|
||||||
|
</summary>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="cmdParms"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteArrayAsync(System.String,System.Object)">
|
||||||
|
<summary>
|
||||||
|
查询,ExecuteArrayAsync("select * from user where age > @age", new { age = 25 })
|
||||||
|
</summary>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="parms"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteDataSetAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||||
|
<summary>
|
||||||
|
查询
|
||||||
|
</summary>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="cmdParms"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteDataSetAsync(System.String,System.Object)">
|
||||||
|
<summary>
|
||||||
|
查询,ExecuteDataSetAsync("select * from user where age > @age; select 2", new { age = 25 })
|
||||||
|
</summary>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="parms"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteDataTableAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||||
|
<summary>
|
||||||
|
查询
|
||||||
|
</summary>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="cmdParms"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteDataTableAsync(System.String,System.Object)">
|
||||||
|
<summary>
|
||||||
|
查询,ExecuteDataTableAsync("select * from user where age > @age", new { age = 25 })
|
||||||
|
</summary>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="parms"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteNonQueryAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||||
|
<summary>
|
||||||
|
在【主库】执行
|
||||||
|
</summary>
|
||||||
|
<param name="cmdType"></param>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="cmdParms"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteNonQueryAsync(System.String,System.Object)">
|
||||||
|
<summary>
|
||||||
|
在【主库】执行,ExecuteNonQueryAsync("delete from user where age > @age", new { age = 25 })
|
||||||
|
</summary>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="parms"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteScalarAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||||
|
<summary>
|
||||||
|
在【主库】执行
|
||||||
|
</summary>
|
||||||
|
<param name="cmdType"></param>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="cmdParms"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.ExecuteScalarAsync(System.String,System.Object)">
|
||||||
|
<summary>
|
||||||
|
在【主库】执行,ExecuteScalarAsync("select 1 from user where age > @age", new { age = 25 })
|
||||||
|
</summary>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="parms"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.QueryAsync``1(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||||
|
<summary>
|
||||||
|
执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new SqlParameter { ParameterName = "age", Value = 25 })
|
||||||
|
</summary>
|
||||||
|
<typeparam name="T"></typeparam>
|
||||||
|
<param name="cmdType"></param>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="cmdParms"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.QueryAsync``1(System.String,System.Object)">
|
||||||
|
<summary>
|
||||||
|
执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new { age = 25 })
|
||||||
|
</summary>
|
||||||
|
<typeparam name="T"></typeparam>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="parms"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.QueryAsync``2(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||||
|
<summary>
|
||||||
|
执行SQL返回对象集合,Query<User>("select * from user where age > @age; select * from address", new SqlParameter { ParameterName = "age", Value = 25 })
|
||||||
|
</summary>
|
||||||
|
<typeparam name="T1"></typeparam>
|
||||||
|
<param name="cmdType"></param>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="cmdParms"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.IAdo.QueryAsync``2(System.String,System.Object)">
|
||||||
|
<summary>
|
||||||
|
执行SQL返回对象集合,Query<User>("select * from user where age > @age; select * from address", new { age = 25 })
|
||||||
|
</summary>
|
||||||
|
<typeparam name="T1"></typeparam>
|
||||||
|
<param name="cmdText"></param>
|
||||||
|
<param name="parms"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
<member name="P:FreeSql.IAop.ParseExpression">
|
<member name="P:FreeSql.IAop.ParseExpression">
|
||||||
<summary>
|
<summary>
|
||||||
可自定义解析表达式
|
可自定义解析表达式
|
||||||
|
72
Providers/FreeSql.Provider.Sqlite/MonoAdapter.cs
Normal file
72
Providers/FreeSql.Provider.Sqlite/MonoAdapter.cs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,6 @@ using SafeObjectPool;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using System.Data.SQLite;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
@ -56,7 +55,7 @@ namespace FreeSql.Sqlite
|
|||||||
|
|
||||||
protected override DbCommand CreateCommand()
|
protected override DbCommand CreateCommand()
|
||||||
{
|
{
|
||||||
return new SQLiteCommand();
|
return MonoAdapter.GetSqliteCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ReturnConnection(ObjectPool<DbConnection> pool, Object<DbConnection> conn, Exception ex)
|
protected override void ReturnConnection(ObjectPool<DbConnection> pool, Object<DbConnection> conn, Exception ex)
|
||||||
|
@ -4,7 +4,6 @@ using System.Collections.Concurrent;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using System.Data.SQLite;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
@ -35,7 +34,7 @@ namespace FreeSql.Sqlite
|
|||||||
|
|
||||||
public void Return(Object<DbConnection> obj, Exception exception, bool isRecreate = false)
|
public void Return(Object<DbConnection> obj, Exception exception, bool isRecreate = false)
|
||||||
{
|
{
|
||||||
if (exception != null && exception is SQLiteException)
|
if (exception != null && MonoAdapter.IsSqliteException(exception))
|
||||||
{
|
{
|
||||||
try { if (obj.Value.Ping() == false) obj.Value.OpenAndAttach(policy.Attaches); } catch { base.SetUnavailable(exception); }
|
try { if (obj.Value.Ping() == false) obj.Value.OpenAndAttach(policy.Attaches); } catch { base.SetUnavailable(exception); }
|
||||||
}
|
}
|
||||||
@ -112,7 +111,7 @@ namespace FreeSql.Sqlite
|
|||||||
|
|
||||||
public DbConnection OnCreate()
|
public DbConnection OnCreate()
|
||||||
{
|
{
|
||||||
var conn = new SQLiteConnection(_connectionString);
|
var conn = MonoAdapter.GetSqliteConnection(_connectionString);
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using System.Data.SQLite;
|
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@ -34,13 +33,16 @@ namespace FreeSql.Sqlite
|
|||||||
dbtype = DbType.Int64;
|
dbtype = DbType.Int64;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
var ret = new SQLiteParameter { ParameterName = QuoteParamterName(parameterName), DbType = dbtype, Value = value };
|
var ret = MonoAdapter.GetSqliteParameter();
|
||||||
|
ret.ParameterName = QuoteParamterName(parameterName);
|
||||||
|
ret.DbType = dbtype;
|
||||||
|
ret.Value = value;
|
||||||
_params?.Add(ret);
|
_params?.Add(ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override DbParameter[] GetDbParamtersByObject(string sql, object obj) =>
|
public override DbParameter[] GetDbParamtersByObject(string sql, object obj) =>
|
||||||
Utils.GetDbParamtersByObject<SQLiteParameter>(sql, obj, "@", (name, type, value) =>
|
Utils.GetDbParamtersByObject<DbParameter>(sql, obj, "@", (name, type, value) =>
|
||||||
{
|
{
|
||||||
var dbtype = (DbType)_orm.CodeFirst.GetDbInfo(type)?.type;
|
var dbtype = (DbType)_orm.CodeFirst.GetDbInfo(type)?.type;
|
||||||
switch (dbtype)
|
switch (dbtype)
|
||||||
@ -56,7 +58,10 @@ namespace FreeSql.Sqlite
|
|||||||
dbtype = DbType.Int64;
|
dbtype = DbType.Int64;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
var ret = new SQLiteParameter { ParameterName = $"@{name}", DbType = dbtype, Value = value };
|
var ret = MonoAdapter.GetSqliteParameter();
|
||||||
|
ret.ParameterName = $"@{name}";
|
||||||
|
ret.DbType = dbtype;
|
||||||
|
ret.Value = value;
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user