public async Task HqlAsync() { var testData = new TestData(Sfi); await(testData.createDataAsync()); using (IStatelessSession s = Sfi.OpenStatelessSession()) { Assert.AreEqual(1, (await(s.CreateQuery("from Contact c join fetch c.Org join fetch c.Org.Country").ListAsync <Contact>())).Count); } await(testData.cleanDataAsync()); }
public void Hql() { var testData = new TestData(sessions); testData.createData(); using (IStatelessSession s = sessions.OpenStatelessSession()) { Assert.AreEqual(1, s.CreateQuery("from Contact c join fetch c.Org join fetch c.Org.Country").List <Contact>().Count); } testData.cleanData(); }
/// <summary> /// Removes the stale persisted grants. /// </summary> protected virtual async Task RemoveGrantsAsync() { const string deleteExpiredGrantsHql = "delete PersistedGrant pg where pg.ID in (:expiredGrantsIDs)"; var expiredTokenFound = int.MaxValue; while (expiredTokenFound >= _options.TokenCleanupBatchSize) { using (var tx = _session.BeginTransaction()) { var expiredGrantsQuery = _session.QueryOver <PersistedGrant>() .Where(g => g.Expiration < DateTime.UtcNow) .OrderBy(g => g.Expiration).Asc .Take(_options.TokenCleanupBatchSize); var expiredGrants = await expiredGrantsQuery.ListAsync(); var expiredGrantsIDs = expiredGrants.Select(pg => pg.ID).ToArray(); expiredTokenFound = expiredGrantsIDs.Length; if (expiredTokenFound > 0) { _logger.LogInformation($"Removing {expiredTokenFound} expired grants"); await _session.CreateQuery(deleteExpiredGrantsHql) .SetParameterList("expiredGrantsIDs", expiredGrantsIDs) .ExecuteUpdateAsync(); await tx.CommitAsync(); if (_operationalStoreNotification != null) { await _operationalStoreNotification.PersistedGrantsRemovedAsync(expiredGrants); } } } } }
private IEnumerable <long> QueryProteinTableForSubstrings(IStatelessSession session, ICollection <string> sequenceList) { List <String> likeClauses = new List <string>(); foreach (String seq in sequenceList) { likeClauses.Add("p.Sequence LIKE '%" + seq + "%'"); } String hql = "SELECT p.Id FROM " + typeof(DbProtein) + " p WHERE\n" + String.Join("\nOR ", likeClauses); IQuery query = session.CreateQuery(hql); return(query.List <long>()); }
public void Flush() { lock (m_lock) { using (var tx = m_statelessSession.BeginTransaction()) { //update the timestamp on the current snapshot m_statelessSession.CreateQuery("update Snapshot set TimeStamp = :timeStamp where Id = 0") .SetInt64("timeStamp", DateTime.Now.ToFileTime()) .ExecuteUpdate(); //flush functions foreach (var f in m_functionCache) { try { m_statelessSession.Insert(f); } catch (NHibernate.Exceptions.GenericADOException) { //this can happen with duplicates, which should not be possible with the caching in ProfilerClient //that may change, though, and we want to be robust to this particular failure } } m_functionCache.Clear(); //flush classes foreach (var c in m_classCache) { try { m_statelessSession.Insert(c); } catch (NHibernate.Exceptions.GenericADOException) { //see above } } m_classCache.Clear(); tx.Commit(); } DoFlush(); m_lastFlush = DateTime.Now; Utilities.FireEvent(this, DataFlush); } }
/// <summary> /// Finds all with custom HQL query using IStatelessSession. /// </summary> /// <param name="queryString">The query string.</param> /// <param name="firstRow">The number of the first row to retrieve.</param> /// <param name="maxRows">The maximum number of results retrieved.</param> /// <returns></returns> public virtual Array FindAllWithCustomQueryStateless(string queryString, int firstRow, int maxRows) { if (string.IsNullOrEmpty(queryString)) { throw new ArgumentNullException("queryString"); } using (IStatelessSession session = GetStatelessSession()) { try { IQuery query = session.CreateQuery(queryString); if (firstRow != int.MinValue) { query.SetFirstResult(firstRow); } if (maxRows != int.MinValue) { query.SetMaxResults(maxRows); } IList result = query.List(); if (result == null || result.Count == 0) { return(null); } Array array = Array.CreateInstance(result[0].GetType(), result.Count); result.CopyTo(array, 0); return(array); } catch (Exception ex) { throw new DataException("Could not perform FindAllWithCustomQueryStateless: " + queryString, ex); } } }
public void ShouldWorkLoadingComplexEntities() { const string crocodileFather = "Crocodile father"; const string crocodileMother = "Crocodile mother"; using (ISession s = sessions.OpenSession()) using (ITransaction tx = s.BeginTransaction()) { var rf = new Reptile { Description = crocodileFather }; var rm = new Reptile { Description = crocodileMother }; var rc1 = new Reptile { Description = "Crocodile" }; var rc2 = new Reptile { Description = "Crocodile" }; var rfamily = new Family <Reptile> { Father = rf, Mother = rm, Childs = new HashedSet <Reptile> { rc1, rc2 } }; s.Save("ReptileFamily", rfamily); tx.Commit(); } const string humanFather = "Fred"; const string humanMother = "Wilma"; using (ISession s = sessions.OpenSession()) using (ITransaction tx = s.BeginTransaction()) { var hf = new Human { Description = "Flinstone", Name = humanFather }; var hm = new Human { Description = "Flinstone", Name = humanMother }; var hc1 = new Human { Description = "Flinstone", Name = "Pebbles" }; var hfamily = new Family <Human> { Father = hf, Mother = hm, Childs = new HashedSet <Human> { hc1 } }; s.Save("HumanFamily", hfamily); tx.Commit(); } using (IStatelessSession s = sessions.OpenStatelessSession()) using (ITransaction tx = s.BeginTransaction()) { IList <Family <Human> > hf = s.CreateQuery("from HumanFamily").List <Family <Human> >(); Assert.That(hf.Count, Is.EqualTo(1)); Assert.That(hf[0].Father.Name, Is.EqualTo(humanFather)); Assert.That(hf[0].Mother.Name, Is.EqualTo(humanMother)); NHibernateUtil.IsInitialized(hf[0].Childs).Should("Lazy collection should NOT be initialized").Be.False(); IList <Family <Reptile> > rf = s.CreateQuery("from ReptileFamily").List <Family <Reptile> >(); Assert.That(rf.Count, Is.EqualTo(1)); Assert.That(rf[0].Father.Description, Is.EqualTo(crocodileFather)); Assert.That(rf[0].Mother.Description, Is.EqualTo(crocodileMother)); NHibernateUtil.IsInitialized(hf[0].Childs).Should("Lazy collection should NOT be initialized").Be.False(); tx.Commit(); } using (ISession s = sessions.OpenSession()) using (ITransaction tx = s.BeginTransaction()) { s.Delete("from HumanFamily"); s.Delete("from ReptileFamily"); tx.Commit(); } }
static IEnumerable<Movie> GetMovies(IStatelessSession session) { return session.CreateQuery("from Movie") .List<Movie>(); }
public IQuery CreateQuery(string query) { return(_session.CreateQuery(query)); }
public IQuery CreateQuery(string queryString) { return(_Session.CreateQuery(queryString)); }
public async Task ShouldWorkLoadingComplexEntitiesAsync() { const string crocodileFather = "Crocodile father"; const string crocodileMother = "Crocodile mother"; using (ISession s = Sfi.OpenSession()) using (ITransaction tx = s.BeginTransaction()) { var rf = new Reptile { Description = crocodileFather }; var rm = new Reptile { Description = crocodileMother }; var rc1 = new Reptile { Description = "Crocodile" }; var rc2 = new Reptile { Description = "Crocodile" }; var rfamily = new Family <Reptile> { Father = rf, Mother = rm, Childs = new HashSet <Reptile> { rc1, rc2 } }; await(s.SaveAsync("ReptileFamily", rfamily)); await(tx.CommitAsync()); } const string humanFather = "Fred"; const string humanMother = "Wilma"; using (ISession s = Sfi.OpenSession()) using (ITransaction tx = s.BeginTransaction()) { var hf = new Human { Description = "Flinstone", Name = humanFather }; var hm = new Human { Description = "Flinstone", Name = humanMother }; var hc1 = new Human { Description = "Flinstone", Name = "Pebbles" }; var hfamily = new Family <Human> { Father = hf, Mother = hm, Childs = new HashSet <Human> { hc1 } }; await(s.SaveAsync("HumanFamily", hfamily)); await(tx.CommitAsync()); } using (IStatelessSession s = Sfi.OpenStatelessSession()) using (ITransaction tx = s.BeginTransaction()) { IList <Family <Human> > hf = await(s.CreateQuery("from HumanFamily").ListAsync <Family <Human> >()); Assert.That(hf.Count, Is.EqualTo(1)); Assert.That(hf[0].Father.Name, Is.EqualTo(humanFather)); Assert.That(hf[0].Mother.Name, Is.EqualTo(humanMother)); Assert.That(NHibernateUtil.IsInitialized(hf[0].Childs), Is.False, "Lazy collection should NOT be initialized"); IList <Family <Reptile> > rf = await(s.CreateQuery("from ReptileFamily").ListAsync <Family <Reptile> >()); Assert.That(rf.Count, Is.EqualTo(1)); Assert.That(rf[0].Father.Description, Is.EqualTo(crocodileFather)); Assert.That(rf[0].Mother.Description, Is.EqualTo(crocodileMother)); Assert.That(NHibernateUtil.IsInitialized(hf[0].Childs), Is.False, "Lazy collection should NOT be initialized"); await(tx.CommitAsync()); } using (IStatelessSession s = Sfi.OpenStatelessSession()) using (ITransaction tx = s.BeginTransaction()) { IList <Family <Human> > hf = await(s.Query <Family <Human> >().FetchMany(f => f.Childs).ToListAsync()); Assert.That(hf.Count, Is.EqualTo(1)); Assert.That(hf[0].Father.Name, Is.EqualTo(humanFather)); Assert.That(hf[0].Mother.Name, Is.EqualTo(humanMother)); var initialized1 = NHibernateUtil.IsInitialized(hf[0].Childs); Assert.That(initialized1, Is.True, "Lazy collection should be initialized"); IList <Family <Reptile> > rf = await(s.Query <Family <Reptile> >().FetchMany(f => f.Childs).ToListAsync()); Assert.That(rf.Count, Is.EqualTo(1)); Assert.That(rf[0].Father.Description, Is.EqualTo(crocodileFather)); Assert.That(rf[0].Mother.Description, Is.EqualTo(crocodileMother)); var initialized2 = NHibernateUtil.IsInitialized(hf[0].Childs); Assert.That(initialized2, Is.True, "Lazy collection should be initialized"); await(tx.CommitAsync()); } using (ISession s = Sfi.OpenSession()) using (ITransaction tx = s.BeginTransaction()) { await(s.DeleteAsync("from HumanFamily")); await(s.DeleteAsync("from ReptileFamily")); await(tx.CommitAsync()); } }