@@ -7,6 +7,7 @@ import Data.Char (ord)
77import qualified Data.ByteString.Lazy as BS
88import ProjectM36.Base
99import ProjectM36.Relation
10+import ProjectM36.DataTypes.Primitive
1011import ProjectM36.Error
1112import Data.Text.Encoding (decodeUtf8)
1213import qualified ProjectM36.Attribute as A
@@ -25,6 +26,13 @@ data CsvImportError = CsvParseError String |
2526csvDecodeOptions :: DecodeOptions
2627csvDecodeOptions = DecodeOptions {decDelimiter = fromIntegral (ord ',')}
2728
29+--special case from Text- outer quotes are *not* required in CSV, so we have to add them to make it parseable
30+makeAtomFromCSVText :: AttributeName -> AtomType -> T.Text -> Either RelationalError Atom
31+makeAtomFromCSVText attrName aType textIn = makeAtomFromText attrName aType $ if aType == textAtomType then
32+ ("\"" `T.append` textIn `T.append` "\"")
33+ else
34+ textIn
35+
2836csvAsRelation :: BS.ByteString -> Attributes -> Either CsvImportError Relation
2937csvAsRelation inString attrs = case parse (csvWithHeader csvDecodeOptions) inString of
3038 Fail _ _ err -> Left (CsvParseError err)
@@ -36,7 +44,7 @@ csvAsRelation inString attrs = case parse (csvWithHeader csvDecodeOptions) inStr
3644 attrNameSet = S.fromList (V.toList attrNames)
3745 headerSet = S.fromList (V.toList strHeader)
3846 makeTupleList :: HM.HashMap AttributeName String -> [Either CsvImportError Atom]
39− makeTupleList tupMap = V.toList $ V.map (\attr -> either (Left . AttributeMappingError) Right $ makeAtomFromText (A.attributeName attr) (A.atomType attr) (T.pack $ tupMap HM.! (A.attributeName attr))) attrs
47+ makeTupleList tupMap = V.toList $ V.map (\attr -> either (Left . AttributeMappingError) Right $ makeAtomFromCSVText (A.attributeName attr) (A.atomType attr) (T.pack $ tupMap HM.! (A.attributeName attr))) attrs
4048 case attrNameSet == headerSet of
4149 False -> Left $ HeaderAttributeMismatchError (S.difference attrNameSet headerSet)
4250 True -> do