Skip to content

Commit fe7fba2

Browse files
Merge pull request npgsql#225 from danzel/fix-222
Handle IEnumerable<int> parameter
2 parents 464741c + cf8dd2c commit fe7fba2

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

Npgsql/NpgsqlTypes/ArrayHandling.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,10 @@ private void WriteBinaryArrayData(NpgsqlNativeTypeInfo TypeInfo, Array nativeDat
282282

283283
private bool WriteEnumeration(NpgsqlNativeTypeInfo TypeInfo, IEnumerable col, MemoryStream array, Boolean forExtendedQuery, NativeToBackendTypeConverterOptions options)
284284
{
285-
// As this prcedure handles both prepared and plain query representations, in order to not keep if's inside the loops
286-
// we simply set a placeholder here for both openElement ( '{' or '[' ) and closeElement ( '}', or ']' )
287-
byte openElement = (byte)(forExtendedQuery ? ASCIIBytes.BraceCurlyLeft : ASCIIBytes.BraceSquareLeft);
288-
byte closeElement = (byte)(forExtendedQuery ? ASCIIBytes.BraceCurlyRight : ASCIIBytes.BraceSquareRight);
289-
290285
bool writtenSomething = false;
291286
bool firstItem = true;
292287

293-
array.WriteByte(openElement);
288+
array.WriteByte((byte)ASCIIBytes.BraceCurlyLeft);
294289

295290
//write each item with a comma between them.
296291
foreach (object item in col)
@@ -309,7 +304,7 @@ private bool WriteEnumeration(NpgsqlNativeTypeInfo TypeInfo, IEnumerable col, Me
309304

310305
if (writtenSomething)
311306
{
312-
array.WriteByte(closeElement);
307+
array.WriteByte((byte)ASCIIBytes.BraceCurlyRight);
313308

314309
}
315310

tests/CommandTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
using System;
3030
using System.Diagnostics;
31+
using System.Linq;
3132
using Npgsql;
3233
using NUnit.Framework;
3334
using System.Data;
@@ -3757,5 +3758,19 @@ public void TestCommentInQuery03()
37573758
Assert.AreEqual(1, command.ExecuteScalar());
37583759
}
37593760
}
3761+
3762+
[Test]
3763+
public void TestIEnumerableAsArray()
3764+
{
3765+
using (var command = new NpgsqlCommand("SELECT :array", Conn))
3766+
{
3767+
var expected = new[] { 1, 2, 3, 4 };
3768+
command.Parameters.AddWithValue("array", expected.Select(x => x));
3769+
var res = command.ExecuteScalar() as int[];
3770+
3771+
Assert.NotNull(res);
3772+
CollectionAssert.AreEqual(expected, res);
3773+
}
3774+
}
37603775
}
37613776
}

0 commit comments

Comments
 (0)