Skip to content

NH-4043 - Complete keyword registration in dialects. #654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public class Order
public string And { get; set; }
public string Column { get; set; }
public string Name { get; set; }
public string Abracadabra { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<property name="And"/>
<property name="Column"/>
<property name="Name"/>
<property name="Abracadabra"/>
</class>

</hibernate-mapping>
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Engine;
using NHibernate.Mapping;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.Tools.hbm2ddl.SchemaMetadataUpdaterTest
{
Expand All @@ -23,8 +26,8 @@ public void CanRetrieveReservedWords()
var metaData = dialect.GetDataBaseSchema(connectionHelper.Connection);
var reserved = metaData.GetReservedWords();
Assert.That(reserved, Is.Not.Empty);
Assert.That(reserved, Has.Member("SELECT"));
Assert.That(reserved, Has.Member("FROM"));
Assert.That(reserved, Has.Member("SELECT").IgnoreCase);
Assert.That(reserved, Has.Member("FROM").IgnoreCase);
}
finally
{
Expand All @@ -35,7 +38,7 @@ public void CanRetrieveReservedWords()
[Test]
public void UpdateReservedWordsInDialect()
{
var reservedDb = new HashSet<string>();
var reservedDb = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var configuration = TestConfigurationHelper.GetDefaultConfiguration();
var dialect = Dialect.Dialect.GetDialect(configuration.Properties);
var connectionHelper = new ManagedProviderConnectionHelper(configuration.Properties);
Expand All @@ -55,25 +58,118 @@ public void UpdateReservedWordsInDialect()

var sf = (ISessionFactoryImplementor) configuration.BuildSessionFactory();
SchemaMetadataUpdater.Update(sf);
var match = reservedDb.Intersect(sf.Dialect.Keywords);
var match = reservedDb.Intersect(sf.Dialect.Keywords, StringComparer.OrdinalIgnoreCase);

// tests that nothing in the first metaData.GetReservedWords() is left out of the second metaData.GetReservedWords() call.
// i.e. always passes.
Assert.That(match, Is.EquivalentTo(reservedDb));
}

[Test]
public void EnsureReservedWordsHardCodedInDialect()
{
var reservedDb = new HashSet<string>();
var configuration = TestConfigurationHelper.GetDefaultConfiguration();
var dialect = Dialect.Dialect.GetDialect(configuration.Properties);
var connectionHelper = new ManagedProviderConnectionHelper(configuration.Properties);
connectionHelper.Prepare();
try
{
var metaData = dialect.GetDataBaseSchema(connectionHelper.Connection);
foreach (var rw in metaData.GetReservedWords())
{
if (rw.Contains(" ")) continue;
reservedDb.Add(rw.ToLowerInvariant());
}
}
finally
{
connectionHelper.Release();
}

var sf = (ISessionFactoryImplementor)configuration.BuildSessionFactory();

// use the dialect as configured, with no update
var match = reservedDb.Intersect(sf.Dialect.Keywords).ToList();

// tests that nothing in metaData.GetReservedWords() is left out of the Dialect.Keywords (without a refresh).
var differences = reservedDb.Except(match).ToList();
if (differences.Count > 0)
{
Console.WriteLine("Update Dialect {0} with RegisterKeyword:", sf.Dialect.GetType().Name);
foreach (var keyword in differences.OrderBy(x => x))
{
Console.WriteLine(" RegisterKeyword(\"{0}\");", keyword);
}
}

if (sf.ConnectionProvider.Driver is OdbcDriver)
{
Assert.Inconclusive("ODBC has excess keywords reserved");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not supported by the "result comparison tool" currently, causing a build failure. You already need this change in teamcity.build file.

Feel free to replicate it in your PR, I will adjust mine if your get merged before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now available as a PR on its own. #655

Copy link
Member

@fredericDelaporte fredericDelaporte Jul 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is merged, you can rebase/merge master for avoiding Assert.Inconclusive causing a failure for builds using results comparison.

}

Assert.That(match, Is.EquivalentTo(reservedDb));
}

[Test, Explicit]
public void CheckForExcessReservedWordsHardCodedInDialect()
{
var reservedDb = new HashSet<string>();
var configuration = TestConfigurationHelper.GetDefaultConfiguration();
var dialect = Dialect.Dialect.GetDialect(configuration.Properties);
var connectionHelper = new ManagedProviderConnectionHelper(configuration.Properties);
connectionHelper.Prepare();
try
{
var metaData = dialect.GetDataBaseSchema(connectionHelper.Connection);
foreach (var rw in metaData.GetReservedWords())
{
reservedDb.Add(rw.ToLowerInvariant());
}
}
finally
{
connectionHelper.Release();
}

var sf = (ISessionFactoryImplementor)configuration.BuildSessionFactory();

// use the dialect as configured, with no update
// tests that nothing in Dialect.Keyword is not in metaData.GetReservedWords()
var differences = sf.Dialect.Keywords.Except(reservedDb).Except(AnsiSqlKeywords.Sql2003).ToList();
if (differences.Count > 0)
{
Console.WriteLine("Excess RegisterKeyword in Dialect {0}:", sf.Dialect.GetType().Name);
foreach (var keyword in differences.OrderBy(x => x))
{
Console.WriteLine(" RegisterKeyword(\"{0}\");", keyword);
}
}

// Don't fail incase the driver returns nothing.
// This is an info-only test.
}

[Test]
public void ExplicitAutoQuote()
{
var configuration = TestConfigurationHelper.GetDefaultConfiguration();
configuration.AddResource("NHibernate.Test.Tools.hbm2ddl.SchemaMetadataUpdaterTest.HeavyEntity.hbm.xml",
GetType().Assembly);

SchemaMetadataUpdater.QuoteTableAndColumns(configuration);
var dialect = Dialect.Dialect.GetDialect(configuration.GetDerivedProperties());
dialect.Keywords.Add("Abracadabra");

SchemaMetadataUpdater.Update(configuration, dialect);
SchemaMetadataUpdater.QuoteTableAndColumns(configuration, dialect);

var cm = configuration.GetClassMapping(typeof(Order));
Assert.That(cm.Table.IsQuoted);
var culs = new List<Column>(cm.Table.ColumnIterator);
Assert.That(GetColumnByName(culs, "From").IsQuoted);
Assert.That(GetColumnByName(culs, "And").IsQuoted);
Assert.That(GetColumnByName(culs, "Select").IsQuoted);
Assert.That(GetColumnByName(culs, "Abracadabra").IsQuoted);
Assert.That(!GetColumnByName(culs, "Name").IsQuoted);
}

Expand All @@ -97,7 +193,7 @@ public void AutoQuoteTableAndColumnsAtStratup()
[Test]
public void AutoQuoteTableAndColumnsAtStratupIncludeKeyWordsImport()
{
var reservedDb = new HashSet<string>();
var reservedDb = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var configuration = TestConfigurationHelper.GetDefaultConfiguration();
var dialect = Dialect.Dialect.GetDialect(configuration.Properties);
var connectionHelper = new ManagedProviderConnectionHelper(configuration.Properties);
Expand All @@ -119,8 +215,8 @@ public void AutoQuoteTableAndColumnsAtStratupIncludeKeyWordsImport()
configuration.AddResource("NHibernate.Test.Tools.hbm2ddl.SchemaMetadataUpdaterTest.HeavyEntity.hbm.xml",
GetType().Assembly);
var sf = (ISessionFactoryImplementor)configuration.BuildSessionFactory();
var match = reservedDb.Intersect(sf.Dialect.Keywords);
Assert.That(match, Is.EquivalentTo(reservedDb));
var match = reservedDb.Intersect(sf.Dialect.Keywords, StringComparer.OrdinalIgnoreCase);
Assert.That(match, Is.EquivalentTo(reservedDb).IgnoreCase);
}

private static Column GetColumnByName(IEnumerable<Column> columns, string colName)
Expand Down Expand Up @@ -167,14 +263,19 @@ public void WhenConfiguredOnlyExplicitAutoQuote()
configuration.AddResource("NHibernate.Test.Tools.hbm2ddl.SchemaMetadataUpdaterTest.HeavyEntity.hbm.xml",
GetType().Assembly);

SchemaMetadataUpdater.QuoteTableAndColumns(configuration);
var dialect = Dialect.Dialect.GetDialect(configuration.GetDerivedProperties());
dialect.Keywords.Add("Abracadabra");

SchemaMetadataUpdater.Update(configuration, dialect);
SchemaMetadataUpdater.QuoteTableAndColumns(configuration, dialect);

var cm = configuration.GetClassMapping(typeof(Order));
Assert.That(cm.Table.IsQuoted);
var culs = new List<Column>(cm.Table.ColumnIterator);
Assert.That(GetColumnByName(culs, "From").IsQuoted);
Assert.That(GetColumnByName(culs, "And").IsQuoted);
Assert.That(GetColumnByName(culs, "Select").IsQuoted);
Assert.That(GetColumnByName(culs, "Abracadabra").IsQuoted);
Assert.That(!GetColumnByName(culs, "Name").IsQuoted);
}
}
Expand Down
Loading