Skip to content

Commit 91104e3

Browse files
authored
Merge pull request #172 from valente500/tds-ansistring
Added TDS_BLOB support to DbType.AnsiString.
2 parents 41023e3 + 3e180d4 commit 91104e3

File tree

4 files changed

+49
-23
lines changed

4 files changed

+49
-23
lines changed

src/AdoNetCore.AseClient/Internal/FormatItem.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public static FormatItem CreateForParameter(AseParameter parameter, DbEnvironmen
8484
{
8585
switch (parameter.DbType)
8686
{
87+
case DbType.AnsiString:
88+
format.BlobType = BlobType.BLOB_LONGCHAR;
89+
break;
8790
case DbType.String:
8891
format.BlobType = BlobType.BLOB_UNICHAR;
8992
break;

src/AdoNetCore.AseClient/Internal/TypeMap.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ internal static class TypeMap
1010
{
1111
private const int VarLongBoundary = 255;
1212
//Above this length in bytes, send strings as TDS_BLOBs
13+
private const int AnsiStringAsBlobBoundary = 16384;
1314
private const int StringAsBlobBoundary = 16384;
1415
private const int BinaryAsBlobBoundary = 16384;
1516

@@ -26,7 +27,12 @@ internal static class TypeMap
2627
{DbType.UInt64, (value, length) => value == DBNull.Value ? TdsDataType.TDS_UINTN : TdsDataType.TDS_UINT8},
2728
{DbType.String, (value, length) => length <= StringAsBlobBoundary ? TdsDataType.TDS_LONGBINARY : TdsDataType.TDS_BLOB},
2829
{DbType.StringFixedLength, (value, length) => TdsDataType.TDS_LONGBINARY},
29-
{DbType.AnsiString, (value, length) => length <= VarLongBoundary ? TdsDataType.TDS_VARCHAR : TdsDataType.TDS_LONGCHAR},
30+
{DbType.AnsiString, (value, length) =>
31+
length <= AnsiStringAsBlobBoundary
32+
? length <= VarLongBoundary
33+
? TdsDataType.TDS_VARCHAR
34+
: TdsDataType.TDS_LONGCHAR
35+
: TdsDataType.TDS_BLOB},
3036
{DbType.AnsiStringFixedLength, (value, length) => length <= VarLongBoundary ? TdsDataType.TDS_VARCHAR : TdsDataType.TDS_LONGCHAR},
3137
{DbType.Binary, (value, length) =>
3238
length <= BinaryAsBlobBoundary

src/AdoNetCore.AseClient/Internal/ValueWriter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,14 @@ private static void WriteTDS_BLOB(object value, Stream stream, FormatItem format
313313
case string s:
314314
stream.WriteByte((byte)SerializationType.SER_DEFAULT);
315315
stream.WriteNullableUShortPrefixedByteArray(format.ClassId);
316-
stream.WriteBlobSpecificIntPrefixedByteArray(Encoding.Unicode.GetBytes(s));
316+
if (format.BlobType == BlobType.BLOB_UNICHAR)
317+
{
318+
stream.WriteBlobSpecificIntPrefixedByteArray(Encoding.Unicode.GetBytes(s));
319+
}
320+
else
321+
{
322+
stream.WriteBlobSpecificIntPrefixedByteArray(Cast<byte[]>(value, format, enc));
323+
}
317324
stream.WriteBlobSpecificTerminator();
318325
break;
319326
case byte[] ba:

test/AdoNetCore.AseClient.Tests/Integration/Insert/TextTests.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,41 @@ public void TearDown()
5757
}
5858
}
5959

60-
public static IEnumerable<TestCaseData> Insert_Parameter_Cases()
60+
public static IEnumerable<DbType> Insert_Parameter_Types()
6161
{
62-
yield return new TestCaseData(1);
63-
yield return new TestCaseData(10);
64-
yield return new TestCaseData(100);
65-
yield return new TestCaseData(127);
66-
yield return new TestCaseData(1000);
67-
yield return new TestCaseData(4096);
68-
yield return new TestCaseData(4097);
69-
yield return new TestCaseData(8192);
70-
yield return new TestCaseData(8193);
71-
yield return new TestCaseData(10000);
72-
yield return new TestCaseData(16384);
73-
yield return new TestCaseData(16385);
74-
yield return new TestCaseData(100000);
75-
yield return new TestCaseData(1000000);
62+
yield return DbType.AnsiString;
63+
yield return DbType.String;
7664
}
7765

78-
[TestCaseSource(nameof(Insert_Parameter_Cases))]
79-
public void Insert_Parameter_Dapper(int count)
66+
public static IEnumerable<int> Insert_Parameter_Counts()
67+
{
68+
yield return 1;
69+
yield return 10;
70+
yield return 100;
71+
yield return 127;
72+
yield return 1000;
73+
yield return 4096;
74+
yield return 4097;
75+
yield return 8192;
76+
yield return 8193;
77+
yield return 10000;
78+
yield return 16384;
79+
yield return 16385;
80+
yield return 100000;
81+
yield return 1000000;
82+
}
83+
84+
[Test]
85+
public void Insert_Parameter_Dapper(
86+
[ValueSource(nameof(Insert_Parameter_Types))] DbType dbType,
87+
[ValueSource(nameof(Insert_Parameter_Counts))] int count)
8088
{
8189
var value = new string('1', count);
8290
using (var connection = GetConnection())
8391
{
8492
connection.Execute("set textsize 1000000");
8593
var p = new DynamicParameters();
86-
p.Add("@text_field", value, DbType.String);
94+
p.Add("@text_field", value, dbType);
8795
connection.Execute("insert into [dbo].[insert_text_tests] (text_field) values (@text_field)", p);
8896
var insertedLength = connection.QuerySingle<int>("select top 1 len(text_field) from [dbo].[insert_text_tests]");
8997
Assert.AreEqual(value.Length, insertedLength);
@@ -92,8 +100,10 @@ public void Insert_Parameter_Dapper(int count)
92100
Insert_Parameter_VerifyResult(GetConnection, "insert_text_tests", "text_field", value);
93101
}
94102

95-
[TestCaseSource(nameof(Insert_Parameter_Cases))]
96-
public void Insert_Parameter_With_Date_Dapper(int count)
103+
[Test]
104+
public void Insert_Parameter_With_Date_Dapper(
105+
[ValueSource(nameof(Insert_Parameter_Types))] DbType dbType,
106+
[ValueSource(nameof(Insert_Parameter_Counts))] int count)
97107
{
98108
var value = new string('1', count);
99109
var date = new DateTime(2001, 2, 3, 4, 5, 6);
@@ -102,7 +112,7 @@ public void Insert_Parameter_With_Date_Dapper(int count)
102112
{
103113
connection.Execute("set textsize 1000000");
104114
var p = new DynamicParameters();
105-
p.Add("@text_field", value, DbType.String);
115+
p.Add("@text_field", value, dbType);
106116
p.Add("@date_field", date, DbType.DateTime);
107117
connection.Execute("insert into [dbo].[insert_text_date_tests] (text_field, date_field) values (@text_field, @date_field)", p);
108118
var insertedLength = connection.QuerySingle<int>("select top 1 len(text_field) from [dbo].[insert_text_date_tests]");

0 commit comments

Comments
 (0)