Skip to content

Adjust string parameter sizes for MSSQL #1844

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 4 commits into from
Sep 18, 2018
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
212 changes: 212 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1300/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Data;
using System.Data.SqlClient;
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Connection;
using NHibernate.Criterion;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Engine;
using NHibernate.Exceptions;
using NHibernate.Linq;
using NHibernate.Type;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1300
{
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override bool AppliesTo(Dialect.Dialect dialect)
{
return dialect is MsSql2000Dialect;
}

protected override bool AppliesTo(ISessionFactoryImplementor factory)
{
return factory.ConnectionProvider.Driver is SqlClientDriver;
}

protected override void Configure(Configuration configuration)
{
using (var cp = ConnectionProviderFactory.NewConnectionProvider(cfg.Properties))
{
if (cp.Driver is SqlClientDriver)
{
configuration.SetProperty(Environment.ConnectionDriver, typeof(TestSqlClientDriver).AssemblyQualifiedName);
}
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}

protected override void OnSetUp()
{
base.OnSetUp();
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Name = "Bob" };
session.Save(e1);
transaction.Commit();
}
}

[Test]
public async Task InsertShouldUseMappedSizeAsync()
{
Driver.ClearCommands();

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Name = "1", AnsiName = "2", FullText = "3", AnsiFullText = "4" };
await (session.SaveAsync(e1));
await (transaction.CommitAsync());
Assert.That(Driver.LastCommandParameters.Single(x => (string) x.Value == "1").SqlDbType, Is.EqualTo(SqlDbType.NVarChar));
Assert.That(Driver.LastCommandParameters.Single(x => (string) x.Value == "1").Size, Is.EqualTo(3));
Assert.That(Driver.LastCommandParameters.Single(x => (string) x.Value == "2").SqlDbType, Is.EqualTo(SqlDbType.VarChar));
Assert.That(Driver.LastCommandParameters.Single(x => (string) x.Value == "2").Size, Is.EqualTo(3));
Assert.That(Driver.LastCommandParameters.Single(x => (string) x.Value == "3").SqlDbType, Is.EqualTo(SqlDbType.NVarChar));
Assert.That(Driver.LastCommandParameters.Single(x => (string) x.Value == "3").Size, Is.EqualTo(MsSql2000Dialect.MaxSizeForClob));
Assert.That(Driver.LastCommandParameters.Single(x => (string) x.Value == "4").SqlDbType, Is.EqualTo(SqlDbType.VarChar));
Assert.That(Driver.LastCommandParameters.Single(x => (string) x.Value == "4").Size, Is.EqualTo(MsSql2000Dialect.MaxSizeForAnsiClob));
}
}

[Test]
public void InsertWithTooLongValuesShouldThrowAsync()
{
Driver.ClearCommands();

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Name = "Alal", AnsiName = "Alal" };

var ex = Assert.ThrowsAsync<GenericADOException>(
async () =>
{
await (session.SaveAsync(e1));
await (transaction.CommitAsync());
});

var sqlEx = ex.InnerException as SqlException;
Assert.That(sqlEx, Is.Not.Null);
Assert.That(sqlEx.Number, Is.EqualTo(8152));
}
}

[TestCase("Name", SqlDbType.NVarChar)]
[TestCase("AnsiName", SqlDbType.VarChar)]
public async Task LinqEqualsShouldUseMappedSizeAsync(string property, SqlDbType expectedDbType, CancellationToken cancellationToken = default(CancellationToken))
{
Driver.ClearCommands();

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (property == "Name")
{
await (session.Query<Entity>().Where(x => x.Name == "Bob").ToListAsync(cancellationToken));
}
else
{
await (session.Query<Entity>().Where(x => x.AnsiName == "Bob").ToListAsync(cancellationToken));
}
Assert.That(Driver.LastCommandParameters.First().Size, Is.EqualTo(3));
Assert.That(Driver.LastCommandParameters.First().SqlDbType, Is.EqualTo(expectedDbType));
}
}

[Test]
public async Task MappedAsShouldUseExplicitSizeAsync()
{
Driver.ClearCommands();

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
await (session.Query<Entity>().Where(x => x.Name == "Bob".MappedAs(TypeFactory.Basic("AnsiString(200)"))).ToListAsync());

Assert.That(Driver.LastCommandParameters.First().Size, Is.EqualTo(200));
Assert.That(Driver.LastCommandParameters.First().SqlDbType, Is.EqualTo(SqlDbType.VarChar));
}
}

[TestCase("Name", SqlDbType.NVarChar)]
[TestCase("AnsiName", SqlDbType.VarChar)]
public async Task HqlLikeShouldUseLargerSizeAsync(string property, SqlDbType expectedDbType, CancellationToken cancellationToken = default(CancellationToken))
{
Driver.ClearCommands();

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
await (session.CreateQuery("from Entity where " + property + " like :name").SetParameter("name", "%Bob%").ListAsync<Entity>(cancellationToken));

Assert.That(Driver.LastCommandParameters.First().Size, Is.GreaterThanOrEqualTo(5));
Assert.That(Driver.LastCommandParameters.First().SqlDbType, Is.EqualTo(expectedDbType));
}
}

[TestCase("Name", SqlDbType.NVarChar)]
[TestCase("AnsiName", SqlDbType.VarChar)]
public async Task CriteriaEqualsShouldUseMappedSizeAsync(string property, SqlDbType expectedDbType, CancellationToken cancellationToken = default(CancellationToken))
{
Driver.ClearCommands();

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
Driver.ClearCommands();

await (session.CreateCriteria<Entity>().Add(Restrictions.Eq(property, "Bob"))
.ListAsync<Entity>(cancellationToken));

Assert.That(Driver.LastCommandParameters.First().Size, Is.GreaterThanOrEqualTo(3));
Assert.That(Driver.LastCommandParameters.First().SqlDbType, Is.EqualTo(expectedDbType));
}
}

[TestCase("Name", SqlDbType.NVarChar)]
[TestCase("AnsiName", SqlDbType.VarChar)]
public async Task CriteriaLikeShouldUseLargerSizeAsync(string property, SqlDbType expectedDbType, CancellationToken cancellationToken = default(CancellationToken))
{
Driver.ClearCommands();

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
await (session.CreateCriteria<Entity>().Add(Restrictions.Like(property, "%Bob%"))
.ListAsync<Entity>(cancellationToken));

Assert.That(Driver.LastCommandParameters.First().Size, Is.GreaterThanOrEqualTo(5));
Assert.That(Driver.LastCommandParameters.First().SqlDbType, Is.EqualTo(expectedDbType));
}
}
private TestSqlClientDriver Driver => Sfi.ConnectionProvider.Driver as TestSqlClientDriver;
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1300/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace NHibernate.Test.NHSpecificTest.GH1300
{
class Entity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string AnsiName { get; set; }
public virtual string FullText { get; set; }
public virtual string AnsiFullText { get; set; }
}
}
Loading