Files
voidandClaude Sonnet 4.6 bc4a6b504f 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>
2026-11-06 22:12:17 +08:00

126 lines
2.8 KiB
Go

package row
import (
"encoding/base64"
"time"
)
// jsonlEncoder emits one JSON object per line. Types are mapped deliberately
// so downstream tools see useful values: decimals become strings to preserve
// precision, binary columns become base64, date/time columns become strings
// and NULL becomes JSON null.
type jsonlEncoder struct {
cols []Column
idx []int
}
func (e *jsonlEncoder) Header() []byte { return nil }
func (e *jsonlEncoder) Encode(dst []byte, r Row) ([]byte, error) {
dst = append(dst, '{')
for n, i := range e.idx {
if n > 0 {
dst = append(dst, ',')
}
dst = appendJSONString(dst, []byte(e.cols[i].Name))
dst = append(dst, ':')
dst = appendJSONValue(dst, e.cols[i], r.Values[i])
}
return append(dst, '}', '\n'), nil
}
func appendJSONValue(dst []byte, col Column, v Value) []byte {
if v.IsNull() {
return append(dst, "null"...)
}
switch {
case IsInteger(col.Type), IsFloat(col.Type):
if looksNumeric(v.Raw) {
return append(dst, v.Raw...)
}
return appendJSONString(dst, v.Raw)
case IsDecimal(col.Type):
return appendJSONString(dst, v.Raw)
case IsBinary(col.Type):
dst = append(dst, '"')
dst = append(dst, base64.StdEncoding.EncodeToString(v.Raw)...)
return append(dst, '"')
case IsDate(col.Type):
return appendJSONString(dst, v.Raw)
case IsDateTime(col.Type):
if s, ok := formatRFC3339(v.Raw); ok {
return appendJSONString(dst, []byte(s))
}
return appendJSONString(dst, v.Raw)
default:
return appendJSONString(dst, v.Raw)
}
}
func looksNumeric(b []byte) bool {
if len(b) == 0 {
return false
}
for i, c := range b {
if (c >= '0' && c <= '9') || c == '-' || c == '+' || c == '.' || c == 'e' || c == 'E' {
continue
}
if i == 0 && c == ' ' {
continue
}
return false
}
return true
}
var dateTimeLayouts = []string{
"2006-01-02 15:04:05.999999999",
"2006-01-02 15:04:05",
"2006-01-02T15:04:05Z07:00",
"2006-01-02T15:04:05.999999999Z07:00",
}
func formatRFC3339(raw []byte) (string, bool) {
s := string(raw)
for _, layout := range dateTimeLayouts {
t, err := time.ParseInLocation(layout, s, time.UTC)
if err != nil {
continue
}
if t.Nanosecond() != 0 {
return t.UTC().Format(time.RFC3339Nano), true
}
return t.UTC().Format(time.RFC3339), true
}
return "", false
}
const hexDigits = "0123456789abcdef"
func appendJSONString(dst, s []byte) []byte {
dst = append(dst, '"')
for _, c := range s {
switch c {
case '"', '\\':
dst = append(dst, '\\', c)
case '\n':
dst = append(dst, '\\', 'n')
case '\r':
dst = append(dst, '\\', 'r')
case '\t':
dst = append(dst, '\\', 't')
case '\b':
dst = append(dst, '\\', 'b')
case '\f':
dst = append(dst, '\\', 'f')
default:
if c < 0x20 {
dst = append(dst, '\\', 'u', '0', '0', hexDigits[c>>4], hexDigits[c&0xf])
} else {
dst = append(dst, c)
}
}
}
return append(dst, '"')
}