Skip to content

Commit 029d231

Browse files
committed
Refactoring of SchemaAction matters
SVN: trunk@3959
1 parent 5914cbd commit 029d231

File tree

6 files changed

+130
-122
lines changed

6 files changed

+130
-122
lines changed

src/NHibernate/Cfg/Configuration.cs

+17-12
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,11 @@ public string[] GenerateDropSchemaScript(Dialect.Dialect dialect)
671671
{
672672
foreach (var table in TableMappings)
673673
{
674-
if (table.IsPhysicalTable && table.SchemaDrop)
674+
if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Drop))
675675
{
676676
foreach (var fk in table.ForeignKeyIterator)
677677
{
678-
if (fk.HasPhysicalConstraint && fk.ReferencedTable.SchemaDrop)
678+
if (fk.HasPhysicalConstraint && IncludeAction(fk.ReferencedTable.SchemaActions, SchemaAction.Drop))
679679
{
680680
script.Add(fk.SqlDropString(dialect, defaultCatalog, defaultSchema));
681681
}
@@ -686,7 +686,7 @@ public string[] GenerateDropSchemaScript(Dialect.Dialect dialect)
686686

687687
foreach (var table in TableMappings)
688688
{
689-
if (table.IsPhysicalTable && table.SchemaDrop)
689+
if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Drop))
690690
{
691691
script.Add(table.SqlDropString(dialect, defaultCatalog, defaultSchema));
692692
}
@@ -708,6 +708,11 @@ public string[] GenerateDropSchemaScript(Dialect.Dialect dialect)
708708
return script.ToArray();
709709
}
710710

711+
public static bool IncludeAction(SchemaAction actionsSource, SchemaAction includedAction)
712+
{
713+
return (actionsSource & includedAction) != SchemaAction.None;
714+
}
715+
711716
/// <summary>
712717
/// Generate DDL for creating tables
713718
/// </summary>
@@ -723,7 +728,7 @@ public string[] GenerateSchemaCreationScript(Dialect.Dialect dialect)
723728

724729
foreach (var table in TableMappings)
725730
{
726-
if (table.IsPhysicalTable && table.SchemaExport)
731+
if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Export))
727732
{
728733
script.Add(table.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
729734
script.AddRange(table.SqlCommentStrings(dialect, defaultCatalog, defaultSchema));
@@ -732,7 +737,7 @@ public string[] GenerateSchemaCreationScript(Dialect.Dialect dialect)
732737

733738
foreach (var table in TableMappings)
734739
{
735-
if (table.IsPhysicalTable && table.SchemaExport)
740+
if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Export))
736741
{
737742
if (!dialect.SupportsUniqueConstraintInCreateAlterTable)
738743
{
@@ -755,7 +760,7 @@ public string[] GenerateSchemaCreationScript(Dialect.Dialect dialect)
755760
{
756761
foreach (var fk in table.ForeignKeyIterator)
757762
{
758-
if (fk.HasPhysicalConstraint && fk.ReferencedTable.SchemaExport)
763+
if (fk.HasPhysicalConstraint && IncludeAction(fk.ReferencedTable.SchemaActions, SchemaAction.Export))
759764
{
760765
script.Add(fk.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
761766
}
@@ -1920,7 +1925,7 @@ public string[] GenerateSchemaUpdateScript(Dialect.Dialect dialect, DatabaseMeta
19201925
var script = new List<string>(50);
19211926
foreach (var table in TableMappings)
19221927
{
1923-
if (table.IsPhysicalTable && table.SchemaUpdate)
1928+
if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Update))
19241929
{
19251930
ITableMetadata tableInfo = databaseMetadata.GetTableMetadata(table.Name, table.Schema ?? defaultSchema,
19261931
table.Catalog ?? defaultCatalog, table.IsQuoted);
@@ -1941,7 +1946,7 @@ public string[] GenerateSchemaUpdateScript(Dialect.Dialect dialect, DatabaseMeta
19411946

19421947
foreach (var table in TableMappings)
19431948
{
1944-
if (table.IsPhysicalTable && table.SchemaUpdate)
1949+
if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Update))
19451950
{
19461951
ITableMetadata tableInfo = databaseMetadata.GetTableMetadata(table.Name, table.Schema, table.Catalog,
19471952
table.IsQuoted);
@@ -1950,7 +1955,7 @@ public string[] GenerateSchemaUpdateScript(Dialect.Dialect dialect, DatabaseMeta
19501955
{
19511956
foreach (var fk in table.ForeignKeyIterator)
19521957
{
1953-
if (fk.HasPhysicalConstraint && fk.ReferencedTable.SchemaUpdate)
1958+
if (fk.HasPhysicalConstraint && IncludeAction(fk.ReferencedTable.SchemaActions, SchemaAction.Update))
19541959
{
19551960
bool create = tableInfo == null
19561961
||
@@ -1997,10 +2002,10 @@ public void ValidateSchema(Dialect.Dialect dialect, DatabaseMetadata databaseMet
19972002
string defaultCatalog = PropertiesHelper.GetString(Environment.DefaultCatalog, properties, null);
19982003
string defaultSchema = PropertiesHelper.GetString(Environment.DefaultSchema, properties, null);
19992004

2000-
var iter = this.TableMappings;
2005+
var iter = TableMappings;
20012006
foreach (var table in iter)
20022007
{
2003-
if (table.IsPhysicalTable && table.SchemaValidate)
2008+
if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Validate))
20042009
{
20052010
/*NH Different Implementation :
20062011
TableMetadata tableInfo = databaseMetadata.getTableMetadata(
@@ -2011,7 +2016,7 @@ public void ValidateSchema(Dialect.Dialect dialect, DatabaseMetadata databaseMet
20112016
ITableMetadata tableInfo = databaseMetadata.GetTableMetadata(
20122017
table.Name,
20132018
table.Schema??defaultSchema,
2014-
table.Catalog,//??defaultCatalog,
2019+
table.Catalog??defaultCatalog,
20152020
table.IsQuoted);
20162021
if (tableInfo == null)
20172022
throw new HibernateException("Missing table: " + table.Name);

src/NHibernate/Cfg/Mappings.cs

+40-6
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,7 @@ public Table AddTable(string schema, string catalog, string name, string subsele
258258
table.Schema = schema;
259259
table.Catalog = catalog;
260260
table.Subselect = subselect;
261-
table.SchemaDrop = SchemaActionRequested(schemaAction, "drop");
262-
table.SchemaUpdate = SchemaActionRequested(schemaAction, "update");
263-
table.SchemaExport = SchemaActionRequested(schemaAction, "export");
264-
table.SchemaValidate = SchemaActionRequested(schemaAction, "validate");
261+
table.SchemaActions = GetSchemaActions(schemaAction);
265262
tables[key] = table;
266263
}
267264
else
@@ -273,9 +270,46 @@ public Table AddTable(string schema, string catalog, string name, string subsele
273270
return table;
274271
}
275272

276-
private static bool SchemaActionRequested(string schemaAction, string check)
273+
private static SchemaAction GetSchemaActions(string schemaAction)
277274
{
278-
return string.IsNullOrEmpty(schemaAction) || schemaAction.Contains("all") || schemaAction.Contains(check);
275+
if (string.IsNullOrEmpty(schemaAction))
276+
{
277+
return SchemaAction.All;
278+
}
279+
else
280+
{
281+
SchemaAction sa = SchemaAction.None;
282+
string[] acts = schemaAction.Split(new[] {',', ' '});
283+
foreach (var s in acts)
284+
{
285+
switch (s.ToLowerInvariant())
286+
{
287+
case "":
288+
case "all":
289+
sa |= SchemaAction.All;
290+
break;
291+
case "drop":
292+
sa |= SchemaAction.Drop;
293+
break;
294+
case "update":
295+
sa |= SchemaAction.Update;
296+
break;
297+
case "export":
298+
sa |= SchemaAction.Export;
299+
break;
300+
case "validate":
301+
sa |= SchemaAction.Validate;
302+
break;
303+
case "none":
304+
sa |= SchemaAction.None;
305+
break;
306+
default:
307+
throw new MappingException(
308+
string.Format("Invalid schema-export value; Expected(all drop update export validate none), Found ({0})", s));
309+
}
310+
}
311+
return sa;
312+
}
279313
}
280314

281315
public Table AddDenormalizedTable(string schema, string catalog, string name, bool isAbstract, string subselect, Table includedTable)

src/NHibernate/Mapping/Table.cs

+15-25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99

1010
namespace NHibernate.Mapping
1111
{
12+
[Flags]
13+
public enum SchemaAction
14+
{
15+
None = 0,
16+
Drop = 1,
17+
Update= 2,
18+
Export= 4,
19+
Validate= 8,
20+
All = Drop | Update | Export | Validate
21+
}
22+
1223
/// <summary>
1324
/// Represents a Table in a database that an object gets mapped against.
1425
/// </summary>
@@ -84,10 +95,7 @@ public int GetHashCode(ForeignKeyKey obj)
8495
private string subselect;
8596
private string rowId;
8697
private bool isSchemaQuoted;
87-
private bool schemaDrop = true;
88-
private bool schemaUpdate = true;
89-
private bool schemaExport = true;
90-
private bool schemaValidate = true;
98+
private SchemaAction schemaActions = SchemaAction.All;
9199

92100

93101
/// <summary>
@@ -924,28 +932,10 @@ public bool IsPhysicalTable
924932
get { return !IsSubselect && !IsAbstractUnionTable; }
925933
}
926934

927-
public bool SchemaDrop
928-
{
929-
get { return schemaDrop; }
930-
set { schemaDrop = value; }
931-
}
932-
933-
public bool SchemaUpdate
934-
{
935-
get { return schemaUpdate; }
936-
set { schemaUpdate = value; }
937-
}
938-
939-
public bool SchemaExport
940-
{
941-
get { return schemaExport; }
942-
set { schemaExport = value; }
943-
}
944-
945-
public bool SchemaValidate
935+
public SchemaAction SchemaActions
946936
{
947-
get { return schemaValidate; }
948-
set { schemaValidate = value; }
937+
get { return schemaActions; }
938+
set { schemaActions = value; }
949939
}
950940

951941
public string RowId

src/NHibernate/Tool/hbm2ddl/SchemaExport.cs

+17-23
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,19 @@ namespace NHibernate.Tool.hbm2ddl
1919
/// </remarks>
2020
public class SchemaExport
2121
{
22-
private readonly string[] dropSQL;
23-
private readonly string[] createSQL;
22+
private static readonly ILog log = LogManager.GetLogger(typeof (SchemaExport));
2423
private readonly IDictionary<string, string> connectionProperties;
25-
private string outputFile = null;
24+
private readonly string[] createSQL;
2625
private readonly Dialect.Dialect dialect;
27-
private string delimiter = null;
28-
29-
private static readonly ILog log = LogManager.GetLogger(typeof(SchemaExport));
26+
private readonly string[] dropSQL;
27+
private string delimiter;
28+
private string outputFile;
3029

3130
/// <summary>
3231
/// Create a schema exported for a given Configuration
3332
/// </summary>
3433
/// <param name="cfg">The NHibernate Configuration to generate the schema from.</param>
35-
public SchemaExport(Configuration cfg)
36-
: this(cfg, cfg.Properties)
37-
{
38-
}
34+
public SchemaExport(Configuration cfg) : this(cfg, cfg.Properties) {}
3935

4036
/// <summary>
4137
/// Create a schema exporter for the given Configuration, with the given
@@ -107,7 +103,7 @@ public void Drop(bool script, bool export)
107103
}
108104

109105
private void Execute(Action<string> scriptAction, bool export, bool format, bool throwOnError, TextWriter exportOutput,
110-
IDbCommand statement, string sql)
106+
IDbCommand statement, string sql)
111107
{
112108
try
113109
{
@@ -169,8 +165,8 @@ private void Execute(Action<string> scriptAction, bool export, bool format, bool
169165
/// This overload is provided mainly to enable use of in memory databases.
170166
/// It does NOT close the given connection!
171167
/// </remarks>
172-
public void Execute(bool script, bool export, bool justDrop, bool format,
173-
IDbConnection connection, TextWriter exportOutput)
168+
public void Execute(bool script, bool export, bool justDrop, bool format, IDbConnection connection,
169+
TextWriter exportOutput)
174170
{
175171
if (script)
176172
{
@@ -182,8 +178,8 @@ public void Execute(bool script, bool export, bool justDrop, bool format,
182178
}
183179
}
184180

185-
public void Execute(Action<string> scriptAction, bool export, bool justDrop, bool format,
186-
IDbConnection connection, TextWriter exportOutput)
181+
public void Execute(Action<string> scriptAction, bool export, bool justDrop, bool format, IDbConnection connection,
182+
TextWriter exportOutput)
187183
{
188184
IDbCommand statement = null;
189185

@@ -236,10 +232,8 @@ public void Execute(Action<string> scriptAction, bool export, bool justDrop, boo
236232
}
237233
}
238234
}
239-
240235
}
241236

242-
243237
/// <summary>
244238
/// Executes the Export of the Schema.
245239
/// </summary>
@@ -261,21 +255,22 @@ public void Execute(bool script, bool export, bool justDrop, bool format)
261255
Execute(null, export, justDrop, format);
262256
}
263257
}
258+
264259
public void Execute(Action<string> scriptAction, bool export, bool justDrop, bool format)
265260
{
266261
IDbConnection connection = null;
267262
StreamWriter fileOutput = null;
268263
IConnectionProvider connectionProvider = null;
269264

270-
Dictionary<string, string> props = new Dictionary<string, string>();
271-
foreach (KeyValuePair<string, string> de in dialect.DefaultProperties)
265+
var props = new Dictionary<string, string>();
266+
foreach (var de in dialect.DefaultProperties)
272267
{
273268
props[de.Key] = de.Value;
274269
}
275270

276271
if (connectionProperties != null)
277272
{
278-
foreach (KeyValuePair<string, string> de in connectionProperties)
273+
foreach (var de in connectionProperties)
279274
{
280275
props[de.Key] = de.Value;
281276
}
@@ -314,7 +309,6 @@ public void Execute(Action<string> scriptAction, bool export, bool justDrop, boo
314309
connectionProvider.Dispose();
315310
}
316311
}
317-
318312
}
319313

320314
/// <summary>
@@ -350,8 +344,8 @@ private static string Format(string sql)
350344

351345
if (StringHelper.StartsWithCaseInsensitive(sql, "create table"))
352346
{
353-
StringBuilder result = new StringBuilder(60);
354-
StringTokenizer tokens = new StringTokenizer(sql, "(,)", true);
347+
var result = new StringBuilder(60);
348+
var tokens = new StringTokenizer(sql, "(,)", true);
355349

356350
int depth = 0;
357351

0 commit comments

Comments
 (0)