Skip to content

Commit

Permalink
- 增加 pgsql 扩展方法 ISelect<T>.DistinctOn;#1680
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Dec 8, 2023
1 parent efedfc4 commit 139c19e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 291 deletions.
281 changes: 0 additions & 281 deletions FreeSql/FreeSql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion FreeSql/Internal/CommonProvider/CodeFirstProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected void SyncStructure(params TypeSchemaAndName[] objects)
if (objects == null) return;
var syncObjects = objects.Where(a => a.tableSchema?.Type != null &&
(
a.tableSchema.Type.Name == "DynamicDbContext" && a.tableSchema.Columns.Any()
a.tableSchema.Type == typeof(object) && a.tableSchema.Columns.Any()
||
a.tableSchema.Type != typeof(object) && _dicSycedGetOrAdd(a.tableSchema.Type).ContainsKey(GetTableNameLowerOrUpper(a.tableName)) == false
) &&
Expand Down
44 changes: 35 additions & 9 deletions Providers/FreeSql.Provider.PostgreSQL/PostgreSQLExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,41 @@ public static partial class FreeSqlPostgreSQLGlobalExtensions
/// <returns></returns>
public static OnConflictDoUpdate<T1> OnConflictDoUpdate<T1>(this IInsert<T1> that, Expression<Func<T1, object>> columns = null) where T1 : class => new FreeSql.PostgreSQL.Curd.OnConflictDoUpdate<T1>(that.InsertIdentity(), columns);

#region ExecutePgCopy
/// <summary>
/// 批量插入或更新(操作的字段数量超过 2000 时收益大)<para></para>
/// 实现原理:使用 PgCopy 插入临时表,再执行 INSERT INTO t1 select * from #temp ON CONFLICT(""id"") DO UPDATE SET ...
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="that"></param>
/// <returns></returns>
public static int ExecutePgCopy<T>(this IInsertOrUpdate<T> that) where T : class
/// <summary>
/// PostgreSQL<para></para>
/// select distinct on(subject) * from score order by subject, score desc, name;
/// </summary>
public static ISelect<T1> DistinctOn<T1, TKey>(this ISelect<T1> query, Expression<Func<T1, object>> selector)
{
var select = query as FreeSql.PostgreSQL.Curd.PostgreSQLSelect<T1>;
if (select == null) throw new Exception($"{nameof(DistinctOn)} 是 FreeSql.Provider.PostgreSQL 特有的功能");
var s0p = select as Select0Provider;
var orderByOld = s0p._orderby;
var orderByNew = "";
try
{
s0p._orderby = null;
select.OrderBy(selector);
orderByNew = s0p._orderby;
}
finally
{
s0p._orderby = orderByOld;
}
if (orderByNew.StartsWith(" \r\nORDER BY "))
s0p._select = $"{s0p._select} distinct on({orderByNew.Substring(12)}) ";
return select;
}

#region ExecutePgCopy
/// <summary>
/// 批量插入或更新(操作的字段数量超过 2000 时收益大)<para></para>
/// 实现原理:使用 PgCopy 插入临时表,再执行 INSERT INTO t1 select * from #temp ON CONFLICT(""id"") DO UPDATE SET ...
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="that"></param>
/// <returns></returns>
public static int ExecutePgCopy<T>(this IInsertOrUpdate<T> that) where T : class
{
var upsert = that as InsertOrUpdateProvider<T>;
if (upsert._source.Any() != true || upsert._tempPrimarys.Any() == false) return 0;
Expand Down

0 comments on commit 139c19e

Please sign in to comment.