selda-postgresql 0.1.8.1 → 0.1.8.2
raw patch · 4 files changed
+42/−43 lines, 4 filesdep ~bytestringdep ~timePVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
Dependency ranges changed: bytestring, time
API changes (from Hackage documentation)
+ Database.Selda.PostgreSQL: PGConnectionString :: Text -> Maybe Text -> PGConnectInfo
+ Database.Selda.PostgreSQL: [pgConnectionString] :: PGConnectInfo -> Text
+ Database.Selda.PostgreSQL: instance Data.String.IsString Database.Selda.PostgreSQL.PGConnectInfo
Files
- LICENSE +0/−21
- selda-postgresql.cabal +3/−4
- src/Database/Selda/PostgreSQL.hs +25/−8
- src/Database/Selda/PostgreSQL/Encoding.hs +14/−10
− LICENSE
@@ -1,21 +0,0 @@-MIT License--Copyright (c) 2017-2019 Anton Ekblad--Permission is hereby granted, free of charge, to any person obtaining a copy-of this software and associated documentation files (the "Software"), to deal-in the Software without restriction, including without limitation the rights-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell-copies of the Software, and to permit persons to whom the Software is-furnished to do so, subject to the following conditions:--The above copyright notice and this permission notice shall be included in all-copies or substantial portions of the Software.--THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE-SOFTWARE.
selda-postgresql.cabal view
@@ -1,12 +1,11 @@ name: selda-postgresql-version: 0.1.8.1+version: 0.1.8.2 synopsis: PostgreSQL backend for the Selda database EDSL. description: PostgreSQL backend for the Selda database EDSL. Requires the PostgreSQL @libpq@ development libraries to be installed. homepage: https://github.com/valderman/selda license: MIT-license-file: LICENSE author: Anton Ekblad maintainer: anton@ekblad.cc category: Database@@ -29,7 +28,7 @@ CPP build-depends: base >=4.9 && <5- , bytestring >=0.9 && <0.11+ , bytestring >=0.9 && <0.12 , exceptions >=0.8 && <0.11 , selda >=0.5 && <0.6 , selda-json >=0.1 && <0.2@@ -38,7 +37,7 @@ build-depends: postgresql-binary >=0.12 && <0.13 , postgresql-libpq >=0.9 && <0.10- , time >=1.5 && <1.10+ , time >=1.5 && <1.12 , uuid-types >=1.0 && <1.1 hs-source-dirs: src
src/Database/Selda/PostgreSQL.hs view
@@ -10,6 +10,7 @@ import Data.Monoid #endif import Data.ByteString (ByteString)+import Data.String (IsString (..)) import qualified Data.Text as T import Database.Selda.Backend hiding (toText) import Database.Selda.JSON@@ -23,6 +24,7 @@ import qualified Data.ByteString.Char8 as BS (pack, unpack) import Data.Dynamic import Data.Foldable (for_)+import Data.Int (Int64) import Data.Text.Encoding import Database.Selda.PostgreSQL.Encoding import Database.PostgreSQL.LibPQ hiding (user, pass, db, host)@@ -49,7 +51,18 @@ -- | Password for authentication, if necessary. , pgPassword :: Maybe T.Text }+ | PGConnectionString+ { -- | Custom connection PostgreSQL connection string.+ pgConnectionString :: T.Text+ , pgSchema :: Maybe T.Text+ } +instance IsString PGConnectInfo where+ fromString s = PGConnectionString+ { pgConnectionString = fromString s+ , pgSchema = Nothing+ }+ -- | Connect to the given database on the given host, on the default PostgreSQL -- port (5432): --@@ -98,6 +111,7 @@ , "connect_timeout=10", " " , "client_encoding=UTF8" ]+pgConnString PGConnectionString{..} = encodeUtf8 pgConnectionString #endif -- | Perform the given computation over a PostgreSQL database.@@ -180,7 +194,7 @@ -- For when we use 'autoPrimaryGen' on 'Int' field isGenericIntPrimaryKey :: SqlTypeRep -> [ColAttr] -> Bool- isGenericIntPrimaryKey ty attrs = ty == TInt && and ((`elem` attrs) <$> bigserialQue)+ isGenericIntPrimaryKey ty attrs = ty == TInt64 && and ((`elem` attrs) <$> bigserialQue) -- | Create a `SeldaBackend` for PostgreSQL `Connection` pgBackend :: Connection -- ^ PostgreSQL connection object.@@ -252,7 +266,7 @@ Right (_, vals) <- pgQueryRunner c False tableinfo [] if null vals then do- pure $ TableInfo [] [] []+ pure $ TableInfo (mkTableName tbl) [] [] [] else do Right (_, pkInfo) <- pgQueryRunner c False pkquery [] Right (_, us) <- pgQueryRunner c False uniquequery []@@ -261,7 +275,8 @@ Right (_, ixs) <- pgQueryRunner c False ixquery [] colInfos <- mapM (describe fks (map toText ixs)) vals x <- pure $ TableInfo- { tableColumnInfos = colInfos+ { tableInfoName = mkTableName tbl+ , tableColumnInfos = colInfos , tableUniqueGroups = map (map mkColName) uniques , tablePrimaryKey = [mkColName pk | [SqlString pk] <- pkInfo] }@@ -342,7 +357,7 @@ describe _ _ results = throwM $ SqlError $ "bad result from table info query: " ++ show results -pgQueryRunner :: Connection -> Bool -> T.Text -> [Param] -> IO (Either Int (Int, [[SqlValue]]))+pgQueryRunner :: Connection -> Bool -> T.Text -> [Param] -> IO (Either Int64 (Int, [[SqlValue]])) pgQueryRunner c return_lastid q ps = do mres <- execParams c (encodeUtf8 q') [fromSqlValue p | Param p <- ps] Binary unlessError c errmsg mres $ \res -> do@@ -354,7 +369,7 @@ q' | return_lastid = q <> " RETURNING LASTVAL();" | otherwise = q - getLastId res = (maybe 0 id . fmap readInt) <$> getvalue res 0 0+ getLastId res = (maybe 0 id . fmap readInt64) <$> getvalue res 0 0 pgRun :: Connection -> Dynamic -> [Param] -> IO (Int, [[SqlValue]]) pgRun c hdl ps = do@@ -427,8 +442,9 @@ mkTypeRep :: T.Text -> Either T.Text SqlTypeRep mkTypeRep "bigserial" = Right TRowID-mkTypeRep "int8" = Right TInt-mkTypeRep "bigint" = Right TInt+mkTypeRep "int4" = Right TInt32+mkTypeRep "int8" = Right TInt64+mkTypeRep "bigint" = Right TInt64 mkTypeRep "float8" = Right TFloat mkTypeRep "double precision" = Right TFloat mkTypeRep "timestamp with time zone" = Right TDateTime@@ -444,7 +460,8 @@ -- | Custom column types for postgres. pgColType :: PPConfig -> SqlTypeRep -> T.Text pgColType _ TRowID = "BIGINT"-pgColType _ TInt = "INT8"+pgColType _ TInt64 = "INT8"+pgColType _ TInt32 = "INT4" pgColType _ TFloat = "FLOAT8" pgColType _ TDateTime = "TIMESTAMP" pgColType _ TBlob = "BYTEA"
src/Database/Selda/PostgreSQL/Encoding.hs view
@@ -1,15 +1,15 @@ {-# LANGUAGE GADTs, BangPatterns, OverloadedStrings, CPP #-} -- | Encoding/decoding for PostgreSQL. module Database.Selda.PostgreSQL.Encoding- ( toSqlValue, fromSqlValue, fromSqlType, readInt, readBool+ ( toSqlValue, fromSqlValue, fromSqlType, readInt64, readBool ) where #ifdef __HASTE__ -toSqlValue, fromSqlValue, fromSqlType, readInt, readBool :: a+toSqlValue, fromSqlValue, fromSqlType, readInt64, readBool :: a toSqlValue = undefined fromSqlValue = undefined fromSqlType = undefined-readInt = undefined+readInt64 = undefined readBool = undefined #else@@ -52,9 +52,12 @@ -- | Convert a parameter into an postgres parameter triple. fromSqlValue :: Lit a -> Maybe (Oid, BS.ByteString, Format) fromSqlValue (LBool b) = Just (boolType, bytes $ Enc.bool b, Binary)-fromSqlValue (LInt n) = Just ( intType+fromSqlValue (LInt64 n) = Just ( intType , bytes $ Enc.int8_int64 $ fromIntegral n , Binary)+fromSqlValue (LInt32 n) = Just ( int32Type+ , bytes $ Enc.int4_int32 $ fromIntegral n+ , Binary) fromSqlValue (LDouble f) = Just (doubleType, bytes $ Enc.float8 f, Binary) fromSqlValue (LText s) = Just (textType, bytes $ Enc.text_strict s, Binary) fromSqlValue (LDateTime t) = Just ( timestampType@@ -74,7 +77,8 @@ -- | Get the corresponding OID for an SQL type representation. fromSqlType :: SqlTypeRep -> Oid fromSqlType TBool = boolType-fromSqlType TInt = intType+fromSqlType TInt64 = intType+fromSqlType TInt32 = int32Type fromSqlType TFloat = doubleType fromSqlType TText = textType fromSqlType TDateTime = timestampType@@ -89,9 +93,9 @@ toSqlValue :: Oid -> BS.ByteString -> SqlValue toSqlValue t val | t == boolType = SqlBool $ parse Dec.bool val- | t == intType = SqlInt $ fromIntegral $ parse (Dec.int :: Value Int64) val- | t == int32Type = SqlInt $ fromIntegral $ parse (Dec.int :: Value Int32) val- | t == int16Type = SqlInt $ fromIntegral $ parse (Dec.int :: Value Int16) val+ | t == intType = SqlInt64 $ parse (Dec.int :: Value Int64) val+ | t == int32Type = SqlInt32 $ parse (Dec.int :: Value Int32) val+ | t == int16Type = SqlInt32 $ fromIntegral $ parse (Dec.int :: Value Int16) val | t == doubleType = SqlFloat $ parse Dec.float8 val | t == blobType = SqlBlob $ parse Dec.bytea_strict val | t == uuidType = SqlBlob $ uuid2bs $ parse Dec.uuid val@@ -115,8 +119,8 @@ Left _ -> error "unable to decode value" -- | Read an Int from a binary encoded pgint8.-readInt :: BS.ByteString -> Int-readInt = fromIntegral . parse (Dec.int :: Value Int64)+readInt64 :: BS.ByteString -> Int64+readInt64 = parse (Dec.int :: Value Int64) readBool :: T.Text -> Bool readBool = go . T.map toLower