git.iko.soy/typeable/postgresql-simple
on master help sign in
Ported documentation fixes from mysql-simple
47d306fLeon P Smith
src/Database/PostgreSQL/Simple.hs+22−3
@@ -697,10 +697,11 @@ fmtError msg q xs = throw FormatError {
697-- >
697-- >
698-- > import Database.PostgreSQL.Simple
698-- > import Database.PostgreSQL.Simple
699-- >
699-- >
700−-- > hello :: IO [Only Int]
700+-- > hello :: IO Int
701-- > hello = do
701-- > hello = do
702-- > conn <- connectPostgreSQL ""
702-- > conn <- connectPostgreSQL ""
703−-- > query_ conn "select 2 + 2"
703+-- > [Only i] <- query_ conn "select 2 + 2"
704+-- > return i
704--
705--
705-- A 'Query' value does not represent the actual query that will be
706-- A 'Query' value does not represent the actual query that will be
706-- executed, but is a template for constructing the final query.
707-- executed, but is a template for constructing the final query.
@@ -761,11 +762,26 @@ fmtError msg q xs = throw FormatError {
761-- The same kind of problem can arise with string literals if you have
762-- The same kind of problem can arise with string literals if you have
762-- the @OverloadedStrings@ language extension enabled. Again, just
763-- the @OverloadedStrings@ language extension enabled. Again, just
763-- use an explicit type signature if this happens.
764-- use an explicit type signature if this happens.
765+--
766+-- Finally, remember that the compiler must be able to infer the type
767+-- of a query's /results/ as well as its parameters. We might like
768+-- the following example to work:
769+--
770+-- > print =<< query_ conn "select 2 + 2"
771+--
772+-- Unfortunately, while a quick glance tells us that the result type
773+-- should be a single row containing a single numeric column, the
774+-- compiler has no way to infer what the types are. We can easily fix
775+-- this by providing an explicit type annotation:
776+--
777+-- > xs <- query_ conn "select 2 + 2"
778+-- > print (xs :: [Only Int])
764
779
765-- $only_param
780-- $only_param
766--
781--
767-- Haskell lacks a single-element tuple type, so if you have just one
782-- Haskell lacks a single-element tuple type, so if you have just one
768−-- value you want substituted into a query, what should you do?
783+-- value you want substituted into a query or a single-column result,
784+-- what should you do?
769--
785--
770-- The obvious approach would appear to be something like this:
786-- The obvious approach would appear to be something like this:
771--
787--
@@ -780,6 +796,9 @@ fmtError msg q xs = throw FormatError {
780--
796--
781-- > execute conn "insert into users (first_name) values (?)"
797-- > execute conn "insert into users (first_name) values (?)"
782-- > ["Nuala"]
798-- > ["Nuala"]
799+--
800+-- A row of /n/ query results is represented using an /n/-tuple, so
801+-- you should use 'Only' to represent a single-column result.
783
802
784-- $in
803-- $in
785--
804--