module Hasql.PostgresTypeInfo where

import Database.PostgreSQL.LibPQ qualified as LibPQ
import Hasql.Prelude hiding (bool)

-- | A Postgresql type info
data PTI = PTI {PTI -> OID
ptiOID :: !OID, PTI -> Maybe OID
ptiArrayOID :: !(Maybe OID)}

-- | A Word32 and a LibPQ representation of an OID
data OID = OID {OID -> Word32
oidWord32 :: !Word32, OID -> Oid
oidPQ :: !LibPQ.Oid, OID -> Format
oidFormat :: !LibPQ.Format}

mkOID :: LibPQ.Format -> Word32 -> OID
mkOID :: Format -> Word32 -> OID
mkOID Format
format Word32
x =
  Word32 -> Oid -> Format -> OID
OID Word32
x ((CUInt -> Oid
LibPQ.Oid (CUInt -> Oid) -> (Word32 -> CUInt) -> Word32 -> Oid
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Word32 -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral) Word32
x) Format
format

mkPTI :: LibPQ.Format -> Word32 -> Maybe Word32 -> PTI
mkPTI :: Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
format Word32
oid Maybe Word32
arrayOID =
  OID -> Maybe OID -> PTI
PTI (Format -> Word32 -> OID
mkOID Format
format Word32
oid) ((Word32 -> OID) -> Maybe Word32 -> Maybe OID
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Format -> Word32 -> OID
mkOID Format
format) Maybe Word32
arrayOID)

-- * Constants

abstime :: PTI
abstime :: PTI
abstime = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
702 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1023)

aclitem :: PTI
aclitem :: PTI
aclitem = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1033 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1034)

bit :: PTI
bit :: PTI
bit = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1560 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1561)

bool :: PTI
bool :: PTI
bool = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
16 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1000)

box :: PTI
box :: PTI
box = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
603 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1020)

bpchar :: PTI
bpchar :: PTI
bpchar = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1042 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1014)

bytea :: PTI
bytea :: PTI
bytea = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
17 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1001)

char :: PTI
char :: PTI
char = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
18 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1002)

cid :: PTI
cid :: PTI
cid = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
29 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1012)

cidr :: PTI
cidr :: PTI
cidr = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
650 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
651)

circle :: PTI
circle :: PTI
circle = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
718 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
719)

cstring :: PTI
cstring :: PTI
cstring = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
2275 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1263)

date :: PTI
date :: PTI
date = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1082 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1182)

daterange :: PTI
daterange :: PTI
daterange = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3912 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3913)

float4 :: PTI
float4 :: PTI
float4 = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
700 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1021)

float8 :: PTI
float8 :: PTI
float8 = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
701 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1022)

gtsvector :: PTI
gtsvector :: PTI
gtsvector = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3642 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3644)

inet :: PTI
inet :: PTI
inet = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
869 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1041)

int2 :: PTI
int2 :: PTI
int2 = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
21 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1005)

int2vector :: PTI
int2vector :: PTI
int2vector = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
22 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1006)

int4 :: PTI
int4 :: PTI
int4 = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
23 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1007)

int4range :: PTI
int4range :: PTI
int4range = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3904 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3905)

int8 :: PTI
int8 :: PTI
int8 = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
20 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1016)

int8range :: PTI
int8range :: PTI
int8range = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3926 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3927)

interval :: PTI
interval :: PTI
interval = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1186 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1187)

json :: PTI
json :: PTI
json = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
114 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
199)

jsonb :: PTI
jsonb :: PTI
jsonb = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3802 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3807)

line :: PTI
line :: PTI
line = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
628 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
629)

lseg :: PTI
lseg :: PTI
lseg = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
601 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1018)

macaddr :: PTI
macaddr :: PTI
macaddr = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
829 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1040)

money :: PTI
money :: PTI
money = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
790 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
791)

name :: PTI
name :: PTI
name = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
19 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1003)

numeric :: PTI
numeric :: PTI
numeric = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1700 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1231)

numrange :: PTI
numrange :: PTI
numrange = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3906 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3907)

oid :: PTI
oid :: PTI
oid = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
26 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1028)

oidvector :: PTI
oidvector :: PTI
oidvector = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
30 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1013)

path :: PTI
path :: PTI
path = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
602 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1019)

point :: PTI
point :: PTI
point = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
600 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1017)

polygon :: PTI
polygon :: PTI
polygon = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
604 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1027)

record :: PTI
record :: PTI
record = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
2249 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
2287)

refcursor :: PTI
refcursor :: PTI
refcursor = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1790 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
2201)

regclass :: PTI
regclass :: PTI
regclass = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
2205 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
2210)

regconfig :: PTI
regconfig :: PTI
regconfig = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3734 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3735)

regdictionary :: PTI
regdictionary :: PTI
regdictionary = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3769 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3770)

regoper :: PTI
regoper :: PTI
regoper = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
2203 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
2208)

regoperator :: PTI
regoperator :: PTI
regoperator = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
2204 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
2209)

regproc :: PTI
regproc :: PTI
regproc = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
24 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1008)

regprocedure :: PTI
regprocedure :: PTI
regprocedure = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
2202 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
2207)

regtype :: PTI
regtype :: PTI
regtype = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
2206 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
2211)

reltime :: PTI
reltime :: PTI
reltime = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
703 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1024)

text :: PTI
text :: PTI
text = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
25 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1009)

tid :: PTI
tid :: PTI
tid = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
27 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1010)

time :: PTI
time :: PTI
time = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1083 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1183)

timestamp :: PTI
timestamp :: PTI
timestamp = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1114 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1115)

timestamptz :: PTI
timestamptz :: PTI
timestamptz = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1184 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1185)

timetz :: PTI
timetz :: PTI
timetz = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1266 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1270)

tinterval :: PTI
tinterval :: PTI
tinterval = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
704 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1025)

tsquery :: PTI
tsquery :: PTI
tsquery = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3615 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3645)

tsrange :: PTI
tsrange :: PTI
tsrange = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3908 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3909)

tstzrange :: PTI
tstzrange :: PTI
tstzrange = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3910 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3911)

tsvector :: PTI
tsvector :: PTI
tsvector = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
3614 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
3643)

txid_snapshot :: PTI
txid_snapshot :: PTI
txid_snapshot = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
2970 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
2949)

textUnknown :: PTI
textUnknown :: PTI
textUnknown = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Text Word32
705 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
705)

binaryUnknown :: PTI
binaryUnknown :: PTI
binaryUnknown = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
705 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
705)

uuid :: PTI
uuid :: PTI
uuid = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
2950 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
2951)

varbit :: PTI
varbit :: PTI
varbit = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1562 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1563)

varchar :: PTI
varchar :: PTI
varchar = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
1043 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1015)

void :: PTI
void :: PTI
void = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
2278 Maybe Word32
forall a. Maybe a
Nothing

xid :: PTI
xid :: PTI
xid = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
28 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
1011)

xml :: PTI
xml :: PTI
xml = Format -> Word32 -> Maybe Word32 -> PTI
mkPTI Format
LibPQ.Binary Word32
142 (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just Word32
143)