using FreeSql; using FreeSql.ClickHouse.Curd; using FreeSql.Internal.CommonProvider; using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; using FreeSql.DataAnnotations; public static partial class FreeSqlClickHouseGlobalExtensions { /// /// 特殊处理类似 string.Format 的使用方法,防止注入,以及 IS NULL 转换 /// /// /// /// public static string FormatClickHouse(this string that, params object[] args) => _clickHouseAdo.Addslashes(that, args); static FreeSql.ClickHouse.ClickHouseAdo _clickHouseAdo = new FreeSql.ClickHouse.ClickHouseAdo(); /// /// Clickhouse limit by /// public static ISelect LimitBy(this ISelect that, Expression> selector, int limit, int offset = 0) { if (limit <= 0 && offset <= 0) return that; var s0p = that as ClickHouseSelect; var oldOrderBy = s0p._orderby; s0p._orderby = ""; try { s0p.InternalOrderBy(selector); s0p._limitBy = $"LIMIT {(offset > 0 ? $"{offset}, " : "")}{limit} BY {s0p._orderby}"; } finally { s0p._orderby = oldOrderBy; } return that; } /// /// Clickhouse 采样子句 /// public static ISelect Sample(this ISelect that, decimal k, int n, decimal m) { var s0p = that as ClickHouseSelect; if (k > 0) if (m > 0) s0p._sample = $"SAMPLE {k} OFFSET {m}"; else s0p._sample = $"SAMPLE {k}"; else if (n > 0) s0p._sample = $"SAMPLE {k}"; return that; } /// /// 批量快速插入 /// /// /// /// public static async Task ExecuteClickHouseBulkCopyAsync(this IInsert that) where T : class { try { var insert = that as ClickHouseInsert; await insert.InternalBulkCopyAsync(); } catch (Exception e) { throw e; } return 0; } /// /// 批量快速插入 /// /// /// /// public static int ExecuteClickHouseBulkCopy(this IInsert insert) where T : class { return ExecuteClickHouseBulkCopyAsync(insert).ConfigureAwait(false).GetAwaiter().GetResult(); } }