dbf

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 11 Imported by: 0

README

dbf

Package dbf reads and writes DBF files.

Installation

You can incorporate the library into your local workspace with the following 'go get' command:

go get github.com/serg-volodeev/dbf

Using

Code needing to call into the library needs to include the following import statement:

import (
    "github.com/serg-volodeev/dbf"
)

Limitations

The following field types are supported:

  • Character
  • Numeric
  • Logical
  • Date

Memo fields are not supported. Index files are not supported.

Examples

Сreate a file and write one record.

f, err := os.Create("products.dbf")
if err != nil {
    log.Fatal(err)
}
defer f.Close()

fields := dbf.NewFields()
fields.AddCharacterField("NAME", 30)
fields.AddNumericField("COUNT", 8, 0)
fields.AddNumericField("PRICE", 12, 2)
fields.AddDateField("DATE")

w, err := dbf.NewWriter(f, fields, 1251)
if err != nil {
    log.Fatal(err)
}

w.SetStringFieldValue(0, "Apple")
w.SetIntFieldValue(1, 1200)
w.SetFloatFieldValue(2, 18.20)
w.SetDateFieldValue(3, time.Date(2021, 2, 12, 0, 0, 0, 0, time.UTC))

w.Write()

w.Flash()

if w.Err() != nil {
    log.Fatal(w.Err())
}

Read records.

f, err := os.Open("products.dbf")
if err != nil {
    log.Fatal(err)
}
defer f.Close()

r, err := dbf.NewReader(f)
if err != nil {
    log.Fatal(err)
}

for r.Read() {
    name := r.StringFieldValue(0)
    count := r.IntFieldValue(1)
    price := r.FloatFieldValue(2)
    date := r.DateFieldValue(3)
    
    fmt.Println(name, count, price, date)
}

if r.Err() != nil {
    log.Fatal(r.Err())
}

License

Copyright (C) Sergey Volodeev. Released under MIT license.

Documentation

Overview

Package dbf reads and writes DBF files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fields added in v0.2.0

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

Fields for creating file structure.

func NewFields added in v0.2.0

func NewFields() *Fields

NewFields returns a pointer to a structure Fields.

func (*Fields) AddCharacterField added in v0.3.0

func (f *Fields) AddCharacterField(name string, length int)

AddCharacterField adds a character field to the structure.

func (*Fields) AddDateField added in v0.3.0

func (f *Fields) AddDateField(name string)

AddDateField adds a date field to the structure.

func (*Fields) AddLogicalField added in v0.3.0

func (f *Fields) AddLogicalField(name string)

AddLogicalField adds a logical field to the structure.

func (*Fields) AddNumericField added in v0.3.0

func (f *Fields) AddNumericField(name string, length, dec int)

AddNumericField adds a numeric field to the structure.

func (*Fields) Count added in v0.2.0

func (f *Fields) Count() int

Count returns the number of fields.

func (*Fields) Err added in v0.4.0

func (f *Fields) Err() error

Err returns the first error that was encountered by the Fields.

func (*Fields) FieldInfo added in v0.3.0

func (f *Fields) FieldInfo(index int) (name, typ string, length, dec int)

FieldInfo returns field information by index.

type Reader

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

The Reader reads records from a DBF file.

func NewReader

func NewReader(rd io.Reader) (*Reader, error)

NewReader returns a new Reader that reads from r.

func (*Reader) BoolFieldValue added in v0.4.0

func (r *Reader) BoolFieldValue(index int) bool

BoolFieldValue returns the value of the field by index. Field type must be Logical.

func (*Reader) CodePage

func (r *Reader) CodePage() int

CodePage returns the code page set in the file header.

func (*Reader) DateFieldValue added in v0.4.0

func (r *Reader) DateFieldValue(index int) time.Time

DateFieldValue returns the value of the field by index. Field type must be Date.

func (*Reader) Deleted added in v0.4.1

func (r *Reader) Deleted() bool

Deleted returns deleted record flag.

func (*Reader) Err added in v0.4.0

func (r *Reader) Err() error

Err returns the first error that was encountered by the Reader.

func (*Reader) Fields

func (r *Reader) Fields() *Fields

Fields returns the file structure.

func (*Reader) FloatFieldValue added in v0.4.0

func (r *Reader) FloatFieldValue(index int) float64

FloatFieldValue returns the value of the field by index. Field type must be Numeric.

func (*Reader) IntFieldValue added in v0.4.0

func (r *Reader) IntFieldValue(index int) int64

IntFieldValue returns the value of the field by index. Field type must be Numeric. If field decimal places is not zero, then it returns the integer part of the number.

func (*Reader) ModDate added in v0.4.1

func (r *Reader) ModDate() time.Time

ModDate returns the modified date in the file header.

func (*Reader) Read

func (r *Reader) Read() bool

Read reads one record from r. Returns false if end of file is reached or an error occurs.

func (*Reader) RecordCount

func (r *Reader) RecordCount() uint32

RecordCount returns the number of records in the DBF file.

func (*Reader) SetCodePage

func (r *Reader) SetCodePage(cp int)

SetCodePage sets the code page if no code page is set in the file header.

Supported code pages:

437   - US MS-DOS
850   - International MS-DOS
1252  - Windows ANSI
10000 - Standard Macintosh
852   - Easern European MS-DOS
866   - Russian MS-DOS
865   - Nordic MS-DOS
1255  - Hebrew Windows
1256  - Arabic Windows
10007 - Russian Macintosh
1250  - Eastern European Windows
1251  - Russian Windows
1254  - Turkish Windows
1253  - Greek Windows

func (*Reader) StringFieldValue added in v0.4.0

func (r *Reader) StringFieldValue(index int) string

StringFieldValue returns the value of the field by index. Field type must be Character.

type Writer

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

A Writer writes records in DBF file. The writes of individual records are buffered. After all data has been written, the client should call the Flush method to guarantee all data has been forwarded to the underlying io.Writer.

func NewWriter

func NewWriter(ws io.WriteSeeker, fields *Fields, codePage int) (*Writer, error)

NewWriter returns a new Writer that writes to w. The function writes the header of the DBF file. If you call the Flash method afterwards, an empty file will be created.

Supported code pages:

437   - US MS-DOS
850   - International MS-DOS
1252  - Windows ANSI
10000 - Standard Macintosh
852   - Easern European MS-DOS
866   - Russian MS-DOS
865   - Nordic MS-DOS
1255  - Hebrew Windows
1256  - Arabic Windows
10007 - Russian Macintosh
1250  - Eastern European Windows
1251  - Russian Windows
1254  - Turkish Windows
1253  - Greek Windows

If the codePage parameter is zero, the text fields will not be encoded.

func (*Writer) Err added in v0.4.0

func (w *Writer) Err() error

Err returns the first error that was encountered by the Writer.

func (*Writer) Flush

func (w *Writer) Flush()

Flush writes any buffered data to the underlying io.Writer.

func (*Writer) SetBoolFieldValue added in v0.4.0

func (w *Writer) SetBoolFieldValue(index int, value bool)

SetBoolFieldValue assigns a value to a field by index. Field type must be Logical.

func (*Writer) SetDateFieldValue added in v0.4.0

func (w *Writer) SetDateFieldValue(index int, value time.Time)

SetDateFieldValue assigns a value to a field by index. Field type must be Date.

func (*Writer) SetDeteted added in v0.4.1

func (w *Writer) SetDeteted(deleted bool)

SetDeleted sets record mark is deleted.

func (*Writer) SetFloatFieldValue added in v0.4.0

func (w *Writer) SetFloatFieldValue(index int, value float64)

SetFloatFieldValue assigns a value to a field by index. Field type must be Numeric.

func (*Writer) SetIntFieldValue added in v0.4.0

func (w *Writer) SetIntFieldValue(index int, value int64)

SetIntFieldValue assigns a value to a field by index. Field type must be Numeric.

func (*Writer) SetStringFieldValue added in v0.4.0

func (w *Writer) SetStringFieldValue(index int, value string)

SetStringFieldValue assigns a value to a field by index. Field type must be Character.

func (*Writer) Write

func (w *Writer) Write()

Write writes a single record to w.

Jump to

Keyboard shortcuts

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