//
//
// 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.Collections;
using System.Runtime.InteropServices;
using NpgsqlTypes;
namespace PlDotNET.Handler
{
///
/// A type handler for the PostgreSQL var bit string data type.
///
///
/// See https://www.postgresql.org/docs/current/static/datatype-bit.html.
///
[OIDHandler(OID.VARBITOID, OID.VARBITARRAYOID)]
public partial class VarBitStringHandler : ObjectTypeHandler
{
///
/// Initializes a new instance of the class.
///
public VarBitStringHandler()
{
this.ElementOID = OID.VARBITOID;
this.ArrayOID = OID.VARBITARRAYOID;
}
///
/// C function declared in pldotnet_conversions.h.
/// See ::pldotnet_GetDatumVarBitAttributes().
///
[LibraryImport("@PKG_LIBDIR/pldotnet.so")]
public static unsafe partial void pldotnet_GetDatumVarBitAttributes(IntPtr datum, ref int len, ref byte* dat);
///
/// C function declared in pldotnet_conversions.h.
/// See ::pldotnet_CreateDatumVarBit().
///
[LibraryImport("@PKG_LIBDIR/pldotnet.so")]
public static partial IntPtr pldotnet_CreateDatumVarBit(int len, byte[] dat);
///
/// Creates a BitArray object from a PostgreSQL bit string data type.
///
public static unsafe BitArray CreateBitArray(IntPtr datum)
{
int bitLen = 0;
byte* bitDat = null;
pldotnet_GetDatumVarBitAttributes(datum, ref bitLen, ref bitDat);
int byteLen = (bitLen / 8) + ((bitLen % 8) > 0 ? 1 : 0);
byte[] bytes = new byte[byteLen];
for (int i = 0; i < byteLen; i++)
{
bytes[i] = bitDat[i];
}
// the reverse BitArray constructed from byte[]
BitArray auxiliar = new(bytes);
BitArray result = new(bitLen);
for (int i = 0, cont = 0; i < byteLen; i++)
{
for (int j = 7; j >= 0; j--)
{
if (cont == bitLen)
{
break;
}
result[cont++] = auxiliar[(i * 8) + j];
}
}
return result;
}
///
/// Creates a PostgreSQL bit string data type from a BitArray object.
///
public static IntPtr CreateDatum(BitArray value)
{
int bitLen = value.Length;
int byteLen = (bitLen / 8) + ((bitLen % 8) > 0 ? 1 : 0);
// the reverse BitArray; it will be used to call "CopyTo"
BitArray auxiliar = new(byteLen * 8);
for (int i = 0, cont = 0; i < byteLen; i++)
{
for (int j = 7; j >= 0; j--)
{
if (cont == bitLen)
{
break;
}
auxiliar[(i * 8) + j] = value[cont++];
}
}
byte[] bytes = new byte[byteLen];
auxiliar.CopyTo(bytes, 0);
return pldotnet_CreateDatumVarBit(bitLen, bytes);
}
///
public override BitArray InputValue(IntPtr datum)
{
return CreateBitArray(datum);
}
///
public override IntPtr OutputValue(BitArray value)
{
return CreateDatum(value);
}
}
///
/// A type handler for the PostgreSQL bit string data type.
///
///
/// See https://www.postgresql.org/docs/current/static/datatype-bit.html.
///
[OIDHandler(OID.BITOID, OID.BITARRAYOID)]
public class BitStringHandler : ObjectTypeHandler
{
///
/// Initializes a new instance of the class.
///
public BitStringHandler()
{
this.ElementOID = OID.BITOID;
this.ArrayOID = OID.BITARRAYOID;
}
///
public override BitArray InputValue(IntPtr datum)
{
return VarBitStringHandler.CreateBitArray(datum);
}
///
public override IntPtr OutputValue(BitArray value)
{
return VarBitStringHandler.CreateDatum(value);
}
}
}