packages feed

HDBC-postgresql-hstore (empty) → 0.0.1.2

raw patch · 4 files changed

+104/−0 lines, 4 filesdep +HDBCdep +attoparsecdep +basesetup-changed

Dependencies added: HDBC, attoparsec, base, containers, text

Files

+ Database/HDBC/PostgreSQL/HStore.hs view
@@ -0,0 +1,74 @@+-- | Read and write stuff in hstore columns.+--+-- Insert crap into table:+--+-- @+-- let myStuff = [(\"test\", \"shmest\"), (\"spam\", \"eggs\"), (\"sausage\", \"salad\")]+-- withTransaction conn $ \trans -> run trans (\"INSERT INTO test_hstore (stuff) VALUES (\" ++ hsQuery myStuff  ++ \");\") (hsParams myStuff)+-- @+--+-- Parse hstore data:+--+-- @+-- rows <- quickQuery conn \"SELECT * FROM test_hstore;\" []+-- forM_ rows $ \[pk, hstuff] -> print $ (fromSql pk :: Integer, hsParse hstuff)+-- @+--++module Database.HDBC.PostgreSQL.HStore where++import Database.HDBC++import Data.Text (Text)+import Data.Text.Encoding (decodeUtf8)+import qualified Data.Text as T+import qualified Data.Attoparsec.Text as AP++import qualified Data.List as L+import qualified Data.Map as M++-- * Make a query++-- | Generate a placeholder string.+hsQuery :: [(String, String)] -> String+hsQuery = L.intercalate " || " . map (\(_, _) -> "hstore(?, ?)")++-- | Generate a parameter list.+hsParams :: [(String, String)] -> [SqlValue]+hsParams = L.concatMap (\(k, v) -> [toSql k, toSql v])++-- * Parse results++-- | Parse a SqlByteString with hstore data to a Map Text Text.+hsParse :: SqlValue -> M.Map Text Text+hsParse (SqlByteString bs) = case hstoreParser `AP.parseOnly` decodeUtf8 bs of+                                 Left err -> error err+                                 Right val -> val++-- ** Parser internals++-- | Parse hstore-formatted value.+hstoreParser :: AP.Parser (M.Map Text Text)+hstoreParser = do+    pairs <- kvPair `AP.sepBy` AP.string (T.pack ", ")+    return $! M.fromList pairs++-- | Parse one key-value pair.+kvPair :: AP.Parser (Text, Text)+kvPair = do+    key <- doubleQuoted+    AP.string $ T.pack "=>"+    value <- doubleQuoted+    return (key, value)++-- | Grab a value, unquote, unslash.+doubleQuoted :: AP.Parser Text+doubleQuoted = do+    AP.char '"'+    str <- AP.scan False $ \s c -> if s then Just False+                                        else if c == '"'+                                             then Nothing+                                             else Just (c == '\\')+    AP.char '"'++    return $! T.replace (T.pack "\\\"") (T.singleton '"') str
+ HDBC-postgresql-hstore.cabal view
@@ -0,0 +1,21 @@+-- Initial hdbc-postgresql-hstore.cabal generated by cabal init.  For +-- further documentation, see http://haskell.org/cabal/users-guide/++name:                HDBC-postgresql-hstore+version:             0.0.1.2+synopsis:            Manipulate data in PostgreSQL "hstore" columns.+description:         Some helpers to get and set hstore fields with a Data.Map interface.+homepage:            https://bitbucket.org/dpwiz/hdbc-postgresql-hstore+license:             MIT+license-file:        LICENSE+author:              Alexander Bondarenko+maintainer:          aenor.realm@gmail.com+-- copyright:           +category:            Database+build-type:          Simple+cabal-version:       >=1.8++library+  exposed-modules:     Database.HDBC.PostgreSQL.HStore+  -- other-modules:       +  build-depends:       base ==4.5.*, HDBC ==2.3.*, text ==0.11.*, attoparsec ==0.10.*, containers ==0.4.*
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright (C) 2012 Alexander Bondarenko++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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain