{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, GeneralizedNewtypeDeriving, CPP #-}

------------------------------------------------------------------------------
-- |
-- Module:      Database.SQLite.Simple.Types
-- Copyright:   (c) 2011 MailRank, Inc.
--              (c) 2011-2012 Leon P Smith
--              (c) 2012-2013 Janne Hellsten
-- License:     BSD3
-- Maintainer:  Janne Hellsten <jjhellst@gmail.com>
-- Portability: portable
--
-- Top-level module for sqlite-simple.
--
--
------------------------------------------------------------------------------

module Database.SQLite.Simple.Types
    (
      Null(..)
    , Only(..)
    , Query(..)
    , (:.)(..)
    ) where

import           Control.Arrow (first)
import           Data.String (IsString(..))
import           Data.Tuple.Only (Only(..))
import           Data.Typeable (Typeable)
import qualified Data.Text as T

#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup
#endif

-- | A placeholder for the SQL @NULL@ value.
data Null = Null
          deriving (ReadPrec [Null]
ReadPrec Null
Int -> ReadS Null
ReadS [Null]
(Int -> ReadS Null)
-> ReadS [Null] -> ReadPrec Null -> ReadPrec [Null] -> Read Null
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Null]
$creadListPrec :: ReadPrec [Null]
readPrec :: ReadPrec Null
$creadPrec :: ReadPrec Null
readList :: ReadS [Null]
$creadList :: ReadS [Null]
readsPrec :: Int -> ReadS Null
$creadsPrec :: Int -> ReadS Null
Read, Int -> Null -> ShowS
[Null] -> ShowS
Null -> String
(Int -> Null -> ShowS)
-> (Null -> String) -> ([Null] -> ShowS) -> Show Null
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Null] -> ShowS
$cshowList :: [Null] -> ShowS
show :: Null -> String
$cshow :: Null -> String
showsPrec :: Int -> Null -> ShowS
$cshowsPrec :: Int -> Null -> ShowS
Show, Typeable)

instance Eq Null where
    Null
_ == :: Null -> Null -> Bool
== Null
_ = Bool
False
    Null
_ /= :: Null -> Null -> Bool
/= Null
_ = Bool
False

-- | A query string. This type is intended to make it difficult to
-- construct a SQL query by concatenating string fragments, as that is
-- an extremely common way to accidentally introduce SQL injection
-- vulnerabilities into an application.
--
-- This type is an instance of 'IsString', so the easiest way to
-- construct a query is to enable the @OverloadedStrings@ language
-- extension and then simply write the query in double quotes.
--
-- > {-# LANGUAGE OverloadedStrings #-}
-- >
-- > import Database.SQLite.Simple
-- >
-- > q :: Query
-- > q = "select ?"
--
-- The underlying type is a 'Text', and literal Haskell strings that
-- contain Unicode characters will be correctly transformed to UTF-8.
newtype Query = Query {
      Query -> Text
fromQuery :: T.Text
    } deriving (Query -> Query -> Bool
(Query -> Query -> Bool) -> (Query -> Query -> Bool) -> Eq Query
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Query -> Query -> Bool
$c/= :: Query -> Query -> Bool
== :: Query -> Query -> Bool
$c== :: Query -> Query -> Bool
Eq, Eq Query
Eq Query
-> (Query -> Query -> Ordering)
-> (Query -> Query -> Bool)
-> (Query -> Query -> Bool)
-> (Query -> Query -> Bool)
-> (Query -> Query -> Bool)
-> (Query -> Query -> Query)
-> (Query -> Query -> Query)
-> Ord Query
Query -> Query -> Bool
Query -> Query -> Ordering
Query -> Query -> Query
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Query -> Query -> Query
$cmin :: Query -> Query -> Query
max :: Query -> Query -> Query
$cmax :: Query -> Query -> Query
>= :: Query -> Query -> Bool
$c>= :: Query -> Query -> Bool
> :: Query -> Query -> Bool
$c> :: Query -> Query -> Bool
<= :: Query -> Query -> Bool
$c<= :: Query -> Query -> Bool
< :: Query -> Query -> Bool
$c< :: Query -> Query -> Bool
compare :: Query -> Query -> Ordering
$ccompare :: Query -> Query -> Ordering
$cp1Ord :: Eq Query
Ord, Typeable)

instance Show Query where
    show :: Query -> String
show = Text -> String
forall a. Show a => a -> String
show (Text -> String) -> (Query -> Text) -> Query -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Text
fromQuery

instance Read Query where
    readsPrec :: Int -> ReadS Query
readsPrec Int
i = ((Text, String) -> (Query, String))
-> [(Text, String)] -> [(Query, String)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Text -> Query) -> (Text, String) -> (Query, String)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Text -> Query
Query) ([(Text, String)] -> [(Query, String)])
-> (String -> [(Text, String)]) -> ReadS Query
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> [(Text, String)]
forall a. Read a => Int -> ReadS a
readsPrec Int
i

instance IsString Query where
    fromString :: String -> Query
fromString = Text -> Query
Query (Text -> Query) -> (String -> Text) -> String -> Query
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

instance Semigroup Query where
    Query Text
a <> :: Query -> Query -> Query
<> Query Text
b = Text -> Query
Query (Text -> Text -> Text
T.append Text
a Text
b)
    {-# INLINE (<>) #-}

instance Monoid Query where
    mempty :: Query
mempty = Text -> Query
Query Text
T.empty
    mappend :: Query -> Query -> Query
mappend = Query -> Query -> Query
forall a. Semigroup a => a -> a -> a
(<>)
    {-# INLINE mappend #-}

-- | A composite type to parse your custom data structures without
-- having to define dummy newtype wrappers every time.
--
--
-- > instance FromRow MyData where ...
--
-- > instance FromRow MyData2 where ...
--
--
-- then I can do the following for free:
--
-- @
-- res <- query' c "..."
-- forM res $ \\(MyData{..} :. MyData2{..}) -> do
--   ....
-- @
data h :. t = h :. t deriving ((h :. t) -> (h :. t) -> Bool
((h :. t) -> (h :. t) -> Bool)
-> ((h :. t) -> (h :. t) -> Bool) -> Eq (h :. t)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall h t. (Eq h, Eq t) => (h :. t) -> (h :. t) -> Bool
/= :: (h :. t) -> (h :. t) -> Bool
$c/= :: forall h t. (Eq h, Eq t) => (h :. t) -> (h :. t) -> Bool
== :: (h :. t) -> (h :. t) -> Bool
$c== :: forall h t. (Eq h, Eq t) => (h :. t) -> (h :. t) -> Bool
Eq,Eq (h :. t)
Eq (h :. t)
-> ((h :. t) -> (h :. t) -> Ordering)
-> ((h :. t) -> (h :. t) -> Bool)
-> ((h :. t) -> (h :. t) -> Bool)
-> ((h :. t) -> (h :. t) -> Bool)
-> ((h :. t) -> (h :. t) -> Bool)
-> ((h :. t) -> (h :. t) -> h :. t)
-> ((h :. t) -> (h :. t) -> h :. t)
-> Ord (h :. t)
(h :. t) -> (h :. t) -> Bool
(h :. t) -> (h :. t) -> Ordering
(h :. t) -> (h :. t) -> h :. t
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall h t. (Ord h, Ord t) => Eq (h :. t)
forall h t. (Ord h, Ord t) => (h :. t) -> (h :. t) -> Bool
forall h t. (Ord h, Ord t) => (h :. t) -> (h :. t) -> Ordering
forall h t. (Ord h, Ord t) => (h :. t) -> (h :. t) -> h :. t
min :: (h :. t) -> (h :. t) -> h :. t
$cmin :: forall h t. (Ord h, Ord t) => (h :. t) -> (h :. t) -> h :. t
max :: (h :. t) -> (h :. t) -> h :. t
$cmax :: forall h t. (Ord h, Ord t) => (h :. t) -> (h :. t) -> h :. t
>= :: (h :. t) -> (h :. t) -> Bool
$c>= :: forall h t. (Ord h, Ord t) => (h :. t) -> (h :. t) -> Bool
> :: (h :. t) -> (h :. t) -> Bool
$c> :: forall h t. (Ord h, Ord t) => (h :. t) -> (h :. t) -> Bool
<= :: (h :. t) -> (h :. t) -> Bool
$c<= :: forall h t. (Ord h, Ord t) => (h :. t) -> (h :. t) -> Bool
< :: (h :. t) -> (h :. t) -> Bool
$c< :: forall h t. (Ord h, Ord t) => (h :. t) -> (h :. t) -> Bool
compare :: (h :. t) -> (h :. t) -> Ordering
$ccompare :: forall h t. (Ord h, Ord t) => (h :. t) -> (h :. t) -> Ordering
$cp1Ord :: forall h t. (Ord h, Ord t) => Eq (h :. t)
Ord,Int -> (h :. t) -> ShowS
[h :. t] -> ShowS
(h :. t) -> String
(Int -> (h :. t) -> ShowS)
-> ((h :. t) -> String) -> ([h :. t] -> ShowS) -> Show (h :. t)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall h t. (Show h, Show t) => Int -> (h :. t) -> ShowS
forall h t. (Show h, Show t) => [h :. t] -> ShowS
forall h t. (Show h, Show t) => (h :. t) -> String
showList :: [h :. t] -> ShowS
$cshowList :: forall h t. (Show h, Show t) => [h :. t] -> ShowS
show :: (h :. t) -> String
$cshow :: forall h t. (Show h, Show t) => (h :. t) -> String
showsPrec :: Int -> (h :. t) -> ShowS
$cshowsPrec :: forall h t. (Show h, Show t) => Int -> (h :. t) -> ShowS
Show,ReadPrec [h :. t]
ReadPrec (h :. t)
Int -> ReadS (h :. t)
ReadS [h :. t]
(Int -> ReadS (h :. t))
-> ReadS [h :. t]
-> ReadPrec (h :. t)
-> ReadPrec [h :. t]
-> Read (h :. t)
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall h t. (Read h, Read t) => ReadPrec [h :. t]
forall h t. (Read h, Read t) => ReadPrec (h :. t)
forall h t. (Read h, Read t) => Int -> ReadS (h :. t)
forall h t. (Read h, Read t) => ReadS [h :. t]
readListPrec :: ReadPrec [h :. t]
$creadListPrec :: forall h t. (Read h, Read t) => ReadPrec [h :. t]
readPrec :: ReadPrec (h :. t)
$creadPrec :: forall h t. (Read h, Read t) => ReadPrec (h :. t)
readList :: ReadS [h :. t]
$creadList :: forall h t. (Read h, Read t) => ReadS [h :. t]
readsPrec :: Int -> ReadS (h :. t)
$creadsPrec :: forall h t. (Read h, Read t) => Int -> ReadS (h :. t)
Read,Typeable)

infixr 3 :.