dbase

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: BSD-3-Clause Imports: 24 Imported by: 0

Documentation

Overview

Package dbase provides comprehensive tools for reading, writing, and managing dBase-format database files.

This package supports multiple dBase file versions including FoxPro, FoxBase, and dBase III/IV formats. It offers cross-platform compatibility with optimized I/O operations for both Unix and Windows systems.

Key Features:

  • Read and write dBase (.DBF) and memo (.FPT) files
  • Support for multiple dBase file versions and data types
  • Flexible data representation (maps, JSON, Go structs)
  • Character encoding conversion and code page interpretation
  • Safe concurrent operations with built-in synchronization
  • Comprehensive error handling with detailed trace information
  • Navigation and search capabilities within tables

Supported Data Types:

  • Character (C), Memo (M), Varchar (V) - string data
  • Numeric (N), Integer (I), Float (F), Currency (Y), Double (B) - numeric data
  • Date (D), DateTime (T) - temporal data
  • Logical (L) - boolean data
  • Blob (W), Varbinary (Q) - binary data

Basic Usage:

config := &dbase.Config{
    Filename: "example.dbf",
}
table, err := dbase.OpenTable(config)
if err != nil {
    log.Fatal(err)
}
defer table.Close()

for !table.EOF() {
    row, err := table.Next()
    if err != nil {
        log.Fatal(err)
    }
    // Process row data
}

Common use cases include migrating legacy dBase systems, data conversion and integration, interfacing with legacy applications, and building tools for dBase file manipulation.

Index

Constants

View Source
const (
	// MaxColumnNameLength is the maximum length for column names (10 characters)
	MaxColumnNameLength = 10
	// MaxCharacterLength is the maximum length for character fields (254 characters)
	MaxCharacterLength = 254
	// MaxNumericLength is the maximum length for numeric fields (20 digits)
	MaxNumericLength = 20
	// MaxFloatLength is the maximum length for float fields (20 digits)
	MaxFloatLength = 20
	// MaxIntegerValue is the maximum value for integer fields
	MaxIntegerValue = math.MaxInt32
	// MinIntegerValue is the minimum value for integer fields
	MinIntegerValue = math.MinInt32
	// MaxFieldsPerRecord is the maximum number of fields per record (255)
	MaxFieldsPerRecord = 255
	// MaxCharactersPerRecord is the maximum number of characters per record (65500)
	MaxCharactersPerRecord = 65500
	// MaxTableFileSize is the maximum size of a table file in bytes (2GB)
	MaxTableFileSize = 2 << 30
	// MaxRecordsPerTable is the maximum number of records per table (1 billion)
	MaxRecordsPerTable = 1000000000
	// MaxIndexKeyLength is the maximum length for index keys (100 characters)
	MaxIndexKeyLength = 100
	// MaxCompactIndexKeyLength is the maximum length for compact index keys (240 characters)
	MaxCompactIndexKeyLength = 240
	// NumericPrecisionDigits is the number of precision digits for numeric calculations (16)
	NumericPrecisionDigits = 16
)

dBase format limits and constraints https://learn.microsoft.com/en-us/previous-versions/visualstudio/foxpro/3kfd3hw9(v=vs.71)

Variables

View Source
var (
	// ErrEOF is returned when the end of a dBase database file is reached
	ErrEOF = errors.New("EOF")
	// ErrBOF is returned when the row pointer is attempted to be moved before the first row
	ErrBOF = errors.New("BOF")
	// ErrIncomplete is returned when the read of a row or column did not finish
	ErrIncomplete = errors.New("INCOMPLETE")
	// ErrNoFPT is returned when a file operation is attempted on a non-existent FPT file
	ErrNoFPT = errors.New("FPT_FILE_NOT_FOUND")
	// ErrNoDBF is returned when a file operation is attempted on a non-existent DBF file
	ErrNoDBF = errors.New("DBF_FILE_NOT_FOUND")
	// ErrInvalidPosition is returned when an invalid column position is used (x<1 or x>number of columns)
	ErrInvalidPosition = errors.New("INVALID_POSITION")
	// ErrInvalidEncoding is returned when an invalid encoding is encountered
	ErrInvalidEncoding = errors.New("INVALID_ENCODING")
	// ErrUnknownDataType is returned when an invalid data type is used
	ErrUnknownDataType = errors.New("UNKNOWN_DATA_TYPE")
)

Functions

func Debug

func Debug(enabled bool, out io.Writer)

Debug enables or disables debug logging for the dbase package. If debug is true, debug messages will be printed to the specified io.Writer (default: os.Stdout). If out is nil, the output destination is not changed.

func RegisterCustomEncoding

func RegisterCustomEncoding(codePageMark byte, encoding encoding.Encoding)

RegisterCustomEncoding registers a custom encoding for a specific code page mark. This allows support for additional encodings beyond the built-in ones.

func ValidateFileVersion

func ValidateFileVersion(version byte, untested bool) error

ValidateFileVersion checks if the dBase file version is supported and tested. If untested is true, validation is bypassed and any version is accepted.

Types

type Column

type Column struct {
	FieldName [11]byte // Column name with a maximum of 10 characters. If less than 10, it is padded with null characters (0x00).
	DataType  byte     // Column type
	Position  uint32   // Displacement of column in row
	Length    uint8    // Length of column (in bytes)
	Decimals  uint8    // Number of decimal places
	Flag      Flag     // Column flag
	Next      uint32   // Value of autoincrement Next value
	Step      uint8    // Value of autoincrement Step value
	Reserved  [8]byte  // Reserved
}

Column is a struct containing the column information

func NewColumn

func NewColumn(name string, dataType DataType, length uint8, decimals uint8, nullable bool) (*Column, error)

NewColumn creates a new column with the specified properties. The name must be between 1 and 10 characters long. The length parameter is only used for character, varbinary, varchar, numeric, and float data types. The decimals parameter specifies the number of decimal places for numeric and float types. The nullable parameter indicates whether the column can contain null values.

func (*Column) Name

func (c *Column) Name() string

Name returns the name of the column as a trimmed string (maximum length 10). Null bytes are trimmed from the end of the name.

func (*Column) Reflect

func (c *Column) Reflect() (reflect.Type, error)

Reflect returns the Go type that corresponds to the column's data type. This is useful for determining the expected Go type when working with column values.

func (*Column) Type

func (c *Column) Type() string

Type returns the data type of the column as a single character string.

type ColumnFlag

type ColumnFlag byte

Column flags indicate whether a column is hidden, can be null, is binary or is autoincremented

const (
	// HiddenFlag indicates the column is hidden from view (0x01)
	HiddenFlag ColumnFlag = 0x01
	// NullableFlag indicates the column can contain null values (0x02)
	NullableFlag ColumnFlag = 0x02
	// BinaryFlag indicates the column contains binary data (NOCPTRANS) (0x04)
	BinaryFlag ColumnFlag = 0x04 // NOCPTRANS
	// AutoincrementFlag indicates the column is auto-incremented (0x08)
	AutoincrementFlag ColumnFlag = 0x08
)

type Config

type Config struct {
	Filename                          string            // The filename of the DBF file.
	Converter                         EncodingConverter // The encoding converter to use.
	Exclusive                         bool              // If true the file is opened in exclusive mode.
	Untested                          bool              // If true the file version is not checked.
	TrimSpaces                        bool              // If true, spaces are trimmed from the start and end of string values.
	CollapseSpaces                    bool              // If true, any length of spaces is replaced by a single space.
	DisableConvertFilenameUnderscores bool              // If false underscores in the table filename are converted to spaces.
	ReadOnly                          bool              // If true the file is opened in read-only mode.
	WriteLock                         bool              // Whether or not the write operations should lock the record
	ValidateCodePage                  bool              // Whether or not the code page mark should be validated.
	InterpretCodePage                 bool              // Whether or not the code page mark should be interpreted. Ignores the defined converter.
	IO                                IO                // The IO interface to use.
}

Config is a struct containing the configuration for opening a Foxpro/dbase databse or table. The filename is mandatory.

The other fields are optional and are false by default. If Converter and InterpretCodePage are both not set the package will try to interpret the code page mark. To open untested files set Untested to true. Tested files are defined in the constants.go file.

type DataType

type DataType byte

DataType defines the possible types of a column

const (
	Character DataType = 0x43 // C - Character (string)
	Currency  DataType = 0x59 // Y - Currency (float64)
	Double    DataType = 0x42 // B - Double (float64)
	Date      DataType = 0x44 // D - Date (time.Time)
	DateTime  DataType = 0x54 // T - DateTime (time.Time)
	Float     DataType = 0x46 // F - Float (float64)
	Integer   DataType = 0x49 // I - Integer (int32)
	Logical   DataType = 0x4C // L - Logical (bool)
	Memo      DataType = 0x4D // M - Memo (string)
	Numeric   DataType = 0x4E // N - Numeric (int64)
	Blob      DataType = 0x57 // W - Blob ([]byte)
	General   DataType = 0x47 // G - General (string)
	Picture   DataType = 0x50 // P - Picture (string)
	Varbinary DataType = 0x51 // Q - Varbinary ([]byte)
	Varchar   DataType = 0x56 // V - Varchar (string)
)

func (DataType) String

func (t DataType) String() string

String returns the type of the column as string representation.

type Database

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

func OpenDatabase

func OpenDatabase(config *Config) (*Database, error)

OpenDatabase opens a dbase/foxpro database file and all related tables The database file must be a DBC file and the tables must be DBF files and in the same directory as the database

func (*Database) Close

func (db *Database) Close() error

Close the database file and all related tables

func (*Database) Names

func (db *Database) Names() []string

Returns the names of every table in the database

func (*Database) Schema

func (db *Database) Schema() map[string][]*Column

Returns the complete database schema

func (*Database) Tables

func (db *Database) Tables() map[string]*File

Returns all table of the database

type DefaultConverter

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

func ConverterFromCodePage

func ConverterFromCodePage(codePageMark byte) DefaultConverter

NewDefaultConverterFromCodePage returns a new EncodingConverter from a code page mark

func NewDefaultConverter

func NewDefaultConverter(encoding encoding.Encoding) DefaultConverter

NewDefaultConverter creates a new DefaultConverter with the specified encoding.

func (DefaultConverter) CodePage

func (c DefaultConverter) CodePage() byte

CodePageMark returns corresponding code page mark for the encoding

func (DefaultConverter) Decode

func (c DefaultConverter) Decode(in []byte) ([]byte, error)

Decode decodes a specified encoding to byte slice to a UTF8 byte slice

func (DefaultConverter) Encode

func (c DefaultConverter) Encode(in []byte) ([]byte, error)

Decode decodes a UTF8 byte slice to the specified encoding byte slice

type EncodingConverter

type EncodingConverter interface {
	Decode(in []byte) ([]byte, error)
	Encode(in []byte) ([]byte, error)
	CodePage() byte
}

EncodingConverter is an interface for encoding conversion between UTF8 and other encoding

type Error

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

Error is a wrapper for errors that occur in the dbase package

func NewError

func NewError(err string) Error

NewError creates a new Error with the given error message.

func NewErrorf

func NewErrorf(format string, a ...interface{}) Error

NewErrorf creates a new Error with formatted message using fmt.Sprintf.

func WrapError

func WrapError(err error) Error

WrapError wraps an existing error into a dbase Error with trace information.

func (Error) Details

func (e Error) Details(err error) Error

Details adds an additional error detail to this Error.

func (Error) Error

func (e Error) Error() string

type Field

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

Field is a row data field

func (Field) Column

func (field Field) Column() *Column

Column returns the field column definition

func (Field) GetValue

func (field Field) GetValue() interface{}

Value returns the field value

func (Field) Name

func (field Field) Name() string

Name returns the field name

func (*Field) SetValue

func (field *Field) SetValue(value interface{}) error

SetValue allows to change the field value

func (Field) Type

func (field Field) Type() DataType

Type returns the field type

type File

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

File is the main struct to handle a dBase file. Each file type is basically a Table or a Memo file.

func NewTable

func NewTable(version FileVersion, config *Config, columns []*Column, memoBlockSize uint16, io IO) (*File, error)

NewTable creates a new dBase file with the specified version, configuration, and columns. The memoBlockSize parameter specifies the block size for memo files (use 0 for default). The io parameter specifies the IO implementation to use (use nil for default).

Note: This function is intended for development and testing purposes. Consider using established dBase tools for production applications.

func OpenTable

func OpenTable(config *Config) (*File, error)

OpenTable opens a dBase database file (and the memo file if needed). The config parameter is required to specify the file path, encoding, file handles (IO) and other options. If config.IO is nil, the default implementation is used depending on the operating system.

func (*File) BOF

func (file *File) BOF() bool

BOF returns true if the internal row pointer is before the first row.

func (*File) BytesToRow

func (file *File) BytesToRow(data []byte) (*Row, error)

BytesToRow converts raw row data to a Row struct. If the data references a memo (FPT) file, that file is also read.

func (*File) Close

func (file *File) Close() error

Close closes all file handlers for the dBase file and its associated memo file.

func (*File) Column

func (file *File) Column(pos int) *Column

Column returns the column at the specified position, or nil if the position is invalid.

func (*File) ColumnNames

func (file *File) ColumnNames() []string

ColumnNames returns a slice containing all column names in the dBase table.

func (*File) ColumnPos

func (file *File) ColumnPos(column *Column) int

ColumnPos returns the position of the specified column, or -1 if not found.

func (*File) ColumnPosByName

func (file *File) ColumnPosByName(colname string) int

ColumnPosByName returns the position of a column by name, or -1 if not found.

func (*File) Columns

func (file *File) Columns() []*Column

Columns returns all columns in the dBase table.

func (*File) ColumnsCount

func (file *File) ColumnsCount() uint16

ColumnsCount returns the number of columns in the dBase table.

func (*File) Create

func (file *File) Create() error

Create creates a new dBase database file (and the memo file if needed).

func (*File) Deleted

func (file *File) Deleted() (bool, error)

Deleted returns true if the row at the current internal row pointer position is marked as deleted.

func (*File) EOF

func (file *File) EOF() bool

EOF returns true if the internal row pointer is at the end of file.

func (*File) GetColumnModification

func (file *File) GetColumnModification(position int) *Modification

GetColumnModification returns the column modification for the column at the specified position.

func (*File) GetHandle

func (file *File) GetHandle() (interface{}, interface{})

GetHandle returns the file handles being used (dBase file handle, memo file handle).

func (*File) GetIO

func (file *File) GetIO() IO

GetIO returns the IO implementation currently being used by this file.

func (*File) GoTo

func (file *File) GoTo(row uint32) error

GoTo sets the internal row pointer to the specified row number. Returns an EOF error if positioning beyond the end of file and positions the pointer at lastRow+1.

func (*File) Header

func (file *File) Header() *Header

Header returns the dBase file header struct for inspection.

func (*File) Init

func (file *File) Init() error

Init creates the dBase files and writes the header and columns to them.

func (*File) Interpret

func (file *File) Interpret(raw []byte, column *Column) (interface{}, error)

Converts raw column data to the correct type for the given column For C and M columns a charset conversion is done For M columns the data is read from the memo file At this moment not all FoxPro column types are supported. When reading column values, the value returned by this package is always `interface{}`.

The supported column types with their return Go types are:

| Column Type | Column Type Name | Golang type | | ----------- | ---------------- | ----------- | | B | Double | float64 | | C | Character | string | | D | Date | time.Time | | F | Float | float64 | | I | Integer | int32 | | L | Logical | bool | | M | Memo | string | | M | Memo (Binary) | []byte | | N | Numeric (0 decimals) | int64 | | N | Numeric (with decimals) | float64 | | T | DateTime | time.Time | | Y | Currency | float64 |

Not all available column types have been implemented because we don't use them in our DBFs

func (*File) NewField

func (file *File) NewField(pos int, value interface{}) (*Field, error)

NewField creates a new field with the specified value and column at the given position. Returns an error if the column position is invalid.

func (*File) NewFieldByName

func (file *File) NewFieldByName(name string, value interface{}) (*Field, error)

NewFieldByName creates a new field with the specified value and column identified by name. Returns an error if the column is not found.

func (*File) NewRow

func (file *File) NewRow() *Row

NewRow creates a new Row struct with the same column structure as the dBase file. The row is positioned at the next available row position.

func (*File) Next

func (file *File) Next() (*Row, error)

Next reads the current row and increments the row pointer by one.

func (*File) Pointer

func (file *File) Pointer() uint32

Pointer returns the current row pointer position.

func (*File) ReadColumns

func (file *File) ReadColumns() ([]*Column, *Column, error)

ReadColumns reads column definitions from the dBase file header, starting at position 32, until it finds the header row terminator END_OF_COLUMN (0x0D).

func (*File) ReadHeader

func (file *File) ReadHeader() error

ReadHeader reads the dBase file header from the file handle.

func (*File) ReadMemo

func (file *File) ReadMemo(address []byte, column *Column) ([]byte, bool, error)

ReadMemo reads one or more blocks from the memo file for the specified memo column. Returns the raw data and a boolean indicating if the data is text (true) or binary (false).

func (*File) ReadMemoHeader

func (file *File) ReadMemoHeader() error

ReadMemoHeader reads the memo file header from the given file handle.

func (*File) ReadNullFlag

func (file *File) ReadNullFlag(position uint64, column *Column) (bool, bool, error)

ReadNullFlag reads the null flag field at the end of the row. The null flag field indicates if the field has a variable length. Returns true as the first value if the field is variable length, and true as the second value if the field is null.

func (*File) ReadRow

func (file *File) ReadRow(position uint32) ([]byte, error)

ReadRow reads the raw row data of one row at the specified row position.

func (*File) Represent

func (file *File) Represent(field *Field, padding bool) ([]byte, error)

Represent converts column data to the byte representation of the columns data type For M values the data is written to the memo file and the address is returned

func (*File) Row

func (file *File) Row() (*Row, error)

Row returns the row at the current file row pointer position.

func (*File) RowFromJSON

func (file *File) RowFromJSON(j []byte) (*Row, error)

Converts a JSON-encoded row into the row representation

func (*File) RowFromMap

func (file *File) RowFromMap(m map[string]interface{}) (*Row, error)

Converts a map of interfaces into the row representation

func (*File) RowFromStruct

func (file *File) RowFromStruct(v interface{}) (*Row, error)

Converts a struct into the row representation The struct must have the same field names as the columns in the table or the dbase tag must be set. The dbase tag can be used to name the field. For example: `dbase:"my_field_name"`

func (*File) Rows

func (file *File) Rows(skipInvalid bool, skipDeleted bool) ([]*Row, error)

Rows returns all rows in the dBase file as a slice. If skipInvalid is true, invalid rows are skipped instead of returning an error. If skipDeleted is true, deleted rows are excluded from the result.

func (*File) RowsCount

func (file *File) RowsCount() uint32

RowsCount returns the number of rows in the dBase file.

func (*File) Search

func (file *File) Search(field *Field, exactMatch bool) ([]*Row, error)

Search searches for rows that contain the specified value in the given field. If exactMatch is true, only exact matches are returned; otherwise, partial matches are included.

func (*File) SetColumnModification

func (file *File) SetColumnModification(position int, mod *Modification)

SetColumnModification sets a modification for the column at the specified position. If the position is out of range, the operation is ignored.

func (*File) SetColumnModificationByName

func (file *File) SetColumnModificationByName(name string, mod *Modification) error

SetColumnModificationByName sets a modification for the column with the specified name. Returns an error if the column is not found.

func (*File) Skip

func (file *File) Skip(offset int64)

Skip adds the specified offset to the internal row pointer. If the result would position beyond the end of file, positions the pointer at lastRow+1. If the result would be negative, positions the pointer at 0. Note: This method does not skip deleted rows automatically.

func (*File) TableName

func (file *File) TableName() string

TableName returns the name of the dBase table.

func (*File) WriteColumns

func (file *File) WriteColumns() error

WriteColumns writes the column definitions to the end of the header in the dBase file.

func (*File) WriteHeader

func (file *File) WriteHeader() error

WriteHeader writes the header to the dBase file.

func (*File) WriteMemo

func (file *File) WriteMemo(address []byte, data []byte, text bool, length int) ([]byte, error)

WriteMemo writes memo data to the memo file and returns the address of the memo. The text parameter indicates whether the data is text (true) or binary (false). The length parameter specifies the length of the data to write.

func (*File) WriteMemoHeader

func (file *File) WriteMemoHeader(size int) error

WriteMemoHeader writes the memo header to the memo file. The size parameter specifies the number of blocks the new memo data will occupy.

func (*File) WriteRow

func (file *File) WriteRow(row *Row) error

WriteRow writes the raw row data to the specified row position in the dBase file.

type FileExtension

type FileExtension string

Allowed file extensions for the different file types

const (
	DBC FileExtension = ".DBC" // Database file extension
	DCT FileExtension = ".DCT" // Database container file extension
	DBF FileExtension = ".DBF" // Table file extension
	FPT FileExtension = ".FPT" // Memo file extension
	SCX FileExtension = ".SCX" // Form file extension
	LBX FileExtension = ".LBX" // Label file extension
	MNX FileExtension = ".MNX" // Menu file extension
	PJX FileExtension = ".PJX" // Project file extension
	RPX FileExtension = ".RPX" // Report file extension
	VCX FileExtension = ".VCX" // Visual class library file extension
)

type FileVersion

type FileVersion byte

Supported and testet file versions - other files may work but are not tested The file version check has to be bypassed when opening a file type that is not supported https://learn.microsoft.com/en-us/previous-versions/visualstudio/foxpro/st4a0s68(v=vs.71)

const (
	// FoxPro represents FoxPro file format (0x30)
	FoxPro FileVersion = 0x30
	// FoxProAutoincrement represents FoxPro file format with autoincrement support (0x31)
	FoxProAutoincrement FileVersion = 0x31
	// FoxProVar represents FoxPro file format with variable length fields (0x32)
	FoxProVar FileVersion = 0x32
)

Supported and testet file types - other file types may work but are not tested

const (
	// FoxBase represents FoxBase file format (0x02)
	FoxBase FileVersion = 0x02
	// FoxBase2 represents FoxBase 2 file format (0xFB)
	FoxBase2 FileVersion = 0xFB
	// FoxBasePlus represents FoxBase Plus file format (0x03)
	FoxBasePlus FileVersion = 0x03
	// DBaseSQLTable represents dBase SQL table format (0x43)
	DBaseSQLTable FileVersion = 0x43
	// FoxBasePlusMemo represents FoxBase Plus with memo support (0x83)
	FoxBasePlusMemo FileVersion = 0x83
	// DBaseMemo represents dBase with memo support (0x8B)
	DBaseMemo FileVersion = 0x8B
	// DBaseSQLMemo represents dBase SQL with memo support (0xCB)
	DBaseSQLMemo FileVersion = 0xCB
	// FoxPro2Memo represents FoxPro 2 with memo support (0xF5)
	FoxPro2Memo FileVersion = 0xF5
)

Not tested file versions - these may work but are not officially supported

type Flag

type Flag byte

Flag represents a general purpose flag type for bit operations.

func (Flag) Has

func (f Flag) Has(mask byte) bool

Has checks if the flag has the specified mask bits set.

func (Flag) HasAll

func (f Flag) HasAll(mask byte) bool

HasAll checks if the flag has all the specified mask bits set.

type GenericIO

type GenericIO struct {
	Handle        io.ReadWriteSeeker
	RelatedHandle io.ReadWriteSeeker
}

GenericIO implements the IO interface for generic io.ReadWriteSeeker. Handle is the main file handle, relatedHandle is the memo file handle.

func (GenericIO) Close

func (g GenericIO) Close(file *File) error

func (GenericIO) Create

func (g GenericIO) Create(file *File) error

func (GenericIO) Deleted

func (g GenericIO) Deleted(file *File) (bool, error)

func (GenericIO) GoTo

func (g GenericIO) GoTo(file *File, row uint32) error

func (GenericIO) OpenTable

func (g GenericIO) OpenTable(config *Config) (*File, error)

func (GenericIO) ReadColumns

func (g GenericIO) ReadColumns(file *File) ([]*Column, *Column, error)

func (GenericIO) ReadHeader

func (g GenericIO) ReadHeader(file *File) error

func (GenericIO) ReadMemo

func (g GenericIO) ReadMemo(file *File, address []byte, column *Column) ([]byte, bool, error)

func (GenericIO) ReadMemoHeader

func (g GenericIO) ReadMemoHeader(file *File) error

func (GenericIO) ReadNullFlag

func (g GenericIO) ReadNullFlag(file *File, position uint64, column *Column) (bool, bool, error)

func (GenericIO) ReadRow

func (g GenericIO) ReadRow(file *File, position uint32) ([]byte, error)

func (GenericIO) Search

func (g GenericIO) Search(file *File, field *Field, exactMatch bool) ([]*Row, error)

func (GenericIO) Skip

func (g GenericIO) Skip(file *File, offset int64)

func (GenericIO) WriteColumns

func (g GenericIO) WriteColumns(file *File) error

func (GenericIO) WriteHeader

func (g GenericIO) WriteHeader(file *File) error

func (GenericIO) WriteMemo

func (g GenericIO) WriteMemo(address []byte, file *File, raw []byte, text bool, length int) ([]byte, error)

func (GenericIO) WriteMemoHeader

func (g GenericIO) WriteMemoHeader(file *File, size int) error

func (GenericIO) WriteRow

func (g GenericIO) WriteRow(file *File, row *Row) error
type Header struct {
	FileType   byte     // File type flag
	Year       uint8    // Last update year (0-99)
	Month      uint8    // Last update month
	Day        uint8    // Last update day
	RowsCount  uint32   // Number of rows in file
	FirstRow   uint16   // Position of first data row
	RowLength  uint16   // Length of one data row, including delete flag
	Reserved   [16]byte // Reserved
	TableFlags byte     // Table flags
	CodePage   byte     // Code page mark
}

Containing DBF header information like dBase FileType, last change and rows count. https://docs.microsoft.com/en-us/previous-versions/visualstudio/foxpro/st4a0s68(v=vs.80)#table-header-record-structure

func (*Header) ColumnsCount

func (h *Header) ColumnsCount() uint16

ColumnsCount returns the calculated number of columns from the header information alone. This is the fastest way to determine the number of columns without reading the column definitions.

func (*Header) FileSize

func (h *Header) FileSize() int64

FileSize returns the calculated file size in bytes based on the header information.

func (*Header) Modified

func (h *Header) Modified(base int) time.Time

Modified parses the year, month and day stored in the header to time.Time. The year is stored in decades (2 digits) and added to the base century. If base is 0, the default base century 2000 is used (years 2000-2099).

func (*Header) RecordsCount

func (h *Header) RecordsCount() uint32

RecordsCount returns the number of records (rows) in the dBase table.

func (*Header) ValidateFileSize

func (h *Header) ValidateFileSize() error

ValidateFileSize checks if the calculated file size exceeds the maximum allowed size

type IO

type IO interface {
	OpenTable(config *Config) (*File, error)
	Close(file *File) error
	Create(file *File) error
	ReadHeader(file *File) error
	WriteHeader(file *File) error
	ReadColumns(file *File) ([]*Column, *Column, error)
	WriteColumns(file *File) error
	ReadMemoHeader(file *File) error
	WriteMemoHeader(file *File, size int) error
	ReadMemo(file *File, address []byte, column *Column) ([]byte, bool, error)
	WriteMemo(address []byte, file *File, raw []byte, text bool, length int) ([]byte, error)
	ReadNullFlag(file *File, position uint64, column *Column) (bool, bool, error)
	ReadRow(file *File, position uint32) ([]byte, error)
	WriteRow(file *File, row *Row) error
	Search(file *File, field *Field, exactMatch bool) ([]*Row, error)
	GoTo(file *File, row uint32) error
	Skip(file *File, offset int64)
	Deleted(file *File) (bool, error)
}

IO is the interface for working with dBase files. It provides methods for opening, reading, writing, and managing dBase database files and their memo files. Three implementations are available: - WindowsIO (for direct file access on Windows) - UnixIO (for direct file access on Unix-like systems) - GenericIO (for custom file access implementing io.ReadWriteSeeker)

type Marker

type Marker byte

Important byte markers for the dBase file

const (
	// Null represents a null byte marker (0x00)
	Null Marker = 0x00
	// Blank represents a space/blank marker (0x20)
	Blank Marker = 0x20
	// ColumnEnd represents the end of column definitions marker (0x0D)
	ColumnEnd Marker = 0x0D
	// Active represents an active (non-deleted) row marker, same as Blank
	Active Marker = Blank
	// Deleted represents a deleted row marker (0x2A)
	Deleted Marker = 0x2A
	// EOFMarker represents the end of file marker (0x1A)
	EOFMarker Marker = 0x1A
)

type MemoHeader

type MemoHeader struct {
	NextFree  uint32  // Location of next free block
	Unused    [2]byte // Unused
	BlockSize uint16  // Block size (bytes per block)
}

The raw header of the Memo file.

type Modification

type Modification struct {
	TrimSpaces  bool                                   // Trim spaces from string values
	Convert     func(interface{}) (interface{}, error) // Conversion function to convert the value
	ExternalKey string                                 // External key to use for the column
}

Modification allows to change the column name or value type of a column when reading the table The TrimSpaces option is only used for a specific column, if the general TrimSpaces option in the config is false.

type Row

type Row struct {
	Position   uint32 // Position of the row in the file
	ByteOffset int64  // Byte offset of the row in the file
	Deleted    bool   // Deleted flag
	// contains filtered or unexported fields
}

Row is a struct containing the row Position, deleted flag and data fields

func (*Row) Add

func (row *Row) Add() error

Appends the row as a new entry to the file

func (*Row) BoolValueByName

func (row *Row) BoolValueByName(name string) (bool, error)

Returns the value of a row at the given column name as a bool If the value is not a bool, an error is returned

func (*Row) BytesValueByName

func (row *Row) BytesValueByName(name string) ([]byte, error)

Returns the value of a row at the given column name as a []byte If the value is not a []byte, []uint8 or string, an error is returned

func (*Row) Field

func (row *Row) Field(pos int) *Field

Returns the field of a row by position or nil if not found

func (*Row) FieldByName

func (row *Row) FieldByName(name string) *Field

Returns the field of a row by name or nil if not found

func (*Row) Fields

func (row *Row) Fields() []*Field

Returns all fields of the current row

func (*Row) FloatValueByName

func (row *Row) FloatValueByName(name string) (float64, error)

Returns the value of a row at the given column name as a float64 If the value is not castable to a float64, an error is returned

func (*Row) Increment

func (row *Row) Increment() error

Increment increases set the value of the auto increment Column to the Next value Also increases the Next value by the amount of Step Rewrites the columns header

func (*Row) IntValueByName

func (row *Row) IntValueByName(name string) (int64, error)

IntValueByName returns the value of a row for the specified column name as an int64. The value will be cast to int64 if possible. Returns an error if the column is not found or the value cannot be cast to int64.

func (*Row) MustBoolValueByName

func (row *Row) MustBoolValueByName(name string) bool

Returns the value of a row at the given column name as a bool MustBoolValueByName panics if the value is not found or not a bool

func (*Row) MustBytesValueByName

func (row *Row) MustBytesValueByName(name string) []byte

Returns the value of a row at the given column name as a []byte MustBytesValueByName panics if the value is not found or not a []byte

func (*Row) MustFloatValueByName

func (row *Row) MustFloatValueByName(name string) float64

Returns the value of a row at the given column name as a float64 MustFloatValueByName panics if the value is not found or not a float64

func (*Row) MustIntValueByName

func (row *Row) MustIntValueByName(name string) int64

Returns the value of a row at the given column name as an int32 MustIntValueByName panics if the value is not found or not an int32

func (*Row) MustStringValueByName

func (row *Row) MustStringValueByName(name string) string

MustStringValueByName returns the value of a row for the specified column name as a string. Panics if the column is not found or the value is not a string or byte slice.

func (*Row) MustTimeValueByName

func (row *Row) MustTimeValueByName(name string) time.Time

Returns the value of a row at the given column name as a time.Time MustTimeValueByName panics if the value is not found or not a time.Time

func (*Row) MustValueByName

func (row *Row) MustValueByName(name string) interface{}

MustValueByName returns the value of a row for the column with the specified name. Panics if the column is not found.

func (*Row) StringValueByName

func (row *Row) StringValueByName(name string) (string, error)

StringValueByName returns the value of a row for the specified column name as a string. If the value is a byte slice, it is converted to a string. Returns an error if the column is not found or the value is not a string or byte slice.

func (*Row) TimeValueByName

func (row *Row) TimeValueByName(name string) (time.Time, error)

Returns the value of a row at the given column name as a time.Time If the value is not a time.Time, an error is returned

func (*Row) ToBytes

func (row *Row) ToBytes() ([]byte, error)

Converts the row back to raw dbase data

func (*Row) ToJSON

func (row *Row) ToJSON() ([]byte, error)

Returns a complete row as a JSON object.

func (*Row) ToMap

func (row *Row) ToMap() (map[string]interface{}, error)

Returns a complete row as a map.

func (*Row) ToStruct

func (row *Row) ToStruct(v interface{}) error

Converts a row to a struct. The struct must have the same field names as the columns in the table or the dbase tag must be set. dbase tags can be used to name the field. For example: `dbase:"<table_name>.<field_name>"` or `dbase:"<field_name>"`

func (*Row) Value

func (row *Row) Value(pos int) interface{}

Value returns the value of a row at the specified position. Panics if the position is out of bounds.

func (*Row) ValueByName

func (row *Row) ValueByName(name string) (interface{}, error)

ValueByName returns the value of a row for the column with the specified name. Returns an error if the column is not found.

func (*Row) Values

func (row *Row) Values() []interface{}

Values returns all values of a row as a slice of interface{}. Fields with nil values are included in the result.

func (*Row) Write

func (row *Row) Write() error

Writes the row to the file at the row pointer position

type Table

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

Table is a struct containing the table columns, modifications and the row pointer

type TableFlag

type TableFlag byte

Table flags indicate the type of the table https://learn.microsoft.com/en-us/previous-versions/visualstudio/foxpro/st4a0s68(v=vs.71)

const (
	// StructuralFlag indicates the table is a structural table (0x01)
	StructuralFlag TableFlag = 0x01
	// MemoFlag indicates the table has an associated memo file (0x02)
	MemoFlag TableFlag = 0x02
	// DatabaseFlag indicates the table is part of a database (0x04)
	DatabaseFlag TableFlag = 0x04
)

func (TableFlag) Defined

func (t TableFlag) Defined(flag byte) bool

Defined checks if the table flag is set to the given flag value.

type UnixIO

type UnixIO struct{}

UnixIO implements the IO interface for unix systems.

var DefaultIO UnixIO

DefaultIO is the default IO implementation for Unix-like systems.

func (UnixIO) Close

func (u UnixIO) Close(file *File) error

func (UnixIO) Create

func (u UnixIO) Create(file *File) error

func (UnixIO) Deleted

func (u UnixIO) Deleted(file *File) (bool, error)

func (UnixIO) GoTo

func (u UnixIO) GoTo(file *File, row uint32) error

func (UnixIO) OpenTable

func (u UnixIO) OpenTable(config *Config) (*File, error)

func (UnixIO) ReadColumns

func (u UnixIO) ReadColumns(file *File) ([]*Column, *Column, error)

func (UnixIO) ReadHeader

func (u UnixIO) ReadHeader(file *File) error

func (UnixIO) ReadMemo

func (u UnixIO) ReadMemo(file *File, blockdata []byte, column *Column) ([]byte, bool, error)

func (UnixIO) ReadMemoHeader

func (u UnixIO) ReadMemoHeader(file *File) error

func (UnixIO) ReadNullFlag

func (u UnixIO) ReadNullFlag(file *File, rowPosition uint64, column *Column) (bool, bool, error)

func (UnixIO) ReadRow

func (u UnixIO) ReadRow(file *File, position uint32) ([]byte, error)

func (UnixIO) Search

func (u UnixIO) Search(file *File, field *Field, exactMatch bool) ([]*Row, error)

func (UnixIO) Skip

func (u UnixIO) Skip(file *File, offset int64)

func (UnixIO) WriteColumns

func (u UnixIO) WriteColumns(file *File) error

func (UnixIO) WriteHeader

func (u UnixIO) WriteHeader(file *File) error

func (UnixIO) WriteMemo

func (u UnixIO) WriteMemo(address []byte, file *File, raw []byte, text bool, length int) ([]byte, error)

func (UnixIO) WriteMemoHeader

func (u UnixIO) WriteMemoHeader(file *File, size int) error

func (UnixIO) WriteRow

func (u UnixIO) WriteRow(file *File, row *Row) error

Jump to

Keyboard shortcuts

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