Added generic ToField and FromField classes #196
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch adds default generic-based implementations for
ToField
and
FromField
classes. These are useful when you need to retrieveresults from sql-functions returning
setof some_type
or fromsql-queries returning columns with records.
I should mention one major change in a core module as I'm not sure
whether it's acceptable for you (and the community) or not.
The short story: in order to compose existing parsers into a record
parser we need to bypass typecheck of a
Field
variable. ThereforetypeOid
in aField
record was changed fromPQ.Oid
toMaybe PQ.Oid
.The long story: when we parse a record value we have no information
about types of it's fields. More precisely there are two cases:
typename
equals to real type nameWe are still able to retrieve metadata of the type, but this will be
a performance hit as we will have extra query for each execution of
the parser.
typename
equals to"record"
We don't have any data, so there is nothing to do! Even worse, we
can gen
"record"
instead of our custom type any time when weforget to cast results to it.
Hence to parse a record value we have to rely on a corresponding haskell
type. We already have a lot of good parsers for base types and it
would be unforgivable not to employ them :)
But there is a problem: all of them are checking
Field
variable foroid (or typename), which in our case is far from required value.
So (from my point of view) it's naturally to make
typeOid
fieldoptional.
Aside of type unsafety there might be another pitfalls like degraded
performance or excessive memory consumption, but I'm neither expert in
haskell nor familiar with internals of
postgresql-simple
to say forsure they aren't the cases here.
All tests were passed against PostgreSQL v.9.5.1.