Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pcre-light dependency #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions Database/MySQL/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ module Database.MySQL.Simple
, formatQuery
) where

import Prelude hiding (take, takeWhile)
import Blaze.ByteString.Builder (Builder, fromByteString, toByteString)
import Blaze.ByteString.Builder.Char8 (fromChar)
import Control.Applicative ((<$>), pure)
import Control.Applicative
import Control.Exception (Exception, bracket, onException, throw, throwIO)
import Control.Monad.Fix (fix)
import Data.ByteString (ByteString)
Expand All @@ -98,9 +99,9 @@ import Database.MySQL.Simple.QueryParams (QueryParams(..))
import Database.MySQL.Simple.QueryResults (QueryResults(..))
import Database.MySQL.Simple.Result (ResultError(..))
import Database.MySQL.Simple.Types (Binary(..), In(..), Only(..), Query(..))
import Text.Regex.PCRE.Light (compile, caseless, match)
import qualified Data.ByteString.Char8 as B
import qualified Database.MySQL.Base as Base
import Data.Attoparsec.Char8 hiding (Result)

-- | Exception thrown if a 'Query' could not be formatted correctly.
-- This may occur if the number of \'@?@\' characters in the query
Expand Down Expand Up @@ -154,18 +155,28 @@ formatQuery conn q@(Query template) qs
formatMany :: (QueryParams q) => Connection -> Query -> [q] -> IO ByteString
formatMany _ q [] = fmtError "no rows supplied" q []
formatMany conn q@(Query template) qs = do
case match re template [] of
Just [_,before,qbits,after] -> do
case parse parser template of
Done _ (before,qbits,after) -> do
bs <- mapM (buildQuery conn q qbits . renderParams) qs
return . toByteString . mconcat $ fromByteString before :
intersperse (fromChar ',') bs ++
[fromByteString after]
_ -> error "foo"
where
re = compile "^([^?]+\\bvalues\\s*)\
\(\\(\\s*[?](?:\\s*,\\s*[?])*\\s*\\))\
\([^?]*)$"
[caseless]
skipWhile1 f = satisfy f *> skipWhile f
parser = do
-- Skip VALUES keyword
skipWhile1 (/= '?') *> take 1 *> stringCI "values" *> skipSpace
-- Take (...?
before <- char '(' *> takeTill (== '?')
-- Skip ?,?,?...
skipMany (skipSpace *> char ',' *> skipSpace *> char '?')
-- Take )
qbits <- takeWhile (== ' ') <* char ')'
after <- takeWhile (/= '?')
endOfInput
return ('(' `B.cons` before, qbits `B.snoc` ')', after)


buildQuery :: Connection -> Query -> ByteString -> [Action] -> IO Builder
buildQuery conn q template xs = zipParams (split template) <$> mapM sub xs
Expand Down
1 change: 0 additions & 1 deletion mysql-simple.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ library
blaze-textual,
bytestring >= 0.9,
mysql >= 0.1.1.1,
pcre-light,
old-locale,
text >= 0.11.0.2,
time
Expand Down