예제 #1
0
        public void SelectStringSqlTest()
        {
            Configuration   cfg     = new Configuration();
            ISessionFactory factory = cfg.BuildSessionFactory();

            ISessionFactoryImplementor factoryImpl = (ISessionFactoryImplementor)factory;
            SqlSelectBuilder           select      = new SqlSelectBuilder(factoryImpl);

            select.SetSelectClause("column1, column2");
            select.SetFromClause("select_test", "select_test_alias");
            select.SetOuterJoins(
                new SqlString(" LEFT OUTER JOIN before ON select_test_alias.column1 = before.column1"),
                new SqlString(" after.some_field = after.another_field "));
            select.SetOrderByClause(new SqlString("column1 DESC"));

            select.SetWhereClause("select_test_alias", new string[] { "identity_column" }, NHibernateUtil.Int64);

            SqlString sqlString = select.ToSqlString();

            string expectedSql = new StringBuilder().Append("SELECT ")
                                 .Append("column1, column2 ")
                                 .Append("FROM select_test select_test_alias ")
                                 .Append("LEFT OUTER JOIN before ON select_test_alias.column1 = before.column1 ")
                                 .Append("WHERE ")
                                 .Append("after.some_field = after.another_field")
                                 .Append(" AND ")
                                 .Append("select_test_alias.identity_column = ? ")
                                 .Append("ORDER BY column1 DESC")
                                 .ToString();


            Assert.AreEqual(expectedSql, sqlString.ToString(), "SQL String");
            Assert.AreEqual(1, sqlString.GetParameterCount(), "One parameter");
        }
        protected SqlString GenerateIdInsertSelect(IQueryable persister, string tableAlias, IASTNode whereClause)
        {
            var            select         = new SqlSelectBuilder(Factory);
            SelectFragment selectFragment = new SelectFragment(Factory.Dialect)
                                            .AddColumns(tableAlias, persister.IdentifierColumnNames, persister.IdentifierColumnNames);

            select.SetSelectClause(selectFragment.ToFragmentString().Substring(2));

            string    rootTableName    = persister.TableName;
            SqlString fromJoinFragment = persister.FromJoinFragment(tableAlias, true, false);

            select.SetFromClause(rootTableName + " " + tableAlias + fromJoinFragment);

            var whereJoinFragment = GetWhereJoinFragment(persister, tableAlias);

            SqlString userWhereClause = SqlString.Empty;

            if (whereClause.ChildCount != 0)
            {
                // If a where clause was specified in the update/delete query, use it to limit the
                // returned ids here...
                try
                {
                    var nodes = new CommonTreeNodeStream(whereClause);
                    var gen   = new SqlGenerator(Factory, nodes);
                    gen.whereClause();
                    userWhereClause = gen.GetSQL().Substring(7);
                }
                catch (RecognitionException e)
                {
                    throw new HibernateException("Unable to generate id select for DML operation", e);
                }
                if (whereJoinFragment.Length > 0)
                {
                    whereJoinFragment = whereJoinFragment.Append(" and ");
                }
            }

            select.SetWhereClause(whereJoinFragment + userWhereClause);

            var insert = new InsertSelect();

            if (Factory.Settings.IsCommentsEnabled)
            {
                insert.SetComment("insert-select for " + persister.EntityName + " ids");
            }
            insert.SetTableName(persister.TemporaryIdTableName);
            insert.SetSelect(select);
            return(insert.ToSqlString());
        }