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,20 +7,22 @@ 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; }
[FreeSql.DataAnnotations.Column(IsNullable = true )]
[FreeSql.DataAnnotations.Column(IsNullable = true)]
public string Name { get; set; }
[FreeSql.DataAnnotations.Column(IsNullable = false)]
@ -35,6 +37,7 @@ namespace FreeSql.Tests.MySql
public int? Points { get; set; }
}
[FreeSql.DataAnnotations.Table(Name = "ClickHouseTest")]
public class TestClickHouse
{
@ -45,12 +48,14 @@ 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()
{
var id = BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0);
var id = BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0);
var item = new TestClickHouse();
item.Id = id;
item.Name = "李四";
@ -66,7 +71,6 @@ namespace FreeSql.Tests.MySql
g.clickHouse.Aop.AuditValue -= audit;
}
[Fact]
public void CreateTalbe()
{
@ -76,11 +80,11 @@ namespace FreeSql.Tests.MySql
[Fact]
public void TestInsert()
{
Stopwatch stopwatch =new Stopwatch();
Stopwatch stopwatch = new Stopwatch();
var fsql = g.clickHouse;
List<TestClickHouse> list=new List<TestClickHouse>();
List<CollectDataEntity> list1=new List<CollectDataEntity>();
var date=DateTime.Now;
List<TestClickHouse> list = new List<TestClickHouse>();
List<CollectDataEntity> list1 = new List<CollectDataEntity>();
var date = DateTime.Now;
for (int i = 1; i < 1000000; i++)
{
//list.Add(new TestClickHouse
@ -101,14 +105,14 @@ namespace FreeSql.Tests.MySql
}
fsql.Delete<CollectDataEntity>().Where(t => 1 == 1).ExecuteAffrows();
stopwatch.Start();
var insert=fsql.Insert(list1);
var insert = fsql.Insert(list1);
stopwatch.Stop();
Debug.WriteLine("审计数据用时:" + stopwatch.ElapsedMilliseconds.ToString());
stopwatch.Restart();
insert.ExecuteAffrows();
//fsql.GetRepository<CollectDataEntity>().Insert(list1);
stopwatch.Stop();
Debug.WriteLine("转换并插入用时:" +stopwatch.ElapsedMilliseconds.ToString());
Debug.WriteLine("转换并插入用时:" + stopwatch.ElapsedMilliseconds.ToString());
//var items = fsql.Select<TestClickHouse>().Where(o=>o.Id>900).OrderByDescending(o=>o.Id).ToList();
//Assert.Equal(100, items.Count);
}
@ -118,9 +122,9 @@ namespace FreeSql.Tests.MySql
{
var fsql = g.clickHouse;
var list=fsql.Select<TestClickHouse>()
.Page(1,100)
.Where(o=>o.Id>200&&o.Id<500)
var list = fsql.Select<TestClickHouse>()
.Page(1, 100)
.Where(o => o.Id > 200 && o.Id < 500)
.Count(out var count).ToList();
//Assert.Equal(100, list.Count);
}
@ -129,7 +133,7 @@ namespace FreeSql.Tests.MySql
public void TestDelete()
{
var fsql = g.clickHouse;
var count1=fsql.Select<TestClickHouse>().Count();
var count1 = fsql.Select<TestClickHouse>().Count();
fsql.Delete<TestClickHouse>().Where(o => o.Id < 500).ExecuteAffrows();
var count2 = fsql.Select<TestClickHouse>().Count();
//Assert.NotEqual(count1, count2);
@ -140,27 +144,25 @@ namespace FreeSql.Tests.MySql
{
var fsql = g.clickHouse;
fsql.Update<TestClickHouse>().Where(o => o.Id > 900)
.Set(o=>o.Name,"修改后的值")
.Set(o => o.Name, "修改后的值")
.ExecuteAffrows();
}
[Fact]
public void TestRepositorySelect()
{
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();
}
[Fact]
public void TestRepositoryInsert()
{
var fsql = g.clickHouse;
long id = BitConverter.ToInt64(Guid.NewGuid().ToByteArray(),0);
var list=fsql.GetRepository<TestClickHouse>().Insert(new TestClickHouse { Id= id, Name="张三"});
var data=fsql.GetRepository<TestClickHouse,long>().Get(id);
long id = BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0);
var list = fsql.GetRepository<TestClickHouse>().Insert(new TestClickHouse { Id = id, Name = "张三" });
var data = fsql.GetRepository<TestClickHouse, long>().Get(id);
}
[Fact]
@ -168,9 +170,13 @@ 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="张三"
DateTime createTime = DateTime.Now;
fsql.Insert(new TestAuditValue
{
Id = id,
CreateTime = createTime,
Age = 18,
Name = "张三"
}).ExecuteAffrows();
var date1 = fsql.GetRepository<TestAuditValue>().Where(o => o.CreateTime == createTime)
@ -187,54 +193,54 @@ 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()
{
Stopwatch stopwatch = new Stopwatch();
var fsql = g.clickHouse;
var repository=fsql.GetRepository<TestAuditValue>();
List<TestAuditValue> list=new List<TestAuditValue>();
var repository = fsql.GetRepository<TestAuditValue>();
List<TestAuditValue> list = new List<TestAuditValue>();
for (int i = 1; i < 5; i++)
{
list.Add(new TestAuditValue
{
Id = new Random().Next(),
Age=1, Name=i.ToString(), State=true, CreateTime=DateTime.Now,
UpdateTime=DateTime.Now,
Age = 1,
Name = i.ToString(),
State = true,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
Enable = false
});
}
list = repository.Insert(list);
//var list = repository.Select.ToList();
list.ForEach(o=>o.UpdateTime = DateTime.Now);
list.ForEach(o => o.UpdateTime = DateTime.Now);
list.ForEach(o => o.Enable = true);
stopwatch.Start();
repository.Update(list);
stopwatch.Stop();
Debug.WriteLine("更新用时:" + stopwatch.ElapsedMilliseconds.ToString());
}
[Fact]
public async void TestInsertUpdateData()
{
//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 repository = fsql.GetRepository<CollectDataEntity>();
await repository.DeleteAsync(o => o.Id > 0);
List<CollectDataEntity> tables = new List<CollectDataEntity>();
for (int i = 1; i < 3; i++)
{
@ -246,7 +252,7 @@ namespace FreeSql.Tests.MySql
EquipmentCode = "11",
UnitStr = "111",
PropertyCode = "1111",
NumericValue=1111.1119999912500M
NumericValue = 1111.1119999912500M
});
}
@ -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)
{
@ -21,7 +21,7 @@ namespace FreeSql.ClickHouse
{
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
var dbtype = (DbType)_orm.CodeFirst.GetDbInfo(type)?.type;
DbParameter ret = new ClickHouseDbParameter { ParameterName = parameterName, DbType= dbtype, Value = value };//QuoteParamterName(parameterName)
DbParameter ret = new ClickHouseDbParameter { ParameterName = parameterName, DbType = dbtype, Value = value };//QuoteParamterName(parameterName)
if (col != null)
{
var dbtype2 = (DbType)_orm.DbFirst.GetDbType(new DatabaseModel.DbColumnInfo { DbTypeText = col.DbTypeText, DbTypeTextFull = col.Attribute.DbType, MaxLength = col.DbSize });
@ -53,19 +53,28 @@ namespace FreeSql.ClickHouse
var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
if (tp != null)
{
ret.DbType = (DbType)tp.Value;
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);
return string.Format(sql, col.Attribute.DbType);
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)