-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support backend-specific functionality; JSON support for postgres.
Squashed commit of the following: commit c3791db7e06edf0c338d082958073945cd274f39 Author: Anton Ekblad <anton@ekblad.cc> Date: Wed May 8 03:35:35 2019 +0200 JSON support. commit c675b1c7447f108908a4c7795848f91352770e19 Author: Anton Ekblad <anton@ekblad.cc> Date: Tue May 7 15:43:48 2019 +0200 Add unsafe operator function, to define custom SQL operators. commit 45d71ae1c791f1b93bada94b52e6e40015c8f2eb Author: Anton Ekblad <anton@ekblad.cc> Date: Wed May 1 02:49:35 2019 +0200 Add unsafe sink function. commit c3083c016794bf0c26217c75b9570ce49c2aa9d9 Author: Anton Ekblad <anton@ekblad.cc> Date: Wed May 1 02:18:35 2019 +0200 Fix prepared statement type inference with constrained backend. commit f64c4d069ca3eb574539efdd754939b92b4297a5 Author: Anton Ekblad <anton@ekblad.cc> Date: Mon Apr 29 21:31:28 2019 +0200 Parameterise SeldaT over backend. #80
- Loading branch information
Showing
29 changed files
with
470 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ packages: | |
selda-sqlite/ | ||
selda-postgresql/ | ||
selda-tests/ | ||
selda-json/ | ||
./ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
cabal-version: >=1.10 | ||
name: selda-json | ||
version: 0.1.0.0 | ||
synopsis: JSON support for the Selda database library. | ||
-- description: | ||
homepage: https://selda.link | ||
-- bug-reports: | ||
license: MIT | ||
license-file: LICENSE | ||
author: Anton Ekblad | ||
maintainer: anton@ekblad.cc | ||
-- copyright: | ||
category: Database | ||
build-type: Simple | ||
extra-source-files: CHANGELOG.md | ||
|
||
library | ||
exposed-modules: | ||
Database.Selda.JSON | ||
-- other-modules: | ||
-- other-extensions: | ||
build-depends: | ||
aeson >=1.0 && <1.5 | ||
, base >=4.8 && <5 | ||
, bytestring >=0.10 && <0.11 | ||
, selda >=0.4 && <0.5 | ||
, text >=1.0 && <1.3 | ||
hs-source-dirs: src | ||
default-language: Haskell2010 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{-# LANGUAGE GADTs, OverloadedStrings #-} | ||
module Database.Selda.JSON (JSONBackend (..)) where | ||
import Database.Selda (Text, Col, Inner) | ||
import Database.Selda.Backend | ||
import Database.Selda.Unsafe (sink, sink2) | ||
import Data.Aeson (Value (Null), encode, decode') | ||
import qualified Data.ByteString.Lazy as BSL (ByteString, fromStrict, toStrict) | ||
import Data.Text.Encoding (encodeUtf8) | ||
|
||
class JSONValue a | ||
instance JSONValue Value | ||
instance JSONValue a => JSONValue (Maybe a) | ||
|
||
-- | Any backend that supports JSON lookups in queries. | ||
class JSONBackend b where | ||
-- | Look up the given key in the given JSON column. | ||
(~>) :: JSONValue a => Col b a -> Col b Text -> Col b (Maybe Value) | ||
infixl 8 ~> | ||
|
||
-- | Convert the given JSON column to plain text. | ||
jsonToText :: Col b Value -> Col b Text | ||
|
||
instance JSONBackend b => JSONBackend (Inner b) where | ||
(~>) = sink2 (~>) | ||
jsonToText = sink jsonToText | ||
|
||
decodeError :: Show a => a -> b | ||
decodeError x = error $ "fromSql: json column with invalid json: " ++ show x | ||
|
||
typeError :: Show a => a -> b | ||
typeError x = error $ "fromSql: json column with non-text value: " ++ show x | ||
|
||
textToLazyBS :: Text -> BSL.ByteString | ||
textToLazyBS = BSL.fromStrict . encodeUtf8 | ||
|
||
instance SqlType Value where | ||
mkLit = LCustom TJSON . LBlob . BSL.toStrict . encode | ||
sqlType _ = TJSON | ||
defaultValue = mkLit Null | ||
fromSql (SqlBlob t) = maybe (decodeError t) id (decode' $ BSL.fromStrict t) | ||
fromSql (SqlString t) = maybe (decodeError t) id (decode' $ textToLazyBS t) | ||
fromSql x = typeError x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.