Skip to content

Use SqlStringBuilder for batching Future/QueryBatch queries #2232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/NHibernate/Async/Driver/BasicResultSetsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public virtual async Task<DbDataReader> GetReaderAsync(int? commandTimeout, Canc
var batcher = Session.Batcher;
SqlType[] sqlTypes = Commands.SelectMany(c => c.ParameterTypes).ToArray();
ForEachSqlCommand((sqlLoaderCommand, offset) => sqlLoaderCommand.ResetParametersIndexesForTheCommand(offset));
var command = batcher.PrepareQueryCommand(CommandType.Text, sqlString, sqlTypes);
var command = batcher.PrepareQueryCommand(CommandType.Text, Sql, sqlTypes);
if (commandTimeout.HasValue)
{
command.CommandTimeout = commandTimeout.Value;
Expand Down
29 changes: 22 additions & 7 deletions src/NHibernate/Driver/BasicResultSetsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ namespace NHibernate.Driver
public partial class BasicResultSetsCommand: IResultSetsCommand
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(BasicResultSetsCommand));
private SqlString sqlString = SqlString.Empty;
private readonly string _statementTerminator;
private SqlString _sqlString = SqlString.Empty;

public BasicResultSetsCommand(ISessionImplementor session)
{
Commands = new List<ISqlCommand>();
Session = session;
_statementTerminator = session.Factory.Dialect.StatementTerminator.ToString() + Environment.NewLine;
}

protected List<ISqlCommand> Commands { get; private set; }
Expand All @@ -32,25 +30,42 @@ public BasicResultSetsCommand(ISessionImplementor session)
public virtual void Append(ISqlCommand command)
{
Commands.Add(command);
sqlString = sqlString.Append(command.Query, _statementTerminator);
_sqlString = null;
}

public bool HasQueries
{
get { return Commands.Count > 0; }
}

public virtual SqlString Sql
public virtual SqlString Sql => _sqlString ?? (_sqlString = GetSqlString());

private SqlString GetSqlString()
{
get { return sqlString; }
switch (Commands.Count)
{
case 0:
return SqlString.Empty;
case 1:
return Commands[0].Query;
}

var statementTerminator = Session.Factory.Dialect.StatementTerminator.ToString() + Environment.NewLine;
var builder = new SqlStringBuilder(Commands.Sum(c => c.Query.Count) + Commands.Count);
foreach (var command in Commands)
{
builder.Add(command.Query).Add(statementTerminator);
}

return builder.ToSqlString();
}

public virtual DbDataReader GetReader(int? commandTimeout)
{
var batcher = Session.Batcher;
SqlType[] sqlTypes = Commands.SelectMany(c => c.ParameterTypes).ToArray();
ForEachSqlCommand((sqlLoaderCommand, offset) => sqlLoaderCommand.ResetParametersIndexesForTheCommand(offset));
var command = batcher.PrepareQueryCommand(CommandType.Text, sqlString, sqlTypes);
var command = batcher.PrepareQueryCommand(CommandType.Text, Sql, sqlTypes);
if (commandTimeout.HasValue)
{
command.CommandTimeout = commandTimeout.Value;
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/SqlCommand/SqlString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public SqlString(params object[] parts)
: this((IEnumerable<object>)parts)
{ }

private SqlString(IEnumerable<object> parts)
internal SqlString(IEnumerable<object> parts)
{
_parts = new List<Part>();
_parameters = new SortedList<int, Parameter>();
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/SqlCommand/SqlStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public SqlStringBuilder RemoveAt(int index)
/// <returns>The SqlString that was built.</returns>
public SqlString ToSqlString()
{
return new SqlString(sqlParts.ToArray());
return new SqlString(sqlParts);
}

public override string ToString()
Expand Down