Skip to content

Commit 3595ef9

Browse files
dev: example-app c# query service (#7717)
1 parent dc00a4b commit 3595ef9

File tree

2 files changed

+118
-216
lines changed

2 files changed

+118
-216
lines changed

ydb/docs/en/core/dev/example-app/_includes/example-dotnet.md

Lines changed: 59 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -31,112 +31,79 @@ public static async Task Run(
3131
App code snippet for creating a session:
3232

3333
```c#
34-
using var tableClient = new TableClient(driver, new TableClientConfig());
34+
using var queryClient = new QueryService(driver);
3535
```
3636

3737
{% include [steps/02_create_table.md](steps/02_create_table.md) %}
3838

39-
To create tables, use the `session.ExecuteSchemeQuery` method with a DDL (Data Definition Language) YQL query.
39+
To create tables, use the `queryClient.Exec` method with a DDL (Data Definition Language) YQL query.
4040

4141
```c#
42-
var response = await tableClient.SessionExec(async session =>
43-
{
44-
return await session.ExecuteSchemeQuery(@"
45-
CREATE TABLE series (
46-
series_id Uint64 NOT NULL,
47-
title Utf8,
48-
series_info Utf8,
49-
release_date Date,
50-
PRIMARY KEY (series_id)
51-
);
52-
53-
CREATE TABLE seasons (
54-
series_id Uint64,
55-
season_id Uint64,
56-
title Utf8,
57-
first_aired Date,
58-
last_aired Date,
59-
PRIMARY KEY (series_id, season_id)
60-
);
61-
62-
CREATE TABLE episodes (
63-
series_id Uint64,
64-
season_id Uint64,
65-
episode_id Uint64,
66-
title Utf8,
67-
air_date Date,
68-
PRIMARY KEY (series_id, season_id, episode_id)
69-
);
70-
");
71-
});
72-
73-
response.Status.EnsureSuccess();
42+
await queryClient.Exec(@"
43+
CREATE TABLE series (
44+
series_id Uint64 NOT NULL,
45+
title Utf8,
46+
series_info Utf8,
47+
release_date Date,
48+
PRIMARY KEY (series_id)
49+
);
50+
51+
CREATE TABLE seasons (
52+
series_id Uint64,
53+
season_id Uint64,
54+
title Utf8,
55+
first_aired Date,
56+
last_aired Date,
57+
PRIMARY KEY (series_id, season_id)
58+
);
59+
60+
CREATE TABLE episodes (
61+
series_id Uint64,
62+
season_id Uint64,
63+
episode_id Uint64,
64+
title Utf8,
65+
air_date Date,
66+
PRIMARY KEY (series_id, season_id, episode_id)
67+
);
68+
");
7469
```
7570

7671
{% include [steps/03_write_queries.md](steps/03_write_queries.md) %}
7772

7873
Code snippet for data insert/update:
7974

8075
```c#
81-
var response = await tableClient.SessionExec(async session =>
82-
{
83-
var query = @"
84-
DECLARE $id AS Uint64;
85-
DECLARE $title AS Utf8;
86-
DECLARE $release_date AS Date;
87-
88-
UPSERT INTO series (series_id, title, release_date) VALUES
89-
($id, $title, $release_date);
90-
";
91-
92-
return await session.ExecuteDataQuery(
93-
query: query,
94-
txControl: TxControl.BeginSerializableRW().Commit(),
95-
parameters: new Dictionary<string, YdbValue>
96-
{
97-
{ "$id", YdbValue.MakeUint64(1) },
98-
{ "$title", YdbValue.MakeUtf8("NewTitle") },
99-
{ "$release_date", YdbValue.MakeDate(DateTime.UtcNow) }
100-
}
101-
);
102-
});
103-
104-
response.Status.EnsureSuccess();
76+
await queryClient.Exec(@"
77+
UPSERT INTO series (series_id, title, release_date) VALUES
78+
($id, $title, $release_date);
79+
",
80+
new Dictionary<string, YdbValue>
81+
{
82+
{ "$id", YdbValue.MakeUint64(1) },
83+
{ "$title", YdbValue.MakeUtf8("NewTitle") },
84+
{ "$release_date", YdbValue.MakeDate(DateTime.UtcNow) }
85+
}
86+
);
10587
```
10688

107-
{% include [pragmatablepathprefix.md](auxilary/pragmatablepathprefix.md) %}
108-
10989
{% include [steps/04_query_processing.md](steps/04_query_processing.md) %}
11090

111-
To execute YQL queries, use the `Session.executeDataQuery()` method. The SDK lets you explicitly control the execution of transactions and configure the transaction execution mode using the `TxControl` class. In the code snippet below, a transaction with the `SerializableRW` mode and an automatic commit after executing the request is used. The values of the request parameters are passed in the form of a dictionary name-value in the `parameters` argument.
91+
To execute YQL queries, use the `queryClient.ReadRow` или `queryClient.ReadAllRows` method. The SDK lets you explicitly control the execution of transactions and configure the transaction execution mode using the `TxMode` enum. In the code snippet below, a transaction with the `NoTx` mode and an automatic commit after executing the request is used. The values of the request parameters are passed in the form of a dictionary name-value in the `parameters` argument.
11292

11393
```c#
114-
var response = await tableClient.SessionExec(async session =>
115-
{
116-
var query = @"
117-
DECLARE $id AS Uint64;
118-
94+
var row = await queryClient.ReadRow(@"
11995
SELECT
12096
series_id,
12197
title,
12298
release_date
12399
FROM series
124100
WHERE series_id = $id;
125-
";
126-
127-
return await session.ExecuteDataQuery(
128-
query: query,
129-
txControl: TxControl.BeginSerializableRW().Commit(),
130-
parameters: new Dictionary<string, YdbValue>
131-
{
132-
{ "$id", YdbValue.MakeUint64(id) }
133-
},
134-
);
135-
});
136-
137-
response.Status.EnsureSuccess();
138-
var queryResponse = (ExecuteDataQueryResponse)response;
139-
var resultSet = queryResponse.Result.ResultSets[0];
101+
",
102+
new Dictionary<string, YdbValue>
103+
{
104+
{ "$id", YdbValue.MakeUint64(id) }
105+
}
106+
);
140107
```
141108

142109
{% include [steps/05_results_processing.md](steps/05_results_processing.md) %}
@@ -153,35 +120,19 @@ foreach (var row in resultSet.Rows)
153120
}
154121
```
155122

156-
157-
158123
{% include [scan_query.md](steps/08_scan_query.md) %}
159124

160125
```c#
161-
public void executeScanQuery()
162-
{
163-
var scanStream = TableClient.ExecuteScanQuery(@$"
164-
SELECT series_id, season_id, COUNT(*) AS episodes_count
165-
FROM episodes
166-
GROUP BY series_id, season_id
167-
ORDER BY series_id, season_id;
168-
");
169-
170-
while (await scanStream.Next())
171-
{
172-
scanStream.Response.EnsureSuccess();
173-
174-
var resultSet = scanStream.Response.Result.ResultSetPart;
175-
if (resultSet != null)
126+
await queryClient.Stream(
127+
$"SELECT title FROM seasons ORDER BY series_id, season_id;",
128+
async stream =>
176129
{
177-
foreach (var row in resultSet.Rows)
178-
{
179-
Console.WriteLine($"> ScanQuery, " +
180-
$"series_id: {(ulong)row["series_id"]}, " +
181-
$"season_id: {(ulong?)row["season_id"]}, " +
182-
$"episodes_count: {(ulong)row["episodes_count"]}");
183-
}
184-
}
185-
}
186-
}
130+
await foreach (var part in stream)
131+
{
132+
foreach (var row in part.ResultSet!.Rows)
133+
{
134+
Console.WriteLine(row[0].GetOptionalUtf8());
135+
}
136+
}
137+
});
187138
```

0 commit comments

Comments
 (0)