Upgrade ODBC support

This commit is contained in:
Andy Howard
2026-09-15 17:46:16 +01:00
parent 81a933eaa3
commit 0491c9f4b0
7 changed files with 129 additions and 7 deletions
@@ -8,6 +8,7 @@ using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Linq;
namespace Sacs.UnitTests.Net.Standard.Base.Dbas
{
@@ -51,6 +52,39 @@ namespace Sacs.UnitTests.Net.Standard.Base.Dbas
Assert.That(odbcConnection.ConnectionString, Is.EqualTo(config.ConnectionString));
}
[Test]
public void OpenOdbcDatabase_CreatesOdbcConnectionForNonSqlDatabase()
{
SacsDatabaseConsole.OdbcConfig config = new SacsDatabaseConsole.OdbcConfig
{
Dbms = DbmsType.Postgres,
DatabaseName = "TestDb",
ConnectionString = "Driver={PostgreSQL Unicode};Server=localhost;Database=TestDb;"
};
SacsDatabase database = SacsDatabaseConsole.OpenOdbcDatabase(config);
using DbConnection connection = database.CreateConnection();
Assert.That(connection, Is.TypeOf<OdbcConnection>());
Assert.That(connection.ConnectionString, Is.EqualTo(config.ConnectionString));
}
[Test]
public void CreateDataAdapter_ReturnsOdbcAdapter()
{
SacsDatabaseConsole.OdbcConfig config = new SacsDatabaseConsole.OdbcConfig
{
Dbms = DbmsType.General,
DatabaseName = "TestDb",
ConnectionString = "Driver={Test Driver};Database=TestDb;"
};
SacsDatabase database = SacsDatabaseConsole.OpenOdbcDatabase(config);
using DbDataAdapter adapter = database.CreateDataAdapter();
Assert.That(adapter, Is.TypeOf<OdbcDataAdapter>());
}
// --- Command Parameters ---
[Test]
@@ -107,6 +141,59 @@ namespace Sacs.UnitTests.Net.Standard.Base.Dbas
Assert.That(command.Parameters[2].DbType, Is.EqualTo(DbType.DateTime2));
}
[Test]
public void AddCommandParameter_CreatesOdbcValuesWithExpectedTypesAndOrder()
{
ParameterTestItem item = new ParameterTestItem();
Guid identifier = Guid.Parse("024f2943-12a6-46be-88cf-49980f81e603");
byte[] data = new byte[] { 1, 2, 3 };
using OdbcCommand command = new OdbcCommand();
item.AddParameter(command, "@Text", "value");
item.AddParameter(command, "@Short", (short)12);
item.AddParameter(command, "@Integer", 34);
item.AddParameter(command, "@Long", 56L);
item.AddParameter(command, "@Byte", (byte)78);
item.AddParameter(command, "@Single", 90.1f);
item.AddParameter(command, "@Double", 23.4d);
item.AddParameter(command, "@Decimal", 78.9m);
item.AddParameter(command, "@Boolean", true);
item.AddParameter(command, "@Identifier", identifier);
item.AddParameter(command, "@Data", data);
Assert.That(command.Parameters.Cast<OdbcParameter>().Select(parameter => parameter.ParameterName),
Is.EqualTo(new[] { "@Text", "@Short", "@Integer", "@Long", "@Byte", "@Single", "@Double", "@Decimal", "@Boolean", "@Identifier", "@Data" }));
Assert.That(command.Parameters.Cast<OdbcParameter>().Select(parameter => parameter.DbType),
Is.EqualTo(new[] { DbType.String, DbType.Int16, DbType.Int32, DbType.Int64, DbType.Byte, DbType.Single, DbType.Double, DbType.Decimal, DbType.Boolean, DbType.Guid, DbType.Binary }));
Assert.That(command.Parameters[0].Value, Is.EqualTo("value"));
Assert.That(command.Parameters[9].Value, Is.EqualTo(identifier));
Assert.That(command.Parameters[10].Value, Is.SameAs(data));
}
[Test]
public void AddCommandParameter_CreatesTypedOdbcNulls()
{
ParameterTestItem item = new ParameterTestItem();
using OdbcCommand command = new OdbcCommand();
item.AddParameter<short?>(command, "@Short", null);
item.AddParameter<int?>(command, "@Integer", null);
item.AddParameter<long?>(command, "@Long", null);
item.AddParameter<byte?>(command, "@Byte", null);
item.AddParameter<float?>(command, "@Single", null);
item.AddParameter<double?>(command, "@Double", null);
item.AddParameter<decimal?>(command, "@Decimal", null);
item.AddParameter<bool?>(command, "@Boolean", null);
item.AddParameter<Guid?>(command, "@Identifier", null);
item.AddParameter<DateTime?>(command, "@Date", null);
item.AddParameter<byte[]>(command, "@Data", null);
Assert.That(command.Parameters.Cast<OdbcParameter>().Select(parameter => parameter.Value),
Is.All.EqualTo(DBNull.Value));
Assert.That(command.Parameters.Cast<OdbcParameter>().Select(parameter => parameter.DbType),
Is.EqualTo(new[] { DbType.Int16, DbType.Int32, DbType.Int64, DbType.Byte, DbType.Single, DbType.Double, DbType.Decimal, DbType.Boolean, DbType.Guid, DbType.DateTime, DbType.Binary }));
}
// --- The End ---
}
}