Ejemplo n.º 1
0
		public virtual SqlCommandInfo GetQueryStringAndTypes(ISessionImplementor session, QueryParameters parameters, int startParameterIndex)
		{
			SqlString sqlString = ProcessFilters(parameters, session);
			Dialect.Dialect dialect = session.Factory.Dialect;

			RowSelection selection = parameters.RowSelection;
			bool useLimit = UseLimit(selection, dialect);
			bool hasFirstRow = GetFirstRow(selection) > 0;
			bool useOffset = hasFirstRow && useLimit && dialect.SupportsLimitOffset;
			int limitParameterCount = GetFirstLimitParameterCount(dialect, useLimit, hasFirstRow, useOffset);

			SqlType[] sqlTypes = parameters.PrepareParameterTypes(sqlString, Factory, GetNamedParameterLocs, startParameterIndex + limitParameterCount, useLimit, useOffset);

			if (useLimit)
			{
				sqlString =
					dialect.GetLimitString(sqlString.Trim(), useOffset ? GetFirstRow(selection) : 0, GetMaxOrLimit(dialect, selection));
			}

			sqlString = PreprocessSQL(sqlString, parameters, dialect);
			return new SqlCommandInfo(sqlString, sqlTypes);
		}
Ejemplo n.º 2
0
		/// <summary>
		/// Obtain an <c>IDbCommand</c> with all parameters pre-bound. Bind positional parameters,
		/// named parameters, and limit parameters.
		/// </summary>
		/// <remarks>
		/// Creates an IDbCommand object and populates it with the values necessary to execute it against the 
		/// database to Load an Entity.
		/// </remarks>
		/// <param name="queryParameters">The <see cref="QueryParameters"/> to use for the IDbCommand.</param>
		/// <param name="scroll">TODO: find out where this is used...</param>
		/// <param name="session">The SessionImpl this Command is being prepared in.</param>
		/// <returns>A CommandWrapper wrapping an IDbCommand that is ready to be executed.</returns>
		protected internal virtual IDbCommand PrepareQueryCommand(QueryParameters queryParameters, bool scroll,
		                                                          ISessionImplementor session)
		{
			SqlString sqlString = ProcessFilters(queryParameters, session);
			Dialect.Dialect dialect = session.Factory.Dialect;

			RowSelection selection = queryParameters.RowSelection;
			bool useLimit = UseLimit(selection, dialect);
			bool hasFirstRow = GetFirstRow(selection) > 0;
			bool useOffset = hasFirstRow && useLimit && dialect.SupportsLimitOffset;
			int startIndex = GetFirstLimitParameterCount(dialect, useLimit, hasFirstRow, useOffset);
			// TODO NH bool callable = queryParameters.Callable;

			SqlType[] parameterTypes = queryParameters.PrepareParameterTypes(sqlString, Factory, GetNamedParameterLocs, startIndex, useLimit, useOffset);

			if (useLimit)
			{
				sqlString =
					dialect.GetLimitString(sqlString.Trim(), useOffset ? GetFirstRow(selection) : 0, GetMaxOrLimit(dialect, selection));
			}

			sqlString = PreprocessSQL(sqlString, queryParameters, dialect);

			// TODO NH: Callable for SP -> PrepareCallableQueryCommand
			IDbCommand command =
				session.Batcher.PrepareQueryCommand(CommandType.Text, sqlString, parameterTypes);

			try
			{
				// Added in NH - not in H2.1
				if (selection != null && selection.Timeout != RowSelection.NoValue)
				{
					command.CommandTimeout = selection.Timeout;
				}

				int colIndex = 0;

				if (useLimit && dialect.BindLimitParametersFirst)
				{
					colIndex += BindLimitParameters(command, colIndex, selection, session);
				}
				// TODO NH
				//if (callable)
				//{
				//  colIndex = dialect.RegisterResultSetOutParameter(command, col);
				//}

				colIndex += BindParameterValues(command, queryParameters, colIndex, session);

				if (useLimit && !dialect.BindLimitParametersFirst)
				{
					BindLimitParameters(command, colIndex, selection, session);
				}

				if (!useLimit)
				{
					SetMaxRows(command, selection);
				}
				if (selection != null)
				{
					if (selection.Timeout != RowSelection.NoValue)
					{
						command.CommandTimeout = selection.Timeout;
					}
					// H2.1 handles FetchSize here - not ported
				}
			}
			catch (HibernateException)
			{
				session.Batcher.CloseCommand(command, null);
				throw;
			}
			catch (Exception sqle)
			{
				session.Batcher.CloseCommand(command, null);
				ADOExceptionReporter.LogExceptions(sqle);
				throw;
			}

			return command;
		}