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

Commit d3ec715

Browse files
committed
Merge pull request #422 from jeffgabhart/issue/pg-json-and-arrays
Address broken PostgreSQL tests for Json and Arrays
2 parents 75b7831 + 69b68f3 commit d3ec715

File tree

4 files changed

+131
-3
lines changed

4 files changed

+131
-3
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/PostgreSqlJsonTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ public void Can_save_complex_types_as_JSON()
5555
db.Insert(row);
5656

5757
var result = db.Select<ModelWithJsonType>(
58-
"complex_type_json->'sub_type'->>'name' = 'SubType2'");
58+
"complex_type_json->'SubType'->>'Name' = 'SubType2'");
59+
60+
db.GetLastSql().Print();
5961

6062
Assert.That(result.Count, Is.EqualTo(1));
61-
Assert.That(result[0].Id, Is.EqualTo(2));
62-
Assert.That(result[0].ComplexTypeJson.SubType, Is.EqualTo("SubType2"));
63+
Assert.That(result[0].Id, Is.EqualTo(1));
64+
Assert.That(result[0].ComplexTypeJson.Id, Is.EqualTo(2));
65+
Assert.That(result[0].ComplexTypeJson.SubType.Name, Is.EqualTo("SubType2"));
6366
}
6467
}
6568
}

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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Data;
44
using System.Text;
55
using Npgsql;
6+
using NpgsqlTypes;
67
using ServiceStack.Text;
78

89
namespace ServiceStack.OrmLite.PostgreSQL
@@ -324,5 +325,40 @@ public override long InsertAndGetLastInsertId<T>(IDbCommand dbCmd)
324325

325326
return dbCmd.ExecLongScalar();
326327
}
328+
329+
public override void SetParameter(FieldDefinition fieldDef, IDbDataParameter p)
330+
{
331+
if (fieldDef.CustomFieldDefinition == "json")
332+
{
333+
p.ParameterName = this.GetParam(SanitizeFieldNameForParamName(fieldDef.FieldName));
334+
((NpgsqlParameter) p).NpgsqlDbType = NpgsqlDbType.Json;
335+
return;
336+
}
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+
}
349+
base.SetParameter(fieldDef, p);
350+
}
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+
}
327363
}
328364
}

0 commit comments

Comments
 (0)