crystal-pg
crystal-pg copied to clipboard
Support range types
Currently:
require "pg"
db = DB.open("postgres://postgres:postgres@localhost:5432/postgres")
pp db.scalar("SELECT int4range(10, 20)") # => Bytes
I've been taking a stab at this, and can't seem to figure out where the "rules" for decoding a given message from Postgres wire format come from - @will any advice here?
@miketheman It's in https://github.com/will/crystal-pg/blob/master/src/pg/decoder.cr
Every postgres type has an ID, called oid
. A decoder can handle a series of oid
s. So my guess is that you'll need to define a new Int4Range
decoder and register it, but then you'll also want to define a Postgres::Range
type, which is different than Crystal's Range
.
(this is just from me fiddling with the library, I might be wrong)
@asterite Thanks - I got that far. I'm more interested in how to interpret the bytes returned from PG.
The functions in postgres are usually ending in _send
Here is the range one I think: https://github.com/postgres/postgres/blob/b538c90b1bded5464787e2b8e4431302d24eb601/src/backend/utils/adt/rangetypes.c#L246
You can also call them as functions in postgres if that helps playing around with things faster:
will=# select range_send('[1,3]'::int4range);
range_send
--------------------------------------
\x0200000004000000010000000400000004
(1 row)
I started a Draft PR for discussion.
I can work on this, if it is stalled. Got a prototype working, need some guidance though.