Example #1
0
        public void TestSelect()
        {
            using (var session = OpenSqLiteSession())
            {
                var selectedProductDefinition = new ProductDefinition()
                {
                    Id = 1000, MaterialDefinition = new MaterialDefinition {
                        Id = 1
                    }
                };
                session.Save(selectedProductDefinition.MaterialDefinition);
                session.Save(selectedProductDefinition);

                var selectedProducts = new[] { selectedProductDefinition };

                var query = session.Query <Material>()
                            .Where(x => selectedProducts.Contains(x.ProductDefinition) && selectedProducts.Select(y => y.MaterialDefinition).Contains(x.MaterialDefinition));

                var sessionImpl        = session.GetSessionImplementation();
                var factoryImplementor = sessionImpl.Factory;

                var nhLinqExpression  = new NhLinqExpression(query.Expression, factoryImplementor);
                var translatorFactory = new ASTQueryTranslatorFactory();
                var translator        = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false, sessionImpl.EnabledFilters, factoryImplementor).First();

                TestContext.WriteLine(translator.SQLString);

                Assert.IsNotEmpty(nhLinqExpression.ParameterValuesByName.Values.Where(x => (x.Item1 as IList <ProductDefinition>)?.Contains(selectedProductDefinition) == true));
                Assert.IsNotEmpty(nhLinqExpression.ParameterValuesByName.Values.Where(x => (x.Item1 as IList <MaterialDefinition>)?.Contains(selectedProductDefinition.MaterialDefinition) == true));

                //var result = query.ToList();
            }
        }
Example #2
0
        public static String GetGeneratedSql(IQuery query, ISession session)
        {
            var sessionImp        = (ISessionImplementor)session;
            var translatorFactory = new ASTQueryTranslatorFactory();
            var translators       = translatorFactory.CreateQueryTranslators(query.QueryString, null, false, sessionImp.EnabledFilters, sessionImp.Factory);

            return(translators[0].SQLString);
        }
        public String GetGeneratedSql(IQueryable queryable, ISession session)
        {
            var sessionImp        = (ISessionImplementor)session;
            var nhLinqExpression  = new NhLinqExpression(queryable.Expression, sessionImp.Factory);
            var translatorFactory = new ASTQueryTranslatorFactory();
            var translators       = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false, sessionImp.EnabledFilters, sessionImp.Factory);

            return(translators[0].SQLString);
        }
Example #4
0
        public static string GetGeneratedSql(IQueryable query, ISession session)
        {
            var sessionImpl       = session.GetSessionImplementation();
            var factory           = sessionImpl.Factory;
            var nhLinqExpression  = new NhLinqExpression(query.Expression, factory);
            var translatorFactory = new ASTQueryTranslatorFactory();
            var translator        = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false, sessionImpl.EnabledFilters, factory).FirstOrDefault();

            return(translator.SQLString);
        }
Example #5
0
        public static string ToSqlString(this System.Linq.IQueryable queryable)
        {
            ISession session           = NHibernateSessionManager.Instance.GetSession();
            var      sessionImp        = (ISessionImplementor)session;
            var      nhLinqExpression  = new NhLinqExpression(queryable.Expression, sessionImp.Factory);
            var      translatorFactory = new ASTQueryTranslatorFactory();
            var      translators       = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false, sessionImp.EnabledFilters, sessionImp.Factory);

            return(translators[0].SQLString);
        }
Example #6
0
        public static string GenerateSql(this IQuery query, ISession session)
        {
            query.ThrowIfNull("this");
            session.ThrowIfNull("session");

            var sessionImplementor = session.GetSessionImplementation();
            var translatorFactory  = new ASTQueryTranslatorFactory();
            var translators        = translatorFactory.CreateQueryTranslators(query.QueryString, null, false,
                                                                              sessionImplementor.EnabledFilters, sessionImplementor.Factory);

            return(translators[0].SQLString);
        }
Example #7
0
        public String ToSql(System.Linq.IQueryable queryable)
        {
            var sessionProperty   = typeof(DefaultQueryProvider).GetProperty("Session", BindingFlags.NonPublic | BindingFlags.Instance);
            var session           = sessionProperty.GetValue(queryable.Provider, null) as ISession;
            var sessionImpl       = session.GetSessionImplementation();
            var factory           = sessionImpl.Factory;
            var nhLinqExpression  = new NhLinqExpression(queryable.Expression, factory);
            var translatorFactory = new ASTQueryTranslatorFactory();
            var translator        = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false, sessionImpl.EnabledFilters, factory).First();

            //in case you want the parameters as well
            //var parameters = nhLinqExpression.ParameterValuesByName.ToDictionary(x => x.Key, x => x.Value.Item1);

            return(translator.SQLString);
        }
        public string GetGeneratedSql(IQueryable queryable, ISession session)
        {
            var sessionImp        = (ISessionImplementor)session;
            var nhLinqExpression  = new NhLinqExpression(queryable.Expression, sessionImp.Factory);
            var translatorFactory = new ASTQueryTranslatorFactory();
            var translators       = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false,
                                                                             sessionImp.EnabledFilters, sessionImp.Factory);

            var sql           = translators.First().SQLString;
            var formamttedSql = FormatStyle.Basic.Formatter.Format(sql);
            int i             = 0;
            var map           = ExpressionParameterVisitor.Visit(queryable.Expression, sessionImp.Factory).ToArray();

            formamttedSql = Regex.Replace(formamttedSql, @"\?", m => map[i++].Key.ToString().Replace('"', '\''));

            return(formamttedSql);
        }
Example #9
0
        // <summary>
        /// Retorna o SQL da query informada
        /// </summary>
        /// <param name="session"/>
        /// <param name="queryable"/>
        /// <returns><see cref="string"/></returns>
        public static string GetSql(this NHibernate.ISession session, IQueryable queryable)
        {
            var sessionImpl = session as NHibernate.Impl.SessionImpl;

            var nhLinqExpression = new NhLinqExpression(
                queryable.Expression,
                sessionImpl.Factory);

            var queryTranslatorImpl = new ASTQueryTranslatorFactory()
                                      .CreateQueryTranslators(
                nhLinqExpression,
                null,
                false,
                sessionImpl.EnabledFilters,
                sessionImpl.Factory)
                                      .First() as QueryTranslatorImpl;

            var retorno = PopularParametros(
                nhLinqExpression,
                FormatStyle.Basic.Formatter.Format(queryTranslatorImpl.SQLString));

            return(retorno);
        }
Example #10
0
        private static IQueryTranslator[] CreateTranslators(string expressionStr, IQueryExpression queryExpression, string collectionRole, bool shallow, IDictionary <string, IFilter> enabledFilters, ISessionFactoryImplementor factory)
        {
            IQueryTranslatorFactory2 qtFactory = new ASTQueryTranslatorFactory();

            return(qtFactory.CreateQueryTranslators(expressionStr, queryExpression, collectionRole, shallow, enabledFilters, factory));
        }