Skip to content

Instantly share code, notes, and snippets.

@kayvank
Last active October 27, 2022 12:47
Show Gist options
  • Save kayvank/e16da0f2643dd0b8b813e128c4a6116f to your computer and use it in GitHub Desktop.
Save kayvank/e16da0f2643dd0b8b813e128c4a6116f to your computer and use it in GitHub Desktop.
streamSqlite.hs
#!/usr/bin/env stack
{- stack
script
--resolver lts-18.28
--package sqlite-simple
--package async
--package text
-}
{-
-- | the purpose of this code is to make sure sqlite maybe accessed concurrently
-- Requires stack and sqlite
-}
{-# LANGUAGE OverloadedStrings #-}
import System.IO
import Control.Concurrent.Async (concurrently, forConcurrently)
import Control.Monad ( void )
import Database.SQLite.Simple.FromRow
import Database.SQLite.Simple
data TestField = TestField Int String deriving (Show)
q :: Query
q = "SELECT * from test where str=:str"
dbAction1 :: Connection -> Int -> IO [TestField]
dbAction1 conn num = do
let
f :: [TestField] -> TestField -> IO [TestField]
f ts t = appendFile "streamOut.txt" (show t) >> pure (t:ts)
fold_ conn "select * from test" [] f
instance FromRow TestField where
fromRow = TestField <$> field <*> field
instance ToRow TestField where
toRow (TestField i s) = toRow (i, s)
main :: IO ()
main = do
conn <- open "/tmp/test.db"
res <- (dbAction1 conn 1)
print res
close conn
putStrLn "\n------ completed sqlite stuff ----\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment