/// <summary> /// Creates a new DetachedQuery that is a deep copy of the current instance. /// </summary> /// <returns>The clone.</returns> public DetachedQuery Clone() { DetachedQuery result = new DetachedQuery(hql); CopyTo(result); return(result); }
/// <summary> /// Create a new instance of <see cref="QueryRowsCounter"/>. /// </summary> /// <param name="hqlRowsCount">The HQL.</param> /// <remarks> /// If the query is invalid an exception is throw only when <see cref="IRowsCounter.GetRowsCount(ISession)"/> /// is called. /// </remarks> /// <exception cref="ArgumentNullException">If <paramref name="hqlRowsCount"/> is null or empty.</exception> public QueryRowsCounter(string hqlRowsCount) { if (string.IsNullOrEmpty(hqlRowsCount)) { throw new ArgumentNullException("hqlRowsCount"); } dq = new DetachedQuery(hqlRowsCount); }
public IDetachedQuery TransformToRowCount() { Select s = new Select("count(*)"); s.SetFrom(from.FromWhereClause()); DetachedQuery result = new DetachedQuery(s.Clause); result.CopyParametersFrom(this); return result; }
public void RowsCountUsingParameters() { IDetachedQuery dq = new DetachedQuery("select count(*) from Foo f where f.Name like :p1").SetString("p1", "%1_"); IRowsCounter rc = new QueryRowsCounter(dq); using (ISession s = OpenSession()) { Assert.AreEqual(5, rc.GetRowsCount(s)); } }
public void RowsCountTransforming() { DetachedQuery dq = new DetachedQuery("from Foo f where f.Name like :p1"); dq.SetString("p1", "%1_"); IRowsCounter rc = QueryRowsCounter.Transforming(dq); using (ISession s = OpenSession()) { Assert.AreEqual(5, rc.GetRowsCount(s)); } }
/// <summary> /// Transform an gigen <see cref="DetachedQuery"/> (HQL query) to it's rows count. /// </summary> /// <param name="query">The given <see cref="DetachedQuery"/>.</param> /// <returns> /// A <see cref="QueryRowsCounter"/> based on <paramref name="query"/>, with row count, using /// same parameters and it's values. /// </returns> /// <exception cref="HibernateException">When the query don't start with 'from' clause.</exception> /// <remarks> /// Take care to the query; it can't contain any other clause than "from" and "where". /// Set the parameters and it's values, of <paramref name="query"/> befor call this method. /// </remarks> public static QueryRowsCounter Transforming(DetachedQuery query) { if (!query.Hql.StartsWith("from", StringComparison.InvariantCultureIgnoreCase)) { throw new HibernateException( string.Format( "Can't trasform the HQL to it's counter, the query must start with 'from' clause:{0}", query.Hql)); } QueryRowsCounter result = new QueryRowsCounter("select count(*) " + query.Hql); result.CopyParametersFrom(query); return result; }
/// <summary> /// Transform an gigen <see cref="DetachedQuery"/> (HQL query) to it's rows count. /// </summary> /// <param name="origin">The given <see cref="DetachedQuery"/>.</param> /// <returns> /// A <see cref="QueryRowsCounter"/> based on <paramref name="origin"/>, with row count, using /// same parameters and it's values. /// </returns> /// <exception cref="HibernateException">When the query don't start with 'from' clause.</exception> /// <remarks> /// Take care to the query; it can't contain any other clause than "from" and "where". /// Set the parameters and it's values, of <paramref name="origin"/> befor call this method. /// </remarks> public static QueryRowsCounter Transforming(DetachedQuery origin) { if (origin == null) { throw new ArgumentNullException("origin"); } if (!origin.Hql.StartsWith("from", StringComparison.InvariantCultureIgnoreCase)) { throw new HibernateException( string.Format("Can't trasform the HQL to it's counter, the query must start with 'from' clause:{0}", origin.Hql)); } var result = new QueryRowsCounter(origin); return result; }
//public IList<Domain.User> GetAllSiteUsers(Site site) //{ // using (ISession session = RepositoryHelper.GetSession()) // { // string hql = "from User u where u.Site = :site and u.IsLogicallyDeleted = 0 and u.IsActive = 1"; // IQuery query = session.CreateQuery(hql); // query.SetEntity("site", site); // return query.List<Domain.User>(); // } //} public Paginator<User> GetUserPaginatorBySite(Site site, int pageSize) { if (site == null) throw new ArgumentNullException("site"); IDetachedQuery query = new DetachedQuery("from User u where u.Site = :site and u.IsLogicallyDeleted = 0"); query.SetEntity("site", site); return Repository<User>.GetPaginator(query, pageSize); }
/// <summary> /// /// </summary> /// <param name="origin"></param> /// <returns></returns> public IRowsCounter CopyParametersFrom(DetachedQuery origin) { origin.SetParametersTo(dq); return this; }
public void WithSelfCounter() { DetachedQuery dq = new DetachedQuery("from Foo f where f.Name like :p1"); dq.SetString("p1", "%1_"); IPaginable<Foo> fp = new PaginableRowsCounterQuery<Foo>(LastOpenedSession, dq); Paginator<Foo> ptor = new Paginator<Foo>(2, fp); Assert.IsTrue(ptor.AutoCalcPages); Assert.AreEqual(3, ptor.LastPageNumber); // check page 2 IList<Foo> lpage = ptor.GetPage(2); Assert.AreEqual(2, lpage.Count); Assert.AreEqual(2, ptor.CurrentPageNumber); Assert.AreEqual("N12", lpage[0].Name); Assert.AreEqual("N13", lpage[1].Name); }
protected override IDetachedQuery GetDetachedQuery() { if (origin != null) { var result = new DetachedQuery("select count(*) " + origin.Hql); result.CopyParametersFrom(origin); return result; } return detachedQuery; }
private QueryRowsCounter(DetachedQuery origin) { this.origin = origin; }
public Paginator<User> GetUsersInRolePaginator(Role role, int pageSize) { IDetachedQuery query = new DetachedQuery("from User u where :role in elements(u.Roles) and u.Site = :site and u.IsLogicallyDeleted = 0"); query.SetEntity("role", role); query.SetEntity("site", role.Site); return Repository<User>.GetPaginator(query, pageSize); }
public void ExecutableQuery() { // Simply fetch IDetachedQuery dq = new DetachedQuery("from Foo"); using (ISession s = OpenSession()) { IQuery q = dq.GetExecutableQuery(s); IList l = q.List(); Assert.AreEqual(totalFoo, l.Count); } // With Typed Parameters dq = new DetachedQuery("from Foo f where f.Name=:pn and f.Description=:pd"); dq.SetString("pn", "N2").SetString("pd", "D2"); using (ISession s = OpenSession()) { IQuery q = dq.GetExecutableQuery(s); IList<Foo> l = q.List<Foo>(); Assert.AreEqual(1, l.Count); Assert.AreEqual("N2", l[0].Name); Assert.AreEqual("D2", l[0].Description); } // With UnTyped Parameters dq = new DetachedQuery("from Foo f where f.Name=:pn and f.Description=:pd"); dq.SetParameter("pn", "N2").SetParameter("pd", "D2"); using (ISession s = OpenSession()) { IQuery q = dq.GetExecutableQuery(s); IList<Foo> l = q.List<Foo>(); Assert.AreEqual(1, l.Count); Assert.AreEqual("N2", l[0].Name); Assert.AreEqual("D2", l[0].Description); } // With UnTyped Parameter List dq = new DetachedQuery("from Foo f where f.IntValue in (:pn)"); dq.SetParameterList("pn", new int[] { 2, 3 }); using (ISession s = OpenSession()) { IQuery q = dq.GetExecutableQuery(s); IList<Foo> l = q.List<Foo>(); Assert.That(l.Count, Is.EqualTo(2)); Assert.That(l.Contains(new Foo("N2", "D2")), Is.True); Assert.That(l.Contains(new Foo("N3", "D3")), Is.True); } // Pagination dq = new DetachedQuery("from Foo f order by f.IntValue"); dq.SetFirstResult(0).SetMaxResults(2); using (ISession s = OpenSession()) { IQuery q = dq.GetExecutableQuery(s); IList<Foo> l = q.List<Foo>(); Assert.AreEqual(2, l.Count); Assert.AreEqual("N0", l[0].Name); Assert.AreEqual("N1", l[1].Name); } dq.SetFirstResult(2).SetMaxResults(1); using (ISession s = OpenSession()) { IQuery q = dq.GetExecutableQuery(s); IList<Foo> l = q.List<Foo>(); Assert.AreEqual(1, l.Count); Assert.AreEqual("N2", l[0].Name); } }
public Paginator<Role> GetRolePaginatorBySite(Site site, int pageSize) { IDetachedQuery query = new DetachedQuery("from Role r where r.Site = :site"); query.SetEntity("site", site); return Repository<Role>.GetPaginator(query, pageSize); }
/// <summary> /// Creates a new DetachedQuery that is a deep copy of the current instance. /// </summary> /// <returns>The clone.</returns> public DetachedQuery Clone() { DetachedQuery result = new DetachedQuery(hql); CopyTo(result); return result; }
public void PaginableRowsCount() { DetachedQuery dq = new DetachedQuery("from Foo f where f.Name like :p1"); dq.SetString("p1", "N_"); using (ISession s = OpenSession()) { IPaginable<Foo> fp = new PaginableRowsCounterQuery<Foo>(LastOpenedSession, dq); IList<Foo> l = fp.GetPage(5, 1); Assert.AreEqual(5, l.Count); Assert.AreEqual(10, ((IRowsCounter) fp).GetRowsCount(s)); } }
public void PerformanceDiffSimplyQuery() { DateTime sDQStart = DateTime.Now; DetachedQuery dq = new DetachedQuery("from Foo f where f.Name=:pn and f.Description=:pd"); dq.SetString("pn", "N2").SetString("pd", "D2"); using (ISession s = OpenSession()) { IQuery q = dq.GetExecutableQuery(s); } DateTime sDQStop = DateTime.Now; DateTime sQStart = DateTime.Now; using (ISession s = OpenSession()) { IQuery q = s.CreateQuery("from Foo f where f.Name=:pn and f.Description=:pd").SetString("pn", "N2").SetString("pd", "D2"); } DateTime sQStop = DateTime.Now; Console.WriteLine("DetachedQueryCycle={0} QueryCycl={1} Diff={2}", sDQStop - sDQStart, sQStop - sQStart, (sDQStop - sDQStart) - (sQStop - sQStart)); }
public void Serializable() { DetachedQuery dq = new DetachedQuery("from Foo f where f.Name=:pn and f.Description=:pd"); dq.SetString("pn", "N2").SetString("pd", "D2"); byte[] bytes = SerializationHelper.Serialize(dq); DetachedQuery dqs = (DetachedQuery)SerializationHelper.Deserialize(bytes); using (ISession s = OpenSession()) { dqs.GetExecutableQuery(s).List(); } }
public IList<User> GetUsersInRole(Role role) { IDetachedQuery dq = new DetachedQuery("from User u where :role in elements(u.Roles) and u.Site = :site and u.IsLogicallyDeleted = 0"); dq.SetEntity("role", role); dq.SetEntity("site", role.Site); using (ISession session = Session){ IQuery query = dq.GetExecutableQuery(session); return query.List<User>(); } }
/// <summary> /// /// </summary> /// <param name="searchParameter"></param> /// <returns></returns> public IList ExecuteDetachedQuery(string searchParameter) { using (ITransaction transaction = Session.BeginTransaction()) { try { string hqlQuery = "select Builder, Model, Price, Id" + " from Inventory " + " where Model like :search " + " order by Builder"; IDetachedQuery detachedQuery = new DetachedQuery(hqlQuery) .SetString("search", searchParameter); IQuery executableQuery = detachedQuery.GetExecutableQuery(Session); return executableQuery.List(); } catch (Exception ex) { transaction.Rollback(); throw; } } }