Skip to content

Commit

Permalink
resolve some conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
XmasApple committed Nov 21, 2023
1 parent 90e44cf commit 6939c3d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
19 changes: 15 additions & 4 deletions examples/src/QueryExample/QueryExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ private async Task InteractiveTx()
PRAGMA TablePathPrefix('{BasePath}');
SELECT first_aired FROM seasons
WHERE series_id = $series_id AND season_id = $season_id;
WHERE series_id = $series_id AND season_id = $season_id
LIMIT 1;
";
var parameters1 = new Dictionary<string, YdbValue>
{
Expand Down Expand Up @@ -254,22 +255,31 @@ private async Task StreamSelect()
";


await Client.Query(
var response = await Client.Query(
query,
func: async stream =>
{
var result = new List<Series>();
await foreach (var part in stream)
{
if (part.ResultSet == null) continue;
foreach (var row in part.ResultSet.Rows)
{
Console.WriteLine(Series.FromRow(row));
result.Add(Series.FromRow(row));
}
}
return stream.Next();
return result;
}
).ConfigureAwait(false);
response.EnsureSuccess();
if (response.Result != null)
{
foreach (var series in response.Result)
{
Console.WriteLine(series);
}
}
}

private async Task ReadScalar()
Expand Down Expand Up @@ -362,6 +372,7 @@ private async Task ReadAllResultSets()
{
Console.WriteLine($"\t\t{Series.FromRow(row)}");
}

Console.WriteLine("\t'episodes' contains:");
foreach (var row in episodesSet)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Ydb.Sdk/src/Services/Query/QueryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ public async Task<QueryResponse> Exec(string queryString,
internal static async Task<YdbValue> ReadScalarHelper(ExecuteQueryStream stream)
{
var row = await ReadSingleRowHelper(stream);
if (row.ColumnCount != 1)
{
throw new QueryWrongResultFormatException("Row should contain exactly one field");
}
return row[0];
}

Expand Down
2 changes: 2 additions & 0 deletions src/Ydb.Sdk/src/Value/ResultSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,7 @@ internal Row(Ydb.Value row, IReadOnlyList<Column> columns, IReadOnlyDictionary<s
public YdbValue this[int columnIndex] => new(_columns[columnIndex].Type, _row.Items[columnIndex]);

public YdbValue this[string columnName] => this[_columnsMap[columnName]];

internal int ColumnCount => _columns.Count;
}
}
12 changes: 7 additions & 5 deletions src/Ydb.Sdk/tests/Query/TestQueryIntegration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ public async Task TestSimpleSelect()
});

var responseAllRows = await client.ReadAllRows(queryString);
var responseFirstRow = await client.ReadSingleRow(queryString); // ReadOneRow
var responseSingleRow = await client.ReadSingleRow(queryString); // ReadOneRow

Assert.Equal(StatusCode.Success, responseQuery.Status.StatusCode);
Assert.Equal(StatusCode.Success, responseAllRows.Status.StatusCode);
Assert.Equal(StatusCode.Success, responseFirstRow.Status.StatusCode);
Assert.Equal(StatusCode.Success, responseSingleRow.Status.StatusCode);
Assert.NotNull(responseQuery.Result);
Assert.NotNull(responseFirstRow.Result);
Assert.NotNull(responseSingleRow.Result);
Assert.NotNull(responseAllRows.Result);
Assert.Single(responseQuery.Result!);
Assert.Single(responseAllRows.Result!);

var valueQuery = (int)responseQuery.Result!.First()["sum"];
var valueReadAll = (int)responseAllRows.Result!.First()["sum"];
var valueReadFirst = (int)responseFirstRow.Result!["sum"];
var valueReadSingle = (int)responseSingleRow.Result!["sum"];

Assert.Equal(valueQuery, valueReadAll);
Assert.Equal(valueQuery, valueReadFirst);
Assert.Equal(valueQuery, valueReadSingle);
Assert.Equal(5, valueQuery);
}

Expand Down Expand Up @@ -326,6 +326,8 @@ await Assert.ThrowsAsync<QueryWrongResultFormatException>(() =>
client.ReadScalar(SelectMultipleResultSetsQuery));
await Assert.ThrowsAsync<QueryWrongResultFormatException>(() =>
client.ReadScalar(SelectMultipleRowsQuery));
await Assert.ThrowsAsync<QueryWrongResultFormatException>(() =>
client.ReadScalar(SelectSingleRowQuery));
var response = await client.ReadScalar(SelectScalarQuery);
var resultSet = response.Result;
Assert.NotNull(resultSet);
Expand Down

0 comments on commit 6939c3d

Please sign in to comment.