Skip to content

Commit f5aa820

Browse files
lnufredericDelaporte
authored andcommitted
NH-1285 - Drop/Create script with default_schema/default_catalog (SqlServer) (#448)
1 parent 881a932 commit f5aa820

File tree

14 files changed

+575
-230
lines changed

14 files changed

+575
-230
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH2288/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static void CheckDialect(Configuration configuration)
3535
var sb = new StringBuilder(500);
3636
await (su.ExecuteAsync(x => sb.AppendLine(x), false, false, cancellationToken));
3737
string script = sb.ToString();
38-
Assert.That(script, Does.Contain("if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'dbo.[Aclasses_Id_FK]') AND parent_object_id = OBJECT_ID('dbo.Aclass'))"));
38+
Assert.That(script, Does.Contain("if exists (select 1 from nhibernate.sys.objects where object_id = OBJECT_ID(N'nhibernate.dbo.[Aclasses_Id_FK]') and parent_object_id = OBJECT_ID(N'nhibernate.dbo.Aclass'))"));
3939
}
4040

4141
[Test]

src/NHibernate.Test/DialectTest/MsSql2005DialectFixture.cs

Lines changed: 135 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void GetLimitString()
1717
{
1818
var d = new MsSql2005Dialect();
1919

20-
SqlString str = d.GetLimitString(new SqlString("select distinct c.Contact_Id as Contact1_19_0_, c.Rating as Rating2_19_0_, c.Last_Name as Last_Name3_19_0, c.First_Name as First_Name4_19_0 from dbo.Contact c where COALESCE(c.Rating, 0) > 0 order by c.Rating desc , c.Last_Name , c.First_Name"), new SqlString("111"), new SqlString("222"));
20+
var str = d.GetLimitString(new SqlString("select distinct c.Contact_Id as Contact1_19_0_, c.Rating as Rating2_19_0_, c.Last_Name as Last_Name3_19_0, c.First_Name as First_Name4_19_0 from dbo.Contact c where COALESCE(c.Rating, 0) > 0 order by c.Rating desc , c.Last_Name , c.First_Name"), new SqlString("111"), new SqlString("222"));
2121
Assert.AreEqual(
2222
"SELECT TOP (222) Contact1_19_0_, Rating2_19_0_, Last_Name3_19_0, First_Name4_19_0 FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY q_.Rating2_19_0_ DESC, q_.Last_Name3_19_0, q_.First_Name4_19_0) as __hibernate_sort_row FROM (select distinct c.Contact_Id as Contact1_19_0_, c.Rating as Rating2_19_0_, c.Last_Name as Last_Name3_19_0, c.First_Name as First_Name4_19_0 from dbo.Contact c where COALESCE(c.Rating, 0) > 0) as q_) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row",
2323
str.ToString());
@@ -74,19 +74,19 @@ public void OnlyOffsetLimit()
7474
{
7575
var d = new MsSql2005Dialect();
7676

77-
SqlString str = d.GetLimitString(new SqlString("select distinct c.Contact_Id as Contact1_19_0_, c._Rating as Rating2_19_0_ from dbo.Contact c where COALESCE(c.Rating, 0) > 0 order by c.Rating desc , c.Last_Name , c.First_Name"), null, new SqlString("10"));
77+
var str = d.GetLimitString(new SqlString("select distinct c.Contact_Id as Contact1_19_0_, c._Rating as Rating2_19_0_ from dbo.Contact c where COALESCE(c.Rating, 0) > 0 order by c.Rating desc , c.Last_Name , c.First_Name"), null, new SqlString("10"));
7878
Assert.That(str.ToString(), Is.EqualTo("select distinct TOP (10) c.Contact_Id as Contact1_19_0_, c._Rating as Rating2_19_0_ from dbo.Contact c where COALESCE(c.Rating, 0) > 0 order by c.Rating desc , c.Last_Name , c.First_Name"));
7979
}
8080

8181
[Test]
8282
public void NH1187()
8383
{
84-
MsSql2005Dialect d = new MsSql2005Dialect();
85-
SqlString result = d.GetLimitString(new SqlString("select concat(a.Description,', ', a.Description) as desc from Animal a"), new SqlString("111"), new SqlString("222"));
84+
var d = new MsSql2005Dialect();
85+
var result = d.GetLimitString(new SqlString("select concat(a.Description,', ', a.Description) as desc from Animal a"), new SqlString("111"), new SqlString("222"));
8686
Assert.AreEqual("SELECT TOP (222) desc FROM (select concat(a.Description,', ', a.Description) as desc, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row from Animal a) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", result.ToString());
8787

8888
// The test use the function "cast" because cast need the keyWork "as" too
89-
SqlString str = d.GetLimitString(new SqlString("SELECT fish.id, cast('astring, with,comma' as string) as bar FROM fish"), new SqlString("111"), new SqlString("222"));
89+
var str = d.GetLimitString(new SqlString("SELECT fish.id, cast('astring, with,comma' as string) as bar FROM fish"), new SqlString("111"), new SqlString("222"));
9090
Assert.AreEqual(
9191
"SELECT TOP (222) id, bar FROM (SELECT fish.id, cast('astring, with,comma' as string) as bar, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM fish) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row",
9292
str.ToString());
@@ -97,7 +97,7 @@ public void NH2809()
9797
{
9898
var d = new MsSql2005Dialect();
9999

100-
string t = d.GetTypeName(new BinarySqlType());
100+
var t = d.GetTypeName(new BinarySqlType());
101101
Assert.That(t, Is.EqualTo("VARBINARY(MAX)"));
102102

103103
t = d.GetTypeName(new BinarySqlType(), SqlClientDriver.MaxSizeForLengthLimitedBinary - 1, 0, 0);
@@ -113,18 +113,18 @@ public void NH2809()
113113
[Test]
114114
public void QuotedAndParenthesisStringTokenizerTests_WithComma_InQuotes()
115115
{
116-
MsSql2005Dialect.QuotedAndParenthesisStringTokenizer tokenizier =
117-
new MsSql2005Dialect.QuotedAndParenthesisStringTokenizer(
116+
var tokenizier =
117+
new Dialect.Dialect.QuotedAndParenthesisStringTokenizer(
118118
new SqlString("select concat(a.Description,', ', a.Description) from Animal a"));
119-
string[] expected = new string[]
119+
var expected = new string[]
120120
{
121121
"select",
122122
"concat(a.Description,', ', a.Description)",
123123
"from",
124124
"Animal",
125125
"a"
126126
};
127-
int current = 0;
127+
var current = 0;
128128
foreach (SqlString token in tokenizier)
129129
{
130130
Assert.AreEqual(expected[current], token.ToString());
@@ -136,10 +136,10 @@ public void QuotedAndParenthesisStringTokenizerTests_WithComma_InQuotes()
136136
[Test]
137137
public void QuotedAndParenthesisStringTokenizerTests_WithFunctionCallContainingComma()
138138
{
139-
MsSql2005Dialect.QuotedAndParenthesisStringTokenizer tokenizier =
140-
new MsSql2005Dialect.QuotedAndParenthesisStringTokenizer(
139+
var tokenizier =
140+
new Dialect.Dialect.QuotedAndParenthesisStringTokenizer(
141141
new SqlString("SELECT fish.id, cast('astring, with,comma' as string) as bar, f FROM fish"));
142-
string[] expected = new string[]
142+
var expected = new string[]
143143
{
144144
"SELECT",
145145
"fish.id",
@@ -152,9 +152,9 @@ public void QuotedAndParenthesisStringTokenizerTests_WithFunctionCallContainingC
152152
"FROM",
153153
"fish"
154154
};
155-
int current = 0;
156-
IList<SqlString> tokens = tokenizier.GetTokens();
157-
foreach (SqlString token in tokens)
155+
var current = 0;
156+
var tokens = tokenizier.GetTokens();
157+
foreach (var token in tokens)
158158
{
159159
Assert.AreEqual(expected[current], token.ToString());
160160
current += 1;
@@ -165,10 +165,10 @@ public void QuotedAndParenthesisStringTokenizerTests_WithFunctionCallContainingC
165165
[Test]
166166
public void QuotedStringTokenizerTests()
167167
{
168-
MsSql2005Dialect.QuotedAndParenthesisStringTokenizer tokenizier =
169-
new MsSql2005Dialect.QuotedAndParenthesisStringTokenizer(
168+
var tokenizier =
169+
new Dialect.Dialect.QuotedAndParenthesisStringTokenizer(
170170
new SqlString("SELECT fish.\"id column\", fish.'fish name' as 'bar\\' column', f FROM fish"));
171-
string[] expected = new string[]
171+
var expected = new string[]
172172
{
173173
"SELECT",
174174
"fish.\"id column\"",
@@ -181,41 +181,104 @@ public void QuotedStringTokenizerTests()
181181
"FROM",
182182
"fish"
183183
};
184-
int current = 0;
185-
IList<SqlString> tokens = tokenizier.GetTokens();
186-
foreach (SqlString token in tokens)
184+
var current = 0;
185+
var tokens = tokenizier.GetTokens();
186+
foreach (var token in tokens)
187187
{
188188
Assert.AreEqual(expected[current], token.ToString());
189189
current += 1;
190190
}
191-
Assert.AreEqual(current, expected.Length);
191+
Assert.That(expected, Has.Length.EqualTo(current));
192+
}
193+
194+
[Test]
195+
public void GetIfExistsDropConstraintTest_without_catalog_without_schema()
196+
{
197+
var dialect = new MsSql2005Dialect();
198+
199+
const string expected = "if exists (select 1 from sys.objects" +
200+
" where object_id = OBJECT_ID(N'[Bar]')" +
201+
" and parent_object_id = OBJECT_ID(N'Foo'))";
202+
var ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(null, null, "Foo", "Bar");
203+
Assert.That(ifExistsDropConstraint, Is.EqualTo(expected));
204+
}
205+
206+
[Test]
207+
public void GetIfExistsDropConstraintTest_without_catalog_without_schema_with_quoted_table()
208+
{
209+
var dialect = new MsSql2005Dialect();
210+
211+
const string expected = "if exists (select 1 from sys.objects" +
212+
" where object_id = OBJECT_ID(N'[Bar]')" +
213+
" and parent_object_id = OBJECT_ID(N'[Foo]'))";
214+
var ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(null, null, "[Foo]", "Bar");
215+
Assert.That(ifExistsDropConstraint, Is.EqualTo(expected));
216+
}
217+
218+
[Test]
219+
public void GetIfExistsDropConstraintTest_with_schema()
220+
{
221+
var dialect = new MsSql2005Dialect();
222+
const string expected = "if exists (select 1 from sys.objects" +
223+
" where object_id = OBJECT_ID(N'Other.[Bar]')" +
224+
" and parent_object_id = OBJECT_ID(N'Other.Foo'))";
225+
var ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(null, "Other", "Foo", "Bar");
226+
Assert.That(ifExistsDropConstraint, Is.EqualTo(expected));
227+
}
228+
229+
[Test]
230+
public void GetIfExistsDropConstraintTest_with_quoted_schema()
231+
{
232+
var dialect = new MsSql2005Dialect();
233+
const string expected = "if exists (select 1 from sys.objects" +
234+
" where object_id = OBJECT_ID(N'[Other].[Bar]')" +
235+
" and parent_object_id = OBJECT_ID(N'[Other].Foo'))";
236+
var ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(null, "[Other]", "Foo", "Bar");
237+
Assert.That(ifExistsDropConstraint, Is.EqualTo(expected));
238+
}
239+
240+
[Test]
241+
public void GetIfExistsDropConstraintTest_with_catalog_without_schema()
242+
{
243+
var dialect = new MsSql2005Dialect();
244+
const string expected = "if exists (select 1 from Catalog.sys.objects" +
245+
" where object_id = OBJECT_ID(N'Catalog..[Bar]')" +
246+
" and parent_object_id = OBJECT_ID(N'Catalog..Foo'))";
247+
var ifExistsDropConstraint = dialect.GetIfExistsDropConstraint("Catalog", null, "Foo", "Bar");
248+
Assert.That(ifExistsDropConstraint, Is.EqualTo(expected));
249+
}
250+
251+
[Test]
252+
public void GetIfExistsDropConstraintTest_with_quoted_catalog_without_schema()
253+
{
254+
var dialect = new MsSql2005Dialect();
255+
const string expected = "if exists (select 1 from [Catalog].sys.objects" +
256+
" where object_id = OBJECT_ID(N'[Catalog]..[Bar]')" +
257+
" and parent_object_id = OBJECT_ID(N'[Catalog]..Foo'))";
258+
var ifExistsDropConstraint = dialect.GetIfExistsDropConstraint("[Catalog]", null, "Foo", "Bar");
259+
Assert.That(ifExistsDropConstraint, Is.EqualTo(expected));
192260
}
193261

194262
[Test]
195-
public void GetIfExistsDropConstraintTest_without_schema()
263+
public void GetIfExistsDropConstraintTest_with_catalog_with_schema()
196264
{
197-
MsSql2005Dialect dialect = new MsSql2005Dialect();
198-
Table foo = new Table("Foo");
199-
string expected = "if exists (select 1 from sys.objects" +
200-
" where object_id = OBJECT_ID(N'[Bar]')" +
201-
" AND parent_object_id = OBJECT_ID('Foo'))";
202-
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar");
203-
System.Console.WriteLine(ifExistsDropConstraint);
204-
Assert.AreEqual(expected, ifExistsDropConstraint);
265+
var dialect = new MsSql2005Dialect();
266+
const string expected = "if exists (select 1 from Catalog.sys.objects" +
267+
" where object_id = OBJECT_ID(N'Catalog.Schema.[Bar]')" +
268+
" and parent_object_id = OBJECT_ID(N'Catalog.Schema.Foo'))";
269+
var ifExistsDropConstraint = dialect.GetIfExistsDropConstraint("Catalog", "Schema", "Foo", "Bar");
270+
Assert.That(ifExistsDropConstraint, Is.EqualTo(expected));
205271
}
206272

207273
[Test]
208-
public void GetIfExistsDropConstraintTest_For_Schema_other_than_dbo()
274+
public void GetIfExistsDropConstraintTest_with_quoted_catalog_quoted_schema_quoted_table()
209275
{
210-
MsSql2005Dialect dialect = new MsSql2005Dialect();
211-
Table foo = new Table("Foo");
212-
foo.Schema = "Other";
213-
string expected = "if exists (select 1 from sys.objects" +
214-
" where object_id = OBJECT_ID(N'Other.[Bar]')" +
215-
" AND parent_object_id = OBJECT_ID('Other.Foo'))";
216-
string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar");
217-
System.Console.WriteLine(ifExistsDropConstraint);
218-
Assert.AreEqual(expected, ifExistsDropConstraint);
276+
var dialect = new MsSql2005Dialect();
277+
const string expected = "if exists (select 1 from [Catalog].sys.objects" +
278+
" where object_id = OBJECT_ID(N'[Catalog].[Schema].[Bar]')" +
279+
" and parent_object_id = OBJECT_ID(N'[Catalog].[Schema].[Foo]'))";
280+
var ifExistsDropConstraint = dialect.GetIfExistsDropConstraint("[Catalog]", "[Schema]", "[Foo]", "Bar");
281+
Assert.That(ifExistsDropConstraint, Is.EqualTo(expected));
219282
}
220283

221284
[Test]
@@ -302,5 +365,34 @@ private static void VerifyLimitStringForStoredProcedureCalls(string sql)
302365
limitSql = d.GetLimitString(new SqlString(sql), new SqlString("10"), new SqlString("2"));
303366
Assert.That(limitSql, Is.Null, "Limit and Offset: {0}", sql);
304367
}
368+
369+
[Test]
370+
public void QualifyTableWithCatalogAndWithoutSchema()
371+
{
372+
var d = new MsSql2005Dialect();
373+
374+
var t = new Table
375+
{
376+
Name = "name",
377+
Catalog = "catalog"
378+
};
379+
380+
Assert.That(t.GetQualifiedName(d), Is.EqualTo("catalog..name"));
381+
Assert.That(d.Qualify("catalog", null, "table"), Is.EqualTo("catalog..table"));
382+
}
383+
384+
[Test]
385+
public void QualifyTableWithoutCatalogAndWithoutSchema()
386+
{
387+
var d = new MsSql2005Dialect();
388+
389+
var t = new Table
390+
{
391+
Name = "name"
392+
};
393+
394+
Assert.That(t.GetQualifiedName(d), Is.EqualTo("name"));
395+
Assert.That(d.Qualify(null, null, "table"), Is.EqualTo("table"));
396+
}
305397
}
306-
}
398+
}

src/NHibernate.Test/NHSpecificTest/NH2288/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private static void AssertThatCheckOnTableExistenceIsCorrect(Configuration confi
2323
var sb = new StringBuilder(500);
2424
su.Execute(x => sb.AppendLine(x), false, false);
2525
string script = sb.ToString();
26-
Assert.That(script, Does.Contain("if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'dbo.[Aclasses_Id_FK]') AND parent_object_id = OBJECT_ID('dbo.Aclass'))"));
26+
Assert.That(script, Does.Contain("if exists (select 1 from nhibernate.sys.objects where object_id = OBJECT_ID(N'nhibernate.dbo.[Aclasses_Id_FK]') and parent_object_id = OBJECT_ID(N'nhibernate.dbo.Aclass'))"));
2727
}
2828

2929
[Test]

0 commit comments

Comments
 (0)