Skip to content

Commit e79f47e

Browse files
committed
Applying patch from Richard Birkb, fixing NH-2086
Fixing schema issue with SQL CE SVN: trunk@4934
1 parent b359650 commit e79f47e

File tree

2 files changed

+123
-5
lines changed

2 files changed

+123
-5
lines changed

src/NHibernate.Test/DialectTest/SqlCEDialectFixture.cs

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ namespace NHibernate.Test.DialectTest
77
[TestFixture]
88
public class SqlCEDialectFixture
99
{
10-
[Test]
10+
private MsSqlCeDialect dialect;
11+
12+
[SetUp]
13+
public void SetUp()
14+
{
15+
dialect = new MsSqlCeDialect();
16+
}
17+
18+
[Test]
1119
public void BinaryBlob_mapping_to_SqlCe_types()
1220
{
13-
Dialect dialect = new MsSqlCeDialect();
1421
SimpleValue sv = new SimpleValue();
1522
sv.TypeName = NHibernateUtil.BinaryBlob.Name;
1623
Column column = new Column();
@@ -26,5 +33,68 @@ public void BinaryBlob_mapping_to_SqlCe_types()
2633
column.Length = 8001;
2734
Assert.AreEqual("IMAGE", column.GetSqlType(dialect, null));
2835
}
29-
}
36+
37+
[Test]
38+
public void QuotedSchemaNameWithSqlCE()
39+
{
40+
Table tbl = new Table();
41+
tbl.Schema = "`schema`";
42+
tbl.Name = "`name`";
43+
44+
Assert.AreEqual("\"schema_name\"", tbl.GetQualifiedName(dialect));
45+
Assert.AreEqual("\"schema_table\"", dialect.Qualify("", "\"schema\"", "\"table\""));
46+
}
47+
48+
[Test]
49+
public void QuotedTableNameWithoutSchemaWithSqlCE()
50+
{
51+
Table tbl = new Table();
52+
tbl.Name = "`name`";
53+
54+
Assert.AreEqual("\"name\"", tbl.GetQualifiedName(dialect));
55+
}
56+
57+
[Test]
58+
public void QuotedSchemaNameWithUnqoutedTableInSqlCE()
59+
{
60+
Table tbl = new Table();
61+
tbl.Schema = "`schema`";
62+
tbl.Name = "name";
63+
64+
Assert.AreEqual("\"schema_name\"", tbl.GetQualifiedName(dialect));
65+
Assert.AreEqual("\"schema_table\"", dialect.Qualify("", "\"schema\"", "table"));
66+
}
67+
68+
[Test]
69+
public void QuotedCatalogSchemaNameWithSqlCE()
70+
{
71+
Table tbl = new Table();
72+
tbl.Catalog = "dbo";
73+
tbl.Schema = "`schema`";
74+
tbl.Name = "`name`";
75+
76+
Assert.AreEqual("dbo.\"schema_name\"", tbl.GetQualifiedName(dialect));
77+
Assert.AreEqual("dbo.\"schema_table\"", dialect.Qualify("dbo", "\"schema\"", "\"table\""));
78+
}
79+
80+
[Test]
81+
public void QuotedTableNameWithSqlCE()
82+
{
83+
Table tbl = new Table();
84+
tbl.Name = "`Group`";
85+
86+
Assert.AreEqual("\"Group\"", tbl.GetQualifiedName(dialect));
87+
}
88+
89+
[Test]
90+
public void SchemaNameWithSqlCE()
91+
{
92+
Table tbl = new Table();
93+
tbl.Schema = "schema";
94+
tbl.Name = "name";
95+
96+
Assert.AreEqual("schema_name", tbl.GetQualifiedName(dialect));
97+
Assert.AreEqual("schema_table", dialect.Qualify("", "schema", "table"));
98+
}
99+
}
30100
}

src/NHibernate/Dialect/MsSqlCeDialect.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.Data;
22
using System.Data.Common;
3+
using System.Text;
34
using NHibernate.Dialect.Schema;
4-
using Environment=NHibernate.Cfg.Environment;
5+
using NHibernate.Util;
6+
using Environment = NHibernate.Cfg.Environment;
57

68
namespace NHibernate.Dialect
79
{
@@ -97,5 +99,51 @@ public override IDataBaseSchema GetDataBaseSchema(DbConnection connection)
9799
{
98100
return new MsSqlCeDataBaseSchema(connection);
99101
}
100-
}
102+
103+
public override string Qualify(string catalog, string schema, string table)
104+
{
105+
// SQL Server Compact doesn't support Schemas. So join schema name and table name with underscores
106+
// similar to the SQLLite dialect.
107+
108+
var qualifiedName = new StringBuilder();
109+
bool quoted = false;
110+
111+
if (!string.IsNullOrEmpty(catalog))
112+
{
113+
qualifiedName.Append(catalog).Append(StringHelper.Dot);
114+
}
115+
116+
var tableName = new StringBuilder();
117+
if (!string.IsNullOrEmpty(schema))
118+
{
119+
if (schema.StartsWith(OpenQuote.ToString()))
120+
{
121+
schema = schema.Substring(1, schema.Length - 1);
122+
quoted = true;
123+
}
124+
if (schema.EndsWith(CloseQuote.ToString()))
125+
{
126+
schema = schema.Substring(0, schema.Length - 1);
127+
quoted = true;
128+
}
129+
tableName.Append(schema).Append(StringHelper.Underscore);
130+
}
131+
132+
if (table.StartsWith(OpenQuote.ToString()))
133+
{
134+
table = table.Substring(1, table.Length - 1);
135+
quoted = true;
136+
}
137+
if (table.EndsWith(CloseQuote.ToString()))
138+
{
139+
table = table.Substring(0, table.Length - 1);
140+
quoted = true;
141+
}
142+
143+
string name = tableName.Append(table).ToString();
144+
if (quoted)
145+
name = OpenQuote + name + CloseQuote;
146+
return qualifiedName.Append(name).ToString();
147+
}
148+
}
101149
}

0 commit comments

Comments
 (0)