Merge pull request #973 from ANCB520/master

修复clickHouseSetNoneParameter会报错问题
This commit is contained in:
2881099 2022-01-18 06:54:56 +08:00 committed by GitHub
commit f4163be020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 58 deletions

View File

@ -538,14 +538,5 @@
<param name="that"></param> <param name="that"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
<summary>
批量注入 Repository可以参考代码自行调整
</summary>
<param name="services"></param>
<param name="globalDataFilter"></param>
<param name="assemblies"></param>
<returns></returns>
</member>
</members> </members>
</doc> </doc>

View File

@ -7,16 +7,18 @@ using System.Linq;
using System.Collections; using System.Collections;
using System.Diagnostics; using System.Diagnostics;
using XY.Model.Business; using XY.Model.Business;
using System.ComponentModel.DataAnnotations;
using FreeSql.DataAnnotations;
namespace FreeSql.Tests.MySql namespace FreeSql.Tests.MySql
{ {
public class ClickHouseTest1 public class ClickHouseTest1
{ {
private class TestAuditValue
class TestAuditValue
{ {
[FreeSql.DataAnnotations.Column(IsPrimary = true)] [FreeSql.DataAnnotations.Column(IsPrimary = true)]
public long Id { get; set; } public long Id { get; set; }
[Now] [Now]
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
@ -35,6 +37,7 @@ namespace FreeSql.Tests.MySql
public int? Points { get; set; } public int? Points { get; set; }
} }
[FreeSql.DataAnnotations.Table(Name = "ClickHouseTest")] [FreeSql.DataAnnotations.Table(Name = "ClickHouseTest")]
public class TestClickHouse public class TestClickHouse
{ {
@ -45,7 +48,9 @@ namespace FreeSql.Tests.MySql
public string Name { get; set; } public string Name { get; set; }
public Decimal Money { get; set; } public Decimal Money { get; set; }
} }
class NowAttribute: Attribute { }
private class NowAttribute : Attribute
{ }
[Fact] [Fact]
public void AuditValue() public void AuditValue()
@ -66,7 +71,6 @@ namespace FreeSql.Tests.MySql
g.clickHouse.Aop.AuditValue -= audit; g.clickHouse.Aop.AuditValue -= audit;
} }
[Fact] [Fact]
public void CreateTalbe() public void CreateTalbe()
{ {
@ -142,7 +146,6 @@ namespace FreeSql.Tests.MySql
fsql.Update<TestClickHouse>().Where(o => o.Id > 900) fsql.Update<TestClickHouse>().Where(o => o.Id > 900)
.Set(o => o.Name, "修改后的值") .Set(o => o.Name, "修改后的值")
.ExecuteAffrows(); .ExecuteAffrows();
} }
[Fact] [Fact]
@ -151,7 +154,6 @@ namespace FreeSql.Tests.MySql
var fsql = g.clickHouse; var fsql = g.clickHouse;
var list = fsql.GetRepository<TestClickHouse>().Where(o => o.Id > 900) var list = fsql.GetRepository<TestClickHouse>().Where(o => o.Id > 900)
.ToList(); .ToList();
} }
[Fact] [Fact]
@ -169,8 +171,12 @@ namespace FreeSql.Tests.MySql
var fsql = g.clickHouse; var fsql = g.clickHouse;
long id = BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0); long id = BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0);
DateTime createTime = DateTime.Now; DateTime createTime = DateTime.Now;
fsql.Insert(new TestAuditValue { fsql.Insert(new TestAuditValue
Id = id, CreateTime = createTime, Age =18,Name="张三" {
Id = id,
CreateTime = createTime,
Age = 18,
Name = "张三"
}).ExecuteAffrows(); }).ExecuteAffrows();
var date1 = fsql.GetRepository<TestAuditValue>().Where(o => o.CreateTime == createTime) var date1 = fsql.GetRepository<TestAuditValue>().Where(o => o.CreateTime == createTime)
@ -187,19 +193,16 @@ namespace FreeSql.Tests.MySql
.ToList(); .ToList();
var date7 = fsql.GetRepository<TestAuditValue>().Where(o => o.CreateTime.AddSeconds(10) < createTime) var date7 = fsql.GetRepository<TestAuditValue>().Where(o => o.CreateTime.AddSeconds(10) < createTime)
.ToList(); .ToList();
} }
[Fact] [Fact]
public void TestUpdateTime() public void TestUpdateTime()
{ {
var fsql = g.clickHouse; var fsql = g.clickHouse;
var state = fsql.GetRepository<TestAuditValue>().UpdateDiy.Set(o => o.UpdateTime, DateTime.Now).Where(o => 1 == 1).ExecuteAffrows(); var state = fsql.GetRepository<TestAuditValue>().UpdateDiy.Set(o => o.UpdateTime, DateTime.Now).Where(o => 1 == 1).ExecuteAffrows();
//var state1 = fsql.GetRepository<TestAuditValue>().UpdateDiy.Set(o => o.UpdateTime, null).Where(o => 1 == 1).ExecuteAffrows(); //var state1 = fsql.GetRepository<TestAuditValue>().UpdateDiy.Set(o => o.UpdateTime, null).Where(o => 1 == 1).ExecuteAffrows();
} }
[Fact] [Fact]
public void TestRepositoryUpdateTime() public void TestRepositoryUpdateTime()
{ {
@ -212,7 +215,10 @@ namespace FreeSql.Tests.MySql
list.Add(new TestAuditValue list.Add(new TestAuditValue
{ {
Id = new Random().Next(), Id = new Random().Next(),
Age=1, Name=i.ToString(), State=true, CreateTime=DateTime.Now, Age = 1,
Name = i.ToString(),
State = true,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now, UpdateTime = DateTime.Now,
Enable = false Enable = false
}); });
@ -225,8 +231,8 @@ namespace FreeSql.Tests.MySql
repository.Update(list); repository.Update(list);
stopwatch.Stop(); stopwatch.Stop();
Debug.WriteLine("更新用时:" + stopwatch.ElapsedMilliseconds.ToString()); Debug.WriteLine("更新用时:" + stopwatch.ElapsedMilliseconds.ToString());
} }
[Fact] [Fact]
public async void TestInsertUpdateData() public async void TestInsertUpdateData()
{ {
@ -260,8 +266,54 @@ namespace FreeSql.Tests.MySql
//await repository.UpdateAsync(list); //await repository.UpdateAsync(list);
//stopwatch.Stop(); //stopwatch.Stop();
Debug.WriteLine("更新用时:" + stopwatch.ElapsedMilliseconds.ToString()); Debug.WriteLine("更新用时:" + stopwatch.ElapsedMilliseconds.ToString());
} }
[Fact]
public async void TestInsertDecimalData()
{
//g.clickHouse.CodeFirst.SyncStructure<CollectDataEntity>();
Stopwatch stopwatch = new Stopwatch();
var fsql = g.clickHouse;
var repository = fsql.GetRepository<CollectDataEntity>();
await repository.DeleteAsync(o => o.Id > 0);
var insert = repository.Insert(new CollectDataEntity
{
Id = new Random().Next(),
CollectTime = DateTime.Now,
DataFlag = "1",
EquipmentCode = "11",
UnitStr = "111",
PropertyCode = "1111",
NumericValue = 1111.1119999912500M
});
var list = repository.Orm.Select<CollectDataEntity>().ToList();
//var list = repository.Insert(tables);
//var list = repository.Select.ToList();
//list.ForEach(o=>o.EquipmentCode = "666");
//stopwatch.Start();
//await repository.UpdateAsync(list);
//stopwatch.Stop();
Debug.WriteLine("更新用时:" + stopwatch.ElapsedMilliseconds.ToString());
}
internal class Entity
{
[Required]
public string Id { get; set; }
[Column(StringLength = -2)]
public string Content { get; set; }
}
[Fact]
public void TestInsertNoneParameter()
{
var json = "[{\"date\":\"2021-12-19T02:47:53.4365075 08:00\",\"temperatureC\":6,\"temperatureF\":42,\"summary\":\"Balmy\"},{\"date\":\"2021-12-20T02:47:53.4366893 08:00\",\"temperatureC\":36,\"temperatureF\":96,\"summary\":\"Bracing\"},{\"date\":\"2021-12-21T02:47:53.4366903 08:00\",\"temperatureC\":-15,\"temperatureF\":6,\"summary\":\"Bracing\"},{\"date\":\"2021-12-22T02:47:53.4366904 08:00\",\"temperatureC\":14,\"temperatureF\":57,\"summary\":\"Cool\"},{\"date\":\"2021-12-23T02:47:53.4366905 08:00\",\"temperatureC\":29,\"temperatureF\":84,\"summary\":\"Mild\"}]";
var data = new Entity { Id = Guid.NewGuid().ToString(), Content = json };
var fsql = g.clickHouse;
fsql.Insert(data).NoneParameter().ExecuteAffrows();
}
} }
} }

View File

@ -7,11 +7,11 @@ using System.Data.Common;
using System.Globalization; using System.Globalization;
using System.Data; using System.Data;
using ClickHouse.Client.ADO.Parameters; using ClickHouse.Client.ADO.Parameters;
using System.Text.RegularExpressions;
namespace FreeSql.ClickHouse namespace FreeSql.ClickHouse
{ {
internal class ClickHouseUtils : CommonUtils
class ClickHouseUtils : CommonUtils
{ {
public ClickHouseUtils(IFreeSql orm) : base(orm) public ClickHouseUtils(IFreeSql orm) : base(orm)
{ {
@ -53,19 +53,28 @@ namespace FreeSql.ClickHouse
var tp = _orm.CodeFirst.GetDbInfo(type)?.type; var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
if (tp != null) if (tp != null)
{ {
ret.DbType = (DbType)tp.Value; ret.DbType = (DbType)tp.Value;
} }
return ret; return ret;
}); });
public override string RewriteColumn(ColumnInfo col, string sql) public override string RewriteColumn(ColumnInfo col, string sql)
{ {
col.Attribute.DbType = col.Attribute.DbType.Replace(" NOT NULL", ""); col.Attribute.DbType = col.Attribute.DbType.Replace(" NOT NULL", "");
if (string.IsNullOrWhiteSpace(col?.Attribute.RewriteSql) == false) if (string.IsNullOrWhiteSpace(col?.Attribute.RewriteSql) == false)
return string.Format(col.Attribute.RewriteSql, sql); return string.Format(col.Attribute.RewriteSql, sql);
if (Regex.IsMatch(sql, @"\{\{[\w\d]+_+\d:\{\d\}\}\}"))
{
return string.Format(sql, col.Attribute.DbType); return string.Format(sql, col.Attribute.DbType);
} }
else
{
return sql;
}
}
public override string FormatSql(string sql, params object[] args) => sql?.FormatClickHouse(args); public override string FormatSql(string sql, params object[] args) => sql?.FormatClickHouse(args);
public override string QuoteSqlName(params string[] name) public override string QuoteSqlName(params string[] name)
{ {
if (name.Length == 1) if (name.Length == 1)
@ -79,6 +88,7 @@ namespace FreeSql.ClickHouse
} }
return $"`{string.Join("`.`", name)}`"; return $"`{string.Join("`.`", name)}`";
} }
public override string TrimQuoteSqlName(string name) public override string TrimQuoteSqlName(string name)
{ {
var nametrim = name.Trim(); var nametrim = name.Trim();
@ -86,12 +96,19 @@ namespace FreeSql.ClickHouse
return nametrim; //原生SQL return nametrim; //原生SQL
return $"{nametrim.Trim('`').Replace("`.`", ".").Replace(".`", ".")}"; return $"{nametrim.Trim('`').Replace("`.`", ".").Replace(".`", ".")}";
} }
public override string[] SplitTableName(string name) => GetSplitTableNames(name, '`', '`', 2); public override string[] SplitTableName(string name) => GetSplitTableNames(name, '`', '`', 2);
public override string QuoteParamterName(string name) => $"{{{{{name}:{{0}}}}}}"; public override string QuoteParamterName(string name) => $"{{{{{name}:{{0}}}}}}";
public override string IsNull(string sql, object value) => $"ifnull({sql}, {value})"; public override string IsNull(string sql, object value) => $"ifnull({sql}, {value})";
public override string StringConcat(string[] objs, Type[] types) => $"concat({string.Join(", ", objs)})"; public override string StringConcat(string[] objs, Type[] types) => $"concat({string.Join(", ", objs)})";
public override string Mod(string left, string right, Type leftType, Type rightType) => $"{left} % {right}"; public override string Mod(string left, string right, Type leftType, Type rightType) => $"{left} % {right}";
public override string Div(string left, string right, Type leftType, Type rightType) => $"{left} div {right}"; public override string Div(string left, string right, Type leftType, Type rightType) => $"{left} div {right}";
public override string Now => "now()"; public override string Now => "now()";
public override string NowUtc => "now('UTC')"; public override string NowUtc => "now('UTC')";
@ -108,6 +125,7 @@ namespace FreeSql.ClickHouse
} }
return paramterName; return paramterName;
} }
protected override string QuoteReadColumnAdapter(Type type, Type mapType, string columnName) protected override string QuoteReadColumnAdapter(Type type, Type mapType, string columnName)
{ {
switch (mapType.FullName) switch (mapType.FullName)