@@ -15,14 +15,12 @@ namespace NHibernate.Driver
15
15
public partial class BasicResultSetsCommand : IResultSetsCommand
16
16
{
17
17
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 ;
20
19
21
20
public BasicResultSetsCommand ( ISessionImplementor session )
22
21
{
23
22
Commands = new List < ISqlCommand > ( ) ;
24
23
Session = session ;
25
- _statementTerminator = session . Factory . Dialect . StatementTerminator . ToString ( ) + Environment . NewLine ;
26
24
}
27
25
28
26
protected List < ISqlCommand > Commands { get ; private set ; }
@@ -32,25 +30,42 @@ public BasicResultSetsCommand(ISessionImplementor session)
32
30
public virtual void Append ( ISqlCommand command )
33
31
{
34
32
Commands . Add ( command ) ;
35
- sqlString = sqlString . Append ( command . Query , _statementTerminator ) ;
33
+ _sqlString = null ;
36
34
}
37
35
38
36
public bool HasQueries
39
37
{
40
38
get { return Commands . Count > 0 ; }
41
39
}
42
40
43
- public virtual SqlString Sql
41
+ public virtual SqlString Sql => _sqlString ?? ( _sqlString = GetSqlString ( ) ) ;
42
+
43
+ private SqlString GetSqlString ( )
44
44
{
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 ( ) ;
46
61
}
47
62
48
63
public virtual DbDataReader GetReader ( int ? commandTimeout )
49
64
{
50
65
var batcher = Session . Batcher ;
51
66
SqlType [ ] sqlTypes = Commands . SelectMany ( c => c . ParameterTypes ) . ToArray ( ) ;
52
67
ForEachSqlCommand ( ( sqlLoaderCommand , offset ) => sqlLoaderCommand . ResetParametersIndexesForTheCommand ( offset ) ) ;
53
- var command = batcher . PrepareQueryCommand ( CommandType . Text , sqlString , sqlTypes ) ;
68
+ var command = batcher . PrepareQueryCommand ( CommandType . Text , Sql , sqlTypes ) ;
54
69
if ( commandTimeout . HasValue )
55
70
{
56
71
command . CommandTimeout = commandTimeout . Value ;
0 commit comments