Skip to content

Commit d27335f

Browse files
committed
支持.net 4.6 版本
1 parent aae1a7b commit d27335f

File tree

9 files changed

+87
-37
lines changed

9 files changed

+87
-37
lines changed

EFCore.Taos.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-co
2828
EndProject
2929
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.Taos.Tests", "src\EntityFrameworkCore.Taos.Tests\EntityFrameworkCore.Taos.Tests.csproj", "{7F5F12D6-CA82-4499-AF3E-6CA7E61579CB}"
3030
EndProject
31-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TaosOrm", "TaosOrm\TaosOrm.csproj", "{7F9F4104-3AFF-4D1E-8AD4-154957159ADC}"
31+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TaosOrm", "TaosOrm\TaosOrm.csproj", "{7F9F4104-3AFF-4D1E-8AD4-154957159ADC}"
3232
EndProject
3333
Global
3434
GlobalSection(SolutionConfigurationPlatforms) = preSolution

src/IoTSharp.Data.Taos/DataReaderExtensions.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using Newtonsoft.Json.Linq;
22
using System;
33
using System.Collections.Generic;
4+
using System.ComponentModel.DataAnnotations.Schema;
45
using System.Data;
56
using System.Linq;
7+
using System.Runtime.InteropServices;
68
using System.Text;
79
using TDengineDriver;
8-
using System.ComponentModel.DataAnnotations.Schema;
910
namespace IoTSharp.Data.Taos
1011
{
1112
public static class DataReaderExtensions
@@ -108,7 +109,42 @@ public static string RemoveNull(this string str)
108109
return str?.Trim('\0');
109110
}
110111

111-
112-
112+
public static IntPtr ToIntPtr(this long val)
113+
{
114+
IntPtr lenPtr = Marshal.AllocHGlobal(sizeof(long));
115+
Marshal.WriteInt64(lenPtr, val);
116+
return lenPtr;
117+
}
118+
public static IntPtr ToIntPtr(this int val)
119+
{
120+
IntPtr lenPtr = Marshal.AllocHGlobal(sizeof(int ));
121+
Marshal.WriteInt32(lenPtr, val);
122+
return lenPtr;
123+
}
124+
public static (IntPtr ptr, int len) ToUTF8IntPtr(this string command)
125+
{
126+
127+
#if NET5_0_OR_GREATER
128+
IntPtr commandBuffer = Marshal.StringToCoTaskMemUTF8(command);
129+
int bufferlen = Encoding.UTF8.GetByteCount(command);
130+
#else
131+
var bytes = Encoding.UTF8.GetBytes(command);
132+
int bufferlen = bytes.Length;
133+
IntPtr commandBuffer = Marshal.AllocHGlobal(bufferlen);
134+
Marshal.Copy(bytes, 0, commandBuffer, bufferlen);
135+
#endif
136+
return (commandBuffer, bufferlen);
137+
}
138+
139+
public static void FreeUtf8IntPtr(this IntPtr ptr)
140+
{
141+
#if NET5_0_OR_GREATER
142+
Marshal.FreeCoTaskMem(ptr);
143+
#else
144+
Marshal.FreeHGlobal(ptr);
145+
#endif
146+
}
147+
148+
113149
}
114150
}

src/IoTSharp.Data.Taos/Driver/DatabaseSchema.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.ComponentModel.DataAnnotations;
43
using System.ComponentModel.DataAnnotations.Schema;
5-
using System.Linq;
64
using System.Text;
75
using System.Threading.Tasks;
86

src/IoTSharp.Data.Taos/Driver/TDengineDriver.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using System.Text;
2020
using System.Threading;
2121
using System.Threading.Tasks;
22-
22+
using IoTSharp.Data.Taos;
2323
namespace TDengineDriver
2424
{
2525

@@ -203,12 +203,15 @@ static public string Error(IntPtr res)
203203

204204
static public IntPtr Query(IntPtr conn, string command)
205205
{
206-
IntPtr commandBuffer = Marshal.StringToCoTaskMemUTF8(command);
207-
IntPtr res = Query(conn, commandBuffer);
208-
Marshal.FreeCoTaskMem(commandBuffer);
206+
var var = command.ToUTF8IntPtr();
207+
IntPtr res = Query(conn, var.ptr);
208+
var.ptr.FreeUtf8IntPtr();
209209
return res;
210210
}
211211

212+
213+
214+
212215
[DllImport("taos", EntryPoint = "taos_affected_rows", CallingConvention = CallingConvention.Cdecl)]
213216
static extern public int AffectRows(IntPtr res);
214217

src/IoTSharp.Data.Taos/Driver/TaosBind.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Runtime.InteropServices;
33
using System.Text;
4+
using IoTSharp.Data.Taos;
45

56
namespace TDengineDriver
67
{
@@ -247,16 +248,14 @@ public static TAOS_BIND BindBinary(String val)
247248
{
248249

249250
TAOS_BIND bind = new TAOS_BIND();
250-
// IntPtr unmanagedBinary = Marshal.StringToHGlobalAnsi(val);
251-
IntPtr c_str = Marshal.StringToCoTaskMemUTF8(val);
251+
var strToBytes= val. ToUTF8IntPtr();
252252

253-
var strToBytes = System.Text.Encoding.UTF8.GetBytes(val);
254-
int length = strToBytes.Length;
253+
int length = strToBytes.len;
255254
IntPtr lenPtr = Marshal.AllocHGlobal(sizeof(ulong));
256255
Marshal.WriteInt64(lenPtr, length);
257256

258257
bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_BINARY;
259-
bind.buffer = c_str;
258+
bind.buffer = strToBytes.ptr;
260259
bind.buffer_length = length;
261260
bind.length = lenPtr;
262261
bind.is_null = IntPtr.Zero;
@@ -266,19 +265,16 @@ public static TAOS_BIND BindBinary(String val)
266265
public static TAOS_BIND BindNchar(String val)
267266
{
268267
TAOS_BIND bind = new TAOS_BIND();
269-
var strToBytes = System.Text.Encoding.UTF8.GetBytes(val);
270-
// IntPtr unmanagedNchar = (IntPtr)Marshal.StringToHGlobalAnsi(val);
271-
IntPtr c_str = (IntPtr)Marshal.StringToCoTaskMemUTF8(val);
272-
273268

274-
int length = strToBytes.Length;
275-
IntPtr lenPtr = Marshal.AllocHGlobal(sizeof(ulong));
276-
Marshal.WriteInt64(lenPtr, length);
269+
// IntPtr unmanagedNchar = (IntPtr)Marshal.StringToHGlobalAnsi(val);
270+
var strToBytes = val.ToUTF8IntPtr();
271+
long length = strToBytes.len;
272+
277273

278274
bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_NCHAR;
279-
bind.buffer = c_str;
280-
bind.buffer_length = length;
281-
bind.length = lenPtr;
275+
bind.buffer = strToBytes.ptr;
276+
bind.buffer_length = (int)length;
277+
bind.length = length.ToIntPtr();
282278
bind.is_null = IntPtr.Zero;
283279

284280
return bind;
@@ -322,10 +318,15 @@ public static void FreeTaosBind(TAOS_BIND[] binds)
322318
{
323319
foreach (TAOS_BIND bind in binds)
324320
{
321+
#if NET5_0_OR_GREATER
325322
if (bind.buffer_type == (int)TDengineDataType.TSDB_DATA_TYPE_NCHAR || bind.buffer_type == (int)TDengineDataType.TSDB_DATA_TYPE_BINARY)
326323
Marshal.FreeCoTaskMem(bind.buffer);
327324
else
328-
Marshal.FreeHGlobal(bind.buffer);
325+
Marshal.FreeHGlobal(bind.buffer);
326+
#else
327+
Marshal.FreeHGlobal(bind.buffer);
328+
#endif
329+
329330
Marshal.FreeHGlobal(bind.length);
330331
if (bind.is_null != IntPtr.Zero)
331332
{

src/IoTSharp.Data.Taos/Driver/taosField.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ public taosField()
2020
public byte type;
2121
[MarshalAs(UnmanagedType.U2, SizeConst = 2)]
2222
public short size;
23-
23+
#if NET5_0_OR_GREATER
2424
public string Name => Encoding.UTF8.GetString(_name,0,_name.AsSpan().IndexOf((byte)0));
25-
25+
#else
26+
public string Name => Encoding.UTF8.GetString(_name, 0, Array.IndexOf( _name,(byte)0));
27+
#endif
2628
public TDengineDataType DataType => (TDengineDataType)type;
2729
public Type CrlType
2830
{

src/IoTSharp.Data.Taos/IoTSharp.Data.Taos.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
IoTSharp.Data.Taos.TaosParameter
1717
IoTSharp.Data.Taos.TaosTransaction
1818
</Description>
19-
<TargetFrameworks>net5;net6</TargetFrameworks>
19+
<TargetFrameworks>net5;net6;net4.6</TargetFrameworks>
2020
<LangVersion>10</LangVersion>
2121
<PackageTags>Taos;Data;ADO.NET;Entity Framework,;EF; Core;Data O/RM,;entity-framework-core;TDengine;IoTSharp</PackageTags>
2222

@@ -62,8 +62,12 @@
6262
<PackagePath></PackagePath>
6363
</None>
6464
</ItemGroup>
65-
66-
<ItemGroup>
65+
<ItemGroup Condition="'$(TargetFramework)' == 'net4.6'">
66+
<Reference Include="System.ComponentModel.DataAnnotations" />
67+
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
68+
</ItemGroup>
69+
<ItemGroup >
6770
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
71+
6872
</ItemGroup>
6973
</Project>

src/IoTSharp.Data.Taos/TaosConnection.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ public TaosConnection()
4040
{
4141
if (_dll_isloaded == false)
4242
{
43-
#if NET45
44-
45-
configDir = "C:/TDengine/cfg";
46-
#else
47-
43+
#if NET5_0_OR_GREATER
4844
if ( RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
4945
{
5046
configDir = "/etc/taos";
@@ -54,6 +50,8 @@ public TaosConnection()
5450
{
5551
configDir = "C:/TDengine/cfg";
5652
}
53+
#else
54+
configDir = "C:/TDengine/cfg";
5755
#endif
5856
TDengine.Options((int)TDengineInitOption.TSDB_OPTION_CONFIGDIR , this.configDir);
5957
TDengine.Options((int)TDengineInitOption.TSDB_OPTION_SHELL_ACTIVITY_TIMER , "60");

src/IoTSharp.Data.Taos/TaosDataReader.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,9 @@ public virtual TimeSpan GetTimeSpan(int ordinal)
366366
/// <param name="ordinal">The zero-based column ordinal.</param>
367367
/// <returns>The value of the column.</returns>
368368
public override string GetString(int ordinal) => (string)GetValue(ordinal);
369-
369+
#if NET5_0_OR_GREATER
370370
public System.Text.Json.JsonDocument GetJsonDocument(int ordinal) => System.Text.Json.JsonDocument.Parse(GetString(ordinal));
371+
#endif
371372
public Newtonsoft.Json.Linq.JToken GetJToken(int ordinal) => Newtonsoft.Json.Linq.JToken.Parse(GetString(ordinal));
372373

373374
/// <summary>
@@ -575,8 +576,15 @@ public override object GetValue(int ordinal)
575576
break;
576577
case TDengineDataType.TSDB_DATA_TYPE_BINARY:
577578
{
579+
#if NET5_0_OR_GREATER
578580
string v8 = Marshal.PtrToStringUTF8(data, GetContentLength(ordinal));
579581
result = v8?.RemoveNull();
582+
#else
583+
byte[] buffer = new byte[GetContentLength(ordinal)];
584+
Marshal.Copy(data, buffer, 0, buffer.Length);
585+
string v8 = Encoding.UTF8.GetString(buffer);
586+
result = v8?.RemoveNull();
587+
#endif
580588
}
581589
break;
582590
case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP:

0 commit comments

Comments
 (0)