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>
153 lines
3.6 KiB
Go
153 lines
3.6 KiB
Go
// Package row defines scanned values, key tuples and the line encoders used
|
|
// to serialize rows into dump, csv and jsonl shards.
|
|
package row
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// Value is one column value as returned by the MySQL driver with
|
|
// parseTime disabled. A nil Raw means SQL NULL; an empty non-nil Raw means
|
|
// the empty string.
|
|
type Value struct {
|
|
Raw []byte
|
|
}
|
|
|
|
func (v Value) IsNull() bool { return v.Raw == nil }
|
|
func (v Value) Bytes() []byte { return v.Raw }
|
|
func (v Value) String() string { return string(v.Raw) }
|
|
|
|
// DriverValue returns the value as a bind parameter.
|
|
func (v Value) DriverValue() any {
|
|
if v.Raw == nil {
|
|
return nil
|
|
}
|
|
return v.Raw
|
|
}
|
|
|
|
// KeyPart is a single component of a key tuple. Values are held as text so a
|
|
// key can be serialized into state.json and rebound to the driver without
|
|
// carrying driver-specific types.
|
|
type KeyPart struct {
|
|
Null bool `json:"null,omitempty"`
|
|
Val string `json:"val,omitempty"`
|
|
}
|
|
|
|
// DriverValue converts the part into a value accepted by database/sql.
|
|
func (p KeyPart) DriverValue() any {
|
|
if p.Null {
|
|
return nil
|
|
}
|
|
return p.Val
|
|
}
|
|
|
|
// Key is a tuple of values identifying a row position in a unique index.
|
|
type Key []KeyPart
|
|
|
|
func KeyOf(values ...string) Key {
|
|
parts := make(Key, len(values))
|
|
for i, v := range values {
|
|
parts[i] = KeyPart{Val: v}
|
|
}
|
|
return parts
|
|
}
|
|
|
|
// Args returns the key as driver bind parameters.
|
|
func (k Key) Args() []any {
|
|
out := make([]any, len(k))
|
|
for i, p := range k {
|
|
out[i] = p.DriverValue()
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (k Key) IsZero() bool { return len(k) == 0 }
|
|
|
|
func (k Key) Equal(o Key) bool {
|
|
if len(k) != len(o) {
|
|
return false
|
|
}
|
|
for i := range k {
|
|
if k[i] != o[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (k Key) String() string {
|
|
if len(k) == 0 {
|
|
return "()"
|
|
}
|
|
parts := make([]string, len(k))
|
|
for i, p := range k {
|
|
if p.Null {
|
|
parts[i] = "NULL"
|
|
} else {
|
|
parts[i] = p.Val
|
|
}
|
|
}
|
|
return "(" + strings.Join(parts, ",") + ")"
|
|
}
|
|
|
|
// FirstLast returns the two parts of a composite (or single) key.
|
|
func (k Key) FirstLast() (KeyPart, KeyPart) {
|
|
if len(k) == 0 {
|
|
return KeyPart{Null: true}, KeyPart{Null: true}
|
|
}
|
|
return k[0], k[len(k)-1]
|
|
}
|
|
|
|
// Row holds a scanned row in select-list order.
|
|
type Row struct {
|
|
Values []Value
|
|
}
|
|
|
|
// Chunk is a batch of rows produced by one scan step. Keys is parallel to
|
|
// Rows and holds the unique-index key of each row; Buckets is parallel to
|
|
// Rows and is populated only in date sharding mode.
|
|
type Chunk struct {
|
|
Rows []Row
|
|
Keys []Key
|
|
Buckets []string
|
|
FirstKey Key
|
|
LastKey Key
|
|
}
|
|
|
|
func (c *Chunk) Len() int { return len(c.Rows) }
|
|
|
|
// Column describes one exported column for encoding purposes.
|
|
type Column struct {
|
|
Name string
|
|
Type string // lowercase information_schema DATA_TYPE
|
|
}
|
|
|
|
func isOneOf(t string, set ...string) bool {
|
|
for _, s := range set {
|
|
if t == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func IsInteger(t string) bool {
|
|
return isOneOf(t, "tinyint", "smallint", "mediumint", "int", "integer", "bigint", "year")
|
|
}
|
|
|
|
func IsFloat(t string) bool {
|
|
return isOneOf(t, "float", "double", "real")
|
|
}
|
|
|
|
func IsDecimal(t string) bool { return isOneOf(t, "decimal", "numeric") }
|
|
|
|
func IsBinary(t string) bool {
|
|
return isOneOf(t, "binary", "varbinary", "tinyblob", "blob", "mediumblob", "longblob", "bit", "geometry", "geometrycollection", "point", "linestring", "polygon", "multipoint", "multilinestring", "multipolygon")
|
|
}
|
|
|
|
func IsDate(t string) bool { return t == "date" }
|
|
|
|
func IsDateTime(t string) bool { return isOneOf(t, "datetime", "timestamp") }
|
|
|
|
func IsTimeType(t string) bool { return isOneOf(t, "date", "datetime", "timestamp") }
|