Example #1
0
        public int Delete(TSearchCriteria[] criteria)
        {
            if (this.Context.ReadOnly)
            {
                throw new InvalidOperationException("Cannot delete via read-only persistence context.");
            }

            var query = new HqlQuery(string.Format("delete {0} x", typeof(TEntity).Name));

            var or = new HqlOr();

            foreach (var c in criteria)
            {
                var and = new HqlAnd(HqlCondition.FromSearchCriteria("x", c));
                if (and.Conditions.Count > 0)
                {
                    or.Conditions.Add(and);
                }
            }

            if (or.Conditions.Count > 0)
            {
                query.Conditions.Add(or);
            }


            return(ExecuteHqlDml(query));
        }
Example #2
0
        public long Count(TSearchCriteria[] criteria, EntityFindOptions options)
        {
            // cannot defer count queries, because we have no way of proxying the result
            // without changing the return type of this method
            if (options.Defer)
            {
                throw new NotSupportedException("Count queries do not support the 'defer' option.");
            }

            var query = new HqlQuery(string.Format("select count(*) from {0} x", typeof(TEntity).Name))
            {
                Cacheable = options.Cache
            };

            // for a "count" query, sort conditions that may be present in the
            // criteria object must be ignored- therefore, only the conditions are added to the query
            var or = new HqlOr();

            foreach (var c in criteria)
            {
                var and = new HqlAnd(HqlCondition.FromSearchCriteria("x", c));
                if (and.Conditions.Count > 0)
                {
                    or.Conditions.Add(and);
                }
            }

            if (or.Conditions.Count > 0)
            {
                query.Conditions.Add(or);
            }

            // expect exactly one integer result
            return(ExecuteHqlUnique <long>(query));
        }
        /// <summary>
        /// Adds the specified criteria to the specified query, pre-pending the specified qualifier.
        /// </summary>
        /// <param name="qualifier"></param>
        /// <param name="criteria"></param>
        /// <param name="query"></param>
        /// <param name="remapHqlExprFunction"></param>
        /// <remarks>
        /// All HQL dot expressions are passed through the <paramref name="remapHqlExprFunction"/>, allowing the expression
        /// to be modified prior to be added to the query.
        /// </remarks>
        public static void AddCriteriaToQuery(string qualifier, WorklistItemSearchCriteria[] criteria, HqlProjectionQuery query,
                                              Converter <string, string> remapHqlExprFunction)
        {
            var or = new HqlOr();

            foreach (var c in criteria)
            {
                if (c.IsEmpty)
                {
                    continue;
                }

                var conditions = HqlCondition.FromSearchCriteria(qualifier, c, remapHqlExprFunction);
                var and        = new HqlAnd(conditions);
                if (and.Conditions.Count > 0)
                {
                    or.Conditions.Add(and);
                }
            }

            if (or.Conditions.Count > 0)
            {
                query.Conditions.Add(or);
            }
        }
        public IList <ProcedureTypeGroup> Find(ProcedureTypeGroupSearchCriteria criteria, Type subClass, SearchResultPage page)
        {
            HqlQuery query = new HqlQuery(string.Format("from {0} x", subClass.Name));

            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("x", criteria));
            query.Sorts.AddRange(HqlSort.FromSearchCriteria("x", criteria));
            query.Page = page;

            return(ExecuteHql <ProcedureTypeGroup>(query));
        }
        public void TestInList()
        {
            List <int> numbers = new List <int>();

            numbers.Add(1);
            numbers.Add(2);
            numbers.Add(3);

            AreEqual(new HqlCondition("x in (?,?,?)", new object[] { 1, 2, 3 }), HqlCondition.In("x", numbers));
        }
Example #6
0
        private static HqlProjectionQuery GetBaseResultRecipientQuery(OrderSearchCriteria orderSearchCriteria, ResultRecipientSearchCriteria recipientSearchCriteria)
        {
            var hqlFrom = new HqlFrom(typeof(Order).Name, "o");

            hqlFrom.Joins.Add(new HqlJoin("o.ResultRecipients", "rr"));

            var query = new HqlProjectionQuery(hqlFrom);

            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("rr", recipientSearchCriteria));
            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("o", orderSearchCriteria));
            return(query);
        }
Example #7
0
        private static HqlProjectionQuery GetBaseVisitPractitionerQuery(VisitSearchCriteria visitSearchCriteria, VisitPractitionerSearchCriteria practitionerSearchCriteria)
        {
            var hqlFrom = new HqlFrom(typeof(Visit).Name, "v");

            hqlFrom.Joins.Add(new HqlJoin("v.Practitioners", "vp"));

            var query = new HqlProjectionQuery(hqlFrom);

            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("vp", practitionerSearchCriteria));
            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("v", visitSearchCriteria));
            return(query);
        }
Example #8
0
        public IList <ModalityProcedureStep> Find(ModalityProcedureStepSearchCriteria mpsCriteria, ProcedureSearchCriteria procedureCriteria)
        {
            var hqlFrom = new HqlFrom(typeof(ModalityProcedureStep).Name, "mps");

            hqlFrom.Joins.Add(new HqlJoin("mps.Procedure", "rp", HqlJoinMode.Inner, true));

            var query = new HqlProjectionQuery(hqlFrom);

            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("mps", mpsCriteria));
            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("rp", procedureCriteria));

            return(ExecuteHql <ModalityProcedureStep>(query));
        }
Example #9
0
        private List <OrderNoteboxItem> BuildOrderNoteboxItems(List <NoteboxItem> inboxItems)
        {
            if (inboxItems.Count == 0)
            {
                return(new List <OrderNoteboxItem>());
            }

            // Get all the patients for all the items
            var patients        = CollectionUtils.Unique(CollectionUtils.Map <NoteboxItem, Patient>(inboxItems, item => item.Patient));
            var patientQuery    = new HqlProjectionQuery(new HqlFrom(typeof(PatientProfile).Name, "pp"));
            var patientCriteria = new PatientProfileSearchCriteria();

            patientCriteria.Patient.In(patients);
            patientQuery.Conditions.AddRange(HqlCondition.FromSearchCriteria("pp", patientCriteria));
            var profiles = ExecuteHql <PatientProfile>(patientQuery);

            // Have to manually get the postings (and later their recipients) to work around a Hibernate fetch="subselect" issue.
            // The subselect somehow removed the "top(100)" and "order by" clause.  Making the query slow.
            // Load all the postings for all the notes.  There may be more than one postings per orderNote.
            // Therefore it is inappropriate to just use the postings in the base query.
            var notes           = CollectionUtils.Unique(CollectionUtils.Map <NoteboxItem, Note>(inboxItems, item => item.Note));
            var postingQuery    = new HqlProjectionQuery(new HqlFrom(typeof(NotePosting).Name, "np"));
            var postingCriteria = new NotePostingSearchCriteria();

            postingCriteria.Note.In(notes);
            postingQuery.Conditions.AddRange(HqlCondition.FromSearchCriteria("np", postingCriteria));
            postingQuery.Froms[0].Joins.Add(new HqlJoin("np.Recipient", null, HqlJoinMode.Left, true));
            var postings = ExecuteHql <NotePosting>(postingQuery);

            // Build order notebox items
            var orderNoteboxItems = CollectionUtils.Map(inboxItems,
                                                        delegate(NoteboxItem item)
            {
                // Find the appropriate patient profile based on OrderingFacility
                var profile = CollectionUtils.SelectFirst(profiles,
                                                          pp => pp.Patient.Equals(item.Patient) &&
                                                          pp.Mrn.AssigningAuthority.Code.Equals(item.OrderingFacilityInformationAuthority.Code));

                // Find all the recipients
                var postingsForThisNote = CollectionUtils.Select(postings, np => np.Note.Equals(item.Note));
                var recipients          = CollectionUtils.Map <NotePosting, object>(postingsForThisNote,
                                                                                    posting => posting is StaffNotePosting
                                                        ? (object)((StaffNotePosting)posting).Recipient
                                                        : (object)((GroupNotePosting)posting).Recipient);

                return(new OrderNoteboxItem(item.Note, item.Order, item.Patient, profile, item.Author, recipients,
                                            item.DiagnosticServiceName, item.NotePostingAcknowledged));
            });

            return(orderNoteboxItems);
        }
Example #10
0
        public IList <ConfigurationDocument> Find(ConfigurationDocumentSearchCriteria documentCriteria, ConfigurationDocumentBodySearchCriteria bodyCriteria, SearchResultPage page)
        {
            var hqlFrom = new HqlFrom(typeof(ConfigurationDocument).Name, "doc");

            hqlFrom.Joins.Add(new HqlJoin("doc.Body", "body"));

            var query = new HqlProjectionQuery(hqlFrom);

            query.Selects.Add(new HqlSelect("doc"));
            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("doc", documentCriteria));
            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("body", bodyCriteria));
            query.Page = page;

            return(ExecuteHql <ConfigurationDocument>(query));
        }
 public void TestExpressionFromSearchCriteria()
 {
     AreEqual(new HqlCondition("x = ?", new object[] { 1 }), HqlCondition.GetCondition("x", SearchConditionTest.Equal, new object[] { 1 }));
     AreEqual(new HqlCondition("x <> ?", new object[] { 1 }), HqlCondition.GetCondition("x", SearchConditionTest.NotEqual, new object[] { 1 }));
     AreEqual(new HqlCondition("x like ?", new object[] { "foo" }), HqlCondition.GetCondition("x", SearchConditionTest.Like, new object[] { "foo" }));
     AreEqual(new HqlCondition("x not like ?", new object[] { "foo" }), HqlCondition.GetCondition("x", SearchConditionTest.NotLike, new object[] { "foo" }));
     AreEqual(new HqlCondition("x between ? and ?", new object[] { 1, 2 }), HqlCondition.GetCondition("x", SearchConditionTest.Between, new object[] { 1, 2 }));
     AreEqual(new HqlCondition("x in (?,?,?)", new object[] { 1, 2, 3 }), HqlCondition.GetCondition("x", SearchConditionTest.In, new object[] { 1, 2, 3 }));
     AreEqual(new HqlCondition("x not in (?,?,?)", new object[] { 1, 2, 3 }), HqlCondition.GetCondition("x", SearchConditionTest.NotIn, new object[] { 1, 2, 3 }));
     AreEqual(new HqlCondition("x < ?", new object[] { 1 }), HqlCondition.GetCondition("x", SearchConditionTest.LessThan, new object[] { 1 }));
     AreEqual(new HqlCondition("x <= ?", new object[] { 1 }), HqlCondition.GetCondition("x", SearchConditionTest.LessThanOrEqual, new object[] { 1 }));
     AreEqual(new HqlCondition("x > ?", new object[] { 1 }), HqlCondition.GetCondition("x", SearchConditionTest.MoreThan, new object[] { 1 }));
     AreEqual(new HqlCondition("x >= ?", new object[] { 1 }), HqlCondition.GetCondition("x", SearchConditionTest.MoreThanOrEqual, new object[] { 1 }));
     AreEqual(new HqlCondition("x is null", new object[] { }), HqlCondition.GetCondition("x", SearchConditionTest.Null, new object[] { 1 }));
     AreEqual(new HqlCondition("x is not null", new object[] { }), HqlCondition.GetCondition("x", SearchConditionTest.NotNull, new object[] { 1 }));
 }
 public void TestExpressionFactoryMethods()
 {
     AreEqual(new HqlCondition("x = ?", new object[] { 1 }), HqlCondition.EqualTo("x", 1));
     AreEqual(new HqlCondition("x <> ?", new object[] { 1 }), HqlCondition.NotEqualTo("x", 1));
     AreEqual(new HqlCondition("x like ?", new object[] { "foo" }), HqlCondition.Like("x", "foo"));
     AreEqual(new HqlCondition("x not like ?", new object[] { "foo" }), HqlCondition.NotLike("x", "foo"));
     AreEqual(new HqlCondition("x between ? and ?", new object[] { 1, 2 }), HqlCondition.Between("x", 1, 2));
     AreEqual(new HqlCondition("x in (?,?,?)", new object[] { 1, 2, 3 }), HqlCondition.In("x", 1, 2, 3));
     AreEqual(new HqlCondition("x not in (?,?,?)", new object[] { 1, 2, 3 }), HqlCondition.NotIn("x", 1, 2, 3));
     AreEqual(new HqlCondition("x < ?", new object[] { 1 }), HqlCondition.LessThan("x", 1));
     AreEqual(new HqlCondition("x <= ?", new object[] { 1 }), HqlCondition.LessThanOrEqual("x", 1));
     AreEqual(new HqlCondition("x > ?", new object[] { 1 }), HqlCondition.MoreThan("x", 1));
     AreEqual(new HqlCondition("x >= ?", new object[] { 1 }), HqlCondition.MoreThanOrEqual("x", 1));
     AreEqual(new HqlCondition("x is null", new object[] {}), HqlCondition.IsNull("x"));
     AreEqual(new HqlCondition("x is not null", new object[] {}), HqlCondition.IsNotNull("x"));
 }
        public bool AssertUserHasToken(string userName, string token)
        {
            UserSearchCriteria whereUser = new UserSearchCriteria();

            whereUser.UserName.EqualTo(userName);

            AuthorityTokenSearchCriteria whereToken = new AuthorityTokenSearchCriteria();

            whereToken.Name.EqualTo(token);

            // want this to be as fast as possible - use joins and only select the count
            HqlQuery query = new HqlQuery("select count(*) from User u join u.AuthorityGroups g join g.AuthorityTokens t");

            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("u", whereUser));
            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("t", whereToken));

            // expect exactly one integer result
            return(ExecuteHqlUnique <long>(query) > 0);
        }
        public string[] FindTokensByUserName(string userName)
        {
            UserSearchCriteria where = new UserSearchCriteria();
            where.UserName.EqualTo(userName);

            // want this to be as fast as possible - use joins and only select the AuthorityToken names
            HqlQuery query = new HqlQuery("select distinct t.Name from User u join u.AuthorityGroups g join g.AuthorityTokens t");

            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("u", where));

            // take advantage of query caching if possible
            query.Cacheable = true;

            IList <string> tokens = this.ExecuteHql <string>(query);

            string[] result = new string[tokens.Count];
            tokens.CopyTo(result, 0);
            return(result);
        }
Example #15
0
        /// <summary>
        /// Establishes the root query (the 'from' clause and any 'join' clauses).
        /// </summary>
        /// <param name="query"></param>
        /// <param name="args"></param>
        public override void AddRootQuery(HqlProjectionQuery query, QueryBuilderArgs args)
        {
            var procedureStepClasses = args.ProcedureStepClasses;

            // if we have 1 procedure step class, we can say "from x"
            // otherwise we need to say "from ProcedureStep where ps.class = ..."
            if (procedureStepClasses.Length == 1)
            {
                var procStepClass = CollectionUtils.FirstElement(procedureStepClasses);
                query.Froms.Add(new HqlFrom(procStepClass.Name, "ps", WorklistJoins));
            }
            else
            {
                // either 0 or > 1 classes were specified
                query.Froms.Add(new HqlFrom(typeof(ProcedureStep).Name, "ps", WorklistJoins));
                if (procedureStepClasses.Length > 1)
                {
                    query.Conditions.Add(HqlCondition.IsOfClass("ps", procedureStepClasses));
                }
            }
        }
Example #16
0
        public IList <TEntity> Find(TSearchCriteria[] criteria, SearchResultPage page, EntityFindOptions options)
        {
            var query = new HqlProjectionQuery(new HqlFrom(typeof(TEntity).Name, "x"))
            {
                Page = page, Cacheable = options.Cache
            };

            // add fetch joins
            foreach (var fetchJoin in GetDefaultFetchJoins())
            {
                query.Froms[0].Joins.Add(new HqlJoin("x." + fetchJoin, null, HqlJoinMode.Inner, true));
            }

            // apply lock hint
            if (options.LockForUpdate)
            {
                query.SetLockMode("x", LockMode.Upgrade);
            }

            var or = new HqlOr();

            foreach (var c in criteria)
            {
                var and = new HqlAnd(HqlCondition.FromSearchCriteria("x", c));
                if (and.Conditions.Count > 0)
                {
                    or.Conditions.Add(and);
                }

                query.Sorts.AddRange(HqlSort.FromSearchCriteria("x", c));
            }

            if (or.Conditions.Count > 0)
            {
                query.Conditions.Add(or);
            }


            return(ExecuteHql <TEntity>(query, options.Defer));
        }
        /// <summary>
        /// NHibernate has a bug where criteria that de-reference properties not joined into the From clause are not
        /// always handled properly.  For example, in order to evaluate a criteria such as "ps.Scheduling.Performer.Staff.Name like ?",
        /// NHiberate will inject a theta-join on Staff into the SQL.  This works ok by itself.  However, when evaluating a criteria
        /// such as "ps.Scheduling.Performer.Staff.Name.FamilyName like ? or ps.Performer.Staff.Name.FamilyName like ?", NHibernate
        /// injects two Staff theta-joins into the SQL, which incorrectly results in a cross-join situation.
        /// This method modifies any query that has criteria on ps.Scheduling.Performer.Staff or ps.Performer.Staff,
        /// by adding in explicit joins to Staff for these objects, and then substituting the original conditions
        /// with conditions based on these joins.
        /// </summary>
        public static void NHibernateBugWorkaround(HqlFrom from, IList <HqlCondition> conditions, Converter <string, string> remapHqlExprFunction)
        {
            var scheduledStaff = remapHqlExprFunction("ps.Scheduling.Performer.Staff");
            var performerStaff = remapHqlExprFunction("ps.Performer.Staff");

            for (var i = 0; i < conditions.Count; i++)
            {
                var condition = conditions[i];
                if (condition is HqlJunction)
                {
                    NHibernateBugWorkaround(from, ((HqlJunction)condition).Conditions, remapHqlExprFunction);
                }
                else if (condition.Hql.StartsWith(scheduledStaff))
                {
                    // add join for sst (scheduled staff) if not added
                    if (!CollectionUtils.Contains(from.Joins, j => j.Alias == "sst"))
                    {
                        from.Joins.Add(new HqlJoin(scheduledStaff, "sst", HqlJoinMode.Left));
                    }

                    // replace the condition with a new condition, using the joined Staff
                    var newHql = condition.Hql.Replace(scheduledStaff, "sst");
                    conditions[i] = new HqlCondition(newHql, condition.Parameters);
                }
                else if (condition.Hql.StartsWith(performerStaff))
                {
                    // add join for pst (performer staff) if not added
                    if (!CollectionUtils.Contains(from.Joins, j => j.Alias == "pst"))
                    {
                        from.Joins.Add(new HqlJoin(performerStaff, "pst", HqlJoinMode.Left));
                    }
                    // replace the condition with a new condition, using the joined Staff
                    var newHql = condition.Hql.Replace(performerStaff, "pst");
                    conditions[i] = new HqlCondition(newHql, condition.Parameters);
                }
            }
        }
Example #18
0
        public Guid[] FindDataGroupsByUserName(string userName)
        {
            UserSearchCriteria where = new UserSearchCriteria();
            where.UserName.EqualTo(userName);

            AuthorityGroupSearchCriteria groupWhere = new AuthorityGroupSearchCriteria();

            groupWhere.DataGroup.EqualTo(true);

            // want this to be as fast as possible - use joins and only select the AuthorityToken names
            HqlQuery query = new HqlQuery("select distinct g.OID from User u join u.AuthorityGroups g");

            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("u", where));
            query.Conditions.AddRange(HqlCondition.FromSearchCriteria("g", groupWhere));

            // take advantage of query caching if possible
            query.Cacheable = true;

            IList <Guid> oids   = ExecuteHql <Guid>(query);
            var          result = new Guid[oids.Count];

            oids.CopyTo(result, 0);
            return(result);
        }
		private static void AreEqual(HqlCondition c1, HqlCondition c2)
		{
			Assert.AreEqual(c1.Hql, c2.Hql);
			Assert.IsTrue(CollectionUtils.Equal<object>(c1.Parameters, c2.Parameters, true));
		}
 private static void AreEqual(HqlCondition c1, HqlCondition c2)
 {
     Assert.AreEqual(c1.Hql, c2.Hql);
     Assert.IsTrue(CollectionUtils.Equal <object>(c1.Parameters, c2.Parameters, true));
 }