1
+ using System ;
2
+ using System . Collections ;
3
+ using System . Collections . Generic ;
4
+ using System . Data ;
5
+ using NHibernate . AdoNet ;
6
+ using NHibernate . Cfg ;
7
+ using NHibernate . Cfg . Loquacious ;
8
+ using NHibernate . Engine ;
9
+ using NHibernate . SqlCommand ;
10
+ using NHibernate . SqlTypes ;
11
+ using NUnit . Framework ;
12
+ using SharpTestsEx ;
13
+
14
+ namespace NHibernate . Test . Insertordering
15
+ {
16
+ public class InsertOrderingFixture : TestCase
17
+ {
18
+ const int batchSize = 10 ;
19
+ const int instancesPerEach = 12 ;
20
+ const int typesOfEntities = 3 ;
21
+ protected override IList Mappings
22
+ {
23
+ get { return new [ ] { "Insertordering.Mapping.hbm.xml" } ; }
24
+ }
25
+
26
+ protected override string MappingsAssembly
27
+ {
28
+ get { return "NHibernate.Test" ; }
29
+ }
30
+
31
+ protected override void Configure ( Configuration configuration )
32
+ {
33
+ configuration . DataBaseIntegration ( x =>
34
+ {
35
+ x . BatchSize = batchSize ;
36
+ x . OrderInserts = true ;
37
+ x . Batcher < StatsBatcherFactory > ( ) ;
38
+ } ) ;
39
+ }
40
+
41
+ [ Test ]
42
+ public void BatchOrdering ( )
43
+ {
44
+ using ( ISession s = OpenSession ( ) )
45
+ using ( s . BeginTransaction ( ) )
46
+ {
47
+ for ( int i = 0 ; i < instancesPerEach ; i ++ )
48
+ {
49
+ var user = new User { UserName = "user-" + i } ;
50
+ var group = new Group { Name = "group-" + i } ;
51
+ s . Save ( user ) ;
52
+ s . Save ( group ) ;
53
+ user . AddMembership ( group ) ;
54
+ }
55
+ StatsBatcher . Reset ( ) ;
56
+ s . Transaction . Commit ( ) ;
57
+ }
58
+
59
+ int expectedBatchesPerEntity = ( instancesPerEach / batchSize ) + ( ( instancesPerEach % batchSize ) == 0 ? 0 : 1 ) ;
60
+ StatsBatcher . BatchSizes . Count . Should ( ) . Be ( expectedBatchesPerEntity * typesOfEntities ) ;
61
+
62
+ using ( ISession s = OpenSession ( ) )
63
+ {
64
+ s . BeginTransaction ( ) ;
65
+ IList users = s . CreateQuery ( "from User u left join fetch u.Memberships m left join fetch m.Group" ) . List ( ) ;
66
+ foreach ( object user in users )
67
+ {
68
+ s . Delete ( user ) ;
69
+ }
70
+ s . Transaction . Commit ( ) ;
71
+ }
72
+ }
73
+
74
+ #region Nested type: StatsBatcher
75
+
76
+ public class StatsBatcher : SqlClientBatchingBatcher
77
+ {
78
+ private static string batchSQL ;
79
+ private static IList < int > batchSizes = new List < int > ( ) ;
80
+ private static int currentBatch = - 1 ;
81
+
82
+ public StatsBatcher ( ConnectionManager connectionManager , IInterceptor interceptor )
83
+ : base ( connectionManager , interceptor ) { }
84
+
85
+ public static IList < int > BatchSizes
86
+ {
87
+ get { return batchSizes ; }
88
+ }
89
+
90
+ public static void Reset ( )
91
+ {
92
+ batchSizes = new List < int > ( ) ;
93
+ currentBatch = - 1 ;
94
+ batchSQL = null ;
95
+ }
96
+
97
+ public override IDbCommand PrepareBatchCommand ( CommandType type , SqlString sql , SqlType [ ] parameterTypes )
98
+ {
99
+ IDbCommand result = base . PrepareBatchCommand ( type , sql , parameterTypes ) ;
100
+ string sqlstring = sql . ToString ( ) ;
101
+ if ( batchSQL == null || ! sqlstring . Equals ( batchSQL ) )
102
+ {
103
+ currentBatch ++ ;
104
+ batchSQL = sqlstring ;
105
+ batchSizes . Insert ( currentBatch , 0 ) ;
106
+ Console . WriteLine ( "--------------------------------------------------------" ) ;
107
+ Console . WriteLine ( "Preparing statement [" + batchSQL + "]" ) ;
108
+ }
109
+ return result ;
110
+ }
111
+
112
+ public override void AddToBatch ( IExpectation expectation )
113
+ {
114
+ batchSizes [ currentBatch ] ++ ;
115
+ Console . WriteLine ( "Adding to batch [" + batchSQL + "]" ) ;
116
+ base . AddToBatch ( expectation ) ;
117
+ }
118
+
119
+ protected override void DoExecuteBatch ( IDbCommand ps )
120
+ {
121
+ Console . WriteLine ( "executing batch [" + batchSQL + "]" ) ;
122
+ Console . WriteLine ( "--------------------------------------------------------" ) ;
123
+ batchSQL = null ;
124
+ base . DoExecuteBatch ( ps ) ;
125
+ }
126
+ }
127
+
128
+ #endregion
129
+
130
+ #region Nested type: StatsBatcherFactory
131
+
132
+ public class StatsBatcherFactory : IBatcherFactory
133
+ {
134
+ #region IBatcherFactory Members
135
+
136
+ public IBatcher CreateBatcher ( ConnectionManager connectionManager , IInterceptor interceptor )
137
+ {
138
+ return new StatsBatcher ( connectionManager , interceptor ) ;
139
+ }
140
+
141
+ #endregion
142
+ }
143
+
144
+ #endregion
145
+ }
146
+ }
0 commit comments