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
- Variables
- func Debug(enabled bool, out io.Writer)
- func RegisterCustomEncoding(codePageMark byte, encoding encoding.Encoding)
- func ValidateFileVersion(version byte, untested bool) error
- type Column
- type ColumnFlag
- type Config
- type DataType
- type Database
- type DefaultConverter
- type EncodingConverter
- type Error
- type Field
- type File
- func (file *File) BOF() bool
- func (file *File) BytesToRow(data []byte) (*Row, error)
- func (file *File) Close() error
- func (file *File) Column(pos int) *Column
- func (file *File) ColumnNames() []string
- func (file *File) ColumnPos(column *Column) int
- func (file *File) ColumnPosByName(colname string) int
- func (file *File) Columns() []*Column
- func (file *File) ColumnsCount() uint16
- func (file *File) Create() error
- func (file *File) Deleted() (bool, error)
- func (file *File) EOF() bool
- func (file *File) GetColumnModification(position int) *Modification
- func (file *File) GetHandle() (interface{}, interface{})
- func (file *File) GetIO() IO
- func (file *File) GoTo(row uint32) error
- func (file *File) Header() *Header
- func (file *File) Init() error
- func (file *File) Interpret(raw []byte, column *Column) (interface{}, error)
- func (file *File) NewField(pos int, value interface{}) (*Field, error)
- func (file *File) NewFieldByName(name string, value interface{}) (*Field, error)
- func (file *File) NewRow() *Row
- func (file *File) Next() (*Row, error)
- func (file *File) Pointer() uint32
- func (file *File) ReadColumns() ([]*Column, *Column, error)
- func (file *File) ReadHeader() error
- func (file *File) ReadMemo(address []byte, column *Column) ([]byte, bool, error)
- func (file *File) ReadMemoHeader() error
- func (file *File) ReadNullFlag(position uint64, column *Column) (bool, bool, error)
- func (file *File) ReadRow(position uint32) ([]byte, error)
- func (file *File) Represent(field *Field, padding bool) ([]byte, error)
- func (file *File) Row() (*Row, error)
- func (file *File) RowFromJSON(j []byte) (*Row, error)
- func (file *File) RowFromMap(m map[string]interface{}) (*Row, error)
- func (file *File) RowFromStruct(v interface{}) (*Row, error)
- func (file *File) Rows(skipInvalid bool, skipDeleted bool) ([]*Row, error)
- func (file *File) RowsCount() uint32
- func (file *File) Search(field *Field, exactMatch bool) ([]*Row, error)
- func (file *File) SetColumnModification(position int, mod *Modification)
- func (file *File) SetColumnModificationByName(name string, mod *Modification) error
- func (file *File) Skip(offset int64)
- func (file *File) TableName() string
- func (file *File) WriteColumns() error
- func (file *File) WriteHeader() error
- func (file *File) WriteMemo(address []byte, data []byte, text bool, length int) ([]byte, error)
- func (file *File) WriteMemoHeader(size int) error
- func (file *File) WriteRow(row *Row) error
- type FileExtension
- type FileVersion
- type Flag
- type GenericIO
- func (g GenericIO) Close(file *File) error
- func (g GenericIO) Create(file *File) error
- func (g GenericIO) Deleted(file *File) (bool, error)
- func (g GenericIO) GoTo(file *File, row uint32) error
- func (g GenericIO) OpenTable(config *Config) (*File, error)
- func (g GenericIO) ReadColumns(file *File) ([]*Column, *Column, error)
- func (g GenericIO) ReadHeader(file *File) error
- func (g GenericIO) ReadMemo(file *File, address []byte, column *Column) ([]byte, bool, error)
- func (g GenericIO) ReadMemoHeader(file *File) error
- func (g GenericIO) ReadNullFlag(file *File, position uint64, column *Column) (bool, bool, error)
- func (g GenericIO) ReadRow(file *File, position uint32) ([]byte, error)
- func (g GenericIO) Search(file *File, field *Field, exactMatch bool) ([]*Row, error)
- func (g GenericIO) Skip(file *File, offset int64)
- func (g GenericIO) WriteColumns(file *File) error
- func (g GenericIO) WriteHeader(file *File) error
- func (g GenericIO) WriteMemo(address []byte, file *File, raw []byte, text bool, length int) ([]byte, error)
- func (g GenericIO) WriteMemoHeader(file *File, size int) error
- func (g GenericIO) WriteRow(file *File, row *Row) error
- type Header
- type IO
- type Marker
- type MemoHeader
- type Modification
- type Row
- func (row *Row) Add() error
- func (row *Row) BoolValueByName(name string) (bool, error)
- func (row *Row) BytesValueByName(name string) ([]byte, error)
- func (row *Row) Field(pos int) *Field
- func (row *Row) FieldByName(name string) *Field
- func (row *Row) Fields() []*Field
- func (row *Row) FloatValueByName(name string) (float64, error)
- func (row *Row) Increment() error
- func (row *Row) IntValueByName(name string) (int64, error)
- func (row *Row) MustBoolValueByName(name string) bool
- func (row *Row) MustBytesValueByName(name string) []byte
- func (row *Row) MustFloatValueByName(name string) float64
- func (row *Row) MustIntValueByName(name string) int64
- func (row *Row) MustStringValueByName(name string) string
- func (row *Row) MustTimeValueByName(name string) time.Time
- func (row *Row) MustValueByName(name string) interface{}
- func (row *Row) StringValueByName(name string) (string, error)
- func (row *Row) TimeValueByName(name string) (time.Time, error)
- func (row *Row) ToBytes() ([]byte, error)
- func (row *Row) ToJSON() ([]byte, error)
- func (row *Row) ToMap() (map[string]interface{}, error)
- func (row *Row) ToStruct(v interface{}) error
- func (row *Row) Value(pos int) interface{}
- func (row *Row) ValueByName(name string) (interface{}, error)
- func (row *Row) Values() []interface{}
- func (row *Row) Write() error
- type Table
- type TableFlag
- type UnixIO
- func (u UnixIO) Close(file *File) error
- func (u UnixIO) Create(file *File) error
- func (u UnixIO) Deleted(file *File) (bool, error)
- func (u UnixIO) GoTo(file *File, row uint32) error
- func (u UnixIO) OpenTable(config *Config) (*File, error)
- func (u UnixIO) ReadColumns(file *File) ([]*Column, *Column, error)
- func (u UnixIO) ReadHeader(file *File) error
- func (u UnixIO) ReadMemo(file *File, blockdata []byte, column *Column) ([]byte, bool, error)
- func (u UnixIO) ReadMemoHeader(file *File) error
- func (u UnixIO) ReadNullFlag(file *File, rowPosition uint64, column *Column) (bool, bool, error)
- func (u UnixIO) ReadRow(file *File, position uint32) ([]byte, error)
- func (u UnixIO) Search(file *File, field *Field, exactMatch bool) ([]*Row, error)
- func (u UnixIO) Skip(file *File, offset int64)
- func (u UnixIO) WriteColumns(file *File) error
- func (u UnixIO) WriteHeader(file *File) error
- func (u UnixIO) WriteMemo(address []byte, file *File, raw []byte, text bool, length int) ([]byte, error)
- func (u UnixIO) WriteMemoHeader(file *File, size int) error
- func (u UnixIO) WriteRow(file *File, row *Row) error
Constants ¶
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 ¶
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 ¶
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 ¶
RegisterCustomEncoding registers a custom encoding for a specific code page mark. This allows support for additional encodings beyond the built-in ones.
func ValidateFileVersion ¶
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 ¶
Name returns the name of the column as a trimmed string (maximum length 10). Null bytes are trimmed from the end of the name.
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) )
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
func OpenDatabase ¶
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
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
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
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field is a row data field
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 ¶
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) BytesToRow ¶
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 ¶
Close closes all file handlers for the dBase file and its associated memo file.
func (*File) Column ¶
Column returns the column at the specified position, or nil if the position is invalid.
func (*File) ColumnNames ¶
ColumnNames returns a slice containing all column names in the dBase table.
func (*File) ColumnPos ¶
ColumnPos returns the position of the specified column, or -1 if not found.
func (*File) ColumnPosByName ¶
ColumnPosByName returns the position of a column by name, or -1 if not found.
func (*File) ColumnsCount ¶
ColumnsCount returns the number of columns in the dBase table.
func (*File) Deleted ¶
Deleted returns true if the row at the current internal row pointer position is marked as deleted.
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) GoTo ¶
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) Interpret ¶
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 ¶
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 ¶
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 ¶
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) ReadColumns ¶
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 ¶
ReadHeader reads the dBase file header from the file handle.
func (*File) ReadMemo ¶
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 ¶
ReadMemoHeader reads the memo file header from the given file handle.
func (*File) ReadNullFlag ¶
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) Represent ¶
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) RowFromJSON ¶
Converts a JSON-encoded row into the row representation
func (*File) RowFromMap ¶
Converts a map of interfaces into the row representation
func (*File) RowFromStruct ¶
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 ¶
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) Search ¶
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 ¶
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) WriteColumns ¶
WriteColumns writes the column definitions to the end of the header in the dBase file.
func (*File) WriteHeader ¶
WriteHeader writes the header to the dBase file.
func (*File) WriteMemo ¶
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 ¶
WriteMemoHeader writes the memo header to the memo file. The size parameter specifies the number of blocks the new memo data will occupy.
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.
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) ReadColumns ¶
func (GenericIO) ReadHeader ¶
func (GenericIO) ReadMemoHeader ¶
func (GenericIO) ReadNullFlag ¶
func (GenericIO) WriteColumns ¶
func (GenericIO) WriteHeader ¶
type Header ¶
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 ¶
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 ¶
FileSize returns the calculated file size in bytes based on the header information.
func (*Header) Modified ¶
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 ¶
RecordsCount returns the number of records (rows) in the dBase table.
func (*Header) ValidateFileSize ¶
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) BoolValueByName ¶
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 ¶
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) FieldByName ¶
Returns the field of a row by name or nil if not found
func (*Row) FloatValueByName ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
MustValueByName returns the value of a row for the column with the specified name. Panics if the column is not found.
func (*Row) StringValueByName ¶
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 ¶
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) ToStruct ¶
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 ¶
Value returns the value of a row at the specified position. Panics if the position is out of bounds.
func (*Row) ValueByName ¶
ValueByName returns the value of a row for the column with the specified name. Returns an error if the column is not found.
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)
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.