public void NativeSQLQuerySpecificationEqualityOnReturns()
        {
            var spec1 = new NativeSQLQuerySpecification("select blah",
                                                        new[]
            {
                new NativeSQLQueryScalarReturn("alias1", NHibernateUtil.Character),
                new NativeSQLQueryScalarReturn("alias2", NHibernateUtil.Decimal)
            },
                                                        null);
            var spec2 = new NativeSQLQuerySpecification("select blah",
                                                        new[]
            {
                // Aliases ordering matters, do not get them mixed. (But type does not matter, I guess this means
                // a case with a same query having sames aliases but different types is never supposed to happen
                // Note that Hibernate does test other properties as of this writing:
                // https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/engine/query/spi/sql/NativeSQLQueryScalarReturn.java
                // And same on other INativeSQLQueryReturn implementations.
                new NativeSQLQueryScalarReturn("alias1", NHibernateUtil.Character),
                new NativeSQLQueryScalarReturn("alias2", NHibernateUtil.Decimal)
            },
                                                        // Empty spaces array should be equivalent to null.
                                                        Array.Empty <string>());

            Assert.IsTrue(spec1.Equals(spec2));
            Assert.IsTrue(spec2.Equals(spec1));
        }
 protected internal virtual NativeSQLQueryPlan GetNativeSQLQueryPlan(NativeSQLQuerySpecification spec)
 {
     using (new SessionIdLoggingContext(SessionId))
     {
         return(factory.QueryPlanCache.GetNativeSQLQueryPlan(spec));
     }
 }
Beispiel #3
0
        public SqlTranslator(ISQLQuery sqlQuery, ISessionFactoryImplementor sessionFactory)
        {
            var sqlQueryImpl = (SqlQueryImpl)sqlQuery;
            NativeSQLQuerySpecification sqlQuerySpec = sqlQueryImpl.GenerateQuerySpecification(sqlQueryImpl.NamedParams);
            var sqlCustomQuery = new SQLCustomQuery(sqlQuerySpec.SqlQueryReturns, sqlQuerySpec.QueryString, sqlQuerySpec.QuerySpaces, sessionFactory);

            loader = new CustomLoader(sqlCustomQuery, sessionFactory);
        }
 public virtual IList <T> List <T>(NativeSQLQuerySpecification spec, QueryParameters queryParameters)
 {
     using (new SessionIdLoggingContext(SessionId))
     {
         var results = new List <T>();
         List(spec, queryParameters, results);
         return(results);
     }
 }
        public void NativeSQLQuerySpecificationInequalityOnQuery()
        {
            var spec1 = new NativeSQLQuerySpecification("select blah", null, null);
            var spec2 = new NativeSQLQuerySpecification("select blargh", null, null);

            TweakHashcode(spec2, spec1.GetHashCode());
            Assert.IsFalse(spec1.Equals(spec2));
            Assert.IsFalse(spec2.Equals(spec1));
        }
        public void NativeSQLQuerySpecificationEqualityOnQuery()
        {
            var spec1 = new NativeSQLQuerySpecification("select blah", null, null);
            // Empty spaces array should be equivalent to null. Maybe results too but current implementation does not handle this.
            var spec2 = new NativeSQLQuerySpecification("select blah", null, Array.Empty <string>());

            Assert.IsTrue(spec1.Equals(spec2));
            Assert.IsTrue(spec2.Equals(spec1));
        }
        public void NativeSQLQuerySpecificationEqualityOnSpaces()
        {
            var spec1 = new NativeSQLQuerySpecification("select blah", null,
                                                        new[] { "space1", "space2" });
            var spec2 = new NativeSQLQuerySpecification("select blah", null,
                                                        new[] { "space1", "space2" });

            Assert.IsTrue(spec1.Equals(spec2));
            Assert.IsTrue(spec2.Equals(spec1));
        }
Beispiel #8
0
 public virtual async Task <IList> ListAsync(NativeSQLQuerySpecification spec, QueryParameters queryParameters, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (BeginProcess())
     {
         var results = new List <object>();
         await(ListAsync(spec, queryParameters, results, cancellationToken)).ConfigureAwait(false);
         return(results);
     }
 }
Beispiel #9
0
 public override IList <T> List <T>(NativeSQLQuerySpecification spec, QueryParameters queryParameters)
 {
     using (new SessionIdLoggingContext(SessionId))
     {
         // TODO pull up
         List <T> results = new List <T>();
         List(spec, queryParameters, results);
         return(results);
     }
 }
 public virtual async Task <IList <T> > ListAsync <T>(NativeSQLQuerySpecification spec, QueryParameters queryParameters, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (new SessionIdLoggingContext(SessionId))
     {
         var results = new List <T>();
         await(ListAsync(spec, queryParameters, results, cancellationToken)).ConfigureAwait(false);
         return(results);
     }
 }
        public void NativeSQLQuerySpecificationInequalityOnSpaceOrderings()
        {
            var spec1 = new NativeSQLQuerySpecification("select blah", null,
                                                        new[] { "space1", "space2" });
            var spec2 = new NativeSQLQuerySpecification("select blah", null,
                                                        new[] { "space2", "space1" });

            TweakHashcode(spec2, spec1.GetHashCode());
            Assert.IsFalse(spec1.Equals(spec2));
            Assert.IsFalse(spec2.Equals(spec1));
        }
        public void WhenChangeSpace_NotEqualHashCode()
        {
            var sr1 = new NativeSQLQuerySpecification("SELECT * FROM SOMETHING", new INativeSQLQueryReturn[] { new NativeSQLQueryScalarReturn("myAlias", NHibernateUtil.Int32), new NativeSQLQueryScalarReturn("otherAlias", NHibernateUtil.Int32) }, new List <string> {
                "SOMETHING"
            });
            var sr2 = new NativeSQLQuerySpecification("SELECT * FROM SOMETHING", new INativeSQLQueryReturn[] { new NativeSQLQueryScalarReturn("myAlias", NHibernateUtil.Int32), new NativeSQLQueryScalarReturn("otherAlias", NHibernateUtil.Int32) }, new List <string> {
                "ANOTHER"
            });

            Assert.AreNotEqual(sr1.GetHashCode(), sr2.GetHashCode());
        }
        public void WhenChangeReturns_NotEqual()
        {
            var sr1 = new NativeSQLQuerySpecification("SELECT * FROM SOMETHING", new INativeSQLQueryReturn[] { new NativeSQLQueryScalarReturn("myAlias", NHibernateUtil.Int32), new NativeSQLQueryScalarReturn("otherAlias", NHibernateUtil.Int32) }, new List <string> {
                "SOMETHING"
            });
            var sr2 = new NativeSQLQuerySpecification("SELECT * FROM SOMETHING", new INativeSQLQueryReturn[] { new NativeSQLQueryScalarReturn("myAliasChanged", NHibernateUtil.Int32), new NativeSQLQueryScalarReturn("otherAlias", NHibernateUtil.Int32) }, new List <string> {
                "SOMETHING"
            });

            Assert.AreNotEqual(sr1, sr2);
        }
 public virtual void List(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results)
 {
     using (new SessionIdLoggingContext(SessionId))
     {
         var query = new SQLCustomQuery(
             spec.SqlQueryReturns,
             spec.QueryString,
             spec.QuerySpaces,
             Factory);
         ListCustomQuery(query, queryParameters, results);
     }
 }
 public virtual async Task ListAsync(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (new SessionIdLoggingContext(SessionId))
     {
         var query = new SQLCustomQuery(
             spec.SqlQueryReturns,
             spec.QueryString,
             spec.QuerySpaces,
             Factory);
         await(ListCustomQueryAsync(query, queryParameters, results, cancellationToken)).ConfigureAwait(false);
     }
 }
        public void NativeSQLQuerySpecificationInequalityOnNullReturn()
        {
            var spec1 = new NativeSQLQuerySpecification("select blah", null, null);
            var spec2 = new NativeSQLQuerySpecification("select blah",
                                                        new[]
            {
                new NativeSQLQueryScalarReturn("alias1", NHibernateUtil.Character)
            },
                                                        null);

            TweakHashcode(spec2, spec1.GetHashCode());
            Assert.IsFalse(spec1.Equals(spec2));
            Assert.IsFalse(spec2.Equals(spec1));
        }
Beispiel #17
0
        public override void List(IList results)
        {
            VerifyParameters();
            IDictionary <string, TypedValue> namedParams = NamedParams;
            NativeSQLQuerySpecification      spec        = GenerateQuerySpecification(namedParams);
            QueryParameters qp = GetQueryParameters(namedParams);

            Before();
            try
            {
                Session.List(spec, qp, results);
            }
            finally
            {
                After();
            }
        }
Beispiel #18
0
        public override IList <T> List <T>()
        {
            VerifyParameters();
            IDictionary <string, TypedValue> namedParams = NamedParams;
            NativeSQLQuerySpecification      spec        = GenerateQuerySpecification(namedParams);
            QueryParameters qp = GetQueryParameters(namedParams);

            Before();
            try
            {
                return(Session.List <T>(spec, qp));
            }
            finally
            {
                After();
            }
        }
Beispiel #19
0
        public override System.Data.DataSet QueryDataSet()
        {
            VerifyParameters();
            IDictionary <string, TypedValue> namedParams = NamedParams;
            NativeSQLQuerySpecification      spec        = GenerateQuerySpecification(namedParams);
            QueryParameters qp = GetQueryParameters(namedParams);

            Before();
            try
            {
                return(Session.QueryDataSet(spec, qp));
            }
            finally
            {
                After();
            }
            //return base.QueryDataSet();
        }
Beispiel #20
0
        public override async Task <IList <T> > ListAsync <T>(CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            VerifyParameters();
            IDictionary <string, TypedValue> namedParams = NamedParams;
            NativeSQLQuerySpecification      spec        = GenerateQuerySpecification(namedParams);
            QueryParameters qp = GetQueryParameters(namedParams);

            Before();
            try
            {
                return(await(Session.ListAsync <T>(spec, qp, cancellationToken)).ConfigureAwait(false));
            }
            finally
            {
                After();
            }
        }
Beispiel #21
0
        public override int ExecuteNativeUpdate(NativeSQLQuerySpecification nativeSQLQuerySpecification, QueryParameters queryParameters)
        {
            using (BeginProcess())
            {
                queryParameters.ValidateParameters();
                NativeSQLQueryPlan plan = GetNativeSQLQueryPlan(nativeSQLQuerySpecification);

                bool success = false;
                int  result;
                try
                {
                    result  = plan.PerformExecuteUpdate(queryParameters, this);
                    success = true;
                }
                finally
                {
                    AfterOperation(success);
                }
                temporaryPersistenceContext.Clear();
                return(result);
            }
        }
Beispiel #22
0
        public override async Task <int> ExecuteNativeUpdateAsync(NativeSQLQuerySpecification nativeSQLQuerySpecification, QueryParameters queryParameters, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (BeginProcess())
            {
                queryParameters.ValidateParameters();
                NativeSQLQueryPlan plan = GetNativeSQLQueryPlan(nativeSQLQuerySpecification);

                bool success = false;
                int  result;
                try
                {
                    result  = await(plan.PerformExecuteUpdateAsync(queryParameters, this, cancellationToken)).ConfigureAwait(false);
                    success = true;
                }
                finally
                {
                    await(AfterOperationAsync(success, cancellationToken)).ConfigureAwait(false);
                }
                temporaryPersistenceContext.Clear();
                return(result);
            }
        }
Beispiel #23
0
        public NativeSQLQueryPlan GetNativeSQLQueryPlan(NativeSQLQuerySpecification spec)
        {
            var plan = (NativeSQLQueryPlan)planCache[spec];

            if (plan == null)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("unable to locate native-sql query plan in cache; generating (" + spec.QueryString + ")");
                }
                plan = new NativeSQLQueryPlan(spec, factory);
                planCache.Put(spec, plan);
            }
            else
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("located native-sql query plan in cache (" + spec.QueryString + ")");
                }
            }

            return(plan);
        }
 public abstract int ExecuteNativeUpdate(NativeSQLQuerySpecification specification, QueryParameters queryParameters);
		private IDictionary<string, HibernateException> CheckNamedQueries()
		{
			IDictionary<string, HibernateException> errors = new Dictionary<string, HibernateException>();

			// Check named HQL queries
			log.Debug("Checking " + namedQueries.Count + " named HQL queries");
			foreach (KeyValuePair<string, NamedQueryDefinition> entry in namedQueries)
			{
				string queryName = entry.Key;
				NamedQueryDefinition qd = entry.Value;
				// this will throw an error if there's something wrong.
				try
				{
					log.Debug("Checking named query: " + queryName);
					//TODO: BUG! this currently fails for named queries for non-POJO entities
					queryPlanCache.GetHQLQueryPlan(qd.QueryString, false, new CollectionHelper.EmptyMapClass<string, IFilter>());
				}
				catch (QueryException e)
				{
					errors[queryName] = e;
				}
				catch (MappingException e)
				{
					errors[queryName] = e;
				}
			}

			log.Debug("Checking " + namedSqlQueries.Count + " named SQL queries");
			foreach (KeyValuePair<string, NamedSQLQueryDefinition> entry in namedSqlQueries)
			{
				string queryName = entry.Key;
				NamedSQLQueryDefinition qd = entry.Value;
				// this will throw an error if there's something wrong.
				try
				{
					log.Debug("Checking named SQL query: " + queryName);
					// TODO : would be really nice to cache the spec on the query-def so as to not have to re-calc the hash;
					// currently not doable though because of the resultset-ref stuff...
					NativeSQLQuerySpecification spec;
					if (qd.ResultSetRef != null)
					{
						ResultSetMappingDefinition definition = sqlResultSetMappings[qd.ResultSetRef];
						if (definition == null)
						{
							throw new MappingException("Unable to find resultset-ref definition: " + qd.ResultSetRef);
						}
						spec = new NativeSQLQuerySpecification(qd.QueryString, definition.GetQueryReturns(), qd.QuerySpaces);
					}
					else
					{
						spec = new NativeSQLQuerySpecification(qd.QueryString, qd.QueryReturns, qd.QuerySpaces);
					}
					queryPlanCache.GetNativeSQLQueryPlan(spec);
				}
				catch (QueryException e)
				{
					errors[queryName] = e;
				}
				catch (MappingException e)
				{
					errors[queryName] = e;
				}
			}

			return errors;
		}
Beispiel #26
0
 public NativeSQLQueryPlan(NativeSQLQuerySpecification specification, ISessionFactoryImplementor factory)
 {
     sourceQuery = specification.QueryString;
     customQuery = new SQLCustomQuery(specification.SqlQueryReturns, specification.QueryString, specification.QuerySpaces, factory);
 }
Beispiel #27
0
 public override int ExecuteNativeUpdate(NativeSQLQuerySpecification nativeQuerySpecification, QueryParameters queryParameters);
		public NativeSQLQueryPlan GetNativeSQLQueryPlan(NativeSQLQuerySpecification spec)
		{
			var plan = (NativeSQLQueryPlan)planCache[spec];

			if (plan == null)
			{
				if (log.IsDebugEnabled)
				{
					log.Debug("unable to locate native-sql query plan in cache; generating (" + spec.QueryString + ")");
				}
				plan = new NativeSQLQueryPlan(spec, factory);
				planCache.Put(spec, plan);
			}
			else
			{
				if (log.IsDebugEnabled)
				{
					log.Debug("located native-sql query plan in cache (" + spec.QueryString + ")");
				}
			}

			return plan;
		}
 public abstract Task <int> ExecuteNativeUpdateAsync(NativeSQLQuerySpecification specification, QueryParameters queryParameters, CancellationToken cancellationToken);
		public NativeSQLQueryPlan(NativeSQLQuerySpecification specification, ISessionFactoryImplementor factory)
		{
			sourceQuery = specification.QueryString;
			customQuery = new SQLCustomQuery(specification.SqlQueryReturns, specification.QueryString, specification.QuerySpaces, factory);
		}
Beispiel #31
0
 public override IList <T> List <T>(NativeSQLQuerySpecification spec, QueryParameters queryParameters);
Beispiel #32
0
        private IDictionary <string, HibernateException> CheckNamedQueries()
        {
            IDictionary <string, HibernateException> errors = new Dictionary <string, HibernateException>();

            // Check named HQL queries
            log.Debug("Checking " + namedQueries.Count + " named HQL queries");
            foreach (var entry in namedQueries)
            {
                string queryName        = entry.Key;
                NamedQueryDefinition qd = entry.Value;
                // this will throw an error if there's something wrong.
                try
                {
                    log.Debug("Checking named query: " + queryName);
                    //TODO: BUG! this currently fails for named queries for non-POJO entities
                    queryPlanCache.GetHQLQueryPlan(qd.QueryString.ToQueryExpression(), false, new CollectionHelper.EmptyMapClass <string, IFilter>());
                }
                catch (QueryException e)
                {
                    errors[queryName] = e;
                }
                catch (MappingException e)
                {
                    errors[queryName] = e;
                }
            }

            log.Debug("Checking " + namedSqlQueries.Count + " named SQL queries");
            foreach (KeyValuePair <string, NamedSQLQueryDefinition> entry in namedSqlQueries)
            {
                string queryName           = entry.Key;
                NamedSQLQueryDefinition qd = entry.Value;
                // this will throw an error if there's something wrong.
                try
                {
                    log.Debug("Checking named SQL query: " + queryName);
                    // TODO : would be really nice to cache the spec on the query-def so as to not have to re-calc the hash;
                    // currently not doable though because of the resultset-ref stuff...
                    NativeSQLQuerySpecification spec;
                    if (qd.ResultSetRef != null)
                    {
                        ResultSetMappingDefinition definition = sqlResultSetMappings[qd.ResultSetRef];
                        if (definition == null)
                        {
                            throw new MappingException("Unable to find resultset-ref definition: " + qd.ResultSetRef);
                        }
                        spec = new NativeSQLQuerySpecification(qd.QueryString, definition.GetQueryReturns(), qd.QuerySpaces);
                    }
                    else
                    {
                        spec = new NativeSQLQuerySpecification(qd.QueryString, qd.QueryReturns, qd.QuerySpaces);
                    }
                    queryPlanCache.GetNativeSQLQueryPlan(spec);
                }
                catch (QueryException e)
                {
                    errors[queryName] = e;
                }
                catch (MappingException e)
                {
                    errors[queryName] = e;
                }
            }

            return(errors);
        }
Beispiel #33
0
 public override void List(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results);