diff --git a/FreeSql/Extensions/AdoNetExtensions.cs b/FreeSql/Extensions/AdoNetExtensions.cs new file mode 100644 index 00000000..ac63b308 --- /dev/null +++ b/FreeSql/Extensions/AdoNetExtensions.cs @@ -0,0 +1,381 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; + +namespace FreeSql +{ + public static class AdoNetExtensions + { + #region Ado.net 扩展方法,类似于 Dapper + + static Dictionary _dicCurd = new Dictionary(); + static object _dicCurdLock = new object(); + static IFreeSql GetCrud(IDbConnection dbconn) + { + if (dbconn == null) throw new ArgumentNullException($"{nameof(dbconn)} 不能为 null"); ; + Type dbconType = dbconn.GetType(); + var connType = dbconType.UnderlyingSystemType; + if (_dicCurd.TryGetValue(connType, out var fsql)) return fsql; + + Type providerType = null; + switch (connType.Name) + { + case "MySqlConnection": + providerType = Type.GetType("FreeSql.MySql.MySqlProvider`1,FreeSql.Provider.MySql")?.MakeGenericType(connType); + if (providerType == null) providerType = Type.GetType("FreeSql.MySql.MySqlProvider`1,FreeSql.Provider.MySqlConnector")?.MakeGenericType(connType); + if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.MySql.dll,可前往 nuget 下载"); + break; + case "SqlConnection": + providerType = Type.GetType("FreeSql.SqlServer.SqlServerProvider`1,FreeSql.Provider.SqlServer")?.MakeGenericType(connType); + if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.SqlServer.dll,可前往 nuget 下载"); + break; + case "NpgsqlConnection": + providerType = Type.GetType("FreeSql.PostgreSQL.PostgreSQLProvider`1,FreeSql.Provider.PostgreSQL")?.MakeGenericType(connType); + if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.PostgreSQL.dll,可前往 nuget 下载"); + break; + case "OracleConnection": + providerType = Type.GetType("FreeSql.Oracle.OracleProvider`1,FreeSql.Provider.Oracle")?.MakeGenericType(connType); + if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.Oracle.dll,可前往 nuget 下载"); + break; + case "SQLiteConnection": + providerType = Type.GetType("FreeSql.Sqlite.SqliteProvider`1,FreeSql.Provider.Sqlite")?.MakeGenericType(connType); + if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.Sqlite.dll,可前往 nuget 下载"); + break; + case "DmConnection": + providerType = Type.GetType("FreeSql.Dameng.DamengProvider`1,FreeSql.Provider.Dameng")?.MakeGenericType(connType); + if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.Dameng.dll,可前往 nuget 下载"); + break; + case "OscarConnection": + providerType = Type.GetType("FreeSql.ShenTong.ShenTongProvider`1,FreeSql.Provider.ShenTong")?.MakeGenericType(connType); + if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.ShenTong.dll,可前往 nuget 下载"); + break; + default: + throw new Exception("未实现"); + } + lock (_dicCurdLock) + { + if (_dicCurd.TryGetValue(connType, out fsql)) return fsql; + lock (_dicCurdLock) + _dicCurd.Add(connType, fsql = Activator.CreateInstance(providerType, new object[] { null, null, null }) as IFreeSql); + } + return fsql; + } + static IFreeSql GetCrud(IDbTransaction dbtran) + { + if (dbtran == null) throw new ArgumentNullException($"{nameof(dbtran)} 不能为 null"); + return GetCrud(dbtran.Connection); + } + + /// + /// 获取 IDbConnection 对应的 IFreeSql 实例 + /// + /// + /// + public static IFreeSql GetIFreeSql(this IDbConnection that) => GetCrud(that); + + #region IDbConnection + /// + /// 插入数据 + /// + /// + /// + public static IInsert Insert(this IDbConnection that) where T1 : class => GetCrud(that).Insert().WithConnection(that as DbConnection); + /// + /// 插入数据,传入实体 + /// + /// + /// + /// + public static IInsert Insert(this IDbConnection that, T1 source) where T1 : class => GetCrud(that).Insert(source).WithConnection(that as DbConnection); + /// + /// 插入数据,传入实体数组 + /// + /// + /// + /// + public static IInsert Insert(this IDbConnection that, T1[] source) where T1 : class => GetCrud(that).Insert(source).WithConnection(that as DbConnection); + /// + /// 插入数据,传入实体集合 + /// + /// + /// + /// + public static IInsert Insert(this IDbConnection that, List source) where T1 : class => GetCrud(that).Insert(source).WithConnection(that as DbConnection); + /// + /// 插入数据,传入实体集合 + /// + /// + /// + /// + public static IInsert Insert(this IDbConnection that, IEnumerable source) where T1 : class => GetCrud(that).Insert(source).WithConnection(that as DbConnection); + + /// + /// 插入或更新数据,此功能依赖数据库特性(低版本可能不支持),参考如下: + /// MySql 5.6+: on duplicate key update + /// PostgreSQL 9.4+: on conflict do update + /// SqlServer 2008+: merge into + /// Oracle 11+: merge into + /// Sqlite: replace into + /// 达梦: merge into + /// 人大金仓:on conflict do update + /// 神通:merge into + /// MsAccess:不支持 + /// 注意区别:FreeSql.Repository 仓储也有 InsertOrUpdate 方法(不依赖数据库特性) + /// + /// + /// + public static IInsertOrUpdate InsertOrUpdate(this IDbConnection that) where T1 : class => GetCrud(that).InsertOrUpdate().WithConnection(that as DbConnection); + + /// + /// 修改数据 + /// + /// + /// + public static IUpdate Update(this IDbConnection that) where T1 : class => GetCrud(that).Update().WithConnection(that as DbConnection); + /// + /// 修改数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + /// + /// + /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + /// + public static IUpdate Update(this IDbConnection that, object dywhere) where T1 : class => GetCrud(that).Update(dywhere).WithConnection(that as DbConnection); + + /// + /// 查询数据 + /// + /// + /// + public static ISelect Select(this IDbConnection that) where T1 : class => GetCrud(that).Select().WithConnection(that as DbConnection); + /// + /// 查询数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + /// + /// + /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + /// + public static ISelect Select(this IDbConnection that, object dywhere) where T1 : class => GetCrud(that).Select(dywhere).WithConnection(that as DbConnection); + + /// + /// 删除数据 + /// + /// + /// + public static IDelete Delete(this IDbConnection that) where T1 : class => GetCrud(that).Delete().WithConnection(that as DbConnection); + /// + /// 删除数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + /// + /// + /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + /// + public static IDelete Delete(this IDbConnection that, object dywhere) where T1 : class => GetCrud(that).Delete(dywhere).WithConnection(that as DbConnection); + + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class => + GetCrud(that).Select().From((s, b) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class => + GetCrud(that).Select().From((s, b, c) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class => + GetCrud(that).Select().From((s, b, c, d) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class => + GetCrud(that).Select().From((s, b, c, d, e) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class => + GetCrud(that).Select().From((s, b, c, d, e, f) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class => + GetCrud(that).Select().From((s, b, c, d, e, f, g) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class => + GetCrud(that).Select().From((s, b, c, d, e, f, g, h) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class => + GetCrud(that).Select().From((s, b, c, d, e, f, g, h, i) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class => + GetCrud(that).Select().From((s, b, c, d, e, f, g, h, i, j) => s); + #endregion + + #region IDbTransaction + /// + /// 插入数据 + /// + /// + /// + public static IInsert Insert(this IDbTransaction that) where T1 : class => GetCrud(that).Insert().WithTransaction(that as DbTransaction); + /// + /// 插入数据,传入实体 + /// + /// + /// + /// + public static IInsert Insert(this IDbTransaction that, T1 source) where T1 : class => GetCrud(that).Insert(source).WithTransaction(that as DbTransaction); + /// + /// 插入数据,传入实体数组 + /// + /// + /// + /// + public static IInsert Insert(this IDbTransaction that, T1[] source) where T1 : class => GetCrud(that).Insert(source).WithTransaction(that as DbTransaction); + /// + /// 插入数据,传入实体集合 + /// + /// + /// + /// + public static IInsert Insert(this IDbTransaction that, List source) where T1 : class => GetCrud(that).Insert(source).WithTransaction(that as DbTransaction); + /// + /// 插入数据,传入实体集合 + /// + /// + /// + /// + public static IInsert Insert(this IDbTransaction that, IEnumerable source) where T1 : class => GetCrud(that).Insert(source).WithTransaction(that as DbTransaction); + + /// + /// 插入或更新数据,此功能依赖数据库特性(低版本可能不支持),参考如下: + /// MySql 5.6+: on duplicate key update + /// PostgreSQL 9.4+: on conflict do update + /// SqlServer 2008+: merge into + /// Oracle 11+: merge into + /// Sqlite: replace into + /// 达梦: merge into + /// 人大金仓:on conflict do update + /// 神通:merge into + /// MsAccess:不支持 + /// 注意区别:FreeSql.Repository 仓储也有 InsertOrUpdate 方法(不依赖数据库特性) + /// + /// + /// + public static IInsertOrUpdate InsertOrUpdate(this IDbTransaction that) where T1 : class => GetCrud(that).InsertOrUpdate().WithTransaction(that as DbTransaction); + + /// + /// 修改数据 + /// + /// + /// + public static IUpdate Update(this IDbTransaction that) where T1 : class => GetCrud(that).Update().WithTransaction(that as DbTransaction); + /// + /// 修改数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + /// + /// + /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + /// + public static IUpdate Update(this IDbTransaction that, object dywhere) where T1 : class => GetCrud(that).Update(dywhere).WithTransaction(that as DbTransaction); + + /// + /// 查询数据 + /// + /// + /// + public static ISelect Select(this IDbTransaction that) where T1 : class => GetCrud(that).Select().WithTransaction(that as DbTransaction); + /// + /// 查询数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + /// + /// + /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + /// + public static ISelect Select(this IDbTransaction that, object dywhere) where T1 : class => GetCrud(that).Select(dywhere).WithTransaction(that as DbTransaction); + + /// + /// 删除数据 + /// + /// + /// + public static IDelete Delete(this IDbTransaction that) where T1 : class => GetCrud(that).Delete().WithTransaction(that as DbTransaction); + /// + /// 删除数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + /// + /// + /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + /// + public static IDelete Delete(this IDbTransaction that, object dywhere) where T1 : class => GetCrud(that).Delete(dywhere).WithTransaction(that as DbTransaction); + + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class => + GetCrud(that).Select().From((s, b) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class => + GetCrud(that).Select().From((s, b, c) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class => + GetCrud(that).Select().From((s, b, c, d) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class => + GetCrud(that).Select().From((s, b, c, d, e) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class => + GetCrud(that).Select().From((s, b, c, d, e, f) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class => + GetCrud(that).Select().From((s, b, c, d, e, f, g) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class => + GetCrud(that).Select().From((s, b, c, d, e, f, g, h) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class => + GetCrud(that).Select().From((s, b, c, d, e, f, g, h, i) => s); + /// + /// 多表查询 + /// + /// + public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class => + GetCrud(that).Select().From((s, b, c, d, e, f, g, h, i, j) => s); + #endregion + + #endregion + } +} \ No newline at end of file diff --git a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs index c90437a1..0a3cf29b 100644 --- a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs +++ b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs @@ -137,7 +137,7 @@ public static partial class FreeSqlGlobalExtensions if (ctorParms == null || ctorParms.Any() == false) return Activator.CreateInstance(that, true); return Activator.CreateInstance(that, ctorParms .Select(a => a.ParameterType.IsInterface || a.ParameterType.IsAbstract || a.ParameterType == typeof(string) || a.ParameterType.IsArray ? - null : + null : Activator.CreateInstance(a.ParameterType, null)).ToArray()); } internal static NewExpression InternalNewExpression(this Type that) @@ -348,7 +348,7 @@ public static partial class FreeSqlGlobalExtensions var select = that as Select1Provider; var tb = select._tables[0].Table; var navs = tb.Properties.Select(a => tb.GetTableRef(a.Key, false)) - .Where(a => a != null && + .Where(a => a != null && a.RefType == FreeSql.Internal.Model.TableRefType.OneToMany && a.RefEntityType == tb.Type).ToArray(); @@ -451,7 +451,7 @@ public static partial class FreeSqlGlobalExtensions var sql2InnerJoinOn = up == false ? string.Join(" and ", tbref.Columns.Select((a, z) => $"wct2.{select._commonUtils.QuoteSqlName(tbref.RefColumns[z].Attribute.Name)} = wct1.{select._commonUtils.QuoteSqlName(a.Attribute.Name)}")) : string.Join(" and ", tbref.Columns.Select((a, z) => $"wct2.{select._commonUtils.QuoteSqlName(a.Attribute.Name)} = wct1.{select._commonUtils.QuoteSqlName(tbref.RefColumns[z].Attribute.Name)}")); - + var sql2ctePath = ""; if (pathSelector != null) { @@ -518,374 +518,4 @@ SELECT "); } #endregion - #region Ado.net 扩展方法,类似于 Dapper - - static Dictionary _dicCurd = new Dictionary(); - static object _dicCurdLock = new object(); - static IFreeSql GetCrud(IDbConnection dbconn) - { - if (dbconn == null) throw new ArgumentNullException($"{nameof(dbconn)} 不能为 null"); ; - Type dbconType = dbconn.GetType(); - var connType = dbconType.UnderlyingSystemType; - if (_dicCurd.TryGetValue(connType, out var fsql)) return fsql; - - Type providerType = null; - switch (connType.Name) - { - case "MySqlConnection": - providerType = Type.GetType("FreeSql.MySql.MySqlProvider`1,FreeSql.Provider.MySql")?.MakeGenericType(connType); - if (providerType == null) providerType = Type.GetType("FreeSql.MySql.MySqlProvider`1,FreeSql.Provider.MySqlConnector")?.MakeGenericType(connType); - if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.MySql.dll,可前往 nuget 下载"); - break; - case "SqlConnection": - providerType = Type.GetType("FreeSql.SqlServer.SqlServerProvider`1,FreeSql.Provider.SqlServer")?.MakeGenericType(connType); - if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.SqlServer.dll,可前往 nuget 下载"); - break; - case "NpgsqlConnection": - providerType = Type.GetType("FreeSql.PostgreSQL.PostgreSQLProvider`1,FreeSql.Provider.PostgreSQL")?.MakeGenericType(connType); - if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.PostgreSQL.dll,可前往 nuget 下载"); - break; - case "OracleConnection": - providerType = Type.GetType("FreeSql.Oracle.OracleProvider`1,FreeSql.Provider.Oracle")?.MakeGenericType(connType); - if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.Oracle.dll,可前往 nuget 下载"); - break; - case "SQLiteConnection": - providerType = Type.GetType("FreeSql.Sqlite.SqliteProvider`1,FreeSql.Provider.Sqlite")?.MakeGenericType(connType); - if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.Sqlite.dll,可前往 nuget 下载"); - break; - case "DmConnection": - providerType = Type.GetType("FreeSql.Dameng.DamengProvider`1,FreeSql.Provider.Dameng")?.MakeGenericType(connType); - if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.Dameng.dll,可前往 nuget 下载"); - break; - case "OscarConnection": - providerType = Type.GetType("FreeSql.ShenTong.ShenTongProvider`1,FreeSql.Provider.ShenTong")?.MakeGenericType(connType); - if (providerType == null) throw new Exception("缺少 FreeSql 数据库实现包:FreeSql.Provider.ShenTong.dll,可前往 nuget 下载"); - break; - default: - throw new Exception("未实现"); - } - lock (_dicCurdLock) - { - if (_dicCurd.TryGetValue(connType, out fsql)) return fsql; - lock (_dicCurdLock) - _dicCurd.Add(connType, fsql = Activator.CreateInstance(providerType, new object[] { null, null, null }) as IFreeSql); - } - return fsql; - } - static IFreeSql GetCrud(IDbTransaction dbtran) - { - if (dbtran == null) throw new ArgumentNullException($"{nameof(dbtran)} 不能为 null"); - return GetCrud(dbtran.Connection); - } - - /// - /// 获取 IDbConnection 对应的 IFreeSql 实例 - /// - /// - /// - public static IFreeSql GetIFreeSql(this IDbConnection that) => GetCrud(that); - - #region IDbConnection - /// - /// 插入数据 - /// - /// - /// - public static IInsert Insert(this IDbConnection that) where T1 : class => GetCrud(that).Insert().WithConnection(that as DbConnection); - /// - /// 插入数据,传入实体 - /// - /// - /// - /// - public static IInsert Insert(this IDbConnection that, T1 source) where T1 : class => GetCrud(that).Insert(source).WithConnection(that as DbConnection); - /// - /// 插入数据,传入实体数组 - /// - /// - /// - /// - public static IInsert Insert(this IDbConnection that, T1[] source) where T1 : class => GetCrud(that).Insert(source).WithConnection(that as DbConnection); - /// - /// 插入数据,传入实体集合 - /// - /// - /// - /// - public static IInsert Insert(this IDbConnection that, List source) where T1 : class => GetCrud(that).Insert(source).WithConnection(that as DbConnection); - /// - /// 插入数据,传入实体集合 - /// - /// - /// - /// - public static IInsert Insert(this IDbConnection that, IEnumerable source) where T1 : class => GetCrud(that).Insert(source).WithConnection(that as DbConnection); - - /// - /// 插入或更新数据,此功能依赖数据库特性(低版本可能不支持),参考如下: - /// MySql 5.6+: on duplicate key update - /// PostgreSQL 9.4+: on conflict do update - /// SqlServer 2008+: merge into - /// Oracle 11+: merge into - /// Sqlite: replace into - /// 达梦: merge into - /// 人大金仓:on conflict do update - /// 神通:merge into - /// MsAccess:不支持 - /// 注意区别:FreeSql.Repository 仓储也有 InsertOrUpdate 方法(不依赖数据库特性) - /// - /// - /// - public static IInsertOrUpdate InsertOrUpdate(this IDbConnection that) where T1 : class => GetCrud(that).InsertOrUpdate().WithConnection(that as DbConnection); - - /// - /// 修改数据 - /// - /// - /// - public static IUpdate Update(this IDbConnection that) where T1 : class => GetCrud(that).Update().WithConnection(that as DbConnection); - /// - /// 修改数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - /// - /// - /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - /// - public static IUpdate Update(this IDbConnection that, object dywhere) where T1 : class => GetCrud(that).Update(dywhere).WithConnection(that as DbConnection); - - /// - /// 查询数据 - /// - /// - /// - public static ISelect Select(this IDbConnection that) where T1 : class => GetCrud(that).Select().WithConnection(that as DbConnection); - /// - /// 查询数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - /// - /// - /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - /// - public static ISelect Select(this IDbConnection that, object dywhere) where T1 : class => GetCrud(that).Select(dywhere).WithConnection(that as DbConnection); - - /// - /// 删除数据 - /// - /// - /// - public static IDelete Delete(this IDbConnection that) where T1 : class => GetCrud(that).Delete().WithConnection(that as DbConnection); - /// - /// 删除数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - /// - /// - /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - /// - public static IDelete Delete(this IDbConnection that, object dywhere) where T1 : class => GetCrud(that).Delete(dywhere).WithConnection(that as DbConnection); - - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class => - GetCrud(that).Select().From((s, b) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class => - GetCrud(that).Select().From((s, b, c) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class => - GetCrud(that).Select().From((s, b, c, d) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class => - GetCrud(that).Select().From((s, b, c, d, e) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class => - GetCrud(that).Select().From((s, b, c, d, e, f) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class => - GetCrud(that).Select().From((s, b, c, d, e, f, g) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class => - GetCrud(that).Select().From((s, b, c, d, e, f, g, h) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class => - GetCrud(that).Select().From((s, b, c, d, e, f, g, h, i) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbConnection that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class => - GetCrud(that).Select().From((s, b, c, d, e, f, g, h, i, j) => s); - #endregion - - #region IDbTransaction - /// - /// 插入数据 - /// - /// - /// - public static IInsert Insert(this IDbTransaction that) where T1 : class => GetCrud(that).Insert().WithTransaction(that as DbTransaction); - /// - /// 插入数据,传入实体 - /// - /// - /// - /// - public static IInsert Insert(this IDbTransaction that, T1 source) where T1 : class => GetCrud(that).Insert(source).WithTransaction(that as DbTransaction); - /// - /// 插入数据,传入实体数组 - /// - /// - /// - /// - public static IInsert Insert(this IDbTransaction that, T1[] source) where T1 : class => GetCrud(that).Insert(source).WithTransaction(that as DbTransaction); - /// - /// 插入数据,传入实体集合 - /// - /// - /// - /// - public static IInsert Insert(this IDbTransaction that, List source) where T1 : class => GetCrud(that).Insert(source).WithTransaction(that as DbTransaction); - /// - /// 插入数据,传入实体集合 - /// - /// - /// - /// - public static IInsert Insert(this IDbTransaction that, IEnumerable source) where T1 : class => GetCrud(that).Insert(source).WithTransaction(that as DbTransaction); - - /// - /// 插入或更新数据,此功能依赖数据库特性(低版本可能不支持),参考如下: - /// MySql 5.6+: on duplicate key update - /// PostgreSQL 9.4+: on conflict do update - /// SqlServer 2008+: merge into - /// Oracle 11+: merge into - /// Sqlite: replace into - /// 达梦: merge into - /// 人大金仓:on conflict do update - /// 神通:merge into - /// MsAccess:不支持 - /// 注意区别:FreeSql.Repository 仓储也有 InsertOrUpdate 方法(不依赖数据库特性) - /// - /// - /// - public static IInsertOrUpdate InsertOrUpdate(this IDbTransaction that) where T1 : class => GetCrud(that).InsertOrUpdate().WithTransaction(that as DbTransaction); - - /// - /// 修改数据 - /// - /// - /// - public static IUpdate Update(this IDbTransaction that) where T1 : class => GetCrud(that).Update().WithTransaction(that as DbTransaction); - /// - /// 修改数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - /// - /// - /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - /// - public static IUpdate Update(this IDbTransaction that, object dywhere) where T1 : class => GetCrud(that).Update(dywhere).WithTransaction(that as DbTransaction); - - /// - /// 查询数据 - /// - /// - /// - public static ISelect Select(this IDbTransaction that) where T1 : class => GetCrud(that).Select().WithTransaction(that as DbTransaction); - /// - /// 查询数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - /// - /// - /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - /// - public static ISelect Select(this IDbTransaction that, object dywhere) where T1 : class => GetCrud(that).Select(dywhere).WithTransaction(that as DbTransaction); - - /// - /// 删除数据 - /// - /// - /// - public static IDelete Delete(this IDbTransaction that) where T1 : class => GetCrud(that).Delete().WithTransaction(that as DbTransaction); - /// - /// 删除数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - /// - /// - /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - /// - public static IDelete Delete(this IDbTransaction that, object dywhere) where T1 : class => GetCrud(that).Delete(dywhere).WithTransaction(that as DbTransaction); - - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class => - GetCrud(that).Select().From((s, b) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class => - GetCrud(that).Select().From((s, b, c) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class => - GetCrud(that).Select().From((s, b, c, d) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class => - GetCrud(that).Select().From((s, b, c, d, e) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class => - GetCrud(that).Select().From((s, b, c, d, e, f) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class => - GetCrud(that).Select().From((s, b, c, d, e, f, g) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class => - GetCrud(that).Select().From((s, b, c, d, e, f, g, h) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class => - GetCrud(that).Select().From((s, b, c, d, e, f, g, h, i) => s); - /// - /// 多表查询 - /// - /// - public static ISelect Select(this IDbTransaction that) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class => - GetCrud(that).Select().From((s, b, c, d, e, f, g, h, i, j) => s); - #endregion - - #endregion -} \ No newline at end of file +} diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index e19b9432..a7b0965c 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -606,6 +606,323 @@ 北京人大金仓信息技术股份有限公司,基于 Kdbndp.dll 的实现 + + + 获取 IDbConnection 对应的 IFreeSql 实例 + + + + + + + 插入数据 + + + + + + + 插入数据,传入实体 + + + + + + + + 插入数据,传入实体数组 + + + + + + + + 插入数据,传入实体集合 + + + + + + + + 插入数据,传入实体集合 + + + + + + + + 插入或更新数据,此功能依赖数据库特性(低版本可能不支持),参考如下: + MySql 5.6+: on duplicate key update + PostgreSQL 9.4+: on conflict do update + SqlServer 2008+: merge into + Oracle 11+: merge into + Sqlite: replace into + 达梦: merge into + 人大金仓:on conflict do update + 神通:merge into + MsAccess:不支持 + 注意区别:FreeSql.Repository 仓储也有 InsertOrUpdate 方法(不依赖数据库特性) + + + + + + + 修改数据 + + + + + + + 修改数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + + + 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + + + + + 查询数据 + + + + + + + 查询数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + + + 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + + + + + 删除数据 + + + + + + + 删除数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + + + 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 插入数据 + + + + + + + 插入数据,传入实体 + + + + + + + + 插入数据,传入实体数组 + + + + + + + + 插入数据,传入实体集合 + + + + + + + + 插入数据,传入实体集合 + + + + + + + + 插入或更新数据,此功能依赖数据库特性(低版本可能不支持),参考如下: + MySql 5.6+: on duplicate key update + PostgreSQL 9.4+: on conflict do update + SqlServer 2008+: merge into + Oracle 11+: merge into + Sqlite: replace into + 达梦: merge into + 人大金仓:on conflict do update + 神通:merge into + MsAccess:不支持 + 注意区别:FreeSql.Repository 仓储也有 InsertOrUpdate 方法(不依赖数据库特性) + + + + + + + 修改数据 + + + + + + + 修改数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + + + 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + + + + + 查询数据 + + + + + + + 查询数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + + + 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + + + + + 删除数据 + + + + + + + 删除数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} + + + 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + + + + 多表查询 + + + 获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值时,返回 null @@ -2626,6 +2943,153 @@ + + + 测试数据库是否连接正确,本方法执行如下命令: + MySql/SqlServer/PostgreSQL/达梦/人大金仓/神通: SELECT 1 + Oracle: SELECT 1 FROM dual + + true: 成功, false: 失败 + + + + 查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】 + + + + + + + + + 查询,ExecuteReaderAsync(dr => {}, "select * from user where age > ?age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + 查询 + + + + + + + 查询,ExecuteArrayAsync("select * from user where age > ?age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + 查询 + + + + + + + 查询,ExecuteDataSetAsync("select * from user where age > ?age; select 2", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + 查询 + + + + + + + 查询,ExecuteDataTableAsync("select * from user where age > ?age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + 在【主库】执行 + + + + + + + + 在【主库】执行,ExecuteNonQueryAsync("delete from user where age > ?age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + 在【主库】执行 + + + + + + + + 在【主库】执行,ExecuteScalarAsync("select 1 from user where age > ?age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > ?age", new SqlParameter { ParameterName = "age", Value = 25 }) + + + + + + + + + + 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > ?age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 执行SQL返回对象集合,Query<User>("select * from user where age > ?age; select * from address", new SqlParameter { ParameterName = "age", Value = 25 }) + + + + + + + + + + 执行SQL返回对象集合,Query<User, Address>("select * from user where age > ?age; select * from address", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + 可自定义解析表达式 @@ -3382,6 +3846,12 @@ 超时 + + + 获取资源 + + + 使用完毕后,归还资源 @@ -3452,6 +3922,12 @@ 资源对象 + + + 从对象池获取对象成功的时候触发,通过该方法统计或初始化对象 + + 资源对象 + 归还对象给对象池的时候触发 @@ -3771,323 +4247,6 @@ 递归层级 - - - 获取 IDbConnection 对应的 IFreeSql 实例 - - - - - - - 插入数据 - - - - - - - 插入数据,传入实体 - - - - - - - - 插入数据,传入实体数组 - - - - - - - - 插入数据,传入实体集合 - - - - - - - - 插入数据,传入实体集合 - - - - - - - - 插入或更新数据,此功能依赖数据库特性(低版本可能不支持),参考如下: - MySql 5.6+: on duplicate key update - PostgreSQL 9.4+: on conflict do update - SqlServer 2008+: merge into - Oracle 11+: merge into - Sqlite: replace into - 达梦: merge into - 人大金仓:on conflict do update - 神通:merge into - MsAccess:不支持 - 注意区别:FreeSql.Repository 仓储也有 InsertOrUpdate 方法(不依赖数据库特性) - - - - - - - 修改数据 - - - - - - - 修改数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - - - 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - - - - - 查询数据 - - - - - - - 查询数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - - - 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - - - - - 删除数据 - - - - - - - 删除数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - - - 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 插入数据 - - - - - - - 插入数据,传入实体 - - - - - - - - 插入数据,传入实体数组 - - - - - - - - 插入数据,传入实体集合 - - - - - - - - 插入数据,传入实体集合 - - - - - - - - 插入或更新数据,此功能依赖数据库特性(低版本可能不支持),参考如下: - MySql 5.6+: on duplicate key update - PostgreSQL 9.4+: on conflict do update - SqlServer 2008+: merge into - Oracle 11+: merge into - Sqlite: replace into - 达梦: merge into - 人大金仓:on conflict do update - 神通:merge into - MsAccess:不支持 - 注意区别:FreeSql.Repository 仓储也有 InsertOrUpdate 方法(不依赖数据库特性) - - - - - - - 修改数据 - - - - - - - 修改数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - - - 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - - - - - 查询数据 - - - - - - - 查询数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - - - 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - - - - - 删除数据 - - - - - - - 删除数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - - - 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - - - - 多表查询 - - - 使用 and 拼接两个 lambda 表达式