Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable value-level codecs #61

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
090a5a4
Update postgresql-client to version that exposes unsafeQuery
JordanMartinez Jun 10, 2021
64507b8
Temporarily only build src dir
JordanMartinez Jun 11, 2021
c823b72
Temporarily ignore SQLite3 code
JordanMartinez Jun 11, 2021
72c24b4
Temporarily ignore insert update and delete
JordanMartinez Jun 11, 2021
c6a3ac7
Provide value-based codecs for query
JordanMartinez Jun 11, 2021
45cc056
Implement insert with value-level codecs
JordanMartinez Jun 11, 2021
c5a1b54
Remove unused forall thing, tup
JordanMartinez Jun 11, 2021
95c97af
Export query' and query1'
JordanMartinez Jun 11, 2021
60691d5
Further clean up imports
JordanMartinez Jun 11, 2021
c1cbe9d
Finish cleaning up class instance
JordanMartinez Jun 11, 2021
e439409
Reenable delete
JordanMartinez Jun 11, 2021
31e384f
Reenable update
JordanMartinez Jun 11, 2021
ec46ca3
Export deleteFrom
JordanMartinez Jun 11, 2021
83bfc53
Implement insert and insert1
JordanMartinez Jun 11, 2021
dfab960
Remove commented out constraints
JordanMartinez Jun 11, 2021
ed3b14b
Almost finish insert and insert1
JordanMartinez Jun 11, 2021
3e18ca7
Remove remaining comment
JordanMartinez Jun 11, 2021
35d104b
Fix imports
JordanMartinez Jun 11, 2021
59614c0
Comment out unused code to prevent warning
JordanMartinez Jun 11, 2021
027377c
Move SQLite3 code back if it compiles
JordanMartinez Jun 11, 2021
f20eb2e
Specify what the database decoder's input and output types will be
JordanMartinez Jun 12, 2021
1b242de
Reimplement SQLite3 backend with value-level codecs
JordanMartinez Jun 12, 2021
aff12e3
Reenable Lit for SQLite3 and provide lit'
JordanMartinez Jun 12, 2021
4eae180
Undo any unnecessary changes if original still works
JordanMartinez Jun 12, 2021
6943a25
Remove old comment
JordanMartinez Jun 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ in upstream
, "validation"
]
, repo = "https://github.com/jordanmartinez/purescript-postgresql-client.git"
, version = "updateTov0.14.1"
, version = "exposeUnsafeQuery"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is using my fork temporarily.

}
with polyform =
{ dependencies =
Expand Down
2 changes: 1 addition & 1 deletion spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ You can edit this file as you like.
, "variant"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs", "guide/src/**/*.purs" ]
, sources = [ "src/**/*.purs" ]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be reverted before a final PR is merged.

}
8 changes: 4 additions & 4 deletions src/Selda/Lit.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import Selda.Expr (Expr(..), Literal(..), None(..))
import Selda.Inner (Inner)
import Selda.PG (litPG)
import Selda.PG.Class (BackendPGClass)
import Selda.SQLite3 (litSQLite3)
import Selda.SQLite3.Class (BackendSQLite3Class)
-- import Selda.SQLite3 (litSQLite3)
-- import Selda.SQLite3.Class (BackendSQLite3Class)
import Simple.JSON (class WriteForeign)
import Unsafe.Coerce (unsafeCoerce)

Expand Down Expand Up @@ -48,8 +48,8 @@ else instance litMaybe ∷ Lit b a ⇒ Lit b (Maybe a) where
else instance ilitPG ∷ ToSQLValue a ⇒ Lit BackendPGClass a where
litImpl = litPG

else instance ilitSQLite3 ∷ WriteForeign a ⇒ Lit BackendSQLite3Class a where
litImpl = litSQLite3
-- else instance ilitSQLite3 ∷ WriteForeign a ⇒ Lit BackendSQLite3Class a where
-- litImpl = litSQLite3

else instance litInner ∷ Lit s a ⇒ Lit (Inner s) a where
litImpl a = case (litImpl a ∷ Col s a) of Col e → Col e
113 changes: 94 additions & 19 deletions src/Selda/PG/Aff.purs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
module Selda.PG.Aff
( insert_
, insert
, insert1
, insert_'
-- , insert
-- , insert1
, insert1_
, insert1_'
, query
, query'
, query1
, query1'
, PGSelda
, deleteFrom
, update
Expand All @@ -17,16 +21,20 @@ import Control.Monad.Reader (ReaderT)
import Data.Array (head) as Array
import Data.Either (Either)
import Data.Maybe (Maybe)
import Database.PostgreSQL (class FromSQLRow, Connection, PGError)
import Database.PostgreSQL (class FromSQLRow, class ToSQLRow, Connection, PGError, fromSQLRow)
import Effect.Aff (Aff)
import Foreign (Foreign)
import Heterogeneous.Folding (class HFoldl)
import Selda (Col, Table)
import Selda.Col (class GetCols)
import Selda.PG.Class (class InsertRecordIntoTableReturning, BackendPGClass)
import Selda.PG.Class as Selda.PG
import Selda.Query (limit)
import Selda.Query.Class (class GenericInsert, runSelda)
import Selda.Query.Type (FullQuery(..))
import Selda.Query.Utils (class ColsToPGHandler, class TableToColsWithoutAlias)
import Selda.Query.Utils (class ColsToPGHandler, class TableToColsWithoutAlias, RecordToArrayForeign, RecordToTuple(..))
import Selda.Table.Constraint (class CanInsertColumnsIntoTable)
import Type.Proxy (Proxy(..))

type PGSelda = ExceptT PGError (ReaderT Connection Aff)

Expand All @@ -35,34 +43,90 @@ type B = BackendPGClass
insert_
∷ ∀ t r
. GenericInsert BackendPGClass PGSelda t r
⇒ HFoldl
(RecordToArrayForeign BackendPGClass)
(Array Foreign)
{ | r }
(Array Foreign)
⇒ Connection → Table t → Array { | r } → Aff (Either PGError Unit)
insert_ conn t r = runSelda conn $ Selda.PG.insert_ t r
insert_ conn t r =
runSelda conn $ Selda.PG.insert_ t r

insert_'
∷ ∀ t r
. GenericInsert BackendPGClass PGSelda t r
⇒ Connection
→ Table t
→ ({ | r } → Array Foreign)
→ Array { | r }
→ Aff (Either PGError Unit)
insert_' conn t decodeRow r =
runSelda conn $ Selda.PG.insert_' t decodeRow r

insert1_
∷ ∀ r t
. GenericInsert BackendPGClass PGSelda t r
⇒ Connection → Table t → { | r } → Aff (Either PGError Unit)
⇒ HFoldl
(RecordToArrayForeign BackendPGClass)
(Array Foreign)
{ | r }
(Array Foreign)
⇒ Connection
→ Table t
→ { | r }
→ Aff (Either PGError Unit)
insert1_ conn t r = runSelda conn $ Selda.PG.insert1_ t r

insert
∷ ∀ r t tr
. InsertRecordIntoTableReturning r t tr
⇒ Connection → Table t → Array { | r } → Aff (Either PGError (Array { | tr }))
insert conn t r = runSelda conn $ Selda.PG.insert t r
insert1_'
∷ ∀ r t
. GenericInsert BackendPGClass PGSelda t r
⇒ Connection
→ Table t
→ ({ | r } → Array Foreign)
→ { | r }
→ Aff (Either PGError Unit)
insert1_' conn t encode r = runSelda conn $ Selda.PG.insert1_' t encode r

-- insert
-- ∷ ∀ r t ret rTuple tr trTuple s
-- . InsertRecordIntoTableReturning r t ret
-- ⇒ ToSQLRow rTuple
-- ⇒ FromSQLRow trTuple
-- ⇒ HFoldl RecordToTuple Unit { | r } rTuple
-- ⇒ ColsToPGHandler s tr trTuple ret
-- ⇒ TableToColsWithoutAlias s t tr
-- ⇒ Connection → Table t → Array { | r } → Aff (Either PGError (Array { | ret }))
-- insert conn t r = runSelda conn $ Selda.PG.insert t r

insert1
∷ ∀ r t tr
. InsertRecordIntoTableReturning r t tr
⇒ Connection → Table t → { | r } → Aff (Either PGError { | tr })
insert1 conn t r = runSelda conn $ Selda.PG.insert1 t r
-- insert1
-- ∷ ∀ r t ret rTuple tr trTuple s
-- . InsertRecordIntoTableReturning r t ret
-- ⇒ ToSQLRow rTuple
-- ⇒ FromSQLRow trTuple
-- ⇒ HFoldl RecordToTuple Unit { | r } rTuple
-- ⇒ ColsToPGHandler s tr trTuple ret
-- ⇒ TableToColsWithoutAlias s t tr
-- ⇒ Connection → Table t → { | r } → Aff (Either PGError { | ret })
-- insert1 conn t r = runSelda conn $ Selda.PG.insert1 t r

query
∷ ∀ o i tup
. ColsToPGHandler B i tup o
⇒ GetCols i
⇒ FromSQLRow tup
⇒ Connection → FullQuery B (Record i) → Aff (Either PGError (Array { | o }))
query conn q = runSelda conn $ Selda.PG.query q
⇒ Connection
→ FullQuery B (Record i)
→ Aff (Either PGError (Array { | o }))
query conn = runSelda conn <<< Selda.PG.query

query'
∷ ∀ o i
. GetCols i
⇒ Connection
→ FullQuery B { | i }
→ (Array Foreign -> Either String { | o })
→ Aff (Either PGError (Array { | o }))
query' conn q decodeRow = runSelda conn $ Selda.PG.query' q decodeRow
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eta-reducing these produces compiler errors.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean? there's $ so the term cannot be eta-reduced

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was thinking query' conn = runSelda conn <<< Selda.PG.query'. It's been so long since I worked on this, I'm not sure anymore.


query1
∷ ∀ o i tup
Expand All @@ -72,7 +136,18 @@ query1
⇒ Connection
→ FullQuery B (Record i)
→ Aff (Either PGError (Maybe { | o }))
query1 conn (FullQuery q) = query conn (FullQuery (limit 1 >>= \_ → q)) <#> map Array.head
query1 conn (FullQuery q) =
query conn (FullQuery (limit 1 >>= \_ → q)) <#> map Array.head

query1'
∷ ∀ o i
. GetCols i
⇒ Connection
→ FullQuery B (Record i)
→ (Array Foreign -> Either String { | o })
→ Aff (Either PGError (Maybe { | o }))
query1' conn (FullQuery q) decodeRow =
query' conn (FullQuery (limit 1 >>= \_ → q)) decodeRow <#> map Array.head

deleteFrom
∷ ∀ r r'
Expand Down
Loading