MCPcopy Create free account
hub / github.com/bytebase/bytebase / CSVToWriter

Function CSVToWriter

backend/component/export/csv.go:22–47  ·  view source on GitHub ↗

CSVToWriter streams query results as CSV directly to the writer. This minimizes memory usage by avoiding intermediate buffering.

(w io.Writer, result *v1pb.QueryResult)

Source from the content-addressed store, hash-verified

20// CSVToWriter streams query results as CSV directly to the writer.
21// This minimizes memory usage by avoiding intermediate buffering.
22func CSVToWriter(w io.Writer, result *v1pb.QueryResult) error {
23 if _, err := w.Write([]byte(strings.Join(result.ColumnNames, ","))); err != nil {
24 return err
25 }
26 if _, err := w.Write([]byte{'\n'}); err != nil {
27 return err
28 }
29 for i, row := range result.Rows {
30 for j, value := range row.Values {
31 if j != 0 {
32 if _, err := w.Write([]byte{','}); err != nil {
33 return err
34 }
35 }
36 if _, err := w.Write(convertValueToBytesInCSV(value)); err != nil {
37 return err
38 }
39 }
40 if i != len(result.Rows)-1 {
41 if _, err := w.Write([]byte{'\n'}); err != nil {
42 return err
43 }
44 }
45 }
46 return nil
47}
48
49func convertValueToBytesInCSV(value *v1pb.RowValue) []byte {
50 if value == nil || value.Kind == nil {

Callers 2

formatExportToZipMethod · 0.92
formatExportToZipFunction · 0.92

Calls 2

convertValueToBytesInCSVFunction · 0.85
JoinMethod · 0.80

Tested by

no test coverage detected