aboutsummaryrefslogtreecommitdiff
path: root/fooes.hs
blob: cc7a0f8c3c53d736f86d186436d9573b88ec63e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE QuasiQuotes, TypeFamilies, GeneralizedNewtypeDeriving, TemplateHaskell,
             OverloadedStrings, GADTs, FlexibleContexts #-}
import Database.Persist.TH
import Database.Persist.Sqlite (runSqlite)
import Control.Monad.IO.Class (liftIO)
import Control.Monad
import Database.Esqueleto hiding (Key)

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
CachedKey
  key String
  UniqueKey key
  deriving Show

AssociatedFiles
  key CachedKeyId Eq
  file FilePath
  UniqueKeyFile key file
  deriving Show
|]

main :: IO ()
main = runSqlite "foo.db" $ do
	runMigration migrateAll
	if True then populate else return ()
	query

populate = do
	forM_ [1..30000] $ \i -> do
		--delete $ from $ \f -> do
		--	where_ (f ^. CachedKeyKey ==. val (show i))
		k <- insert $ CachedKey (show i)
		liftIO $ print ("stored", k)
		insert $ AssociatedFiles k ("file" ++show (i+1))
		--insert $ AssociatedFiles k ("otherfile" ++show (i+2))

query = forM_ [1..1000] $ \i -> do
	r <- select $ from $ \(k, f) -> do
		where_ (k ^. CachedKeyKey ==. val (show i))
		where_ (f ^. AssociatedFilesKey ==. k ^. CachedKeyId)
		return (f ^. AssociatedFilesFile)
	liftIO $ print ("got", r)