Пример #1
0
        public IQuery BuildQuery(string queryst, object[] parameters, ISession session, bool native = false, string queryAlias = null)
        {
            var result = HibernateUtil.TranslateQueryString(queryst, parameters);

            queryst    = result.query;
            parameters = result.Parameters;

            var query = native ? session.CreateSQLQuery(queryst) : session.CreateQuery(queryst);

            if (!native)
            {
                query.SetFlushMode(FlushMode.Never);
            }
            query.SetTimeout(MetadataProvider.GlobalProperties.QueryTimeout());
            LogQuery(queryst, queryAlias, parameters);
            if (result.Parameters == null)
            {
                return(query);
            }
            for (var i = 0; i < result.Parameters.Length; i++)
            {
                if (queryst.Contains(":p" + i))
                {
                    if (parameters[i] == null)
                    {
                        query.SetParameter("p" + i, parameters[i], NHibernateUtil.String);
                    }
                    else
                    {
                        var asEnumerable = parameters[i] as IEnumerable;
                        if (asEnumerable != null)
                        {
                            query.SetParameterList("p" + i, (IEnumerable)parameters[i]);
                        }
                        else
                        {
                            query.SetParameter("p" + i, parameters[i]);
                        }
                    }
                }
                else
                {
                    if (parameters[i] != null)
                    {
                        if (parameters[i] is Int32)
                        {
                            query.SetParameter(i, parameters[i], new Int32Type());
                        }
                        else
                        {
                            query.SetParameter(i, parameters[i]);
                        }
                    }
                }
            }

            return(query);
        }
Пример #2
0
        public void TestQueryTranslation() {
            var result = HibernateUtil.TranslateQueryString(
                "from PropertyValue v where Definition = ? and Condition = ? and Module = ? and UserProfile = ?", 1, null, null, 2);
            Assert.AreEqual("from PropertyValue v where Definition = ? and Condition  is   null  and Module  is   null  and UserProfile = ?", result.query);
            Assert.AreEqual(2, result.Parameters.Length);

            result = HibernateUtil.TranslateQueryString(
                "from PropertyValue v where Definition = ? and Condition = ? and Module = ? and UserProfile = ?", 1, 2, 2, 2);
            Assert.AreEqual("from PropertyValue v where Definition = ? and Condition = ? and Module = ? and UserProfile = ?", result.query);
            Assert.AreEqual(4, result.Parameters.Length);

            result = HibernateUtil.TranslateQueryString(
                            "from PropertyValue v where Definition = ? and Condition = ? and Module = ? and UserProfile = ?   ", 1, null, null, 2);
            Assert.AreEqual("from PropertyValue v where Definition = ? and Condition  is   null  and Module  is   null  and UserProfile = ?   ", result.query);
            Assert.AreEqual(2, result.Parameters.Length);

            result = HibernateUtil.TranslateQueryString(
                            "from PropertyValue");
            Assert.AreEqual("from PropertyValue", result.query);

        }