Exemplo n.º 1
0
        public async Task HqlBulkAsync()
        {
            IStatelessSession ss = Sfi.OpenStatelessSession();
            ITransaction      tx = ss.BeginTransaction();
            var doc = new Document("blah blah blah", "Blahs");

            await(ss.InsertAsync(doc));
            var paper = new Paper {
                Color = "White"
            };

            await(ss.InsertAsync(paper));
            await(tx.CommitAsync());

            tx = ss.BeginTransaction();
            int count =
                await(ss.CreateQuery("update Document set Name = :newName where Name = :oldName").SetString("newName", "Foos").SetString(
                          "oldName", "Blahs").ExecuteUpdateAsync());

            Assert.AreEqual(1, count, "hql-delete on stateless session");
            count = await(ss.CreateQuery("update Paper set Color = :newColor").SetString("newColor", "Goldenrod").ExecuteUpdateAsync());
            Assert.AreEqual(1, count, "hql-delete on stateless session");
            await(tx.CommitAsync());

            tx    = ss.BeginTransaction();
            count = await(ss.CreateQuery("delete Document").ExecuteUpdateAsync());
            Assert.AreEqual(1, count, "hql-delete on stateless session");
            count = await(ss.CreateQuery("delete Paper").ExecuteUpdateAsync());
            Assert.AreEqual(1, count, "hql-delete on stateless session");
            await(tx.CommitAsync());
            ss.Close();
        }
Exemplo n.º 2
0
 private async Task SaveOrUpdate(IEnumerable <ForecastWeather> datas, IStatelessSession session)
 {
     foreach (var weather in datas)
     {
         await session.InsertAsync(weather);
     }
 }
Exemplo n.º 3
0
        public async Task CreateUpdateReadDeleteAsync()
        {
            Document doc;
            DateTime?initVersion;

            using (IStatelessSession ss = Sfi.OpenStatelessSession())
            {
                ITransaction tx;
                using (tx = ss.BeginTransaction())
                {
                    doc = new Document("blah blah blah", "Blahs");
                    await(ss.InsertAsync(doc));
                    Assert.IsNotNull(doc.LastModified);
                    initVersion = doc.LastModified;
                    Assert.IsTrue(initVersion.HasValue);
                    await(tx.CommitAsync());
                }
                await(Task.Delay(1100));                  // Ensure version increment (some dialects lack fractional seconds).
                using (tx = ss.BeginTransaction())
                {
                    doc.Text = "blah blah blah .... blah";
                    await(ss.UpdateAsync(doc));
                    Assert.IsTrue(doc.LastModified.HasValue);
                    Assert.AreNotEqual(initVersion, doc.LastModified);
                    await(tx.CommitAsync());
                }
                using (tx = ss.BeginTransaction())
                {
                    doc.Text = "blah blah blah .... blah blay";
                    await(ss.UpdateAsync(doc));
                    await(tx.CommitAsync());
                }
                var doc2 = await(ss.GetAsync <Document>("Blahs"));
                Assert.AreEqual("Blahs", doc2.Name);
                Assert.AreEqual(doc.Text, doc2.Text);

                doc2 = (Document)await(ss.CreateQuery("from Document where text is not null").UniqueResultAsync());
                Assert.AreEqual("Blahs", doc2.Name);
                Assert.AreEqual(doc.Text, doc2.Text);

                doc2 = (Document)await(ss.CreateSQLQuery("select * from Document").AddEntity(typeof(Document)).UniqueResultAsync());
                Assert.AreEqual("Blahs", doc2.Name);
                Assert.AreEqual(doc.Text, doc2.Text);

                doc2 = await(ss.CreateCriteria <Document>().UniqueResultAsync <Document>());
                Assert.AreEqual("Blahs", doc2.Name);
                Assert.AreEqual(doc.Text, doc2.Text);

                doc2 = (Document)await(ss.CreateCriteria(typeof(Document)).UniqueResultAsync());
                Assert.AreEqual("Blahs", doc2.Name);
                Assert.AreEqual(doc.Text, doc2.Text);

                using (tx = ss.BeginTransaction())
                {
                    await(ss.DeleteAsync(doc));
                    await(tx.CommitAsync());
                }
            }
        }
Exemplo n.º 4
0
        public async Task CreateAsync(Transaction entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            var transaction = TransactionConverter.Convert(entity);

            await _session.InsertAsync(transaction);
        }
Exemplo n.º 5
0
        public async Task CreateAsync(Category entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            var category = CategoryConverter.Convert(entity);

            await _session.InsertAsync(category);
        }
Exemplo n.º 6
0
        public async Task InitIdAsync()
        {
            Paper paper;

            using (IStatelessSession ss = Sfi.OpenStatelessSession())
            {
                ITransaction tx;
                using (tx = ss.BeginTransaction())
                {
                    paper = new Paper {
                        Color = "White"
                    };
                    await(ss.InsertAsync(paper));
                    Assert.IsTrue(paper.Id != 0);
                    await(tx.CommitAsync());
                }
                using (tx = ss.BeginTransaction())
                {
                    await(ss.DeleteAsync(await(ss.GetAsync <Paper>(paper.Id))));
                    await(tx.CommitAsync());
                }
            }
        }
Exemplo n.º 7
0
        public async Task RefreshAsync()
        {
            Paper paper;

            using (IStatelessSession ss = Sfi.OpenStatelessSession())
            {
                using (ITransaction tx = ss.BeginTransaction())
                {
                    paper = new Paper {
                        Color = "whtie"
                    };
                    await(ss.InsertAsync(paper));
                    await(tx.CommitAsync());
                }
            }
            using (IStatelessSession ss = Sfi.OpenStatelessSession())
            {
                using (ITransaction tx = ss.BeginTransaction())
                {
                    var p2 = await(ss.GetAsync <Paper>(paper.Id));
                    p2.Color = "White";
                    await(ss.UpdateAsync(p2));
                    await(tx.CommitAsync());
                }
            }
            using (IStatelessSession ss = Sfi.OpenStatelessSession())
            {
                using (ITransaction tx = ss.BeginTransaction())
                {
                    Assert.AreEqual("whtie", paper.Color);
                    await(ss.RefreshAsync(paper));
                    Assert.AreEqual("White", paper.Color);
                    await(ss.DeleteAsync(paper));
                    await(tx.CommitAsync());
                }
            }
        }
Exemplo n.º 8
0
        public static void ProcessData(VoteDetailCollectionDIO detailList, IStatelessSession session, bool newRecord)
        {
            using (var trans = session.BeginTransaction())
            {
                foreach (var detailEntry in detailList.VoteDetail)
                {
                    MPDBE MP;
                    var   MPCacheKey = detailEntry.FirstName + "//" + detailEntry.LastName;
                    if (MPCache.ContainsKey(MPCacheKey))
                    {
                        MP = MPCache[MPCacheKey];
                    }
                    else
                    {
                        MP = session.QueryOver <MPDBE>().Where(mp => mp.FirstName == detailEntry.FirstName && mp.LastName == detailEntry.LastName).List().FirstOrDefault();
                        if (MP == null)
                        {
                            MP = new MPDBE()
                            {
                                FirstName = detailEntry.FirstName,
                                LastName  = detailEntry.LastName,
                            };
                            session.Insert(MP);
                        }
                        MPCache[MPCacheKey] = MP;
                    }

                    VoteDetailDBE voteDetail;
                    if (!newRecord)
                    {
                        voteDetail = session.QueryOver <VoteDetailDBE>().Where(vd => vd.Vote == detailList.Vote && vd.MP == MP).List().FirstOrDefault();
                        if (voteDetail != null)
                        {
                            continue;
                        }
                    }

                    PoliticalGroupDBE politicalGroup;
                    if (PGCache.ContainsKey(detailEntry.PoliticalGroup))
                    {
                        politicalGroup = PGCache[detailEntry.PoliticalGroup];
                    }
                    else
                    {
                        politicalGroup = session.QueryOver <PoliticalGroupDBE>().Where(pg => pg.Name == detailEntry.PoliticalGroup).List().FirstOrDefault();
                        if (politicalGroup == null)
                        {
                            politicalGroup = new PoliticalGroupDBE()
                            {
                                Name = detailEntry.PoliticalGroup,
                            };
                            session.Insert(politicalGroup);
                        }
                        PGCache[detailEntry.PoliticalGroup] = politicalGroup;
                    }

                    VoteDetailDBE.VoteCastType voteCast;
                    switch (detailEntry.VoteCast)
                    {
                    case "DA":
                        voteCast = VoteDetailDBE.VoteCastType.VotedFor;
                        break;

                    case "NU":
                        voteCast = VoteDetailDBE.VoteCastType.VotedAgainst;
                        break;

                    case "AB":
                        voteCast = VoteDetailDBE.VoteCastType.Abstained;
                        break;

                    case "-":
                        voteCast = VoteDetailDBE.VoteCastType.VotedNone;
                        break;

                    default:
                        throw new InvalidOperationException("Unknown vote type: «" + detailEntry.VoteCast + "»");
                    }

                    voteDetail = new VoteDetailDBE()
                    {
                        Vote           = detailList.Vote,
                        MP             = MP,
                        VoteCast       = voteCast,
                        PoliticalGroup = politicalGroup,
                    };
                    session.InsertAsync(voteDetail);
                }
                trans.CommitAsync();
            }
        }
Exemplo n.º 9
0
 public Task <object> InsertAsync(object entity, CancellationToken cancellationToken = new CancellationToken())
 {
     EnsureWriteLock();
     return(_session.InsertAsync(entity, cancellationToken));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Adds the object asynchronously.
 /// </summary>
 /// <param name="entity">The entity.</param>
 /// <returns>
 /// The generated identifier
 /// </returns>
 public Task <object> AddAsync(T entity)
 {
     return(Session.InsertAsync(entity));
 }