dbf

package module
v0.0.0-...-009ebcb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2025 License: MIT Imports: 12 Imported by: 3

README

dbf dBase III+ library for Go

Package for working with dBase III plus database files. Default encoding is UTF-8.

  1. Package provides both reflection-via-struct interface and direct Row()/FieldValueByName()/AddxxxField() interface.
  2. Once table is created and rows added to it, table structure can not be modified.
  3. Working with reflection-via-struct interface is easier and produces less verbose code.
  4. Use Iterator to iterate over table since it skips deleted rows.

Struct Tags

When using the struct interface, you can use struct tags to customize field mapping:

type Person struct {
    Name    string    `dbf:"NAME"`              // Map to NAME field
    Age     int       `dbf:"AGE,omitempty"`     // Map to AGE field, omit if zero
    Created time.Time `dbf:"CREATED"`           // Map to CREATED field
}

The omitempty tag option works similarly to the encoding/json package. When omitempty is specified for a field, that field will only be written if its value is not the zero value for its type.

Usage

Typical usage db := dbf.New() or dbf.LoadFile(filename)

then use db.NewIterator() and iterate or db.Append()

do not forget db.SaveFile(filename) if you want changes saved.

TODO

File is loaded and kept in-memory. Not a good design choice if file is huge. This should be changed to use buffers and keep some of the data on-disk in the future. Current API structure should allow redesign.

Where to start

Look into cmd directory for examples of use and basic tools to load and export into CSV files.

License

Copyright (C) Tad Vizbaras. Released under MIT license.

Documentation

Overview

Package for working with dBase III plus database files.

1. Package provides both reflection-via-struct interface and direct Row()/FieldValueByName()/AddxxxField() interface. 2. Once table is created and rows added to it, table structure can not be modified. 3. Working with reflection-via-struct interface is easier and produces less verbose code. 4. Use Iterator to iterate over table since it skips deleted rows.

With struct interface, you can use struct tags to customize field mapping:

type Person struct {
   Name    string    `dbf:"NAME"`              // Map to NAME field
   Age     int       `dbf:"AGE,omitempty"`     // Map to AGE field, omit if zero
   Created time.Time `dbf:"CREATED"`           // Map to CREATED field
}

The `omitempty` tag option works similarly to the encoding/json package. When omitempty is specified for a field, that field will only be written if its value is not the zero value for its type.

TODO: File is loaded and kept in-memory. Not a good design choice if file is huge. This should be changed to use buffers and keep some of the data on-disk in the future. Current API structure should allow redesign.

Typical usage db := dbf.New() or dbf.LoadFile(filename)

then use db.NewIterator() and iterate or db.Append()

do not forget db.SaveFile(filename) if you want changes saved.

Index

Constants

This section is empty.

Variables

View Source
var Version = struct {
	major, minor, build int
}{
	1, 0, 2,
}

last mod 02/14/2016

Functions

This section is empty.

Types

type DbfField

type DbfField struct {
	Name      string
	Type      string
	Length    uint8
	Precision uint8
	// contains filtered or unexported fields
}

func (*DbfField) SetFieldName

func (df *DbfField) SetFieldName(fieldName string)

type DbfTable

type DbfTable struct {
	// contains filtered or unexported fields
}

func LoadFile

func LoadFile(fileName string) (table *DbfTable, err error)

LoadFile load dBase III+ from file.

func New

func New() *DbfTable

Create a new dbase table from the scratch

func (*DbfTable) AddBoolField

func (dt *DbfTable) AddBoolField(fieldName string) error

Boolean field stores 't' or 'f' in the cell.

func (*DbfTable) AddDateField

func (dt *DbfTable) AddDateField(fieldName string) error

func (*DbfTable) AddFloatField

func (dt *DbfTable) AddFloatField(fieldName string, length, prec uint8) error

AddFloatField add float.

func (*DbfTable) AddIntField

func (dt *DbfTable) AddIntField(fieldName string, length uint8) error

AddIntField add int.

func (*DbfTable) AddMemoField

func (dt *DbfTable) AddMemoField(fieldName string) error

func (*DbfTable) AddNumberField

func (dt *DbfTable) AddNumberField(fieldName string, length, prec uint8) error

AddNumberField can be used to add int or float number fields.

func (*DbfTable) AddRecord

func (dt *DbfTable) AddRecord() int

AddRecord always adds new rows to the end of file.

func (*DbfTable) AddTextField

func (dt *DbfTable) AddTextField(fieldName string, length uint8) error

AddTextField max size 254 bytes.

func (*DbfTable) Append

func (dt *DbfTable) Append(spec interface{}) (int, error)

Append record to table.

func (*DbfTable) Create

func (dt *DbfTable) Create(spec interface{}) error

Create schema based on the spec struct.

func (*DbfTable) Delete

func (dt *DbfTable) Delete(row int)

Delete row by setting marker.

func (*DbfTable) FieldValue

func (dt *DbfTable) FieldValue(row int, fieldIndex int) string

func (*DbfTable) FieldValueByName

func (dt *DbfTable) FieldValueByName(row int, fieldName string) string

FieldValueByName returns the value of a field given row number and fieldName provided.

func (*DbfTable) Fields

func (dt *DbfTable) Fields() []DbfField

Fields return slice of DbfField

func (*DbfTable) InsertRecord

func (dt *DbfTable) InsertRecord() int

InsertRecord tries to reuse deleted records, and only then add new record to the end of file if no delete slots exist. If you are looping over rows it is better to use AddRecord.

func (*DbfTable) IsDeleted

func (dt *DbfTable) IsDeleted(row int) bool

IsDeleted row.

func (*DbfTable) NewIterator

func (dt *DbfTable) NewIterator() *Iterator

func (*DbfTable) NumRecords

func (dt *DbfTable) NumRecords() int

NumRecords return number of rows in dbase table.

func (*DbfTable) RawFieldValue

func (dt *DbfTable) RawFieldValue(row int, fieldIndex int) string

func (*DbfTable) RawFieldValueByName

func (dt *DbfTable) RawFieldValueByName(row int, fieldName string) string

RawFieldValueByName returns the untrimmed value of a field given row number and fieldName provided.

func (*DbfTable) Read

func (dt *DbfTable) Read(row int, spec interface{}) error

Read data into the spec from DbfTable.

func (*DbfTable) Row

func (dt *DbfTable) Row(row int) []string

Row reads record at index.

func (*DbfTable) SaveFile

func (dt *DbfTable) SaveFile(filename string) error

SaveFile dbf file.

func (*DbfTable) SetFieldValue

func (dt *DbfTable) SetFieldValue(row int, fieldIndex int, value string)

Sets field value by index.

func (*DbfTable) SetFieldValueByName

func (dt *DbfTable) SetFieldValueByName(row int, fieldName string, value string)

Sets field value by name.

func (*DbfTable) Write

func (dt *DbfTable) Write(row int, spec interface{}) (int, error)

Write data into DbfTable from the spec.

type Iterator

type Iterator struct {
	// contains filtered or unexported fields
}

func (*Iterator) Delete

func (it *Iterator) Delete()

Delete row under iterator. This is possible because rows are marked as deleted but are not physically deleted.

func (*Iterator) Index

func (it *Iterator) Index() int

func (*Iterator) Next

func (it *Iterator) Next() bool

Next iterates over records in the table.

func (*Iterator) Read

func (it *Iterator) Read(spec interface{}) error

Read data into struct.

func (*Iterator) Row

func (it *Iterator) Row() []string

Row data as raw slice.

func (*Iterator) Write

func (it *Iterator) Write(spec interface{}) (int, error)

Write record where iterator points to.

Directories

Path Synopsis
cmd
try

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL