Skip to content

Commit 6ce3064

Browse files
authored
feat: named query parameters into API (#71)
1 parent cb5e4a8 commit 6ce3064

File tree

11 files changed

+409
-33
lines changed

11 files changed

+409
-33
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## 0.5.0 [unreleased]
22

3+
### Features
4+
5+
1. [#71](https://github.com/InfluxCommunity/influxdb3-csharp/pull/71): Add support for named query parameters
6+
37
### Others
48

59
1. [#80](https://github.com/InfluxCommunity/influxdb3-csharp/pull/80): Use net8.0 as a default target framework in Tests and Examples

Client.Test.Integration/QueryWriteTest.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Linq;
45
using System.Threading.Tasks;
@@ -123,7 +124,6 @@ public async Task CanDisableCertificateValidation()
123124
await client.WritePointAsync(PointData.Measurement("cpu").SetTag("tag", "c"));
124125
}
125126

126-
127127
[Test]
128128
public async Task WriteDataGzipped()
129129
{
@@ -140,4 +140,28 @@ public async Task WriteDataGzipped()
140140

141141
await client.WritePointAsync(PointData.Measurement("cpu").SetTag("tag", "c").SetField("user", 14.34));
142142
}
143+
144+
[Test]
145+
public async Task QueryWriteParameters()
146+
{
147+
using var client = new InfluxDBClient(new ClientConfig
148+
{
149+
Host = _host,
150+
Token = _token,
151+
Database = _database
152+
});
153+
154+
var testId = DateTime.UtcNow.Millisecond;
155+
await client.WriteRecordAsync($"integration_test,type=used value=1234.0,testId={testId}");
156+
157+
const string sql = "SELECT value FROM integration_test where \"testId\" = $testId";
158+
await foreach (var row in client.Query(sql, namedParameters: new Dictionary<string, object>
159+
{
160+
{ "testId", testId },
161+
}))
162+
{
163+
Assert.That(row, Has.Length.EqualTo(1));
164+
Assert.That(row[0], Is.EqualTo(1234.0));
165+
}
166+
}
143167
}

Client.Test/Client.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<PackageReference Include="coverlet.collector" Version="6.0.0" />
2121
<PackageReference Include="JunitXml.TestLogger" Version="3.1.12" />
2222
<PackageReference Include="WireMock.Net" Version="1.5.48" />
23+
<PackageReference Include="Moq" Version="4.20.70" />
24+
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
2325
</ItemGroup>
2426

2527
<ItemGroup>

Client.Test/InfluxDBClientQueryTest.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Apache.Arrow;
6+
using InfluxDB3.Client.Internal;
7+
using InfluxDB3.Client.Query;
8+
using Moq;
29

310
namespace InfluxDB3.Client.Test;
411

@@ -37,4 +44,59 @@ public void NotSpecifiedDatabase()
3744
Assert.That(ae, Is.Not.Null);
3845
Assert.That(ae.Message, Is.EqualTo("Please specify the 'database' as a method parameter or use default configuration at 'ClientConfig.Database'."));
3946
}
47+
48+
[Test]
49+
public async Task PassNamedParametersToFlightClient()
50+
{
51+
//
52+
// Mock the FlightSqlClient
53+
//
54+
var mockFlightSqlClient = new Mock<IFlightSqlClient>();
55+
mockFlightSqlClient
56+
.Setup(m => m.Execute(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<QueryType>(),
57+
It.IsAny<Dictionary<string, object>>()))
58+
.Returns(new List<RecordBatch>().ToAsyncEnumerable());
59+
60+
//
61+
// Setup the client with the mocked FlightSqlClient
62+
//
63+
_client = new InfluxDBClient(MockServerUrl);
64+
_client.FlightSqlClient.Dispose();
65+
_client.FlightSqlClient = mockFlightSqlClient.Object;
66+
67+
const string query = "select * from cpu where location = $location and core_count = $core-count and production = $production and max_frequency > $max-frequency";
68+
const QueryType queryType = QueryType.SQL;
69+
var namedParameters = new Dictionary<string, object>
70+
{
71+
{ "location", "us" },
72+
{ "core-count", 4 },
73+
{ "production", true },
74+
{ "max-frequency", 3.5 }
75+
};
76+
77+
_ = await _client.QueryPoints(query, database: "my-db", queryType: queryType, namedParameters: namedParameters)
78+
.ToListAsync();
79+
mockFlightSqlClient.Verify(m => m.Execute(query, "my-db", queryType, namedParameters), Times.Exactly(1));
80+
}
81+
82+
[Test]
83+
public void NotSupportedQueryParameterType()
84+
{
85+
_client = new InfluxDBClient(MockServerUrl);
86+
var ae = Assert.ThrowsAsync<ArgumentException>(async () =>
87+
{
88+
_ = await _client
89+
.Query("select * from cpu where location = $location", database: "my-db",
90+
queryType: QueryType.SQL, namedParameters: new Dictionary<string, object>
91+
{
92+
{ "location", DateTime.UtcNow }
93+
})
94+
.ToListAsync();
95+
});
96+
97+
Assert.That(ae, Is.Not.Null);
98+
Assert.That(ae.Message,
99+
Is.EqualTo(
100+
"The parameter 'location' has unsupported type 'System.DateTime'. The supported types are 'string', 'bool', 'int' and 'float'."));
101+
}
40102
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using InfluxDB3.Client.Config;
5+
using InfluxDB3.Client.Internal;
6+
using InfluxDB3.Client.Query;
7+
8+
namespace InfluxDB3.Client.Test.Internal;
9+
10+
public class FlightSqlClientTest : MockServerTest
11+
{
12+
private IFlightSqlClient _flightSqlClient;
13+
14+
[SetUp]
15+
public void SetUp()
16+
{
17+
var config = new ClientConfig
18+
{
19+
Host = MockServerUrl,
20+
Timeout = TimeSpan.FromSeconds(45)
21+
};
22+
23+
_flightSqlClient = new FlightSqlClient(config, InfluxDBClient.CreateAndConfigureHttpClient(config));
24+
}
25+
26+
[TearDownAttribute]
27+
public void TearDownAttribute()
28+
{
29+
_flightSqlClient.Dispose();
30+
}
31+
32+
[Test]
33+
public void PrepareFlightTicket()
34+
{
35+
var prepareFlightTicket = _flightSqlClient.PrepareFlightTicket(
36+
"select * from mem",
37+
"my-db",
38+
QueryType.InfluxQL, new Dictionary<string, object>());
39+
40+
const string ticket =
41+
@"{""database"":""my-db"",""sql_query"":""select * from mem"",""query_type"":""influxql""}";
42+
Assert.Multiple(() =>
43+
{
44+
Assert.That(prepareFlightTicket, Is.Not.Null);
45+
var actual = Encoding.UTF8.GetString(prepareFlightTicket.Ticket.ToByteArray());
46+
Console.WriteLine(actual);
47+
Assert.That(actual, Is.EqualTo(ticket));
48+
});
49+
}
50+
51+
[Test]
52+
public void PrepareFlightTicketNamedParameters()
53+
{
54+
var prepareFlightTicket = _flightSqlClient.PrepareFlightTicket(
55+
"select * from cpu where location = $location and production = $production and count = $count and temperature = $temperature",
56+
"my-db",
57+
QueryType.SQL,
58+
new Dictionary<string, object>
59+
{
60+
{ "location", "us" },
61+
{ "production", true },
62+
{ "count", 10 },
63+
{ "temperature", 23.5 }
64+
});
65+
66+
var ticket = "{\"database\":\"my-db\"," +
67+
"\"sql_query\":\"select * from cpu where location = $location and production = $production and count = $count and temperature = $temperature\"," +
68+
"\"query_type\":\"sql\"," +
69+
"\"params\": {\"location\":\"us\",\"production\":true,\"count\":10,\"temperature\":23.5}}";
70+
Assert.Multiple(() =>
71+
{
72+
Assert.That(prepareFlightTicket, Is.Not.Null);
73+
Assert.That(Encoding.UTF8.GetString(prepareFlightTicket.Ticket.ToByteArray()), Is.EqualTo(ticket));
74+
});
75+
}
76+
}

Client/Client.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
4848
<_Parameter1>InfluxDB3.Client.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100054b3efef02968d05c3dd8481e23fb40ade1fae377f18cf5fa48c673694140f7c00dc0b38d43be297256824dc8489c5224647e77f861ef600514607159b151cf71b094a0ef5736c420cbaa14100acc3b3694e3815597a5e89cf8090ed22bfdad2d5eec49250d88da1345d670b5e131ed9611eed141e04c31d79f166db39cb4a5</_Parameter1>
4949
</AssemblyAttribute>
50+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
51+
<_Parameter1>DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7</_Parameter1>
52+
</AssemblyAttribute>
5053
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
5154
<_Parameter1>InfluxDB3.Client.Test.Integration, PublicKey=0024000004800000940000000602000000240000525341310004000001000100054b3efef02968d05c3dd8481e23fb40ade1fae377f18cf5fa48c673694140f7c00dc0b38d43be297256824dc8489c5224647e77f861ef600514607159b151cf71b094a0ef5736c420cbaa14100acc3b3694e3815597a5e89cf8090ed22bfdad2d5eec49250d88da1345d670b5e131ed9611eed141e04c31d79f166db39cb4a5</_Parameter1>
5255
</AssemblyAttribute>

Client/Config/ClientConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private void ParseGzipThreshold(string? threshold)
174174
{
175175
if (threshold != null)
176176
{
177-
int gzipThreshold = Int32.Parse(threshold);
177+
var gzipThreshold = int.Parse(threshold);
178178
WriteOptions ??= (WriteOptions)WriteOptions.DefaultOptions.Clone();
179179
WriteOptions.GzipThreshold = gzipThreshold;
180180
}

0 commit comments

Comments
 (0)