Skip to content

Commit

Permalink
Merge pull request #1936 from d4ilys/master
Browse files Browse the repository at this point in the history
增加TDengine支持
  • Loading branch information
2881099 authored Nov 30, 2024
2 parents 688960a + c194f35 commit 868e70e
Show file tree
Hide file tree
Showing 29 changed files with 3,074 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Providers\FreeSql.Provider.TDengine\FreeSql.Provider.TDengine.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using FreeSql.Tests.Provider.TDengine.TDengine.Tables;
using Xunit;

namespace FreeSql.Tests.Provider.TDengine.TDengine.TDengineAdo
{
public class TDengineAdoTest
{
IFreeSql fsql => g.tdengine;

[Fact]
void CodeFirstTest()
{
fsql.CodeFirst.SyncStructure<D1001>();
fsql.CodeFirst.SyncStructure<D1002>();
fsql.CodeFirst.SyncStructure<Users>();
}

[Fact]
void InsertTest()
{
var insertAffrows = fsql.Insert(new D1001()
{
Ts = DateTime.Now,
Current = 1,
Voltage = 1,
Describe = "D10021"
}
).ExecuteAffrows();

var insertAffrows2 = fsql.Insert(new D1001()
{
Ts = DateTime.Now,
Current = 1,
Voltage = 1,
Describe = "D10021"
}
).ExecuteAffrows();

var batchInsertAffrows = fsql.Insert(new List<D1002>()
{
new D1002()
{
Ts = DateTime.Now,
Current = 6,
Voltage = 6,
Describe = "D10026"
},
new D1002()
{
Ts = DateTime.Now,
Current = 3,
Voltage = 3,
Describe = "D10023"
},
new D1002()
{
Ts = DateTime.Now,
Current = 4,
Voltage = 4,
Describe = "D10024"
}
}
).ExecuteAffrows();
}

[Fact]
void SelectTest()
{
var subList = fsql.Select<D1001>().ToList(d => new
{
GroupId = d.GroupId
});

var superMetersList = fsql.Select<Meters>().ToList();
}

[Fact]
void WhereSelectTest()
{
var list = fsql.Select<Meters>().Where(d => d.GroupId == 2).ToList();
}

[Fact]
void DeleteTest()
{
var startTime = DateTime.Parse("2024-11-30T02:33:52.308+00:00");
var endTime = DateTime.Parse("2024-11-30T02:40:58.961+00:00");
var executeAffrows = fsql.Delete<Meters>()
.Where(meters => meters.Ts >= startTime && meters.Ts <= endTime && meters.GroupId == 1)
.ExecuteAffrows();
}

[Fact]
void DbFirst_GetDatabases()
{
var databases = fsql.DbFirst.GetDatabases();
foreach (var database in databases)
{
Console.WriteLine(database);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FreeSql.DataAnnotations;
using FreeSql.Provider.TDengine.Attributes;

namespace FreeSql.Tests.Provider.TDengine.TDengine.Tables
{
[TDengineSuperTable(Name = "meters")]
public class Meters
{
[Column(Name = "ts")]
public DateTime Ts { get; set; }

[Column(Name = "current")]
public float Current { get; set; }

[Column(Name = "voltage")]
public int Voltage { get; set; }

[Column(Name = "describe", StringLength = 50)]
public string? Describe { get; set; }

[TDengineTag(Name = "location")]
public virtual string? Location { get; set; }

[TDengineTag(Name = "group_id")]
public virtual int GroupId { get; set; }
}

[TDengineSubTable(SuperTableName = "meters", Name = "d1001")]
public class D1001 : Meters
{
[TDengineTag(Name = "location")]
public override string Location { get; set; } = "BeiJIng.ChaoYang";

[TDengineTag(Name = "group_id")]
public override int GroupId { get; set; } = 1;
}

[TDengineSubTable(SuperTableName = "meters", Name = "d1002")]
public class D1002 : Meters
{
[TDengineTag(Name = "location")]
public new string Location { get; set; } = "California.SanFrancisco";

[TDengineTag(Name = "group_id")]
public new int GroupId { get; set; } = 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FreeSql.DataAnnotations;

namespace FreeSql.Tests.Provider.TDengine.TDengine.Tables
{
[Table(Name = "users")]
public class Users
{
[Column(Name = "ts")]
public DateTime Ts { get; set; }

[Column(Name = "id")]
public float Id { get; set; }

[Column(Name = "address")]
public int Address { get; set; }

[Column(Name = "name", StringLength = 20)]
public string? Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace FreeSql.Tests.Provider.TDengine.TDengineAdo
{
public class TDengineAdoTest
{
IFreeSql fsql => g.tdengine;

[Fact]
public void AuditValue()
{
var executeConnectTest = fsql.Ado.ExecuteConnectTest();

}
}
}
26 changes: 26 additions & 0 deletions FreeSql.Tests/FreeSql.Tests.Provider.TDengine/g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FreeSql.Tests.Provider.TDengine
{
internal class g
{
private static readonly Lazy<IFreeSql> tdengineLazy = new Lazy<IFreeSql>(() =>
{
var fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.TDengine,
"host=localhost;port=6030;username=root;password=taosdata;protocol=Native;db=test;")
.UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}\r\n"))
.UseNoneCommandParameter(true)
.Build();
return fsql;
});

public static IFreeSql tdengine => tdengineLazy.Value;
}
}
29 changes: 29 additions & 0 deletions FreeSql.sln
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.Duckdb", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Tests.Provider.Duckdb", "FreeSql.Tests\FreeSql.Tests.Provider.Duckdb\FreeSql.Tests.Provider.Duckdb.csproj", "{DE79C012-15B1-40EC-AD19-CBD7D489DB8E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.TDengine", "Providers\FreeSql.Provider.TDengine\FreeSql.Provider.TDengine.csproj", "{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Tests.Provider.TDengine", "FreeSql.Tests\FreeSql.Tests.Provider.TDengine\FreeSql.Tests.Provider.TDengine.csproj", "{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -789,6 +793,30 @@ Global
{DE79C012-15B1-40EC-AD19-CBD7D489DB8E}.Release|x64.Build.0 = Release|Any CPU
{DE79C012-15B1-40EC-AD19-CBD7D489DB8E}.Release|x86.ActiveCfg = Release|Any CPU
{DE79C012-15B1-40EC-AD19-CBD7D489DB8E}.Release|x86.Build.0 = Release|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Debug|Any CPU.Build.0 = Debug|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Debug|x64.ActiveCfg = Debug|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Debug|x64.Build.0 = Debug|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Debug|x86.ActiveCfg = Debug|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Debug|x86.Build.0 = Debug|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Release|Any CPU.ActiveCfg = Release|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Release|Any CPU.Build.0 = Release|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Release|x64.ActiveCfg = Release|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Release|x64.Build.0 = Release|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Release|x86.ActiveCfg = Release|Any CPU
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317}.Release|x86.Build.0 = Release|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Debug|x64.ActiveCfg = Debug|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Debug|x64.Build.0 = Debug|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Debug|x86.ActiveCfg = Debug|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Debug|x86.Build.0 = Debug|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Release|Any CPU.Build.0 = Release|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Release|x64.ActiveCfg = Release|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Release|x64.Build.0 = Release|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Release|x86.ActiveCfg = Release|Any CPU
{0C178F1C-0F65-47AA-A1DE-545CBF8543D1}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -831,6 +859,7 @@ Global
{8064870C-22EA-4A58-972D-DBD57D096D91} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
{D9419896-BFB0-47C1-BEFD-A6C48394643B} = {4A92E8A6-9A6D-41A1-9CDA-DE10899648AA}
{4871434E-481D-4306-B6DD-73595C61A473} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
{329BA8B3-4139-4CCE-AFEC-4BE9B7BED317} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
RESX_NeutralResourcesLanguage = en-US
Expand Down
4 changes: 3 additions & 1 deletion FreeSql/DataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public enum DataType {

CustomOracle, CustomSqlServer, CustomMySql, CustomPostgreSQL,

DuckDB
DuckDB,

TDengine
}
}
5 changes: 5 additions & 0 deletions FreeSql/FreeSqlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ public IFreeSql<TMark> Build<TMark>()
if (type == null) throwNotFind("FreeSql.Provider.Duckdb.dll", "FreeSql.Duckdb.DuckdbProvider<>");
break;

case DataType.TDengine:
type = Type.GetType("FreeSql.TDengine.TDengineProvider`1,FreeSql.Provider.TDengine")?.MakeGenericType(typeof(TMark));
if (type == null) throwNotFind("FreeSql.Provider.TDengine.dll", "FreeSql.TDengine.TDengineProvider<>");
break;

default: throw new Exception(CoreErrorStrings.NotSpecified_UseConnectionString_UseConnectionFactory);
}
}
Expand Down
8 changes: 6 additions & 2 deletions FreeSql/Internal/CommonProvider/InsertProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,10 @@ public IInsert<T1> AsType(Type entityType)

public virtual string ToSql() => ToSqlValuesOrSelectUnionAllExtension103(true, null, null, false);

public string ToSqlValuesOrSelectUnionAll(bool isValues = true) => ToSqlValuesOrSelectUnionAllExtension103(isValues, null, null, false);
public string ToSqlValuesOrSelectUnionAll(bool isValues = true, List<string> ignoreColumn = null) => ToSqlValuesOrSelectUnionAllExtension103(isValues, null, null, false, ignoreColumn);
public string ToSqlValuesOrSelectUnionAllExtension101(bool isValues, Action<object, int, StringBuilder> onrow) => ToSqlValuesOrSelectUnionAllExtension103(isValues, null, onrow, false);
public string ToSqlValuesOrSelectUnionAllExtension102(bool isValues, Action<object, int, StringBuilder> onrowPre, Action<object, int, StringBuilder> onrow) => ToSqlValuesOrSelectUnionAllExtension103(isValues, onrowPre, onrow, false);
string ToSqlValuesOrSelectUnionAllExtension103(bool isValues, Action<object, int, StringBuilder> onrowPre, Action<object, int, StringBuilder> onrow, bool isAsTableSplited)
string ToSqlValuesOrSelectUnionAllExtension103(bool isValues, Action<object, int, StringBuilder> onrowPre, Action<object, int, StringBuilder> onrow, bool isAsTableSplited, List<string> ignoreColumn = null)
{
if (_source == null || _source.Any() == false) return null;
var sb = new StringBuilder();
Expand Down Expand Up @@ -643,6 +643,8 @@ string ToSqlValuesOrSelectUnionAllExtension103(bool isValues, Action<object, int
var colidx = 0;
foreach (var col in _table.Columns.Values)
{
//增加忽略插入的列
if (ignoreColumn != null && ignoreColumn.Contains(col.CsName)) continue;
if (col.Attribute.IsIdentity && _insertIdentity == false && string.IsNullOrEmpty(col.DbInsertValue)) continue;
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue;

Expand All @@ -663,6 +665,8 @@ string ToSqlValuesOrSelectUnionAllExtension103(bool isValues, Action<object, int
var colidx2 = 0;
foreach (var col in _table.Columns.Values)
{
//增加忽略插入的列
if (ignoreColumn != null && ignoreColumn.Contains(col.CsName)) continue;
if (col.Attribute.IsIdentity && _insertIdentity == false && string.IsNullOrEmpty(col.DbInsertValue)) continue;
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue;

Expand Down
6 changes: 5 additions & 1 deletion FreeSql/Internal/UtilsExpressionTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static ColumnInfo ColumnAttributeToInfo(TableInfo trytb, object entityDef
if (common.CodeFirst.IsSyncStructureToLower) colattr.Name = colattr.Name.ToLower();
if (common.CodeFirst.IsSyncStructureToUpper) colattr.Name = colattr.Name.ToUpper();

if ((colattr.IsNullable != true || colattr.IsIdentity == true || colattr.IsPrimary == true) && colattr.DbType.Contains("NOT NULL") == false && common._orm.Ado.DataType != DataType.ClickHouse)
if ((colattr.IsNullable != true || colattr.IsIdentity == true || colattr.IsPrimary == true) && colattr.DbType.Contains("NOT NULL") == false && common._orm.Ado.DataType != DataType.ClickHouse && common._orm.Ado.DataType != DataType.TDengine)
{
colattr.IsNullable = false;
colattr.DbType = Regex.Replace(colattr.DbType, @"\bNULL\b", "").Trim() + " NOT NULL";
Expand Down Expand Up @@ -425,6 +425,10 @@ public static ColumnInfo ColumnAttributeToInfo(TableInfo trytb, object entityDef
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.TDengine:
colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.MsAccess:
charPattern = @"(CHAR|CHAR2|CHARACTER|TEXT)\s*(\([^\)]*\))?";
if (strlen < 0) colattr.DbType = $"LONGTEXT{strNotNull}";
Expand Down
Loading

0 comments on commit 868e70e

Please sign in to comment.