Example #1
0
func writeRow(w *yacr.Writer, r row) {
	for _, field := range r {
		w.Write(field)
	}
	w.EndOfRecord()
	if err := w.Err(); err != nil {
		log.Fatalf("Error while writing diff: '%s'\n", err)
	}
}
Example #2
0
// ExportToCSV exports statement result to CSV.
// 'headers' flag turns output of headers on or off.
// NULL values are output as specified by 'nullvalue' parameter.
func (s *Stmt) ExportToCSV(nullvalue string, headers bool, w *yacr.Writer) error {
	if headers {
		for _, header := range s.ColumnNames() {
			w.Write([]byte(header))
		}
		w.EndOfRecord()
		if err := w.Err(); err != nil {
			return err
		}
	}
	s.Select(func(s *Stmt) error {
		for i := 0; i < s.ColumnCount(); i++ {
			rb, null := s.ScanRawBytes(i)
			if null {
				w.Write([]byte(nullvalue))
			} else {
				w.Write(rb)
			}
		}
		w.EndOfRecord()
		return w.Err()
	})
	w.Flush()
	return w.Err()
}