Merge pull request #1434 from d4ilys/master

- 解决QuestDb BulkCopy后CSV文件未删除的情况
This commit is contained in:
2881099 2023-02-23 02:01:29 +08:00 committed by GitHub
commit 1f0b7f1f45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -69,6 +69,7 @@ public static partial class QuestDbGlobalExtensions
/// <returns></returns> /// <returns></returns>
public static async Task<int> ExecuteBulkCopyAsync<T>(this IInsert<T> that) where T : class public static async Task<int> ExecuteBulkCopyAsync<T>(this IInsert<T> that) where T : class
{ {
//思路通过提供的RestAPI imp实现快速复制
if (string.IsNullOrWhiteSpace(RestAPIExtension.BaseUrl)) if (string.IsNullOrWhiteSpace(RestAPIExtension.BaseUrl))
{ {
throw new Exception("BulkCopy功能需要启用RestAPI启用方式new FreeSqlBuilder().UseQuestDbRestAPI(\"localhost:9000\", \"username\", \"password\")"); throw new Exception("BulkCopy功能需要启用RestAPI启用方式new FreeSqlBuilder().UseQuestDbRestAPI(\"localhost:9000\", \"username\", \"password\")");
@ -104,6 +105,7 @@ public static partial class QuestDbGlobalExtensions
} }
}); });
var schema = JsonConvert.SerializeObject(list); var schema = JsonConvert.SerializeObject(list);
//写入CSV文件
using (var writer = new StreamWriter(filePath)) using (var writer = new StreamWriter(filePath))
using (var csv = new CsvWriter(writer, CultureInfo.CurrentCulture)) using (var csv = new CsvWriter(writer, CultureInfo.CurrentCulture))
{ {
@ -144,11 +146,9 @@ public static partial class QuestDbGlobalExtensions
{ {
try try
{ {
// File.Delete(filePath); File.Delete(filePath);
}
catch
{
} }
catch { }
} }
return result; return result;
@ -197,7 +197,7 @@ public static class SampleByExtension
{ {
var _unit = Enum.GetName(typeof(SampleUnits), unit); var _unit = Enum.GetName(typeof(SampleUnits), unit);
IsExistence.Value = true; IsExistence.Value = true;
var samoleByTemple = $"{Environment.NewLine}SAMPLE BY {{0}}{{1}}{Environment.NewLine}"; var samoleByTemple = $"{Environment.NewLine}SAMPLE BY {{0}}{{1}} ";
SamoleByString.Value = string.Format(samoleByTemple, time.ToString(), _unit); SamoleByString.Value = string.Format(samoleByTemple, time.ToString(), _unit);
return select; return select;
} }
@ -234,11 +234,11 @@ public static class LatestOnExtension
public static ISelect<T1> LatestOn<T1, TKey>(this ISelect<T1> select, Expression<Func<T1, DateTime?>> timestamp, public static ISelect<T1> LatestOn<T1, TKey>(this ISelect<T1> select, Expression<Func<T1, DateTime?>> timestamp,
Expression<Func<T1, TKey>> partition) Expression<Func<T1, TKey>> partition)
{ {
Provider(timestamp, partition); InternelImpl(timestamp, partition);
return select; return select;
} }
private static void Provider<T1, TKey>(Expression<Func<T1, DateTime?>> timestamp, private static void InternelImpl<T1, TKey>(Expression<Func<T1, DateTime?>> timestamp,
Expression<Func<T1, TKey>> partition) Expression<Func<T1, TKey>> partition)
{ {
IsExistence.Value = true; IsExistence.Value = true;
@ -264,7 +264,7 @@ public static class LatestOnExtension
Expression<Func<T1, DateTime?>> timestamp, Expression<Func<T1, DateTime?>> timestamp,
Expression<Func<T1, TKey>> partition) where T2 : class Expression<Func<T1, TKey>> partition) where T2 : class
{ {
Provider(timestamp, partition); InternelImpl(timestamp, partition);
return select; return select;
} }
@ -281,7 +281,7 @@ public static class LatestOnExtension
Expression<Func<T1, DateTime?>> timestamp, Expression<Func<T1, DateTime?>> timestamp,
Expression<Func<T1, TKey>> partition) where T2 : class where T3 : class Expression<Func<T1, TKey>> partition) where T2 : class where T3 : class
{ {
Provider(timestamp, partition); InternelImpl(timestamp, partition);
return select; return select;
} }
@ -298,7 +298,7 @@ public static class LatestOnExtension
Expression<Func<T1, DateTime?>> timestamp, Expression<Func<T1, DateTime?>> timestamp,
Expression<Func<T1, TKey>> partition) where T2 : class where T3 : class where T4 : class Expression<Func<T1, TKey>> partition) where T2 : class where T3 : class where T4 : class
{ {
Provider(timestamp, partition); InternelImpl(timestamp, partition);
return select; return select;
} }
} }
@ -310,6 +310,7 @@ public static class RestAPIExtension
internal static async Task<string> ExecAsync(string sql) internal static async Task<string> ExecAsync(string sql)
{ {
//HTTP GET 执行SQL
var result = string.Empty; var result = string.Empty;
var client = QuestDbContainer.GetService<IHttpClientFactory>().CreateClient(); var client = QuestDbContainer.GetService<IHttpClientFactory>().CreateClient();
var url = $"{BaseUrl}/exec?query={HttpUtility.UrlEncode(sql)}"; var url = $"{BaseUrl}/exec?query={HttpUtility.UrlEncode(sql)}";
@ -317,7 +318,6 @@ public static class RestAPIExtension
client.DefaultRequestHeaders.Add("Authorization", authorization); client.DefaultRequestHeaders.Add("Authorization", authorization);
var httpResponseMessage = await client.GetAsync(url); var httpResponseMessage = await client.GetAsync(url);
result = await httpResponseMessage.Content.ReadAsStringAsync(); result = await httpResponseMessage.Content.ReadAsStringAsync();
return result; return result;
} }
@ -330,11 +330,13 @@ public static class RestAPIExtension
if (!BaseUrl.ToLower().StartsWith("http")) if (!BaseUrl.ToLower().StartsWith("http"))
BaseUrl = $"http://{BaseUrl}"; BaseUrl = $"http://{BaseUrl}";
//生成TOKEN
if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password)) if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
{ {
var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")); var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
authorization = $"Basic {base64}"; authorization = $"Basic {base64}";
} }
//RESTAPI需要无参数
buider.UseNoneCommandParameter(true); buider.UseNoneCommandParameter(true);
return buider; return buider;
} }