HulkImport 0.1.0.2 → 0.1.0.3
raw patch · 4 files changed
+40/−17 lines, 4 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ CSV.SQL: instance CSV.SQL.RenderSqlType CSV.Types.SQLVal
+ CSV.SQL: instance CSV.SQL.RenderSqlType Data.Text.Internal.Text
+ CSV.SQL: instance CSV.SQL.RenderSqlType GHC.Types.Int
+ CSV.Types: I :: Int -> SQLVal
+ CSV.Types: NVar :: Text -> SQLVal
+ CSV.Types: data SQLVal
- CSV.Parse: parse :: Text -> CSV Text
+ CSV.Parse: parse :: Text -> CSV SQLVal
- CSV.SQL: toSQL :: CSV Text -> String
+ CSV.SQL: toSQL :: RenderSqlType a => CSV a -> String
Files
- HulkImport.cabal +1/−1
- src/CSV/Parse.hs +14/−10
- src/CSV/SQL.hs +21/−5
- src/CSV/Types.hs +4/−1
HulkImport.cabal view
@@ -1,5 +1,5 @@ name: HulkImport-version: 0.1.0.2+version: 0.1.0.3 synopsis: Easily bulk import CSV data to SQL Server description: Please see README.md homepage: http://github.com/tobs169/HulkImport#readme
src/CSV/Parse.hs view
@@ -1,36 +1,40 @@+{-# LANGUAGE OverloadedStrings #-} module CSV.Parse ( parse ) where import qualified Data.Attoparsec.Text as A import qualified Data.Text as Text+import Control.Applicative import CSV.Types -- | 'parse' parses text in the CSV format. Delimiters are ',' and new lines. -- Quotation is performed with '"'. -- TODO: Files in the wrong format throw an error :( -parse :: Text.Text -> CSV Text.Text+parse :: Text.Text -> CSV SQLVal parse text = case A.parseOnly csvParser text of Left s -> error s Right c -> c -csvParser :: A.Parser (CSV Text.Text)+csvParser :: A.Parser (CSV SQLVal) csvParser = do rows <- A.sepBy1 rowParser (A.skip A.isEndOfLine) return (CSV rows) -rowParser :: A.Parser [Text.Text]-rowParser = A.sepBy1 elementParser $ A.char ','+rowParser :: A.Parser [SQLVal]+rowParser = A.sepBy1 elementParser $ A.string ", " -elementParser :: A.Parser Text.Text-elementParser = do - text <- A.many1 $ A.choice- [ textParser- , quotationParser ]- return (Text.concat text)+elementParser :: A.Parser SQLVal+elementParser =+ I <$> A.decimal+ <|> do+ text <- A.many1 $+ textParser+ <|> quotationParser+ return (NVar $ Text.concat text) textParser :: A.Parser Text.Text textParser = A.takeWhile1 (A.notInClass [',','\n', '"'])
src/CSV/SQL.hs view
@@ -7,18 +7,34 @@ import qualified Data.Text as T -- | Converts CSV data to the Values clause of an insert statement-toSQL :: CSV T.Text -> String+toSQL :: RenderSqlType a => CSV a -> String toSQL = render . csvDoc -csvDoc :: CSV T.Text -> Doc+csvDoc :: RenderSqlType a => CSV a -> Doc csvDoc (CSV rows) = hcat (text "VALUES " : fmap rowDoc rows) <> text "\nGO" -rowDoc :: [T.Text] -> Doc+rowDoc :: RenderSqlType a => [a] -> Doc rowDoc = (<> text ", ") . parens . hcat- . punctuate (text ",")- . map (text . T.unpack)+ . punctuate (text ", ")+ . map elemDoc +elemDoc :: RenderSqlType a => a -> Doc+elemDoc = renderSQL+++class RenderSqlType a where+ renderSQL :: a -> Doc++instance RenderSqlType T.Text where+ renderSQL = (char 'N' <> ) . quotes . text . T.unpack . T.strip ++instance RenderSqlType Int where+ renderSQL = int++instance RenderSqlType SQLVal where+ renderSQL (I i) = renderSQL i+ renderSQL (NVar t) = renderSQL t
src/CSV/Types.hs view
@@ -1,4 +1,7 @@-module CSV.Types ( CSV (..)) where +module CSV.Types ( CSV (..), SQLVal(..)) where +import Data.Text (Text) data CSV a = CSV [[a]] deriving Show++data SQLVal = I Int | NVar Text