packages feed

postgresql-typed 0.4.3 → 0.4.4

raw patch · 7 files changed

+35/−14 lines, 7 files

Files

Database/PostgreSQL/Typed/Array.hs view
@@ -39,10 +39,20 @@   pgArrayDelim :: PGTypeName ta -> Char   pgArrayDelim _ = ',' -instance (PGArrayType ta t, PGParameter t a) => PGParameter ta (PGArray a) where+instance+#if __GLASGOW_HASKELL__ >= 710+    {-# OVERLAPPING #-}+#endif+    (PGArrayType ta t, PGParameter t a) => PGParameter ta (PGArray a) where   pgEncode ta l = buildPGValue $ BSB.char7 '{' <> mconcat (intersperse (BSB.char7 $ pgArrayDelim ta) $ map el l) <> BSB.char7 '}' where     el Nothing = BSB.string7 "null"     el (Just e) = pgDQuote (pgArrayDelim ta : "{}") $ pgEncode (pgArrayElementType ta) e+#if __GLASGOW_HASKELL__ >= 710+-- |Allow entirely non-null arrays as parameter inputs only.+-- (Only supported on ghc >= 7.10 due to instance overlap.)+instance {-# OVERLAPPABLE #-} (PGArrayType ta t, PGParameter t a) => PGParameter ta [a] where+  pgEncode ta = pgEncode ta . map Just+#endif instance (PGArrayType ta t, PGColumn t a) => PGColumn ta (PGArray a) where   pgDecode ta a = either (error . ("pgDecode array (" ++) . (++ ("): " ++ BSC.unpack a))) id $ P.parseOnly pa a where     pa = P.char '{' *> P.sepBy (P.skipSpace *> el <* P.skipSpace) (P.char (pgArrayDelim ta)) <* P.char '}' <* P.endOfInput
Database/PostgreSQL/Typed/Dynamic.hs view
@@ -19,6 +19,7 @@ #endif import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as BSC+import qualified Data.ByteString.Lazy as BSL import Data.Monoid ((<>)) import Data.Int #ifdef USE_SCIENTIFIC@@ -45,6 +46,7 @@   pgEncodeRep :: a -> PGValue   default pgEncodeRep :: PGParameter t a => a -> PGValue   pgEncodeRep x = pgEncodeValue unknownPGTypeEnv (pgTypeOf x) x+  -- |Produce a literal value for interpolation in a SQL statement.  Using 'pgSafeLiteral' is usually safer as it includes type cast.   pgLiteralRep :: a -> BS.ByteString   default pgLiteralRep :: PGParameter t a => a -> BS.ByteString   pgLiteralRep x = pgLiteral (pgTypeOf x) x@@ -62,7 +64,7 @@ pgLiteralString :: PGRep t a => a -> String pgLiteralString = BSC.unpack . pgLiteralRep --- |Produce a safely type-cast literal value for interpolation in a SQL statement.+-- |Produce a safely type-cast literal value for interpolation in a SQL statement, e.g., "'123'::integer". pgSafeLiteral :: PGRep t a => a -> BS.ByteString pgSafeLiteral x = pgLiteralRep x <> BSC.pack "::" <> fromString (pgTypeName (pgTypeOf x)) @@ -104,11 +106,11 @@ instance PGRep "uuid" UUID.UUID #endif --- |Create an expression that literally substitutes each instance of @${expr}@ for the result of @pgSafeLiteral expr@.--- This lets you do safe, type-driven literal substitution into SQL fragments without needing a full query, bypassing placeholder inference and any prepared queries.+-- |Create an expression that literally substitutes each instance of @${expr}@ for the result of @pgSafeLiteral expr@, producing a lazy 'BSL.ByteString'.+-- This lets you do safe, type-driven literal substitution into SQL fragments without needing a full query, bypassing placeholder inference and any prepared queries, for example when using 'Database.PostgreSQL.Typed.Protocol.pgSimpleQuery' or 'Database.PostgreSQL.Typed.Protocol.pgSimpleQueries_'. -- Unlike most other TH functions, this does not require any database connection. pgSubstituteLiterals :: String -> TH.ExpQ-pgSubstituteLiterals sql = TH.AppE (TH.VarE 'BS.concat) . TH.ListE <$> ssl (sqlSplitExprs sql) where+pgSubstituteLiterals sql = TH.AppE (TH.VarE 'BSL.fromChunks) . TH.ListE <$> ssl (sqlSplitExprs sql) where   ssl :: SQLSplit String 'True -> TH.Q [TH.Exp]   ssl (SQLLiteral s l) = (TH.VarE 'fromString `TH.AppE` stringE s :) <$> ssp l   ssl SQLSplitEnd = return []
Database/PostgreSQL/Typed/Enum.hs view
@@ -57,7 +57,8 @@     valn = map (\[PGTextValue v] -> let u = BSC.unpack v in (TH.mkName $ valnf u, map (TH.IntegerL . fromIntegral) $ BS.unpack v, TH.StringL u)) vals   dv <- TH.newName "x"   return-    [ TH.DataD [] typn [] (map (\(n, _, _) -> TH.NormalC n []) valn) [''Eq, ''Ord, ''Enum, ''Ix, ''Bounded, ''Typeable]+    [ TH.DataD [] typn [] (map (\(n, _, _) -> TH.NormalC n []) valn)+      [''Eq, ''Ord, ''Enum, ''Ix, ''Bounded, ''Typeable]     , TH.InstanceD [] (TH.ConT ''Show `TH.AppT` typt)       [ TH.FunD 'show $ map (\(n, _, v) -> TH.Clause [TH.ConP n []]         (TH.NormalB $ TH.LitE v) []) valn
Database/PostgreSQL/Typed/Protocol.hs view
@@ -88,13 +88,14 @@   , pgDBPort :: PortID -- ^ The port, likely either @PortNumber 5432@ or @UnixSocket \"\/tmp\/.s.PGSQL.5432\"@   , pgDBName :: BS.ByteString -- ^ The name of the database   , pgDBUser, pgDBPass :: BS.ByteString+  , pgDBParams :: [(BS.ByteString, BS.ByteString)] -- ^ Extra parameters to set for the connection (e.g., ("TimeZone", "UTC"))   , pgDBDebug :: Bool -- ^ Log all low-level server messages   , pgDBLogMessage :: MessageFields -> IO () -- ^ How to log server notice messages (e.g., @print . PGError@)   }  instance Eq PGDatabase where-  PGDatabase h1 s1 n1 u1 p1 _ _ == PGDatabase h2 s2 n2 u2 p2 _ _ =-    h1 == h2 && s1 == s2 && n1 == n2 && u1 == u2 && p1 == p2+  PGDatabase h1 s1 n1 u1 p1 l1 _ _ == PGDatabase h2 s2 n2 u2 p2 l2 _ _ =+    h1 == h2 && s1 == s2 && n1 == n2 && u1 == u2 && p1 == p2 && l1 == l2  -- |An established connection to the PostgreSQL server. -- These objects are not thread-safe and must only be used for a single request at a time.@@ -208,7 +209,7 @@ -- |A database connection with sane defaults: -- localhost:5432:postgres defaultPGDatabase :: PGDatabase-defaultPGDatabase = PGDatabase "localhost" (PortNumber 5432) (BSC.pack "postgres") (BSC.pack "postgres") BS.empty False defaultLogMessage+defaultPGDatabase = PGDatabase "localhost" (PortNumber 5432) (BSC.pack "postgres") (BSC.pack "postgres") BS.empty [] False defaultLogMessage  connDebug :: PGConnection -> Bool connDebug = pgDBDebug . connDatabase@@ -420,7 +421,7 @@         , connInput = input         , connTransaction = tr         }-  pgSend c $ StartupMessage+  pgSend c $ StartupMessage $     [ (BSC.pack "user", pgDBUser db)     , (BSC.pack "database", pgDBName db)     , (BSC.pack "client_encoding", BSC.pack "UTF8")@@ -428,7 +429,7 @@     , (BSC.pack "bytea_output", BSC.pack "hex")     , (BSC.pack "DateStyle", BSC.pack "ISO, YMD")     , (BSC.pack "IntervalStyle", BSC.pack "iso_8601")-    ]+    ] ++ pgDBParams db   pgFlush c   conn c   where@@ -569,6 +570,7 @@   got c r = return (rowsAffected c, r)  -- |A simple query which may contain multiple queries (separated by semi-colons) whose results are all ignored.+-- This function can also be used for \"SET\" parameter queries if necessary, but it's safer better to use 'pgDBParams'. pgSimpleQueries_ :: PGConnection -> BSL.ByteString -- ^ SQL string                    -> IO () pgSimpleQueries_ h sql = do@@ -581,6 +583,7 @@   res (CommandComplete _) = go   res EmptyQueryResponse = go   res (DataRow _) = go+  res (ParameterStatus _ _) = go   res (ReadyForQuery _) = return ()   res m = fail $ "pgSimpleQueries_: unexpected response: " ++ show m 
Database/PostgreSQL/Typed/Query.hs view
@@ -47,6 +47,10 @@   -- |Change the raw SQL query stored within this query.   -- This is unsafe because the query has already been type-checked, so any change must not change the number or type of results or placeholders (so adding additional static WHERE or ORDER BY clauses is generally safe).   -- This is useful in cases where you need to construct some part of the query dynamically, but still want to infer the result types.+  -- If you want to add dynamic values to the query, it's best to use 'Database.PostgreSQL.Typed.Dynamic.pgSafeLiteral'.+  -- For example:+  --+  -- > [pgSQL|SELECT a FROM t|] `unsafeModifyQuery` (<> (" WHERE a = " <> pgSafeLiteral x))   unsafeModifyQuery :: q -> (BS.ByteString -> BS.ByteString) -> q class PGQuery q PGValues => PGRawQuery q @@ -253,8 +257,8 @@ -- -- [@?@] To disable nullability inference, treating all result values as nullable, thus returning 'Maybe' values regardless of inferred nullability. This makes unexpected NULL errors impossible. -- [@!@] To disable nullability inference, treating all result values as /not/ nullable, thus only returning 'Maybe' where requested. This is makes unexpected NULL errors more likely.--- [@$@] To create a 'PGPreparedQuery' rather than a 'PGSimpleQuery', by default inferring parameter types.--- [@$(type,...)@] To specify specific types to a prepared query (see <http://www.postgresql.org/docs/current/static/sql-prepare.html> for details).+-- [@$@] To create a 'PGPreparedQuery' (using placeholder parameters) rather than the default 'PGSimpleQuery' (using literal substitution).+-- [@$(type,...)@] To specify specific types for a prepared query (see <http://www.postgresql.org/docs/current/static/sql-prepare.html> for details), rather than inferring parameter types by default. -- [@#@] Only do literal @${}@ substitution using 'pgSubstituteLiterals' and return a string, not a query. --  -- 'pgSQL' can also be used at the top-level to execute SQL statements at compile-time (without any parameters and ignoring results).
postgresql-typed.cabal view
@@ -1,5 +1,5 @@ Name:          postgresql-typed-Version:       0.4.3+Version:       0.4.4 Cabal-Version: >= 1.8 License:       BSD3 License-File:  COPYING
test/Connect.hs view
@@ -10,5 +10,6 @@   , pgDBName = "templatepg"   , pgDBUser = "templatepg"   , pgDBDebug = True+  , pgDBParams = [("TimeZone", "UTC")]   }