public async Task <T> ExecuteScalar <T>(string cypherQuery, object queryParams = null) { T result = default(T); IAsyncSession session = _driver.AsyncSession(o => o.WithDatabase(this._database)); _logger.LogDebug($"Executing query: {cypherQuery}"); if (queryParams == null) { queryParams = new {}; } try { IResultCursor resultCursor = await session.RunAsync(cypherQuery, queryParams); IRecord record = await resultCursor.SingleAsync(); result = record[0].As <T>(); _logger.LogDebug("Query executed successfully"); } catch (Exception ex) { _logger.LogError(ex, $"Error executing query. {ex.Message}"); throw; } finally { await session.CloseAsync(); } return(result); }
public async Task ShouldThrowExceptionIfNullResult() { IResultCursor result = null; var exception = await Record.ExceptionAsync(() => result.SingleAsync()); exception.Should().BeOfType <ArgumentNullException>(); }
// ############ // RETURN SINGLE NODE // ############ async void SingleNodeLabelsTest() { // Each IDriver instance maintains a pool of connections inside, as a result, it is recommended to only use one driver per application. // The driver is thread-safe, while the session or the transaction is not thread-safe. IDriver driver = GraphDatabase.Driver("neo4j://localhost:7687", AuthTokens.Basic("neo4j", "123456")); IAsyncSession session = driver.AsyncSession(o => o.WithDatabase("neo4j")); try { IResultCursor cursor = await session.RunAsync("MATCH (a {name: 'Joel Silver'}) RETURN a"); // The recommended way to access these result records is to make use of methods provided by ResultCursorExtensions such as SingleAsync, // ToListAsync, and ForEachAsync. INode person = await cursor.SingleAsync(record => record["a"].As <INode>()); await cursor.ConsumeAsync(); foreach (var item in person.Labels) { Debug.Log(item); } } finally { await session.CloseAsync(); } await driver.CloseAsync(); }
private static async Task AssertCannotAccessRecords(IResultCursor cursor) { await ThrowsResultConsumedException(async() => await cursor.FetchAsync()); await ThrowsResultConsumedException(async() => await cursor.PeekAsync()); ThrowsResultConsumedException(() => cursor.Current); await ThrowsResultConsumedException(async() => await cursor.SingleAsync()); await ThrowsResultConsumedException(async() => await cursor.ToListAsync()); await ThrowsResultConsumedException(async() => await cursor.ForEachAsync(r => { })); }
protected async Task<int> CountPersonAsync(string name) { var session = Driver.AsyncSession(); try { return await session.ReadTransactionAsync(async tx => { IResultCursor result = await tx.RunAsync("MATCH (a:Person {name: $name}) RETURN count(a)", new {name}); return (await result.SingleAsync())[0].As<int>(); }); } finally { await session.CloseAsync(); } }
public static async Task <TReturn> MapSingleAsync <TValue1, TValue2, TValue3, TValue4, TValue5, TValue6, TValue7, TValue8, TValue9, TValue10, TValue11, TValue12, TValue13, TValue14, TValue15, TValue16, TReturn>( this IResultCursor resultCursor, Func <TValue1, TValue2, TValue3, TValue4, TValue5, TValue6, TValue7, TValue8, TValue9, TValue10, TValue11, TValue12, TValue13, TValue14, TValue15, TValue16, TReturn> mapFunc) { return((await resultCursor.SingleAsync().ConfigureAwait(false)).Map(mapFunc)); }
public static async Task <TReturn> MapSingleAsync <TValue1, TValue2, TReturn>( this IResultCursor resultCursor, Func <TValue1, TValue2, TReturn> mapFunc) { return((await resultCursor.SingleAsync().ConfigureAwait(false)).Map(mapFunc)); }
public static async Task <TReturn> MapSingleAsync <TReturn>( this IResultCursor resultCursor) { return((await resultCursor.SingleAsync().ConfigureAwait(false)).Map <TReturn>()); }