diff --git a/examples/src/QueryExample/QueryExample.cs b/examples/src/QueryExample/QueryExample.cs index 4e21c4c4..cbf6d121 100644 --- a/examples/src/QueryExample/QueryExample.cs +++ b/examples/src/QueryExample/QueryExample.cs @@ -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 { @@ -254,22 +255,31 @@ private async Task StreamSelect() "; - await Client.Query( + var response = await Client.Query( query, func: async stream => { + var result = new List(); 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() @@ -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) { diff --git a/src/Ydb.Sdk/src/Services/Query/QueryClient.cs b/src/Ydb.Sdk/src/Services/Query/QueryClient.cs index 2cf7a7f6..4a3afd1f 100644 --- a/src/Ydb.Sdk/src/Services/Query/QueryClient.cs +++ b/src/Ydb.Sdk/src/Services/Query/QueryClient.cs @@ -180,6 +180,10 @@ public async Task Exec(string queryString, internal static async Task ReadScalarHelper(ExecuteQueryStream stream) { var row = await ReadSingleRowHelper(stream); + if (row.ColumnCount != 1) + { + throw new QueryWrongResultFormatException("Row should contain exactly one field"); + } return row[0]; } diff --git a/src/Ydb.Sdk/src/Value/ResultSet.cs b/src/Ydb.Sdk/src/Value/ResultSet.cs index 33f39851..56df8305 100644 --- a/src/Ydb.Sdk/src/Value/ResultSet.cs +++ b/src/Ydb.Sdk/src/Value/ResultSet.cs @@ -129,5 +129,7 @@ internal Row(Ydb.Value row, IReadOnlyList columns, IReadOnlyDictionary new(_columns[columnIndex].Type, _row.Items[columnIndex]); public YdbValue this[string columnName] => this[_columnsMap[columnName]]; + + internal int ColumnCount => _columns.Count; } } diff --git a/src/Ydb.Sdk/tests/Query/TestQueryIntegration.cs b/src/Ydb.Sdk/tests/Query/TestQueryIntegration.cs index e9caaaef..af19f993 100644 --- a/src/Ydb.Sdk/tests/Query/TestQueryIntegration.cs +++ b/src/Ydb.Sdk/tests/Query/TestQueryIntegration.cs @@ -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); } @@ -326,6 +326,8 @@ await Assert.ThrowsAsync(() => client.ReadScalar(SelectMultipleResultSetsQuery)); await Assert.ThrowsAsync(() => client.ReadScalar(SelectMultipleRowsQuery)); + await Assert.ThrowsAsync(() => + client.ReadScalar(SelectSingleRowQuery)); var response = await client.ReadScalar(SelectScalarQuery); var resultSet = response.Result; Assert.NotNull(resultSet);