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

Commit 3f44ae6

Browse files
committed
Add PgSql.Param API for easily creating NpgsqlParameter's
1 parent 3f72b49 commit 3f44ae6

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Npgsql;
2+
3+
namespace ServiceStack.OrmLite.PostgreSQL
4+
{
5+
public static class PgSql
6+
{
7+
public static NpgsqlParameter Param<T>(string name, T value) =>
8+
new NpgsqlParameter(name, PostgreSqlDialect.Instance.GetDbType<T>()) {
9+
Value = value
10+
};
11+
}
12+
}

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Collections.Generic;
44
using System.Data;
55
using System.Linq;
6+
using System.Net;
7+
using System.Net.NetworkInformation;
68
using System.Text;
79
using System.Threading;
810
using System.Threading.Tasks;
@@ -573,8 +575,58 @@ public override string GetLastInsertIdSqlSuffix<T>()
573575

574576
return "; " + SelectIdentitySql;
575577
}
578+
579+
public Dictionary<Type,NpgsqlDbType> TypesMap { get; } = new Dictionary<Type, NpgsqlDbType>
580+
{
581+
[typeof(bool)] = NpgsqlDbType.Boolean,
582+
[typeof(short)] = NpgsqlDbType.Smallint,
583+
[typeof(int)] = NpgsqlDbType.Integer,
584+
[typeof(long)] = NpgsqlDbType.Bigint,
585+
[typeof(float)] = NpgsqlDbType.Real,
586+
[typeof(double)] = NpgsqlDbType.Double,
587+
[typeof(decimal)] = NpgsqlDbType.Numeric,
588+
[typeof(string)] = NpgsqlDbType.Text,
589+
[typeof(char[])] = NpgsqlDbType.Varchar,
590+
[typeof(char)] = NpgsqlDbType.Char,
591+
[typeof(NpgsqlPoint)] = NpgsqlDbType.Point,
592+
[typeof(NpgsqlLSeg)] = NpgsqlDbType.LSeg,
593+
[typeof(NpgsqlPath)] = NpgsqlDbType.Path,
594+
[typeof(NpgsqlPolygon)] = NpgsqlDbType.Polygon,
595+
[typeof(NpgsqlLine)] = NpgsqlDbType.Line,
596+
[typeof(NpgsqlCircle)] = NpgsqlDbType.Circle,
597+
[typeof(NpgsqlBox)] = NpgsqlDbType.Box,
598+
[typeof(BitArray)] = NpgsqlDbType.Varbit,
599+
[typeof(IDictionary<string, string>)] = NpgsqlDbType.Hstore,
600+
[typeof(Guid)] = NpgsqlDbType.Uuid,
601+
[typeof(NpgsqlInet)] = NpgsqlDbType.Cidr,
602+
[typeof(ValueTuple<IPAddress,int>)] = NpgsqlDbType.Inet,
603+
[typeof(IPAddress)] = NpgsqlDbType.Inet,
604+
[typeof(PhysicalAddress)] = NpgsqlDbType.MacAddr,
605+
[typeof(NpgsqlTsQuery)] = NpgsqlDbType.TsQuery,
606+
[typeof(NpgsqlTsVector)] = NpgsqlDbType.TsVector,
607+
[typeof(NpgsqlDate)] = NpgsqlDbType.Date,
608+
[typeof(DateTime)] = NpgsqlDbType.Timestamp,
609+
[typeof(DateTimeOffset)] = NpgsqlDbType.TimestampTz,
610+
[typeof(TimeSpan)] = NpgsqlDbType.Time,
611+
[typeof(NpgsqlTimeSpan)] = NpgsqlDbType.Time,
612+
[typeof(byte[])] = NpgsqlDbType.Bytea,
613+
[typeof(uint)] = NpgsqlDbType.Oid,
614+
[typeof(uint[])] = NpgsqlDbType.Oidvector,
615+
};
576616

577-
public static Dictionary<string, NpgsqlDbType> NativeTypes = new Dictionary<string, NpgsqlDbType> {
617+
public NpgsqlDbType GetDbType<T>() => GetDbType(typeof(T));
618+
public NpgsqlDbType GetDbType(Type type)
619+
{
620+
if (PostgreSqlDialect.Instance.TypesMap.TryGetValue(type, out var paramType))
621+
return paramType;
622+
var genericEnum = type.GetTypeWithGenericTypeDefinitionOf(typeof(IEnumerable<>));
623+
if (genericEnum != null)
624+
return GetDbType(genericEnum.GenericTypeArguments[0]) | NpgsqlDbType.Array;
625+
626+
throw new NotSupportedException($"Type '{type.Name}' not found in 'TypesMap'");
627+
}
628+
629+
public Dictionary<string, NpgsqlDbType> NativeTypes = new Dictionary<string, NpgsqlDbType> {
578630
{ "json", NpgsqlDbType.Json },
579631
{ "jsonb", NpgsqlDbType.Jsonb },
580632
{ "hstore", NpgsqlDbType.Hstore },
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using NpgsqlTypes;
2+
using NUnit.Framework;
3+
4+
namespace ServiceStack.OrmLite.PostgreSQL.Tests
5+
{
6+
public class PgSqlTests
7+
{
8+
[Test]
9+
public void Can_create_NpgsqlParameter()
10+
{
11+
Assert.That(PgSql.Param("p", 1).NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Integer));
12+
Assert.That(PgSql.Param("p", "s").NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Text));
13+
Assert.That(PgSql.Param("p", 'c').NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Char));
14+
Assert.That(PgSql.Param("p", new [] { 1 }).NpgsqlDbType,
15+
Is.EqualTo(NpgsqlDbType.Integer | NpgsqlDbType.Array));
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)