dbf

package module
v0.0.0-...-eb960bd Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2023 License: MIT Imports: 15 Imported by: 0

README

Opening a DBF to read from it

package main

import (
    "github.com/Kirides/go-dbf"
    "golang.org/x/text/encoding/charmap"
)

func main() {
    db, err := dbf.Open(`C:\Path\To\Some.dbf`, charmap.Windows1252.NewDecoder())
    if err != nil {
        panic(err)
    }
    defer db.Close()
}

Table scan

err = db.Scan(func(r dbf.Record) error {
    if !r.Deleted() {
        _, err = r.ToMap() // returns a map[string]interface{}
        if err != nil {
            panic(err)
        }
    }
    return nil
})

if err != nil {
    panic(err)
}

Reading a specific record

// recno is zero based
err := db.RecordAt(/*recno:*/ 5, func(r dbf.Record) {
    if !r.Deleted() {
        _, err = r.ToMap() // returns a map[string]interface{}
        if err != nil {
            panic(err)
        }
    }
})

if err != nil {
    panic(err)
}

Extened fieldnames from DBC

Quick and easy

// Read the accompanying DBC file
err := db.ReadDBC()

Pre-read a DBC for reuse with multiple tables

// Read a DBC that lies relative to a .dbf
dbc, err := dbf.ReadDBC(filepath.Join(`location/of/dbf/`), db.DBC()), charmap.Windows1252.NewDecoder())

err := db.ReadFromDBC(dbc)

Example of reusing a dbc

knownDbcs := make(map[string]*dbf.Dbc)
dec := charmap.Windows1252.NewDecoder()

db, _ := dbf.Open(`...`, dec)

if db.DBC() != "" {
    if d, ok := knownDbcs[db.DBC()]; ok {
        db.ReadFromDBC(d)
    } else {
        dbc, err := dbf.ReadDBC(
                    filepath.Join(
                        filepath.Dir(dbfPath), db.DBC()),
                    dec)
        // ...
        db.ReadFromDBC(dbc)
        knownDbcs[db.DBC()] = dbc
    }
}

Mapped datatypes

  • C -> string
  • V -> string (basic support, might fail on tables with large amount of nullables and/or varchars)
  • M -> string
  • D -> time.Time (in local timezone)
  • T -> time.Time (in local timezone)
  • I -> uint32
  • L -> bool
  • N
    • No decimals: int64
    • Decimals: float64

Currently unsupported datatypes

  • G General (COM)
  • Q Binary
  • B Double

Documentation

Index

Constants

View Source
const (
	FieldFlagNone          = 0x00
	FieldFlagSystem        = 0x01
	FieldFlagNull          = 0x02
	FieldFlagBinary        = 0x04
	FieldFlagBinaryAndNull = 0x06
	FieldFlagAutoInc       = 0x0C
)

Variables

View Source
var ErrInvalidRecordNumber = errors.New("Invalid record")

ErrInvalidRecordNumber is returned whenever a provided record number is invalid

Functions

func MinimumDateTime

func MinimumDateTime() time.Time

MinimumDateTime returns 0001-01-01T00:00:00 @ time.Local

Types

type Dbc

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

Dbc is a database for Visual FoxPro tables

func ReadDBC

func ReadDBC(path string, decoder *encoding.Decoder) (*Dbc, error)

func (*Dbc) TableFields

func (db *Dbc) TableFields(name string) ([]string, error)

TableFields returns the fields of a table

type Dbf

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

Dbf provides methods to access a DBF

func Open

func Open(path string, decoder *encoding.Decoder) (*Dbf, error)

Open opens the specifid DBF

func (*Dbf) CalculatedRecordCount

func (dbf *Dbf) CalculatedRecordCount() int

CalculatedRecordCount returns the calculated RecordCount or -1.

func (*Dbf) Close

func (dbf *Dbf) Close() error

Close closes the underlying DBF/FPT

func (*Dbf) DBC

func (dbf *Dbf) DBC() string

DBC returns the DBF's DBC

func (*Dbf) FieldByName

func (dbf *Dbf) FieldByName(name string) (Field, error)

FieldByName returns a field by it name (Case insensitive)

func (*Dbf) Header

func (dbf *Dbf) Header() Header

Header returns the DBF header

func (*Dbf) ReadDBC

func (dbf *Dbf) ReadDBC() error

ReadDBC reads the DBC to which this table belongs. This updates the internal Fieldnames if they were longer than 10 chars

func (*Dbf) ReadFromDBC

func (dbf *Dbf) ReadFromDBC(db *Dbc) error

ReadFromDBC reads the DBC. This updates the internal Fieldnames if they were longer than 10 chars

func (*Dbf) RecordAt

func (dbf *Dbf) RecordAt(recno uint32, handle func(*Record), options ParseOption) error

RecordAt reads the record at the specified position

func (*Dbf) Scan

func (dbf *Dbf) Scan(walk func(*Record) error, options ParseOption) error

Scan walks the entire table until the end or walk returns a non nil error

func (*Dbf) ScanOffset

func (dbf *Dbf) ScanOffset(offset uint32, walk func(*Record) error, options ParseOption) error

ScanOffset walks the table starting at `offset` until the end or walk returns a non nil error

type Field

type Field struct {
	Name               string
	Type               rune
	Displacement       uint32
	Length             byte
	DecimalCount       byte
	Flags              FieldFlag
	NextAutoIncrement  uint32
	AutoIncrementStep  byte
	Index              int
	VarLengthSizeIndex int
	NullFieldIndex     int
}

Field provides methods to access a DBF

type FieldFlag

type FieldFlag byte

type Flag

type Flag byte

Flag defines flags

const (
	// FlagNone no flags specified
	FlagNone Flag = 0x00
	// FlagCDX File has a supporting structural index
	FlagCDX Flag = 0x01
	// FlagMemo File has a supporting Memo file
	FlagMemo Flag = 0x02
	// FlagDBC File is part of a DBC
	FlagDBC Flag = 0x04
)
type Header struct {
	Type         Type
	ModYear      byte
	ModMonth     byte
	ModDay       byte
	RecordCount  uint32
	HeaderSize   uint16
	RecordLength uint16
	Reserved     [16]byte
	Flags        Flag
	CodePage     byte
}

Header defines the DBF header

func (Header) LastModified

func (h Header) LastModified() time.Time

LastModified returns the last modification date

type ParseOption

type ParseOption byte

ParseOption options for handling row parsing

const (
	// ParseDefault default options
	ParseDefault ParseOption = 0
	// ParseTrimRight strings.TrimRight(s, " ") is applied to `C`-type fields
	ParseTrimRight ParseOption = 1 << 0
)

type Record

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

Record provides methods to work with record

func (*Record) Deleted

func (r *Record) Deleted() bool

Deleted returns a bool that tells if a record is marked as deleted or not

func (*Record) Field

func (r *Record) Field(fieldName string) (interface{}, error)

Field returns a value for that specific field

func (*Record) FieldAt

func (r *Record) FieldAt(fieldIndex int) (interface{}, error)

FieldAt returns a value for that specific field

func (*Record) Recno

func (r *Record) Recno() uint32

Recno returns the record number for the current record

func (*Record) ToMap

func (r *Record) ToMap() (map[string]interface{}, error)

ToMap parses the record into a map[string]interface{}

func (*Record) ToSlice

func (r *Record) ToSlice() ([]interface{}, error)

ToSlice parses the record into a []interface{}

func (*Record) WithSlice

func (r *Record) WithSlice(sf func([]interface{})) error

WithSlice parses the record into a []interface{} The slice is only valid within the current Scan

type Type

type Type byte

Type specifies the table type

const (
	// TypeNone ...
	TypeNone Type = 0x00
	// TypeFoxBase ...
	TypeFoxBase Type = 0x02
	// TypeFoxBasePlusDBaseIII ...
	TypeFoxBasePlusDBaseIII Type = 0x03
	// TypeVisualFoxPro ...
	TypeVisualFoxPro Type = 0x30
	// TypeVisualFoxProAutoInc ...
	TypeVisualFoxProAutoInc Type = 0x31
	// TypeVisualFoxProVar ...
	TypeVisualFoxProVar Type = 0x32
	// TypeDBaseIVTable ...
	TypeDBaseIVTable Type = 0x43
	// TypeDBaseIVSystem ...
	TypeDBaseIVSystem Type = 0x63
	// TypeFoxBasePlusDBaseIIIMemo ...
	TypeFoxBasePlusDBaseIIIMemo Type = 0x83
	// TypeDBaseIVMemo ...
	TypeDBaseIVMemo Type = 0x8B
	// TypeDBaseIVTableMemo ...
	TypeDBaseIVTableMemo Type = 0xCB
	// TypeFoxPro2Memo ...
	TypeFoxPro2Memo Type = 0xF5
	// TypeFoxBase2 ...
	TypeFoxBase2 Type = 0xFB
)

Jump to

Keyboard shortcuts

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