hdbc-aeson (empty) → 0.1.0.0
raw patch · 5 files changed
+137/−0 lines, 5 filesdep +HDBCdep +aesondep +basesetup-changed
Dependencies added: HDBC, aeson, base, convertible, scientific, text, unordered-containers, vector
Files
- HDBC/Aeson.hs +74/−0
- LICENSE +20/−0
- README.md +9/−0
- Setup.hs +2/−0
- hdbc-aeson.cabal +32/−0
+ HDBC/Aeson.hs view
@@ -0,0 +1,74 @@+{-# LANGUAGE OverloadedStrings, MultiParamTypeClasses #-} +module HDBC.Aeson where+import Data.Aeson+import Data.Convertible +import Database.HDBC+import qualified Data.Text as T+import qualified Data.Text.Encoding as T++-- | Run a SQL query and return a value that can be cast via +-- a FromJSON instance.+queryJ :: (IConnection a, FromJSON b)+ => a+ -> Statement+ -> [SqlValue]+ -> IO b+queryJ conn stmt vs = do+ _ <- execute stmt vs+ rows <- fetchAllRowsMap' stmt+ let result = fromJSON . toJSON $ rows+ case result of + Error e -> error $ "Could not parse via JSON: " ++ e+ Success x -> return x++-- | A quick way to do a query and cast it to a FromJSON. +quickQueryJ :: (IConnection a, FromJSON b)+ => a+ -> String+ -> [SqlValue]+ -> IO b+quickQueryJ conn query xs = do+ stmt <- prepare conn query+ queryJ conn stmt xs+++instance ToJSON SqlValue where+ toJSON (SqlByteString x) = String . T.decodeUtf8 $ x+ toJSON (SqlInt32 x) = Number $ fromIntegral x+ toJSON (SqlInteger x) = Number $ fromIntegral x+ toJSON (SqlRational x) = Number $ realToFrac x+ toJSON (SqlDouble x) = Number $ realToFrac x+ toJSON (SqlBool x) = Bool x+ toJSON (SqlLocalTime x) = String . T.pack . show $ x+ toJSON SqlNull = Null+ toJSON x = error $ "Please implement ToJSON instance for SqlValue: " ++ show x++instance Convertible SqlValue Value where+ safeConvert (SqlString x) = return . String . T.pack $ x+ safeConvert (SqlByteString x) = return . String . T.decodeUtf8 $ x+ safeConvert (SqlWord32 x) = return . Number . fromIntegral $ x+ safeConvert (SqlWord64 x) = return . Number . fromIntegral $ x+ safeConvert (SqlInt32 x) = return . Number . fromIntegral $ x+ safeConvert (SqlInt64 x) = return . Number . fromIntegral $ x+ safeConvert (SqlInteger x) = return . Number . fromIntegral $ x+ safeConvert (SqlChar x) = return . String . T.pack $ [x]+ safeConvert (SqlBool x) = return . Bool $ x+ safeConvert (SqlDouble x) = return . Number . realToFrac $ x+ safeConvert (SqlRational x) = return . Number . realToFrac $ x+ safeConvert (SqlNull) = return Null+ safeConvert x = error $ "Please implement Convertible SqlValue Value for " ++ show x+ {-+ implement these later:++ SqlLocalDate Day + SqlLocalTimeOfDay TimeOfDay + SqlZonedLocalTimeOfDay TimeOfDay TimeZone + SqlLocalTime LocalTime + SqlZonedTime ZonedTime + SqlUTCTime UTCTime + SqlDiffTime NominalDiffTime + SqlPOSIXTime POSIXTime + -}+++
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Daniel Choi++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.
+ README.md view
@@ -0,0 +1,9 @@+# hdbc-aeson++Provides a way to reuse FromJSON instances to unmarshall+values from an HDBC database to Haskell values. See+HDBC.Aeson module for API.++## Author++* Daniel Choi <dhchoi@gmail.com>
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hdbc-aeson.cabal view
@@ -0,0 +1,32 @@+-- Initial hdbc-aeson.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++name: hdbc-aeson+version: 0.1.0.0+synopsis: Deserialize from HDBC rows to FromJSON instances+-- description: +homepage: https://github.com/danchoi/hdbc-aeson+license: MIT+license-file: LICENSE+author: Daniel Choi+maintainer: dhchoi@gmail.com+-- copyright: +category: Database+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10++library+ exposed-modules: HDBC.Aeson+ -- other-modules: + -- other-extensions: + build-depends: base >=4.6 && <4.8+ , HDBC+ , text+ , aeson >= 0.8+ , convertible+ , unordered-containers+ , scientific >= 0.3.3.0+ , vector+ -- hs-source-dirs: + default-language: Haskell2010