sqltocsv: github.com/joho/sqltocsv Index | Files

package sqltocsv

import "github.com/joho/sqltocsv"

sqltocsv is a package to make it dead easy to turn arbitrary database query results (in the form of database/sql Rows) into CSV output.

Source and README at https://github.com/joho/sqltocsv

Index

Package Files

sqltocsv.go

func Write Uses

func Write(writer io.Writer, rows *sql.Rows) error

Write will write a CSV file to the writer passed in (with headers) based on whatever is in the sql.Rows you pass in.

func WriteFile Uses

func WriteFile(csvFileName string, rows *sql.Rows) error

WriteFile will write a CSV file to the file name specified (with headers) based on whatever is in the sql.Rows you pass in. It calls WriteCsvToWriter under the hood.

func WriteString Uses

func WriteString(rows *sql.Rows) (string, error)

WriteString will return a string of the CSV. Don't use this unless you've got a small data set or a lot of memory

type Converter Uses

type Converter struct {
    Headers      []string // Column headers to use (default is rows.Columns())
    WriteHeaders bool     // Flag to output headers in your CSV (default is true)
    TimeFormat   string   // Format string for any time.Time values (default is time's default)
    // contains filtered or unexported fields
}

Converter does the actual work of converting the rows to CSV. There are a few settings you can override if you want to do some fancy stuff to your CSV.

func New Uses

func New(rows *sql.Rows) *Converter

New will return a Converter which will write your CSV however you like but will allow you to set a bunch of non-default behaivour like overriding headers or injecting a pre-processing step into your conversion

func (*Converter) SetRowPreProcessor Uses

func (c *Converter) SetRowPreProcessor(processor CsvPreProcessorFunc)

SetRowPreProcessor lets you specify a CsvPreprocessorFunc for this conversion

func (Converter) String Uses

func (c Converter) String() string

String returns the CSV as a string in an fmt package friendly way

func (Converter) Write Uses

func (c Converter) Write(writer io.Writer) error

Write writes the CSV to the Writer provided

func (Converter) WriteFile Uses

func (c Converter) WriteFile(csvFileName string) error

WriteFile writes the CSV to the filename specified, return an error if problem

func (Converter) WriteString Uses

func (c Converter) WriteString() (string, error)

WriteString returns the CSV as a string and an error if something goes wrong

type CsvPreProcessorFunc Uses

type CsvPreProcessorFunc func(row []string, columnNames []string) (outputRow bool, processedRow []string)

CsvPreprocessorFunc is a function type for preprocessing your CSV. It takes the columns after they've been munged into strings but before they've been passed into the CSV writer.

Return an outputRow of false if you want the row skipped otherwise return the processed Row slice as you want it written to the CSV.

Package sqltocsv imports 7 packages (graph) and is imported by 1 packages. Updated 2019-08-16. Refresh now. Tools for package owners.