module Database.MetaHDBC.SimpleSqlParser
( simpleSqlParser )
where
import Data.Char
import Data.List(intersperse)
simpleSqlParser :: String -> ([String], String)
simpleSqlParser sql =
case splitWhen '?' sql of
[] -> ([], "")
(x:xs) -> let (vars, rests) = unzip $ map parseVar xs
in (vars, concat $ intersperse "?" (x:rests))
parseVar :: String -> (String, String)
parseVar [] = ("", [])
parseVar (x:xs) | isLower x = span (\c -> isAlphaNum c || c == '\'') (x:xs)
| otherwise = ("", x:xs)
splitWhen :: (Eq a) => a -> [a] -> [[a]]
splitWhen splitter xs
= case break (== splitter) xs of
(ys, []) -> [ys]
(ys, (_:zs)) -> ys : splitWhen splitter zs