//
//
// PL/.NET (pldotnet) - PostgreSQL support for .NET C# and F# as
// procedural languages (PL)
//
//
// Copyright (c) 2023 Brick Abode
//
// This code is subject to the terms of the PostgreSQL License.
// The full text of the license can be found in the LICENSE file
// at the top level of the pldotnet repository.
//
//
using System;
using System.Buffers;
using System.Runtime.InteropServices;
using System.Text;
using NpgsqlTypes;
namespace PlDotNET.Handler
{
///
/// A type handler for PostgreSQL character data types (text, char, varchar, xml).
///
///
/// See https://www.postgresql.org/docs/current/datatype-character.html.
///
public partial class StringHandler : ObjectTypeHandler
{
///
/// C function declared in pldotnet_conversions.h.
/// See ::pldotnet_GetDatumTextAttributes().
///
[LibraryImport("@PKG_LIBDIR/pldotnet.so")]
public static unsafe partial void pldotnet_GetDatumTextAttributes(IntPtr datum, ref int len, ref byte* buf);
///
/// C function declared in pldotnet_conversions.h.
/// See ::pldotnet_CreateDatumText().
///
[LibraryImport("@PKG_LIBDIR/pldotnet.so")]
public static partial IntPtr pldotnet_CreateDatumText(int len, byte[] buf);
///
/// C function declared in pldotnet_conversions.h.
/// See ::pldotnet_GetDatumCharAttributes().
///
[LibraryImport("@PKG_LIBDIR/pldotnet.so")]
public static unsafe partial void pldotnet_GetDatumCharAttributes(IntPtr datum, ref int len, ref byte* buf);
///
/// C function declared in pldotnet_conversions.h.
/// See ::pldotnet_CreateDatumChar().
///
[LibraryImport("@PKG_LIBDIR/pldotnet.so")]
public static partial IntPtr pldotnet_CreateDatumChar(int len, byte[] buf);
///
/// C function declared in pldotnet_conversions.h.
/// See ::pldotnet_GetDatumVarCharAttributes().
///
[LibraryImport("@PKG_LIBDIR/pldotnet.so")]
public static unsafe partial void pldotnet_GetDatumVarCharAttributes(IntPtr datum, ref int len, ref byte* buf);
///
/// C function declared in pldotnet_conversions.h.
/// See ::pldotnet_CreateDatumVarChar().
///
[LibraryImport("@PKG_LIBDIR/pldotnet.so")]
public static partial IntPtr pldotnet_CreateDatumVarChar(int len, byte[] buf);
///
/// C function declared in pldotnet_conversions.h.
/// See ::pldotnet_GetDatumXmlAttributes().
///
[LibraryImport("@PKG_LIBDIR/pldotnet.so")]
public static unsafe partial void pldotnet_GetDatumXmlAttributes(IntPtr datum, ref int len, ref byte* buf);
///
/// C function declared in pldotnet_conversions.h.
/// See ::pldotnet_CreateDatumXml().
///
[LibraryImport("@PKG_LIBDIR/pldotnet.so")]
public static partial IntPtr pldotnet_CreateDatumXml(int len, byte[] buf);
///
public override unsafe string InputValue(IntPtr datum)
{
int strlen = 0;
byte* str_p = null;
switch ((int)this.ElementOID)
{
case (int)OID.TEXTOID:
pldotnet_GetDatumTextAttributes(datum, ref strlen, ref str_p);
break;
case (int)OID.BPCHAROID:
pldotnet_GetDatumCharAttributes(datum, ref strlen, ref str_p);
break;
case (int)OID.VARCHAROID:
pldotnet_GetDatumVarCharAttributes(datum, ref strlen, ref str_p);
break;
case (int)OID.XMLOID:
pldotnet_GetDatumXmlAttributes(datum, ref strlen, ref str_p);
break;
default:
throw new NotImplementedException($"StringConstructors doesn't support {(OID)this.ElementOID}");
}
return Encoding.UTF8.GetString(str_p, strlen);
}
///
public override IntPtr OutputValue(string value)
{
byte[] encodedBytes = Encoding.UTF8.GetBytes(value);
int len = encodedBytes.Length;
return (int)this.ElementOID switch
{
(int)OID.TEXTOID => pldotnet_CreateDatumText(len, encodedBytes),
(int)OID.BPCHAROID => pldotnet_CreateDatumChar(len, encodedBytes),
(int)OID.VARCHAROID => pldotnet_CreateDatumVarChar(len, encodedBytes),
(int)OID.XMLOID => pldotnet_CreateDatumXml(len, encodedBytes),
_ => throw new NotImplementedException($"StringConstructors doesn't support {(OID)this.ElementOID}"),
};
}
}
///
/// A type handler for PostgreSQL character text data types.
///
///
/// See https://www.postgresql.org/docs/current/datatype-character.html.
///
[OIDHandler(OID.TEXTOID, OID.TEXTARRAYOID)]
public class TextHandler : StringHandler
{
///
/// Initializes a new instance of the class.
///
public TextHandler()
{
this.ElementOID = OID.TEXTOID;
this.ArrayOID = OID.TEXTARRAYOID;
}
}
///
/// A type handler for PostgreSQL character char data types.
///
///
/// See https://www.postgresql.org/docs/current/datatype-character.html.
///
[OIDHandler(OID.BPCHAROID, OID.BPCHARARRAYOID)]
public class CharHandler : StringHandler
{
///
/// Initializes a new instance of the class.
///
public CharHandler()
{
this.ElementOID = OID.BPCHAROID;
this.ArrayOID = OID.BPCHARARRAYOID;
}
}
///
/// A type handler for PostgreSQL character varchar data types.
///
///
/// See https://www.postgresql.org/docs/current/datatype-character.html.
///
[OIDHandler(OID.VARCHAROID, OID.VARCHARARRAYOID)]
public class CharVaryingHandler : StringHandler
{
///
/// Initializes a new instance of the class.
///
public CharVaryingHandler()
{
this.ElementOID = OID.VARCHAROID;
this.ArrayOID = OID.VARCHARARRAYOID;
}
}
///
/// A type handler for PostgreSQL character xml data types.
///
///
/// See https://www.postgresql.org/docs/current/datatype-xml.htm.
///
[OIDHandler(OID.XMLOID, OID.XMLARRAYOID)]
public class XmlHandler : StringHandler
{
///
/// Initializes a new instance of the class.
///
public XmlHandler()
{
this.ElementOID = OID.XMLOID;
this.ArrayOID = OID.XMLARRAYOID;
}
}
}