Closed shane-circuithub closed 12 months ago
@ocharles I figured out how to make Statement
an actual Monad
so that it doesn't need QualifiedDo
. Let me make that change first before you put a bunch of time into reviewing this because it will move a bunch of things around
@ocharles Actually, no, I can't make it a full Monad
. Maybe Bind
from semigroupoids
but not without other compromises and it's not clear that it's a win overall. Maybe you could familiarise yourself with this at first and then we could have a chat about the trade-offs and figure out how to move forward.
Closing in favour of #250.
The motivation behind this PR was to add support for PostreSQL's
WITH
syntax at the statement level, which gives the ability to, e.g., delete some rows from a table and then re-insert those deleted rows into another table, without any round-trips between the application and the database.However, this necessitated some changes to how
Returning
works, which also bled into our interface for running queries, so it's quite an invasive change.Returning
previously bundled two different concepts together: whether or not to generate aRETURNING
clause in the SQL for a manipulation statement, and how to decode the returned rows (if any). It was necessary to break these concepts apart because withWITH
we need the ability to generate manipulation statements withRETURNING
clauses that are never actually decoded at all (the results just get passed to the next statement without touching the application).This resulted in the
Rows
datatype which this PR introduces, which has the following values:select
,insert
,update
anddelete
have all been replaced with a single (polymorphic)run
function.run
takesRows
as an argument (in addition to a statement such as aQuery
or anInsert
) that specifies whether the result of the statement should be read as a single row or a list of rows (or a number of rows affected). This also gains us support for decoding the result of a query directly to aVector
which brings a performance improvement over lists for those who need it.The
Single
,Maybe
,List
andVector
values for theRows
parameter can only be used withQuery
s or manipulation statements with aRETURNING
clause — statements without a returning clause can only useVoid
orRowsAffected
.And finally, to actually generate compound statements using
WITH
, theRel8.Statement.Do
module exports a pseudo-monadic interface can be used with theQualifiedDo
extension. This binds the result of a previous statement to aQuery
, which can then be used by a subsequent statement (e.g., as therows
parameter of anInsert
statement).