@@ -15,14 +15,12 @@ namespace NHibernate.Driver
1515 public partial class BasicResultSetsCommand : IResultSetsCommand
1616 {
1717 private static readonly INHibernateLogger log = NHibernateLogger . For ( typeof ( BasicResultSetsCommand ) ) ;
18- private SqlString sqlString = SqlString . Empty ;
19- private readonly string _statementTerminator ;
18+ private SqlString _sqlString = SqlString . Empty ;
2019
2120 public BasicResultSetsCommand ( ISessionImplementor session )
2221 {
2322 Commands = new List < ISqlCommand > ( ) ;
2423 Session = session ;
25- _statementTerminator = session . Factory . Dialect . StatementTerminator . ToString ( ) + Environment . NewLine ;
2624 }
2725
2826 protected List < ISqlCommand > Commands { get ; private set ; }
@@ -32,25 +30,42 @@ public BasicResultSetsCommand(ISessionImplementor session)
3230 public virtual void Append ( ISqlCommand command )
3331 {
3432 Commands . Add ( command ) ;
35- sqlString = sqlString . Append ( command . Query , _statementTerminator ) ;
33+ _sqlString = null ;
3634 }
3735
3836 public bool HasQueries
3937 {
4038 get { return Commands . Count > 0 ; }
4139 }
4240
43- public virtual SqlString Sql
41+ public virtual SqlString Sql => _sqlString ?? ( _sqlString = GetSqlString ( ) ) ;
42+
43+ private SqlString GetSqlString ( )
4444 {
45- get { return sqlString ; }
45+ switch ( Commands . Count )
46+ {
47+ case 0 :
48+ return SqlString . Empty ;
49+ case 1 :
50+ return Commands [ 0 ] . Query ;
51+ }
52+
53+ var statementTerminator = Session . Factory . Dialect . StatementTerminator . ToString ( ) + Environment . NewLine ;
54+ var builder = new SqlStringBuilder ( Commands . Sum ( c => c . Query . Count ) + Commands . Count ) ;
55+ foreach ( var command in Commands )
56+ {
57+ builder . Add ( command . Query ) . Add ( statementTerminator ) ;
58+ }
59+
60+ return builder . ToSqlString ( ) ;
4661 }
4762
4863 public virtual DbDataReader GetReader ( int ? commandTimeout )
4964 {
5065 var batcher = Session . Batcher ;
5166 SqlType [ ] sqlTypes = Commands . SelectMany ( c => c . ParameterTypes ) . ToArray ( ) ;
5267 ForEachSqlCommand ( ( sqlLoaderCommand , offset ) => sqlLoaderCommand . ResetParametersIndexesForTheCommand ( offset ) ) ;
53- var command = batcher . PrepareQueryCommand ( CommandType . Text , sqlString , sqlTypes ) ;
68+ var command = batcher . PrepareQueryCommand ( CommandType . Text , Sql , sqlTypes ) ;
5469 if ( commandTimeout . HasValue )
5570 {
5671 command . CommandTimeout = commandTimeout . Value ;
0 commit comments