MCPcopy Create free account
hub / github.com/auxten/postgresql-parser / Write

Method Write

pkg/util/encoding/csv/writer.go:50–108  ·  view source on GitHub ↗

Writer writes a single CSV record to w along with any necessary quoting. A record is a slice of strings with each string being one field.

(record []string)

Source from the content-addressed store, hash-verified

48// Writer writes a single CSV record to w along with any necessary quoting.
49// A record is a slice of strings with each string being one field.
50func (w *Writer) Write(record []string) error {
51 if !validDelim(w.Comma) {
52 return errInvalidDelim
53 }
54
55 for n, field := range record {
56 if n > 0 {
57 if _, err := w.w.WriteRune(w.Comma); err != nil {
58 return err
59 }
60 }
61
62 // If we don't have to have a quoted field then just
63 // write out the field and continue to the next field.
64 if !w.fieldNeedsQuotes(field) {
65 if _, err := w.w.WriteString(field); err != nil {
66 return err
67 }
68 continue
69 }
70 if err := w.w.WriteByte('"'); err != nil {
71 return err
72 }
73
74 for _, r1 := range field {
75 var err error
76 switch r1 {
77 case '"':
78 _, err = w.w.WriteString(`""`)
79 case '\r':
80 if !w.UseCRLF {
81 err = w.w.WriteByte('\r')
82 }
83 case '\n':
84 if w.UseCRLF {
85 _, err = w.w.WriteString("\r\n")
86 } else {
87 err = w.w.WriteByte('\n')
88 }
89 default:
90 _, err = w.w.WriteRune(r1)
91 }
92 if err != nil {
93 return err
94 }
95 }
96
97 if err := w.w.WriteByte('"'); err != nil {
98 return err
99 }
100 }
101 var err error
102 if w.UseCRLF {
103 _, err = w.w.WriteString("\r\n")
104 } else {
105 err = w.w.WriteByte('\n')
106 }
107 return err

Callers 9

WriteAllMethod · 0.95
writeAsHexStringFunction · 0.80
EncodeSQLBytesFunction · 0.80
ErrorMethod · 0.80
EncodeEscapedCharFunction · 0.80
NewEncoderMethod · 0.80
newFromHashFunction · 0.80
WriteIPv4BytesMethod · 0.80
WriteIPv6BytesMethod · 0.80

Calls 2

fieldNeedsQuotesMethod · 0.95
validDelimFunction · 0.85

Tested by

no test coverage detected