Initial commit: archer, a Go-native MySQL/MariaDB table archiver
Renamed from arcdb: module path, cmd/archer, internal/archererr, docs, build scripts and ARCHER_* environment variables. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
commit
bc4a6b504f
99 files changed
+12752
No files matched your search
@@ -0,0 +1,68 @@
|
||||
package row
|
||||
|
||||
import "bytes"
|
||||
|
||||
// csvEncoder emits RFC4180 CSV with a column-name header and CRLF line
|
||||
// endings. NULL is written as the configured token and empty strings as empty
|
||||
// fields.
|
||||
type csvEncoder struct {
|
||||
cols []Column
|
||||
idx []int
|
||||
null string
|
||||
}
|
||||
|
||||
func (e *csvEncoder) Header() []byte {
|
||||
var b bytes.Buffer
|
||||
for n, i := range e.idx {
|
||||
if n > 0 {
|
||||
b.WriteByte(',')
|
||||
}
|
||||
writeCSVField(&b, []byte(e.cols[i].Name))
|
||||
}
|
||||
b.WriteString("\r\n")
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func (e *csvEncoder) Encode(dst []byte, r Row) ([]byte, error) {
|
||||
for n, i := range e.idx {
|
||||
if n > 0 {
|
||||
dst = append(dst, ',')
|
||||
}
|
||||
v := r.Values[i]
|
||||
if v.IsNull() {
|
||||
dst = append(dst, e.null...)
|
||||
continue
|
||||
}
|
||||
dst = appendCSVField(dst, v.Raw)
|
||||
}
|
||||
return append(dst, '\r', '\n'), nil
|
||||
}
|
||||
|
||||
func needsCSVQuote(b []byte) bool {
|
||||
for _, c := range b {
|
||||
switch c {
|
||||
case ',', '"', '\r', '\n':
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func appendCSVField(dst, field []byte) []byte {
|
||||
if !needsCSVQuote(field) {
|
||||
return append(dst, field...)
|
||||
}
|
||||
dst = append(dst, '"')
|
||||
for _, c := range field {
|
||||
if c == '"' {
|
||||
dst = append(dst, '"', '"')
|
||||
continue
|
||||
}
|
||||
dst = append(dst, c)
|
||||
}
|
||||
return append(dst, '"')
|
||||
}
|
||||
|
||||
func writeCSVField(b *bytes.Buffer, field []byte) {
|
||||
b.Write(appendCSVField(nil, field))
|
||||
}
|
||||
Reference in new issue
Block a user