| Copyright | (c) Nikos Karagiannidis 2018 |
|---|---|
| License | BSD3 |
| Maintainer | nkarag@gmail.com |
| Stability | stable |
| Portability | POSIX |
| Safe Haskell | None |
| Language | Haskell2010 |
RTable.Data.CSV
Contents
Description
This module implements the RTabular instance of the CSV data type, i.e., implements the interface by which a CSV file can be transformed to/from an RTable.
It is required when we want to do ETL/ELT over CSV files with the DBFunctor package (i.e., with the Julius EDSL for ETL/ELT found in the Etl.Julius module).
The minimum requirement for implementing an RTabular instance for a data type is to implement the toRTable and fromRTable functions. Apart from these two functions, this
module also exports functions for reading and writing CSV data from/to CSV files. Also it supports all types of delimiters (not only commas) and CSVs with or without headers.
(see CSVOptions)
For the CSV data type this module uses the Cassava library (Data.Csv)
Synopsis
- type CSV = Vector Row
- type Row = Vector Column
- type Column = Field
- data CSVOptions = CSVOptions {}
- data YesNo
- readCSV :: FilePath -> IO CSV
- readCSVwithOptions :: CSVOptions -> FilePath -> IO CSV
- readCSVFile :: FilePath -> IO ByteString
- writeCSV :: FilePath -> CSV -> IO ()
- writeCSVFile :: FilePath -> ByteString -> IO ()
- toRTable :: RTabular a => RTableMData -> a -> RTable
- fromRTable :: RTabular a => RTableMData -> RTable -> a
- printCSV :: CSV -> IO ()
- printCSVFile :: ByteString -> IO ()
- copyCSV :: FilePath -> FilePath -> IO ()
- selectNrows :: Int -> ByteString -> ByteString
- projectByIndex :: [Int] -> CSV -> CSV
- headCSV :: CSV -> Row
- tailCSV :: CSV -> CSV
- csvHeaderFromRtable :: RTable -> Header
- data CsvFileDecodingError = CsvFileDecodingError FilePath Text
- data CSVColumnToRDataTypeError = CSVColumnToRDataTypeError ColumnName Text
The CSV data type
type CSV = Vector Row Source #
Definition of a CSV file.
Treating CSV data as opaque byte strings (see Csv type in Cassava library - Data.Csv: type Csv = Vector Record)
type Row = Vector Column Source #
Definition of a CSV Row. Essentially a Row is just a Vector of ByteString (type Record = Vector Field)
data CSVOptions Source #
Options for a CSV file (e.g., delimiter specification, header specification etc.)
Constructors
| CSVOptions | |
Read/Write CSV
reads a CSV file and returns a CSV data type (Treating CSV data as opaque byte strings)
Arguments
| :: CSVOptions | |
| -> FilePath | the CSV file |
| -> IO CSV | the output CSV type |
reads a CSV file based on input options (delimiter and header option) and returns a CSV data type (Treating CSV data as opaque byte strings)
Arguments
| :: FilePath | the CSV file |
| -> IO ByteString | the output CSV |
reads a CSV file and returns a lazy bytestring
write a CSV to a newly created csv file
Arguments
| :: FilePath | the csv file to be created |
| -> ByteString | input CSV |
| -> IO () |
write a CSV (bytestring) to a newly created csv file
CSV as Tabular data
fromRTable :: RTabular a => RTableMData -> RTable -> a Source #
CSV I/O
Arguments
| :: ByteString | input CSV to be printed on screen |
| -> IO () |
print input CSV on screen
Basic CSV processing
copy input csv file to specified output csv file
Arguments
| :: Int | Number of rows to select |
| -> ByteString | Input csv |
| -> ByteString | Output csv |
selectNrows: Returns the first N rows from a CSV file
Column projection on an input CSV file where desired columns are defined by position (index) in the CSV.
tailCSV :: CSV -> CSV Source #
O(1) Yield all but the first row without copying. The CSV may not be empty.
Misc
csvHeaderFromRtable :: RTable -> Header Source #
Exceptions
data CsvFileDecodingError Source #
Exception to signify an error in decoding a CSV file into a CSV data type
Constructors
| CsvFileDecodingError FilePath Text |
Instances
| Eq CsvFileDecodingError Source # | |
Defined in RTable.Data.CSV Methods (==) :: CsvFileDecodingError -> CsvFileDecodingError -> Bool # (/=) :: CsvFileDecodingError -> CsvFileDecodingError -> Bool # | |
| Show CsvFileDecodingError Source # | |
Defined in RTable.Data.CSV Methods showsPrec :: Int -> CsvFileDecodingError -> ShowS # show :: CsvFileDecodingError -> String # showList :: [CsvFileDecodingError] -> ShowS # | |
| Exception CsvFileDecodingError Source # | |
Defined in RTable.Data.CSV Methods toException :: CsvFileDecodingError -> SomeException # fromException :: SomeException -> Maybe CsvFileDecodingError # | |
data CSVColumnToRDataTypeError Source #
Constructors
| CSVColumnToRDataTypeError ColumnName Text |
Instances
| Eq CSVColumnToRDataTypeError Source # | |
Defined in RTable.Data.CSV Methods (==) :: CSVColumnToRDataTypeError -> CSVColumnToRDataTypeError -> Bool # (/=) :: CSVColumnToRDataTypeError -> CSVColumnToRDataTypeError -> Bool # | |
| Show CSVColumnToRDataTypeError Source # | |
Defined in RTable.Data.CSV Methods showsPrec :: Int -> CSVColumnToRDataTypeError -> ShowS # show :: CSVColumnToRDataTypeError -> String # showList :: [CSVColumnToRDataTypeError] -> ShowS # | |
| Exception CSVColumnToRDataTypeError Source # | |
Defined in RTable.Data.CSV | |
Orphan instances
| FromField RDataType Source # | Necessary instance in order to convert a CSV file column value to an |
Methods parseField :: Field -> Parser RDataType # | |
| ToField RDataType Source # | In order to encode an input RTable into a CSV bytestring we need to make Rtuple an instance of the ToNamedRecord typeclass and implement the toNamedRecord function. Where: toNamedRecord :: a -> NamedRecord
type NamedRecord = HashMap ByteString ByteString
namedRecord :: [(ByteString, ByteString)] -> NamedRecord
Construct a named record from a list of name-value ByteString pairs. Use .= to construct such a pair from a name and a value.
(.=) :: ToField a => ByteString -> a -> (ByteString, ByteString)
In our case, we dont need to do this because an RTuple is just a synonym for HM.HashMap ColumnName RDataType and the data type HashMap a b is already an instance of ToNamedRecord. Also we need to make RDataType an instance of ToField ((CV.ToField RDataType)) by implementing toField, so as to be able to convert an RDataType into a ByteString where: toField :: a -> Field
type Field = ByteString
|
| RTabular CSV Source # | CSV data are "Tabular" data thus implement the |
Methods toRTable :: RTableMData -> CSV -> RTable Source # fromRTable :: RTableMData -> RTable -> CSV Source # | |