Files
db_in_45_steps_go/0403/row.go
T
2026-08-05 21:15:44 +02:00

60 lines
1.1 KiB
Go

package db0403
import (
"errors"
"slices"
)
type Schema struct {
Table string
Cols []Column
PKey []int // indexes of primary key columns
}
type Column struct {
Name string
Type CellType
}
type Row []Cell
func (schema *Schema) NewRow() Row {
return make(Row, len(schema.Cols))
}
func (row Row) EncodeKey(schema *Schema) (key []byte)
func (row Row) EncodeVal(schema *Schema) (val []byte) {
check(len(row) == len(schema.Cols))
for idx, value := range row {
if !slices.Contains(schema.PKey, idx) {
check(value.Type == schema.Cols[idx].Type)
val = row[idx].EncodeVal(val)
}
}
return val
}
func (row Row) DecodeKey(schema *Schema, key []byte) (err error)
func (row Row) DecodeVal(schema *Schema, val []byte) (err error) {
check(len(row) == len(schema.Cols))
for idx, col := range schema.Cols {
if slices.Contains(schema.PKey, idx) {
continue
}
row[idx] = Cell{Type: col.Type}
if val, err = row[idx].DecodeVal(val); err != nil {
return err
}
}
if len(val) != 0 {
return errors.New("trailing garbage")
}
return nil
}
// QzBQWVJJOUhU https://trialofcode.org/