This commit is contained in:
2881099 2022-02-23 11:55:53 +08:00
commit 5dc014bc7c
2 changed files with 119 additions and 49 deletions

View File

@ -7,16 +7,18 @@ using System.Linq;
using System.Collections;
using System.Diagnostics;
using XY.Model.Business;
using System.ComponentModel.DataAnnotations;
using FreeSql.DataAnnotations;
namespace FreeSql.Tests.MySql
{
public class ClickHouseTest1
{
class TestAuditValue
private class TestAuditValue
{
[FreeSql.DataAnnotations.Column(IsPrimary = true)]
public long Id { get; set; }
[Now]
public DateTime CreateTime { get; set; }
@ -35,6 +37,7 @@ namespace FreeSql.Tests.MySql
public int? Points { get; set; }
}
[FreeSql.DataAnnotations.Table(Name = "ClickHouseTest")]
public class TestClickHouse
{
@ -45,7 +48,9 @@ namespace FreeSql.Tests.MySql
public string Name { get; set; }
public Decimal Money { get; set; }
}
class NowAttribute: Attribute { }
private class NowAttribute : Attribute
{ }
[Fact]
public void AuditValue()
@ -66,7 +71,6 @@ namespace FreeSql.Tests.MySql
g.clickHouse.Aop.AuditValue -= audit;
}
[Fact]
public void CreateTalbe()
{
@ -142,7 +146,6 @@ namespace FreeSql.Tests.MySql
fsql.Update<TestClickHouse>().Where(o => o.Id > 900)
.Set(o => o.Name, "修改后的值")
.ExecuteAffrows();
}
[Fact]
@ -151,7 +154,6 @@ namespace FreeSql.Tests.MySql
var fsql = g.clickHouse;
var list = fsql.GetRepository<TestClickHouse>().Where(o => o.Id > 900)
.ToList();
}
[Fact]
@ -169,8 +171,12 @@ namespace FreeSql.Tests.MySql
var fsql = g.clickHouse;
long id = BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0);
DateTime createTime = DateTime.Now;
fsql.Insert(new TestAuditValue {
Id = id, CreateTime = createTime, Age =18,Name="张三"
fsql.Insert(new TestAuditValue
{
Id = id,
CreateTime = createTime,
Age = 18,
Name = "张三"
}).ExecuteAffrows();
var date1 = fsql.GetRepository<TestAuditValue>().Where(o => o.CreateTime == createTime)
@ -187,19 +193,16 @@ namespace FreeSql.Tests.MySql
.ToList();
var date7 = fsql.GetRepository<TestAuditValue>().Where(o => o.CreateTime.AddSeconds(10) < createTime)
.ToList();
}
[Fact]
public void TestUpdateTime()
{
var fsql = g.clickHouse;
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();
}
[Fact]
public void TestRepositoryUpdateTime()
{
@ -212,7 +215,10 @@ namespace FreeSql.Tests.MySql
list.Add(new TestAuditValue
{
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,
Enable = false
});
@ -225,8 +231,8 @@ namespace FreeSql.Tests.MySql
repository.Update(list);
stopwatch.Stop();
Debug.WriteLine("更新用时:" + stopwatch.ElapsedMilliseconds.ToString());
}
[Fact]
public async void TestInsertUpdateData()
{
@ -260,8 +266,54 @@ namespace FreeSql.Tests.MySql
//await repository.UpdateAsync(list);
//stopwatch.Stop();
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.Data;
using ClickHouse.Client.ADO.Parameters;
using System.Text.RegularExpressions;
namespace FreeSql.ClickHouse
{
class ClickHouseUtils : CommonUtils
internal class ClickHouseUtils : CommonUtils
{
public ClickHouseUtils(IFreeSql orm) : base(orm)
{
@ -53,19 +53,28 @@ namespace FreeSql.ClickHouse
var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
if (tp != null)
{
ret.DbType = (DbType)tp.Value;
}
return ret;
});
public override string RewriteColumn(ColumnInfo col, string sql)
{
col.Attribute.DbType = col.Attribute.DbType.Replace(" NOT NULL", "");
if (string.IsNullOrWhiteSpace(col?.Attribute.RewriteSql) == false)
return string.Format(col.Attribute.RewriteSql, sql);
if (Regex.IsMatch(sql, @"\{\{[\w\d]+_+\d:\{\d\}\}\}"))
{
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 QuoteSqlName(params string[] name)
{
if (name.Length == 1)
@ -79,6 +88,7 @@ namespace FreeSql.ClickHouse
}
return $"`{string.Join("`.`", name)}`";
}
public override string TrimQuoteSqlName(string name)
{
var nametrim = name.Trim();
@ -86,12 +96,19 @@ namespace FreeSql.ClickHouse
return nametrim; //原生SQL
return $"{nametrim.Trim('`').Replace("`.`", ".").Replace(".`", ".")}";
}
public override string[] SplitTableName(string name) => GetSplitTableNames(name, '`', '`', 2);
public override string QuoteParamterName(string name) => $"{{{{{name}:{{0}}}}}}";
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 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 Now => "now()";
public override string NowUtc => "now('UTC')";
@ -108,6 +125,7 @@ namespace FreeSql.ClickHouse
}
return paramterName;
}
protected override string QuoteReadColumnAdapter(Type type, Type mapType, string columnName)
{
switch (mapType.FullName)