Skip to content

Commit d3536ac

Browse files
committed
Refactoring following discussion with hazzik
1 parent d84d01c commit d3536ac

File tree

11 files changed

+175
-57
lines changed

11 files changed

+175
-57
lines changed

src/NHibernate.Test/DialectTest/MsSql2005DialectFixture.cs

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,42 +220,114 @@ public void GetIfExistsDropConstraintTest_For_Schema_other_than_dbo()
220220

221221

222222
[Test]
223-
public void GetIfExistsDropConstraintTest_without_schema_with_schemName_overload()
223+
public void GetIfExistsDropConstraintTest_without_schema_with_schemaName_overload()
224224
{
225225
MsSql2005Dialect dialect = new MsSql2005Dialect();
226226
Table foo = new Table("Foo");
227227
string expected = "if exists (select 1 from sys.objects" +
228228
" where object_id = OBJECT_ID(N'[Bar]')" +
229229
" AND parent_object_id = OBJECT_ID('Foo'))";
230-
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar", null);
230+
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar", null, null);
231231
System.Console.WriteLine(ifExistsDropConstraint);
232232
Assert.AreEqual(expected, ifExistsDropConstraint);
233233
}
234234

235235
[Test]
236-
public void GetIfExistsDropConstraintTest_For_Schema_other_than_dbo_with_schemName_overload()
236+
public void GetIfExistsDropConstraintTest_For_Schema_other_than_dbo_with_schemaName_overload()
237237
{
238238
MsSql2005Dialect dialect = new MsSql2005Dialect();
239239
Table foo = new Table("Foo");
240240
foo.Schema = "Other";
241241
string expected = "if exists (select 1 from sys.objects" +
242242
" where object_id = OBJECT_ID(N'Other.[Bar]')" +
243243
" AND parent_object_id = OBJECT_ID('Other.Foo'))";
244-
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar", null);
244+
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar", null, null);
245245
System.Console.WriteLine(ifExistsDropConstraint);
246246
Assert.AreEqual(expected, ifExistsDropConstraint);
247247
}
248248

249249
[Test]
250-
public void GetIfExistsDropConstraintTest_For_Schema_other_than_dbo_AndInGlobalConfiguration_with_schemName_overload()
250+
public void GetIfExistsDropConstraintTest_For_Schema_other_than_dbo_AndInGlobalConfiguration_with_schemaName_overload()
251251
{
252252
MsSql2005Dialect dialect = new MsSql2005Dialect();
253253
Table foo = new Table("Foo");
254254
string defaultSchema = "Other";
255255
string expected = "if exists (select 1 from sys.objects" +
256256
" where object_id = OBJECT_ID(N'Other.[Bar]')" +
257257
" AND parent_object_id = OBJECT_ID('Other.Foo'))";
258-
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar", defaultSchema);
258+
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar", null, defaultSchema);
259+
System.Console.WriteLine(ifExistsDropConstraint);
260+
Assert.AreEqual(expected, ifExistsDropConstraint);
261+
}
262+
263+
[Test]
264+
public void GetIfExistsDropConstraintTest_For_Catalog_other_than_dbo_with_catalogName_overload()
265+
{
266+
MsSql2005Dialect dialect = new MsSql2005Dialect();
267+
Table foo = new Table("Foo");
268+
foo.Catalog = "Other";
269+
string expected = "if exists (select 1 from sys.objects" +
270+
" where object_id = OBJECT_ID(N'Other.[Bar]')" +
271+
" AND parent_object_id = OBJECT_ID('Other.Foo'))";
272+
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar", null, null);
273+
System.Console.WriteLine(ifExistsDropConstraint);
274+
Assert.AreEqual(expected, ifExistsDropConstraint);
275+
}
276+
277+
[Test]
278+
public void GetIfExistsDropConstraintTest_For_Schema_other_than_dbo_with_catalogName()
279+
{
280+
MsSql2005Dialect dialect = new MsSql2005Dialect();
281+
Table foo = new Table("Foo");
282+
foo.Schema = "Schema";
283+
foo.Catalog = "Catalog";
284+
string expected = "if exists (select 1 from sys.objects" +
285+
" where object_id = OBJECT_ID(N'Catalog.Schema.[Bar]')" +
286+
" AND parent_object_id = OBJECT_ID('Catalog.Schema.Foo'))";
287+
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar", null, null);
288+
System.Console.WriteLine(ifExistsDropConstraint);
289+
Assert.AreEqual(expected, ifExistsDropConstraint);
290+
}
291+
292+
[Test]
293+
public void GetIfExistsDropConstraintTest_For_TableQuoted_SchemaQuoted_CatalogQuoted()
294+
{
295+
MsSql2005Dialect dialect = new MsSql2005Dialect();
296+
Table foo = new Table("`Foo`");
297+
foo.Schema = "`Schema`";
298+
foo.Catalog = "`Catalog`";
299+
string expected = "if exists (select 1 from sys.objects" +
300+
" where object_id = OBJECT_ID(N'[Catalog].[Schema].[Bar]')" +
301+
" AND parent_object_id = OBJECT_ID('[Catalog].[Schema].[Foo]'))";
302+
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar", null, null);
303+
System.Console.WriteLine(ifExistsDropConstraint);
304+
Assert.AreEqual(expected, ifExistsDropConstraint);
305+
}
306+
307+
[Test]
308+
public void GetIfExistsDropConstraintTest_For_TableNotQuoted_SchemaNotQuoted_CatalogNotQuoted()
309+
{
310+
MsSql2005Dialect dialect = new MsSql2005Dialect();
311+
Table foo = new Table("Foo");
312+
foo.Schema = "Schema";
313+
foo.Catalog = "Catalog";
314+
string expected = "if exists (select 1 from sys.objects" +
315+
" where object_id = OBJECT_ID(N'Catalog.Schema.[Bar]')" +
316+
" AND parent_object_id = OBJECT_ID('Catalog.Schema.Foo'))";
317+
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar", null, null);
318+
System.Console.WriteLine(ifExistsDropConstraint);
319+
Assert.AreEqual(expected, ifExistsDropConstraint);
320+
}
321+
322+
[Test]
323+
public void GetIfExistsDropConstraintTest_For_GlobalSchemaQuoted_other_than_dbo_with_GlocalCatalogQuotedName()
324+
{
325+
MsSql2005Dialect dialect = new MsSql2005Dialect();
326+
Table foo = new Table("Foo");
327+
string expected = "if exists (select 1 from sys.objects" +
328+
" where object_id = OBJECT_ID(N'Catalog.Schema.[Bar]')" +
329+
" AND parent_object_id = OBJECT_ID('Catalog.Schema.Foo'))";
330+
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar", "Catalog", "Schema");
259331
System.Console.WriteLine(ifExistsDropConstraint);
260332
Assert.AreEqual(expected, ifExistsDropConstraint);
261333
}

src/NHibernate/Cfg/Mappings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ private string GetLogicalTableName(string schema, string catalog, string physica
630630

631631
public string GetLogicalTableName(Table table)
632632
{
633-
return GetLogicalTableName(table.GetQuotedSchema(), table.Catalog, table.GetQuotedName());
633+
return GetLogicalTableName(table.GetQuotedSchema(), table.GetQuotedCatalog(), table.GetQuotedName());
634634
}
635635

636636
public ResultSetMappingDefinition GetResultSetMapping(string name)

src/NHibernate/Dialect/Dialect.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ public virtual string GetDropForeignKeyConstraintString(string constraintName)
721721
[Obsolete("Can cause issues when a custom schema is defined(https://nhibernate.jira.com/browse/NH-1285). The new overload with the defaultSchema parameter should be used instead")]
722722
public virtual string GetIfNotExistsCreateConstraint(Table table, string name)
723723
{
724-
return GetIfNotExistsCreateConstraint(table, name, null);
724+
return GetIfNotExistsCreateConstraint(table, name, null, null);
725725
}
726726

727727
/// <summary>
@@ -734,7 +734,7 @@ public virtual string GetIfNotExistsCreateConstraint(Table table, string name)
734734
[Obsolete("Can cause issues when a custom schema is defined(https://nhibernate.jira.com/browse/NH-1285). The new overload with the defaultSchema parameter should be used instead")]
735735
public virtual string GetIfNotExistsCreateConstraintEnd(Table table, string name)
736736
{
737-
return GetIfNotExistsCreateConstraintEnd(table, name, null);
737+
return GetIfNotExistsCreateConstraintEnd(table, name, null, null);
738738
}
739739

740740
/// <summary>
@@ -746,7 +746,7 @@ public virtual string GetIfNotExistsCreateConstraintEnd(Table table, string name
746746
[Obsolete("Can cause issues when a custom schema is defined(https://nhibernate.jira.com/browse/NH-1285). The new overload with the defaultSchema parameter should be used instead")]
747747
public virtual string GetIfExistsDropConstraint(Table table, string name)
748748
{
749-
return GetIfExistsDropConstraint(table, name, null);
749+
return GetIfExistsDropConstraint(table, name, null, null);
750750
}
751751

752752
/// <summary>
@@ -759,7 +759,7 @@ public virtual string GetIfExistsDropConstraint(Table table, string name)
759759
[Obsolete("Can cause issues when a custom schema is defined(https://nhibernate.jira.com/browse/NH-1285). The new overload with the defaultSchema parameter should be used instead")]
760760
public virtual string GetIfExistsDropConstraintEnd(Table table, string name)
761761
{
762-
return GetIfExistsDropConstraintEnd(table, name, null);
762+
return GetIfExistsDropConstraintEnd(table, name, null, null);
763763
}
764764

765765
/// <summary>
@@ -769,7 +769,7 @@ public virtual string GetIfExistsDropConstraintEnd(Table table, string name)
769769
/// <param name="name">The name.</param>
770770
/// <param name="defaultSchema">The defaultSchema.</param>
771771
/// <returns></returns>
772-
public virtual string GetIfNotExistsCreateConstraint(Table table, string name, string defaultSchema)
772+
public virtual string GetIfNotExistsCreateConstraint(Table table, string name, string defaultCatalog, string defaultSchema)
773773
{
774774
return "";
775775
}
@@ -782,7 +782,7 @@ public virtual string GetIfNotExistsCreateConstraint(Table table, string name, s
782782
/// <param name="name">The name.</param>
783783
/// <param name="defaultSchema">The defaultSchema.</param>
784784
/// <returns></returns>
785-
public virtual string GetIfNotExistsCreateConstraintEnd(Table table, string name, string defaultSchema)
785+
public virtual string GetIfNotExistsCreateConstraintEnd(Table table, string name, string defaultCatalog, string defaultSchema)
786786
{
787787
return "";
788788
}
@@ -794,7 +794,7 @@ public virtual string GetIfNotExistsCreateConstraintEnd(Table table, string name
794794
/// <param name="name">The name.</param>
795795
/// <param name="defaultSchema">The defaultSchema.</param>
796796
/// <returns></returns>
797-
public virtual string GetIfExistsDropConstraint(Table table, string name, string defaultSchema)
797+
public virtual string GetIfExistsDropConstraint(Table table, string name, string defaultCatalog, string defaultSchema)
798798
{
799799
return "";
800800
}
@@ -807,7 +807,7 @@ public virtual string GetIfExistsDropConstraint(Table table, string name, string
807807
/// <param name="name">The name.</param>
808808
/// <param name="defaultSchema">The defaultSchema.</param>
809809
/// <returns></returns>
810-
public virtual string GetIfExistsDropConstraintEnd(Table table, string name, string defaultSchema)
810+
public virtual string GetIfExistsDropConstraintEnd(Table table, string name, string defaultCatalog, string defaultSchema)
811811
{
812812
return "";
813813
}

src/NHibernate/Dialect/MsSql2000Dialect.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -442,37 +442,22 @@ public override long TimestampResolutionInTicks
442442
}
443443
}
444444

445-
public override string GetIfExistsDropConstraint(Table table, string name)
445+
public override string GetIfExistsDropConstraint(Table table, string name, string defaultCatalog, string defaultSchema)
446446
{
447-
return GetIfExistsDropConstraint(table, name, null);
448-
}
449-
450-
protected virtual string GetSelectExistingObject(string name, Table table)
451-
{
452-
return GetSelectExistingObject(name, table, null);
453-
}
454-
455-
public override string GetIfNotExistsCreateConstraint(Table table, string name)
456-
{
457-
return GetIfNotExistsCreateConstraint(table, name, null);
458-
}
459-
460-
public override string GetIfExistsDropConstraint(Table table, string name, string defaultSchema)
461-
{
462-
string selectExistingObject = GetSelectExistingObject(name, table, defaultSchema);
447+
string selectExistingObject = GetSelectExistingObject(name, table, defaultCatalog, defaultSchema);
463448
return string.Format(@"if exists ({0})", selectExistingObject);
464449
}
465450

466-
protected virtual string GetSelectExistingObject(string name, Table table, string defaultSchema)
451+
protected virtual string GetSelectExistingObject(string name, Table table, string defaultCatalog, string defaultSchema)
467452
{
468453
string objName = table.GetQuotedSchemaName(this) + Quote(name);
469454
return string.Format("select 1 from sysobjects where id = OBJECT_ID(N'{0}') AND parent_obj = OBJECT_ID('{1}')",
470455
objName, table.GetQuotedName(this));
471456
}
472457

473-
public override string GetIfNotExistsCreateConstraint(Table table, string name, string defaultSchema)
458+
public override string GetIfNotExistsCreateConstraint(Table table, string name, string defaultCatalog, string defaultSchema)
474459
{
475-
string selectExistingObject = GetSelectExistingObject(name, table, defaultSchema);
460+
string selectExistingObject = GetSelectExistingObject(name, table, defaultCatalog, defaultSchema);
476461
return string.Format(@"if not exists ({0})", selectExistingObject);
477462
}
478463

src/NHibernate/Dialect/MsSql2005Dialect.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,22 @@ public override bool SupportsVariableLimit
6363
get { return true; }
6464
}
6565

66-
protected override string GetSelectExistingObject(string name, Table table, string defaultSchema)
66+
protected override string GetSelectExistingObject(string name, Table table, string defaultCatalog, string defaultSchema)
6767
{
68+
// catalog defined on table takes precedence over defaultSchema
69+
string catalog = table.Catalog == null ? defaultCatalog : table.GetQuotedCatalog(this);
70+
if (catalog != null)
71+
{
72+
catalog += ".";
73+
}
6874
// schema defined on table takes precedence over defaultSchema
69-
string schema = table.Schema == null ? defaultSchema : table.GetQuotedSchemaName(this);
75+
string schema = table.Schema == null ? defaultSchema : table.GetQuotedSchema(this);
7076
if (schema != null)
7177
{
7278
schema += ".";
7379
}
74-
string objName = string.Format("{0}{1}", schema, Quote(name));
75-
string parentName = string.Format("{0}{1}", schema, table.GetQuotedName(this));
80+
string objName = string.Format("{0}{1}{2}", catalog, schema, Quote(name)); // always quote the existing object
81+
string parentName = string.Format("{0}{1}{2}", catalog, schema, table.GetQuotedName(this));
7682
return
7783
string.Format(
7884
"select 1 from sys.objects where object_id = OBJECT_ID(N'{0}') AND parent_object_id = OBJECT_ID('{1}')", objName,

src/NHibernate/Mapping/Constraint.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public int ColumnSpan
7171

7272
public IList<Column> Columns
7373
{
74-
get{return columns;}
74+
get { return columns; }
7575
}
7676

7777
/// <summary>
@@ -101,10 +101,10 @@ public virtual string SqlDropString(Dialect.Dialect dialect, string defaultCatal
101101
{
102102
if (IsGenerated(dialect))
103103
{
104-
string ifExists = dialect.GetIfExistsDropConstraint(Table, Name, defaultSchema);
105-
string drop =
106-
string.Format("alter table {0} drop constraint {1}", Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema), Name);
107-
string end = dialect.GetIfExistsDropConstraintEnd(Table, Name, defaultSchema);
104+
var tableName = Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema);
105+
string ifExists = dialect.GetIfExistsDropConstraint(Table, Name, defaultCatalog, defaultSchema);
106+
string drop = string.Format("alter table {0} drop constraint {1}", Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema), Name);
107+
string end = dialect.GetIfExistsDropConstraintEnd(Table, tableName, defaultCatalog, defaultSchema);
108108

109109
return ifExists + System.Environment.NewLine + drop + System.Environment.NewLine + end;
110110
}

src/NHibernate/Mapping/ForeignKey.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ public bool CascadeDeleteEnabled
8686
/// </returns>
8787
public override string SqlDropString(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
8888
{
89-
string ifExists = dialect.GetIfExistsDropConstraint(Table, Name, defaultSchema);
90-
string drop = string.Format("alter table {0} {1}", Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema),
91-
dialect.GetDropForeignKeyConstraintString(Name));
92-
string end = dialect.GetIfExistsDropConstraintEnd(Table, Name, defaultSchema);
89+
var tableName = Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema);
90+
string ifExists = dialect.GetIfExistsDropConstraint(Table, Name, defaultCatalog, defaultSchema);
91+
string drop = string.Format("alter table {0} {1}", Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema), dialect.GetDropForeignKeyConstraintString(Name));
92+
string end = dialect.GetIfExistsDropConstraintEnd(Table, Name, defaultCatalog, defaultSchema);
9393
return ifExists + System.Environment.NewLine + drop + System.Environment.NewLine + end;
9494
}
9595

@@ -178,7 +178,7 @@ public override string ToString()
178178
result.Append(GetType().FullName)
179179
.Append('(')
180180
.Append(Table.Name)
181-
.Append(StringHelper.Join(", " , Columns))
181+
.Append(StringHelper.Join(", ", Columns))
182182
.Append(" ref-columns:")
183183
.Append('(')
184184
.Append(StringHelper.Join(", ", ReferencedColumns))

src/NHibernate/Mapping/Index.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public static string BuildSqlCreateIndexString(Dialect.Dialect dialect, string n
4444

4545
public static string BuildSqlDropIndexString(Dialect.Dialect dialect, Table table, string name, string defaultCatalog, string defaultSchema)
4646
{
47-
string ifExists = dialect.GetIfExistsDropConstraint(table, name, defaultSchema);
47+
string ifExists = dialect.GetIfExistsDropConstraint(table, name, defaultCatalog, defaultSchema);
4848
string drop = string.Format("drop index {0}", StringHelper.Qualify(table.GetQualifiedName(dialect, defaultCatalog, defaultSchema), name));
49-
string end = dialect.GetIfExistsDropConstraintEnd(table, name, defaultSchema);
49+
string end = dialect.GetIfExistsDropConstraintEnd(table, name, defaultCatalog, defaultSchema);
5050
return ifExists + Environment.NewLine + drop + Environment.NewLine + end;
5151
}
5252

src/NHibernate/Mapping/PrimaryKey.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ public override string SqlConstraintString(Dialect.Dialect d, string constraintN
7575
/// </returns>
7676
public override string SqlDropString(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
7777
{
78-
string ifExists = dialect.GetIfExistsDropConstraint(Table, Name, defaultSchema);
78+
var tableName = Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema);
79+
string ifExists = dialect.GetIfExistsDropConstraint(Table, Name, defaultCatalog, defaultSchema);
7980
string drop = string.Format("alter table {0}{1}", Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema), dialect.GetDropPrimaryKeyConstraintString(Name));
80-
string end = dialect.GetIfExistsDropConstraintEnd(Table, Name, defaultSchema);
81+
string end = dialect.GetIfExistsDropConstraintEnd(Table, Name, defaultCatalog, defaultSchema);
8182
return ifExists + Environment.NewLine + drop + Environment.NewLine + end;
8283
}
8384

0 commit comments

Comments
 (0)