module Database.MySQL.Query where
import Data.String (IsString (..))
import Control.Exception (throw, Exception)
import Data.Typeable
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as LC
import qualified Data.ByteString.Builder as BB
import Control.Arrow (first)
import Database.MySQL.Protocol.MySQLValue
import Data.Binary.Put
newtype Query = Query { fromQuery :: L.ByteString } deriving (Eq, Ord, Typeable)
instance Show Query where
show = show . fromQuery
instance Read Query where
readsPrec i = fmap (first Query) . readsPrec i
instance IsString Query where
fromString = Query . BB.toLazyByteString . BB.stringUtf8
renderParams :: Query -> [MySQLValue] -> Query
renderParams (Query qry) params =
let fragments = LC.split '?' qry
in Query . runPut $ merge fragments params
where
merge [x] [] = putLazyByteString x
merge (x:xs) (y:ys) = putLazyByteString x >> putTextField y >> merge xs ys
merge _ _ = throw WrongParamsCount
data WrongParamsCount = WrongParamsCount deriving (Show, Typeable)
instance Exception WrongParamsCount