示例#1
0
		public void TestProjection()
		{
            
            using (var s = OpenSession())
            {
                var proj = new SqlFunctionProjection("AddDays", NHibernateUtil.DateTime,
                                                     new IProjection[]
                                                         {
                                                             Projections.Property<ModelClass>(p=>p.Date1),
                                                             Projections.Property<ModelClass>(p=>p.Value1)
                                                         });
                var criteria = s.CreateCriteria<ModelClass>();
                criteria.SetProjection(proj);

                var sql = GetSql(criteria);

                Assert.That(sql, Is.StringMatching("dateadd\\(day,(.*?)Value1,(.*?)Date1\\)"));
                Console.WriteLine(sql.ToString());
            }      
		}
        private void VisitAritmaticOperation(BinaryExpression expr, ISQLFunction arithmaticOperation)
        {
            var leftVisitor = new SelectArgumentsVisitor(_rootCriteria, _session);
            var rightVisitor = new SelectArgumentsVisitor(_rootCriteria, _session);
            leftVisitor.Visit(expr.Left);
            rightVisitor.Visit(expr.Right);

            var joinedProjections = new List<IProjection>();
            joinedProjections.AddRange(leftVisitor._projections);
            joinedProjections.AddRange(rightVisitor._projections);
            var types = joinedProjections[0].GetTypes(_rootCriteria, CriteriaQuery);
            var projection = new SqlFunctionProjection(arithmaticOperation, types[0], joinedProjections.ToArray());
            _projections.Add(projection);
        }
        private void VisitAddExpression(BinaryExpression expr)
        {
            var leftVisitor = new SelectArgumentsVisitor(_rootCriteria, _session);
            var rightVisitor = new SelectArgumentsVisitor(_rootCriteria, _session);
            leftVisitor.Visit(expr.Left);
            rightVisitor.Visit(expr.Right);

            var joinedProjections = new List<IProjection>();
            joinedProjections.AddRange(leftVisitor._projections);
            joinedProjections.AddRange(rightVisitor._projections);

            IType[] types = joinedProjections[0].GetTypes(_rootCriteria, CriteriaQuery);
            var useConcat = types[0] is AbstractStringType;
            SqlFunctionProjection projection;
            if (useConcat)
            {
                projection = new SqlFunctionProjection("concat", types[0], joinedProjections.ToArray());
            }
            else
            {
                projection = new SqlFunctionProjection(arithmaticAddition, types[0], joinedProjections.ToArray());
            }
            _projections.Add(projection);
        }