Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from vrom911/vrom911/qq
Add QuasiQuoter sql and tests
- Loading branch information
Showing
6 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ TAGS | |
/*.hs.html | ||
/hpc_*.html | ||
/test.tix | ||
.stack-work/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
------------------------------------------------------------------------------ | ||
-- | | ||
-- Module: Database.SQLite.Simple.QQ | ||
-- Copyright: (c) 2011-2012 Leon P Smith | ||
-- (c) 2018 Janne Hellsten | ||
-- License: BSD3 | ||
-- Maintainer: Janne Hellsten <jjhellst@gmail.com> | ||
-- Portability: portable | ||
-- | ||
-- The 'sql' quasiquoter, for writing large @SQL@ statements. | ||
-- | ||
------------------------------------------------------------------------------ | ||
|
||
module Database.SQLite.Simple.QQ | ||
( sql | ||
) where | ||
|
||
import Data.String (fromString) | ||
import Database.SQLite.Simple.Types (Query) | ||
import Language.Haskell.TH (Exp, Q, appE, stringE) | ||
import Language.Haskell.TH.Quote (QuasiQuoter (..)) | ||
|
||
{- | A quasiquoter for writing big @SQL@ queries. | ||
One should consider turning on the @-XQuasiQuotes@ pragma in that module: | ||
@ | ||
{-# LANGUAGE QuasiQuoter #-} | ||
myQuery = query conn [sql| | ||
SELECT | ||
* | ||
FROM | ||
users | ||
WHERE jobTitle = ? | ||
|] jobTitle | ||
@ | ||
-} | ||
sql :: QuasiQuoter | ||
sql = QuasiQuoter | ||
{ quotePat = error "Database.SQLite.Simple.QQ.sql: quasiquoter used in pattern context" | ||
, quoteType = error "Database.SQLite.Simple.QQ.sql: quasiquoter used in type context" | ||
, quoteDec = error "Database.SQLite.Simple.QQ.sql: quasiquoter used in declaration context" | ||
, quoteExp = sqlExp | ||
} | ||
|
||
sqlExp :: String -> Q Exp | ||
sqlExp = appE [| fromString :: String -> Query |] . stringE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
resolver: lts-11.9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{-# LANGUAGE QuasiQuotes #-} | ||
|
||
module QQ ( | ||
testSimpleQQ | ||
, testMultiLinedQQ | ||
) where | ||
|
||
import Common | ||
import Database.SQLite.Simple.QQ (sql) | ||
|
||
testSimpleQQ :: TestEnv -> Test | ||
testSimpleQQ TestEnv{..} = TestCase $ do | ||
q <- query_ conn "SELECT 1+1" :: IO [Only Int] | ||
qq <- query_ conn [sql| | ||
SELECT 1 + 1 | ||
|] :: IO [Only Int] | ||
assertEqual "result" q qq | ||
|
||
|
||
testMultiLinedQQ :: TestEnv -> Test | ||
testMultiLinedQQ TestEnv{..} = TestCase $ do | ||
execute_ conn "CREATE TABLE testQQ (id INTEGER PRIMARY KEY, t TEXT)" | ||
execute_ conn "INSERT INTO testQQ (t) VALUES ('test string')" | ||
q <- query_ conn "SELECT t FROM testQQ" :: IO [Only String] | ||
qq <- query_ conn [sql| | ||
SELECT | ||
t | ||
FROM | ||
testQQ | ||
|
||
|] :: IO [Only String] | ||
assertEqual "result" q qq |