Skip to content

NH-3749 - Remove unnecessary comma in CREATE TABLE statement#393

Closed
lesnyh wants to merge 4 commits intonhibernate:masterfrom
lesnyh:lesnyh-NH-3749
Closed

NH-3749 - Remove unnecessary comma in CREATE TABLE statement#393
lesnyh wants to merge 4 commits intonhibernate:masterfrom
lesnyh:lesnyh-NH-3749

Conversation

@lesnyh
Copy link
Contributor

@lesnyh lesnyh commented Jan 20, 2015

I develop NHibernate Driver for DBMS Linter SQL and found a problem in file Nhibernate\Mapping\Table.cs. The problem is in the following code:
foreach (UniqueKey uk in UniqueKeyIterator)
{
buf.Append(',').Append(uk.SqlConstraintString(dialect));
}
This code produces unnecessary comma in CREATE TABLE statement if dialect doesn't support not null unique constraints. For example, in test Nhibernate.Test.Component.Basic.ComponentWithUniqueConstraintTests.CanBePersistedWithUniqueValues we have the following query:
create table Employee (Id BIGINT not null, Name VARCHAR(255) null, Dob DATE null, HireDate DATE null, primary key (Id),);
I suggest that we check dialect.SupportsNotNullUnique before running foreach loop. Something like this:
if (dialect.SupportsNotNullUnique)
{
foreach (UniqueKey uk in UniqueKeyIterator)
{
buf.Append(',').Append(uk.SqlConstraintString(dialect));
}
}

@hazzik
Copy link
Member

hazzik commented Feb 9, 2015

@lesnyh can you add some tests?

@lesnyh
Copy link
Contributor Author

lesnyh commented Feb 10, 2015

Yes, I will try to add some tests. But to reproduce this problem we need DBMS that doesn't support not null unique constraints (dialect.SupportsNotNullUnique == false). I use DBMS Linter SQL Server and have problems in 21 tests:
NHibernate.Test.Component.Basic.ComponentWithUniqueConstraintTests.CanBePersistedWithUniqueValues
NHibernate.Test.Generatedkeys.Identity.SimpleIdentityGeneratedFixture.SequenceIdentityGenerator
NHibernate.Test.MappingByCode.IntegrationTests.NH3269.FixtureNonPublicProperty.ShouldNotThrowExceptionWhenTryingToSaveInherited2WithDuplicateName
NHibernate.Test.MappingByCode.IntegrationTests.NH3269.FixturePublicProperty.ShouldNotThrowExceptionWhenTryingToSaveInherited2WithDuplicateName
NHibernate.Test.Naturalid.Immutable.ImmutableNaturalIdFixture.NaturalIdCache
NHibernate.Test.Naturalid.Immutable.ImmutableNaturalIdFixture.NaturalIdCheck
NHibernate.Test.Naturalid.Immutable.ImmutableNaturalIdFixture.Update
NHibernate.Test.Naturalid.Mutable.MutableNaturalIdFixture.NaturalIdCache
NHibernate.Test.Naturalid.Mutable.MutableNaturalIdFixture.NonexistentNaturalIdCache
NHibernate.Test.Naturalid.Mutable.MutableNaturalIdFixture.Querying
NHibernate.Test.Naturalid.Mutable.MutableNaturalIdFixture.ReattachmentNaturalIdCheck
NHibernate.Test.NHSpecificTest.NH1989.Fixture.SecondLevelCacheWithDifferentRegionsFuture
NHibernate.Test.NHSpecificTest.NH1989.Fixture.SecondLevelCacheWithMixedCacheableAndNonCacheableFuture
NHibernate.Test.NHSpecificTest.NH1989.Fixture.SecondLevelCacheWithMixedCacheRegionsFuture
NHibernate.Test.NHSpecificTest.NH1989.Fixture.SecondLevelCacheWithSingleCacheableFuture
NHibernate.Test.NHSpecificTest.NH1989.Fixture.SecondLevelCacheWithSingleCacheableQueryFuture
NHibernate.Test.NHSpecificTest.NH2037.Fixture.Test
NHibernate.Test.NHSpecificTest.NH3221.WeirdBehaviour.CanAddATodo
NHibernate.Test.NHSpecificTest.NH3221.WeirdBehaviour.CanAddStuff
NHibernate.Test.NHSpecificTest.NH3221.WeirdBehaviour.CanRemoveATodo
NHibernate.Test.NHSpecificTest.NH3221.WeirdBehaviour.CanRemoveStuff

@lesnyh
Copy link
Contributor Author

lesnyh commented Feb 20, 2015

I think it may be a little bit hard to write a test, because now all NHibernate test dialects support not null unique constraints. I have found interesting comment in file NHibernate\Mapping\Index.cs:
//TODO handle supportsNotNullUnique=false, but such a case does not exist in the wild so far

Copy link
Member

@hazzik hazzik left a comment

Choose a reason for hiding this comment

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

Please add some tests anyway. You can implement own TestDialect class to demonstrate issue.

}

foreach (UniqueKey uk in UniqueKeyIterator)
if (dialect.SupportsNotNullUnique)
Copy link
Member

Choose a reason for hiding this comment

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

I think it's more accurate way is to check that uk.SqlConstraintString(dialect) returns not empty string before adding to buf

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have corrected code.

@lesnyh
Copy link
Contributor Author

lesnyh commented Feb 13, 2017

Can I add our LinterDialect class for DBMS Linter SQL?

@gliljas
Copy link
Member

gliljas commented Feb 13, 2017

That would be unnecessary and make the test bloated. Create a small dialect just for the test instead.,

@lesnyh
Copy link
Contributor Author

lesnyh commented Feb 13, 2017

I have corrected code and added small test.

@lesnyh
Copy link
Contributor Author

lesnyh commented Feb 14, 2017

BTW, the property name SupportsNotNullUnique is a bit confusing because according to the usage of this property it means: "supports nullable columns with unique constraints". You'd better add a comment to clarify what this property mean.

@hazzik
Copy link
Member

hazzik commented Jan 30, 2018

Can you please enable Allow edits from maintainers option on this PR?

@hazzik
Copy link
Member

hazzik commented Apr 11, 2018

Replaced by #1651

@hazzik hazzik closed this Apr 11, 2018
@hazzik hazzik marked this as a duplicate of #1651 Apr 11, 2018
@hazzik hazzik reopened this Apr 11, 2018
@hazzik hazzik closed this Apr 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants