Skip to content

Commit ebf1d17

Browse files
committed
NH1274ExportExcludeFixture.cs: Avoid stealing Console.Out (it was never reset when the test completed).
SchemaExport, SchemaUpdate: Improve parameter naming and add additional overloads so that a TextWriter can be passed in.
1 parent c63bfc9 commit ebf1d17

File tree

3 files changed

+101
-61
lines changed

3 files changed

+101
-61
lines changed

src/NHibernate.Test/NHSpecificTest/NH1274ExportExclude/NH1274ExportExcludeFixture.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public void SchemaExport_Drop_CreatesDropScript()
2020
Configuration configuration = GetConfiguration();
2121
SchemaExport export = new SchemaExport(configuration);
2222
TextWriter tw = new StringWriter();
23-
Console.SetOut(tw);
24-
export.Drop(true, false);
23+
export.Drop(tw, false);
2524
string s = tw.ToString();
2625

2726
var dialect = Dialect.Dialect.GetDialect(configuration.Properties);
@@ -44,10 +43,9 @@ public void SchemaExport_Export_CreatesExportScript()
4443
Configuration configuration = GetConfiguration();
4544
SchemaExport export = new SchemaExport(configuration);
4645
TextWriter tw = new StringWriter();
47-
Console.SetOut(tw);
48-
export.Create(true, false);
46+
export.Create(tw, false);
4947
string s = tw.ToString();
50-
48+
5149
var dialect = Dialect.Dialect.GetDialect(configuration.Properties);
5250
if (dialect.SupportsIfExistsBeforeTableName)
5351
{
@@ -70,8 +68,7 @@ public void SchemaExport_Update_CreatesUpdateScript()
7068
Configuration configuration = GetConfiguration();
7169
SchemaUpdate update = new SchemaUpdate(configuration);
7270
TextWriter tw = new StringWriter();
73-
Console.SetOut(tw);
74-
update.Execute(true, false);
71+
update.Execute(tw.WriteLine, false);
7572

7673
string s = tw.ToString();
7774
Assert.IsTrue(s.Contains("create table Home_Update"));
@@ -130,9 +127,9 @@ protected IList Mappings
130127
get
131128
{
132129
return new string[]
133-
{
134-
"NHSpecificTest." + BugNumber + ".Mappings.hbm.xml"
135-
};
130+
{
131+
"NHSpecificTest." + BugNumber + ".Mappings.hbm.xml"
132+
};
136133
}
137134
}
138135
}

src/NHibernate/Tool/hbm2ddl/SchemaExport.cs

Lines changed: 92 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -94,37 +94,74 @@ public SchemaExport SetDelimiter(string delimiter)
9494
/// <summary>
9595
/// Run the schema creation script
9696
/// </summary>
97-
/// <param name="script"><see langword="true" /> if the ddl should be outputted in the Console.</param>
98-
/// <param name="export"><see langword="true" /> if the ddl should be executed against the Database.</param>
97+
/// <param name="useStdOut"><see langword="true" /> if the ddl should be outputted in the Console.</param>
98+
/// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
9999
/// <remarks>
100100
/// This is a convenience method that calls <see cref="Execute(bool, bool, bool)"/> and sets
101101
/// the justDrop parameter to false.
102102
/// </remarks>
103-
public void Create(bool script, bool export)
103+
public void Create(bool useStdOut, bool execute)
104104
{
105-
Execute(script, export, false);
105+
Execute(useStdOut, execute, false);
106106
}
107107

108-
public void Create(Action<string> scriptAction, bool export)
108+
/// <summary>
109+
/// Run the schema creation script
110+
/// </summary>
111+
/// <param name="scriptAction"> an action that will be called for each line of the generated ddl.</param>
112+
/// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
113+
/// <remarks>
114+
/// This is a convenience method that calls <see cref="Execute(bool, bool, bool)"/> and sets
115+
/// the justDrop parameter to false.
116+
/// </remarks>
117+
public void Create(Action<string> scriptAction, bool execute)
109118
{
110-
Execute(scriptAction, export, false);
119+
Execute(scriptAction, execute, false);
120+
}
121+
122+
/// <summary>
123+
/// Run the schema creation script
124+
/// </summary>
125+
/// <param name="exportOutput"> if non-null, the ddl will be written to this TextWriter.</param>
126+
/// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
127+
/// <remarks>
128+
/// This is a convenience method that calls <see cref="Execute(bool, bool, bool)"/> and sets
129+
/// the justDrop parameter to false.
130+
/// </remarks>
131+
public void Create(TextWriter exportOutput, bool execute)
132+
{
133+
Execute(null, execute, false, exportOutput);
111134
}
112135

113136
/// <summary>
114137
/// Run the drop schema script
115138
/// </summary>
116-
/// <param name="script"><see langword="true" /> if the ddl should be outputted in the Console.</param>
117-
/// <param name="export"><see langword="true" /> if the ddl should be executed against the Database.</param>
139+
/// <param name="useStdOut"><see langword="true" /> if the ddl should be outputted in the Console.</param>
140+
/// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
118141
/// <remarks>
119142
/// This is a convenience method that calls <see cref="Execute(bool, bool, bool)"/> and sets
120143
/// the justDrop parameter to true.
121144
/// </remarks>
122-
public void Drop(bool script, bool export)
145+
public void Drop(bool useStdOut, bool execute)
123146
{
124-
Execute(script, export, true);
147+
Execute(useStdOut, execute, true);
125148
}
126149

127-
private void Execute(Action<string> scriptAction, bool export, bool throwOnError, TextWriter exportOutput,
150+
/// <summary>
151+
/// Run the drop schema script
152+
/// </summary>
153+
/// <param name="exportOutput"> if non-null, the ddl will be written to this TextWriter.</param>
154+
/// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
155+
/// <remarks>
156+
/// This is a convenience method that calls <see cref="Execute(Action&lt;string&gt;, bool, bool, TextWriter)"/> and sets
157+
/// the justDrop parameter to true.
158+
/// </remarks>
159+
public void Drop(TextWriter exportOutput, bool execute)
160+
{
161+
Execute(null, execute, true, exportOutput);
162+
}
163+
164+
private void Execute(Action<string> scriptAction, bool execute, bool throwOnError, TextWriter exportOutput,
128165
IDbCommand statement, string sql)
129166
{
130167
Initialize();
@@ -145,7 +182,7 @@ private void Execute(Action<string> scriptAction, bool export, bool throwOnError
145182
{
146183
exportOutput.WriteLine(formatted);
147184
}
148-
if (export)
185+
if (execute)
149186
{
150187
ExecuteSql(statement, sql);
151188
}
@@ -187,8 +224,8 @@ private void ExecuteSql(IDbCommand cmd, string sql)
187224
/// <summary>
188225
/// Executes the Export of the Schema in the given connection
189226
/// </summary>
190-
/// <param name="script"><see langword="true" /> if the ddl should be outputted in the Console.</param>
191-
/// <param name="export"><see langword="true" /> if the ddl should be executed against the Database.</param>
227+
/// <param name="useStdOut"><see langword="true" /> if the ddl should be outputted in the Console.</param>
228+
/// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
192229
/// <param name="justDrop"><see langword="true" /> if only the ddl to drop the Database objects should be executed.</param>
193230
/// <param name="connection">
194231
/// The connection to use when executing the commands when export is <see langword="true" />.
@@ -200,30 +237,30 @@ private void ExecuteSql(IDbCommand cmd, string sql)
200237
/// This overload is provided mainly to enable use of in memory databases.
201238
/// It does NOT close the given connection!
202239
/// </remarks>
203-
public void Execute(bool script, bool export, bool justDrop, IDbConnection connection,
240+
public void Execute(bool useStdOut, bool execute, bool justDrop, IDbConnection connection,
204241
TextWriter exportOutput)
205242
{
206-
if (script)
243+
if (useStdOut)
207244
{
208-
Execute(Console.WriteLine, export, justDrop, connection, exportOutput);
245+
Execute(Console.WriteLine, execute, justDrop, connection, exportOutput);
209246
}
210247
else
211248
{
212-
Execute(null, export, justDrop, connection, exportOutput);
249+
Execute(null, execute, justDrop, connection, exportOutput);
213250
}
214251
}
215252

216-
public void Execute(Action<string> scriptAction, bool export, bool justDrop, IDbConnection connection,
253+
public void Execute(Action<string> scriptAction, bool execute, bool justDrop, IDbConnection connection,
217254
TextWriter exportOutput)
218255
{
219256
Initialize();
220257
IDbCommand statement = null;
221258

222-
if (export && connection == null)
259+
if (execute && connection == null)
223260
{
224261
throw new ArgumentNullException("connection", "When export is set to true, you need to pass a non null connection");
225262
}
226-
if (export)
263+
if (execute)
227264
{
228265
statement = connection.CreateCommand();
229266
}
@@ -232,14 +269,14 @@ public void Execute(Action<string> scriptAction, bool export, bool justDrop, IDb
232269
{
233270
for (int i = 0; i < dropSQL.Length; i++)
234271
{
235-
Execute(scriptAction, export, false, exportOutput, statement, dropSQL[i]);
272+
Execute(scriptAction, execute, false, exportOutput, statement, dropSQL[i]);
236273
}
237274

238275
if (!justDrop)
239276
{
240277
for (int j = 0; j < createSQL.Length; j++)
241278
{
242-
Execute(scriptAction, export, true, exportOutput, statement, createSQL[j]);
279+
Execute(scriptAction, execute, true, exportOutput, statement, createSQL[j]);
243280
}
244281
}
245282
}
@@ -273,59 +310,66 @@ public void Execute(Action<string> scriptAction, bool export, bool justDrop, IDb
273310
/// <summary>
274311
/// Executes the Export of the Schema.
275312
/// </summary>
276-
/// <param name="script"><see langword="true" /> if the ddl should be outputted in the Console.</param>
277-
/// <param name="export"><see langword="true" /> if the ddl should be executed against the Database.</param>
313+
/// <param name="useStdOut"><see langword="true" /> if the ddl should be outputted in the Console.</param>
314+
/// <param name="execute"><see langword="true" /> if the ddl should be executed against the Database.</param>
278315
/// <param name="justDrop"><see langword="true" /> if only the ddl to drop the Database objects should be executed.</param>
279316
/// <remarks>
280317
/// This method allows for both the drop and create ddl script to be executed.
281318
/// </remarks>
282-
public void Execute(bool script, bool export, bool justDrop)
319+
public void Execute(bool useStdOut, bool execute, bool justDrop)
283320
{
284-
if (script)
321+
if (useStdOut)
285322
{
286-
Execute(Console.WriteLine, export, justDrop);
323+
Execute(Console.WriteLine, execute, justDrop);
287324
}
288325
else
289326
{
290-
Execute(null, export, justDrop);
327+
Execute(null, execute, justDrop);
291328
}
292329
}
293330

294-
public void Execute(Action<string> scriptAction, bool export, bool justDrop)
331+
332+
public void Execute(Action<string> scriptAction, bool execute, bool justDrop)
333+
{
334+
Execute(scriptAction, execute, justDrop, null);
335+
}
336+
337+
338+
public void Execute(Action<string> scriptAction, bool execute, bool justDrop, TextWriter exportOutput)
295339
{
296340
Initialize();
297341
IDbConnection connection = null;
298-
StreamWriter fileOutput = null;
342+
TextWriter fileOutput = exportOutput;
299343
IConnectionProvider connectionProvider = null;
300344

301-
var props = new Dictionary<string, string>();
302-
foreach (var de in dialect.DefaultProperties)
303-
{
304-
props[de.Key] = de.Value;
305-
}
306-
307-
if (configProperties != null)
308-
{
309-
foreach (var de in configProperties)
310-
{
311-
props[de.Key] = de.Value;
312-
}
313-
}
314-
315345
try
316346
{
317-
if (outputFile != null)
347+
if (fileOutput != null && outputFile != null)
318348
{
319349
fileOutput = new StreamWriter(outputFile);
320350
}
321351

322-
if (export)
352+
if (execute)
323353
{
354+
var props = new Dictionary<string, string>();
355+
foreach (var de in dialect.DefaultProperties)
356+
{
357+
props[de.Key] = de.Value;
358+
}
359+
360+
if (configProperties != null)
361+
{
362+
foreach (var de in configProperties)
363+
{
364+
props[de.Key] = de.Value;
365+
}
366+
}
367+
324368
connectionProvider = ConnectionProviderFactory.NewConnectionProvider(props);
325369
connection = connectionProvider.GetConnection();
326370
}
327371

328-
Execute(scriptAction, export, justDrop, connection, fileOutput);
372+
Execute(scriptAction, execute, justDrop, connection, fileOutput);
329373
}
330374
catch (HibernateException)
331375
{

src/NHibernate/Tool/hbm2ddl/SchemaUpdate.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Data.Common;
5-
65
using NHibernate.Cfg;
76
using NHibernate.Util;
87
using Environment=NHibernate.Cfg.Environment;
@@ -118,9 +117,9 @@ public static void Main(string[] args)
118117
/// <summary>
119118
/// Execute the schema updates
120119
/// </summary>
121-
public void Execute(bool script, bool doUpdate)
120+
public void Execute(bool useStdOut, bool doUpdate)
122121
{
123-
if (script)
122+
if (useStdOut)
124123
{
125124
Execute(Console.WriteLine, doUpdate);
126125
}

0 commit comments

Comments
 (0)