public void UsingParametersTemplate()
        {
            var          q  = new DetachedNamedQuery("Foo.Parameters");
            IRowsCounter rc = new NamedQueryRowsCounter("Foo.Count.Parameters", q);

            q.SetString("p1", "%1_");
            SessionFactory.EncloseInTransaction(s => Assert.That(rc.GetRowsCount(s), Is.EqualTo(5)));
        }
예제 #2
0
        public void RowsCountUsingParameters()
        {
            DetachedNamedQuery q = new DetachedNamedQuery("Foo.Count.Parameters");

            q.SetString("p1", "%1_");
            IRowsCounter rc = new NamedQueryRowsCounter(q);

            using (ISession s = OpenSession())
            {
                Assert.AreEqual(5, rc.GetRowsCount(s));
            }
        }
        public void ResultTransformer()
        {
            IDetachedQuery dq = new DetachedNamedQuery("NoFoo.SQL.Parameters");

            dq.SetString("p1", "%1_")
            .SetResultTransformer(new AliasToBeanResultTransformer(typeof(NoFoo)));
            using (ISession s = OpenSession())
            {
                IQuery        q = dq.GetExecutableQuery(s);
                IList <NoFoo> l = q.List <NoFoo>();
                Assert.AreEqual(5, l.Count);
            }
        }
        public void ExecutableNamedQuery()
        {
            IDetachedQuery dq = new DetachedNamedQuery("Foo.WithParameters");

            dq.SetString("pn", "N2");
            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);
            }
            // reusing same IDetachedQuery
            dq.SetString("pn", "@All@");
            using (ISession s = OpenSession())
            {
                IQuery q = dq.GetExecutableQuery(s);
                IList  l = q.List();
                Assert.AreEqual(totalFoo, l.Count);
            }
        }
예제 #5
0
        public static void SetDetachedNamedQueryParameters(DetachedNamedQuery dnq, object parameters)
        {
            object parameterValue;
            Type   nullableType;

            if (parameters == null)
            {
                return;
            }

            foreach (PropertyInfo pInfo in parameters.GetType().GetProperties())
            {
                parameterValue = pInfo.GetValue(parameters, null);
                nullableType   = Nullable.GetUnderlyingType(pInfo.PropertyType);
                if (parameterValue != null)
                {
                    if (pInfo.PropertyType.IsAssignableFrom(typeof(int)))
                    {
                        dnq.SetInt32(pInfo.Name, (int)parameterValue);
                    }
                    else if (pInfo.PropertyType.IsAssignableFrom(typeof(bool)))
                    {
                        dnq.SetBoolean(pInfo.Name, (bool)parameterValue);
                    }
                    else if (pInfo.PropertyType.IsAssignableFrom(typeof(DateTime)))
                    {
                        dnq.SetDateTime(pInfo.Name, (DateTime)parameterValue);
                    }
                    else if (pInfo.PropertyType.IsAssignableFrom(typeof(string)))
                    {
                        dnq.SetString(pInfo.Name, parameterValue.ToString());
                    }
                    else if (nullableType != null && nullableType.IsEnum)
                    {
                        dnq.SetInt32(pInfo.Name, (int)parameterValue);
                    }
                    else
                    {
                        dnq.SetParameter(pInfo.Name, parameterValue);
                    }
                }
            }
        }