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

Commit f237d83

Browse files
committed
Add helpers for PgSql.Array
1 parent a19e0de commit f237d83

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/ServiceStack.OrmLite.PostgreSQL/PgSql.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,14 @@ public static NpgsqlParameter Param<T>(string name, T value) =>
88
new NpgsqlParameter(name, PostgreSqlDialect.Instance.GetDbType<T>()) {
99
Value = value
1010
};
11+
12+
public static string Array<T>(params T[] items) =>
13+
"ARRAY[" + PostgreSqlDialect.Provider.SqlSpread(items) + "]";
14+
15+
public static string Array<T>(T[] items, bool nullIfEmpty) => nullIfEmpty
16+
? "ARRAY[" + NullIfEmpty(PostgreSqlDialect.Provider.SqlSpread(items)) + "]"
17+
: "ARRAY[" + PostgreSqlDialect.Provider.SqlSpread(items) + "]";
18+
19+
public static string NullIfEmpty(string sql) => sql == "ARRAY[]" ? "null" : sql;
1120
}
1221
}

tests/ServiceStack.OrmLite.PostgreSQL.Tests/PgSqlTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,19 @@ public void Can_create_NpgsqlParameter()
1414
Assert.That(PgSql.Param("p", new [] { 1 }).NpgsqlDbType,
1515
Is.EqualTo(NpgsqlDbType.Integer | NpgsqlDbType.Array));
1616
}
17+
18+
[Test]
19+
public void Does_PgSqlArray()
20+
{
21+
Assert.That(PgSql.Array((string[])null), Is.EqualTo("ARRAY[]"));
22+
Assert.That(PgSql.Array(new string[0]), Is.EqualTo("ARRAY[]"));
23+
Assert.That(PgSql.Array(new int[0]), Is.EqualTo("ARRAY[]"));
24+
Assert.That(PgSql.Array(1,2,3), Is.EqualTo("ARRAY[1,2,3]"));
25+
Assert.That(PgSql.Array("A","B","C"), Is.EqualTo("ARRAY['A','B','C']"));
26+
Assert.That(PgSql.Array("A'B","C\"D"), Is.EqualTo("ARRAY['A''B','C\"D']"));
27+
28+
Assert.That(PgSql.Array(new string[0], nullIfEmpty:true), Is.EqualTo("null"));
29+
Assert.That(PgSql.Array(new[]{"A","B","C"}, nullIfEmpty:true), Is.EqualTo("ARRAY['A','B','C']"));
30+
}
1731
}
1832
}

0 commit comments

Comments
 (0)