Exemplo n.º 1
0
        public async Task <List <IRecord> > queryRead(string query)
        {
            List <IRecord> _records = null;

            try
            {
                _records = await session.ReadTransactionAsync(async tx =>
                {
                    // Send cypher query to the database
                    var _reader = await tx.RunAsync(query);

                    List <IRecord> _records = await _reader.ToListAsync();


                    return(_records);
                });
            } catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }

            finally
            {
                // asynchronously close session
                await session.CloseAsync();
            }


            return(_records);
        }
Exemplo n.º 2
0
        public async Task <List <INode> > ConnectDb(string query)
        {
            Driver = CreateDriverWithBasicAuth("bolt://localhost:7687", "neo4j", "1234");
            List <INode>  res     = new List <INode>();
            IAsyncSession session = Driver.AsyncSession(o => o.WithDatabase("neo4j"));

            try
            {
                res = await session.ReadTransactionAsync(async tx =>
                {
                    var results = new List <INode>();
                    var reader  = await tx.RunAsync(query);

                    while (await reader.FetchAsync())
                    {
                        results.Add(reader.Current[0].As <INode>());
                    }

                    return(results);
                });
            }

            finally
            {
                await session.CloseAsync();
            }
            return(res);
        }
Exemplo n.º 3
0
        }                                   // stores driver

        /// <summary>
        /// Connect to database and run a query string
        /// </summary>
        /// <param name="query">Query to run</param>
        /// <returns>Result from database</returns>
        public async Task <List <INode> > ConnectDb(string query)
        {
            // TODO get auth params from some config file
            Driver = CreateDriverWithBasicAuth("bolt://localhost:7687", "neo4j", "1234"); // connect to database
            List <INode>  res     = new List <INode>();                                   // create list to store results in
            IAsyncSession session = Driver.AsyncSession(o => o.WithDatabase("neo4j"));    // start session

            // try to start transaction with query proved. Return results
            try {
                res = await session.ReadTransactionAsync(async tx => {
                    var results = new List <INode>();
                    var reader  = await tx.RunAsync(query);

                    while (await reader.FetchAsync())
                    {
                        results.Add(reader.Current[0].As <INode>());
                    }

                    return(results); // return results
                });
            } finally {
                await session.CloseAsync(); // close session
            }

            return(res); // return results
        }
        public async Task <List <T> > Run <T>(IQuery <T>[] queries, string databaseName, bool defaultDatabase)
        {
            IAsyncSession session = GetAsyncSession(databaseName, defaultDatabase);

            try
            {
                LogRun("queries", databaseName, defaultDatabase);
                var resultToReturn = new List <T>();

                foreach (var query in queries)
                {
                    await session.ReadTransactionAsync(async tx =>
                    {
                        IResultCursor result = await tx.RunAsync(query.Query);
                        resultToReturn.AddRange(await result.ToListAsync(query.ProcessRecord));
                    });
                }

                return(resultToReturn);
            }
            finally
            {
                await session.CloseAsync();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Executes a <see cref="IAsyncSession.ReadTransactionAsync{T}(System.Func{IAsyncTransaction,Task{T}})" /> transaction
        ///     returning the <typeparamref name="T" /> specified.
        /// </summary>
        /// <remarks>
        ///     This should be used with queries not returning <see cref="INode" /> values, for example:
        ///     <c>MATCH (n) RETURN n.title AS title</c>
        /// </remarks>
        /// <typeparam name="T">The type to attempt to cast to. This should be a class.</typeparam>
        /// <param name="session">The <see cref="IAsyncSession" /> to run the transaction on.</param>
        /// <param name="query">The query to execute.</param>
        /// <param name="parameters">The parameters to the query.</param>
        /// <param name="identifier">
        ///     The identifier to cast into <typeparamref name="T" />. e.g. if the query is
        ///     <c>MATCH (n) RETURN n.title AS title</c> the identifier is <c>title</c>.
        /// </param>
        /// <returns>The results of the query.</returns>
        public static async Task <IEnumerable <T> > RunReadTransaction <T>(this IAsyncSession session, string query, object parameters, string identifier)
        {
            Ensure.That(session).IsNotNull();
            Ensure.That(query).IsNotNullOrWhiteSpace();
            Ensure.That(identifier).IsNotNullOrWhiteSpace();

            return(await session.ReadTransactionAsync(tx => ReadTransactionAsList(tx, query, parameters, cursor => cursor.GetValue <T>(identifier))));
        }
Exemplo n.º 6
0
        //todo: create package for DFC.ServiceTaxonomy.Neo4j??
        public async Task <object> ExecuteCypherQueryInNeo4JAsync(string query, IDictionary <string, object> statementParameters)
        {
            IAsyncSession session = _neo4JDriver.AsyncSession();

            try
            {
                return(await session.ReadTransactionAsync(async tx =>
                {
                    _resultCursor = await tx.RunAsync(query, statementParameters);
                    return await GetListOfRecordsAsync();
                }));
            }
            finally
            {
                await session.CloseAsync();
            }
        }
        public async Task <IEnumerable <Entity <TNode> > > GetAllEntities <TNode>(GraphInterfaces.IField field, IEnumerable <string> relationships)
            where TNode : class
        {
            IAsyncSession session = AsyncSession();

            try
            {
                string         cypher  = GetAllCypher <TNode>(field, relationships);
                List <IRecord> records = await session.ReadTransactionAsync(tx => RunCypherWithResults(tx, cypher));

                return(records?.Select(_ => new Entity <TNode> {
                    Node = _[0].As <INode>().Properties.AsJson().AsPoco <TNode>(), Relationships = Transform(_[1].As <IPath>())
                }));
            }
            finally
            {
                await session.CloseAsync();
            }
        }
 public async Task <T> ReadTransactionAsync <T>(Func <IAsyncTransaction, Task <T> > work)
 {
     return(await _session.ReadTransactionAsync(work));
 }