Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 69b68f3

Browse files
committed
Support postgresql integer[] and bigint[] by using CustomField as a hint.
CustomField indicates the underlying postgresql type. Use it to avoid serializing the array and setting NpgsqlDbType.
1 parent fa61794 commit 69b68f3

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Linq;
3+
using NUnit.Framework;
4+
using ServiceStack.DataAnnotations;
5+
using ServiceStack.OrmLite.Tests;
6+
using ServiceStack.Text;
7+
8+
namespace ServiceStack.OrmLite.PostgreSQL.Tests
9+
{
10+
public class ModelWithArrayType
11+
{
12+
public int Id { get; set; }
13+
14+
[CustomField("integer[]")]
15+
public int[] IntegerArray { get; set; }
16+
17+
[CustomField("bigint[]")]
18+
public long[] BigIntegerArray { get; set; }
19+
}
20+
21+
[TestFixture]
22+
public class PostgreSqlArrayTests : OrmLiteTestBase
23+
{
24+
public PostgreSqlArrayTests() : base(Dialect.PostgreSql) {}
25+
26+
[Test]
27+
public void Can_save_integer_array()
28+
{
29+
using (var db = OpenDbConnection())
30+
{
31+
db.DropAndCreateTable<ModelWithArrayType>();
32+
33+
db.GetLastSql().Print();
34+
35+
var row = new ModelWithArrayType
36+
{
37+
Id = 1,
38+
IntegerArray = new []{1,2,3}
39+
};
40+
41+
try
42+
{
43+
db.Insert(row);
44+
}
45+
catch (Exception e)
46+
{
47+
e.Message.Print();
48+
}
49+
50+
var result = db.Select<ModelWithArrayType>();
51+
52+
Assert.That(result.Count, Is.EqualTo(1));
53+
Assert.That(result[0].IntegerArray.Count(), Is.EqualTo(3));
54+
}
55+
}
56+
57+
[Test]
58+
public void Can_save_big_integer_array()
59+
{
60+
using (var db = OpenDbConnection())
61+
{
62+
db.DropAndCreateTable<ModelWithArrayType>();
63+
64+
db.GetLastSql().Print();
65+
66+
var row = new ModelWithArrayType
67+
{
68+
Id = 2,
69+
BigIntegerArray = new long[] { 1, 2, 3, 4 }
70+
};
71+
72+
try
73+
{
74+
db.Insert(row);
75+
}
76+
catch (Exception e)
77+
{
78+
e.Message.Print();
79+
}
80+
81+
var result = db.Select<ModelWithArrayType>();
82+
83+
Assert.That(result.Count, Is.EqualTo(1));
84+
Assert.That(result[0].BigIntegerArray.Count(), Is.EqualTo(4));
85+
}
86+
}
87+
}
88+
}

src/ServiceStack.OrmLite.PostgreSQL.Tests/ServiceStack.OrmLite.PostgreSQL.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<Compile Include="OrmLiteExecuteProcedureTests.cs" />
104104
<Compile Include="OrmLiteInsertTests.cs" />
105105
<Compile Include="OrmLiteSelectTests.cs" />
106+
<Compile Include="PostgreSqlArrayTests.cs" />
106107
<Compile Include="PostgreSqlJsonTests.cs" />
107108
<Compile Include="Properties\AssemblyInfo.cs" />
108109
<Compile Include="OrmLiteGetScalarTests.cs" />

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ public override long InsertAndGetLastInsertId<T>(IDbCommand dbCmd)
325325

326326
return dbCmd.ExecLongScalar();
327327
}
328+
328329
public override void SetParameter(FieldDefinition fieldDef, IDbDataParameter p)
329330
{
330331
if (fieldDef.CustomFieldDefinition == "json")
@@ -333,7 +334,31 @@ public override void SetParameter(FieldDefinition fieldDef, IDbDataParameter p)
333334
((NpgsqlParameter) p).NpgsqlDbType = NpgsqlDbType.Json;
334335
return;
335336
}
337+
if (fieldDef.CustomFieldDefinition == "integer[]")
338+
{
339+
p.ParameterName = this.GetParam(SanitizeFieldNameForParamName(fieldDef.FieldName));
340+
((NpgsqlParameter) p).NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Integer;
341+
return;
342+
}
343+
if (fieldDef.CustomFieldDefinition == "bigint[]")
344+
{
345+
p.ParameterName = this.GetParam(SanitizeFieldNameForParamName(fieldDef.FieldName));
346+
((NpgsqlParameter) p).NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Bigint;
347+
return;
348+
}
336349
base.SetParameter(fieldDef, p);
337350
}
351+
protected override object GetValue<T>(FieldDefinition fieldDef, object obj)
352+
{
353+
if (fieldDef.CustomFieldDefinition == "integer[]")
354+
{
355+
return fieldDef.GetValue(obj);
356+
}
357+
if (fieldDef.CustomFieldDefinition == "bigint[]")
358+
{
359+
return fieldDef.GetValue(obj);
360+
}
361+
return base.GetValue<T>(fieldDef, obj);
362+
}
338363
}
339364
}

0 commit comments

Comments
 (0)