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

Commit

Permalink
Add PgSql.Param API for easily creating NpgsqlParameter's
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Apr 10, 2020
1 parent 3f72b49 commit 3f44ae6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/ServiceStack.OrmLite.PostgreSQL/PgSql.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Npgsql;

namespace ServiceStack.OrmLite.PostgreSQL
{
public static class PgSql
{
public static NpgsqlParameter Param<T>(string name, T value) =>
new NpgsqlParameter(name, PostgreSqlDialect.Instance.GetDbType<T>()) {
Value = value
};
}
}
54 changes: 53 additions & 1 deletion src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -573,8 +575,58 @@ public override string GetLastInsertIdSqlSuffix<T>()

return "; " + SelectIdentitySql;
}

public Dictionary<Type,NpgsqlDbType> TypesMap { get; } = new Dictionary<Type, NpgsqlDbType>
{
[typeof(bool)] = NpgsqlDbType.Boolean,
[typeof(short)] = NpgsqlDbType.Smallint,
[typeof(int)] = NpgsqlDbType.Integer,
[typeof(long)] = NpgsqlDbType.Bigint,
[typeof(float)] = NpgsqlDbType.Real,
[typeof(double)] = NpgsqlDbType.Double,
[typeof(decimal)] = NpgsqlDbType.Numeric,
[typeof(string)] = NpgsqlDbType.Text,
[typeof(char[])] = NpgsqlDbType.Varchar,
[typeof(char)] = NpgsqlDbType.Char,
[typeof(NpgsqlPoint)] = NpgsqlDbType.Point,
[typeof(NpgsqlLSeg)] = NpgsqlDbType.LSeg,
[typeof(NpgsqlPath)] = NpgsqlDbType.Path,
[typeof(NpgsqlPolygon)] = NpgsqlDbType.Polygon,
[typeof(NpgsqlLine)] = NpgsqlDbType.Line,
[typeof(NpgsqlCircle)] = NpgsqlDbType.Circle,
[typeof(NpgsqlBox)] = NpgsqlDbType.Box,
[typeof(BitArray)] = NpgsqlDbType.Varbit,
[typeof(IDictionary<string, string>)] = NpgsqlDbType.Hstore,
[typeof(Guid)] = NpgsqlDbType.Uuid,
[typeof(NpgsqlInet)] = NpgsqlDbType.Cidr,
[typeof(ValueTuple<IPAddress,int>)] = NpgsqlDbType.Inet,
[typeof(IPAddress)] = NpgsqlDbType.Inet,
[typeof(PhysicalAddress)] = NpgsqlDbType.MacAddr,
[typeof(NpgsqlTsQuery)] = NpgsqlDbType.TsQuery,
[typeof(NpgsqlTsVector)] = NpgsqlDbType.TsVector,
[typeof(NpgsqlDate)] = NpgsqlDbType.Date,
[typeof(DateTime)] = NpgsqlDbType.Timestamp,
[typeof(DateTimeOffset)] = NpgsqlDbType.TimestampTz,
[typeof(TimeSpan)] = NpgsqlDbType.Time,
[typeof(NpgsqlTimeSpan)] = NpgsqlDbType.Time,
[typeof(byte[])] = NpgsqlDbType.Bytea,
[typeof(uint)] = NpgsqlDbType.Oid,
[typeof(uint[])] = NpgsqlDbType.Oidvector,
};

public static Dictionary<string, NpgsqlDbType> NativeTypes = new Dictionary<string, NpgsqlDbType> {
public NpgsqlDbType GetDbType<T>() => GetDbType(typeof(T));
public NpgsqlDbType GetDbType(Type type)
{
if (PostgreSqlDialect.Instance.TypesMap.TryGetValue(type, out var paramType))
return paramType;
var genericEnum = type.GetTypeWithGenericTypeDefinitionOf(typeof(IEnumerable<>));
if (genericEnum != null)
return GetDbType(genericEnum.GenericTypeArguments[0]) | NpgsqlDbType.Array;

throw new NotSupportedException($"Type '{type.Name}' not found in 'TypesMap'");
}

public Dictionary<string, NpgsqlDbType> NativeTypes = new Dictionary<string, NpgsqlDbType> {
{ "json", NpgsqlDbType.Json },
{ "jsonb", NpgsqlDbType.Jsonb },
{ "hstore", NpgsqlDbType.Hstore },
Expand Down
18 changes: 18 additions & 0 deletions tests/ServiceStack.OrmLite.PostgreSQL.Tests/PgSqlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using NpgsqlTypes;
using NUnit.Framework;

namespace ServiceStack.OrmLite.PostgreSQL.Tests
{
public class PgSqlTests
{
[Test]
public void Can_create_NpgsqlParameter()
{
Assert.That(PgSql.Param("p", 1).NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Integer));
Assert.That(PgSql.Param("p", "s").NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Text));
Assert.That(PgSql.Param("p", 'c').NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Char));
Assert.That(PgSql.Param("p", new [] { 1 }).NpgsqlDbType,
Is.EqualTo(NpgsqlDbType.Integer | NpgsqlDbType.Array));
}
}
}

0 comments on commit 3f44ae6

Please sign in to comment.