Skip to content

tsingsun/xbase

Repository files navigation

xbase

Language codecov Go Report Card Build Status Release GoDoc

forked from svolodeev/xbase in the beginning,and both the project and author seems no action, so I forked it and refract.

Package xbase is DBF util written in GO,provides fast,idiomatic free mapping between DBF and GO values

A pure Go library for working with DBF files. The main purpose of the library is to organize export-import of data from files in DBF format.

The XBase type is used to work with DBF files. In addition to working with existing files, the XBase object allows you to create a new file of the given structure. Each XBase object can be linked with only one file.

Writing changes to a file

The XBase object contains data for one current record. Changing field values does not cause an immediate change to the file. Changes are saved when the Save() method is called.

Deleting records

Deleting a record does not physically destroy it on disk. The deletion mark is put in a special field of the record.

Error processing

If an error occurs when calling the method, use the Error() method to get its value. By default, methods don't panic. This behavior can be changed.

Serialization and Deserialization

easy to serialize and deserialize according to the code page of the dbf file.

Limitations

The following field types are supported: C, N, L, D. Memo fields are not supported. Index files are not supported.

Examples

File creation.

package main

import (
	"fmt"
	"github.com/tsingsun/xbase"
	"time"
)

func main() {
	// Create file
	db,err := xbase.New()
    if err != nil {
        fmt.Println(err)
        return
    }
	db.AddField("NAME", "C", 30)
	db.AddField("SALARY", "N", 9, 2)
	db.AddField("BDATE", "D")
	db.SetCodePage(1251)
	db.CreateFile("persons.dbf")
	if db.Error() != nil {
		fmt.Println(db.Error())
		return
	}
	defer db.Close()

	// Add record
	db.Add()
	db.SetFieldValue(1, "John Smith")
	db.SetFieldValue(2, 1234.56)
	db.SetFieldValue(3, time.Date(1998, 2, 20, 0, 0, 0, 0, time.UTC))
	db.Save()
	if db.Error() != nil {
		fmt.Println(db.Error())
		return
	}
}

Reading file.

package main

import (
	"fmt"
    "github.com/tsingsun/xbase"
)

func main() {
	// Open the DBF file
	db, err := xbase.Open("persons.dbf", true)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Close the file when done
	defer db.Close()

	// Read the records
	db.First()
	for !db.EOF() {
		name := db.FieldValueAsString(1)
		salary := db.FieldValueAsFloat(2)
		bDate := db.FieldValueAsDate(3)
		fmt.Println(name, salary, bDate)
		db.Next()
	}
}

File information.

package main

import (
	"fmt"
	"github.com/tsingsun/xbase"
)

func main() {
	db,err := xbase.Open("persons.dbf", true)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer db.Close()

	fmt.Println("Record count:", db.RecCount())
	fmt.Println("Field count:", db.FieldCount())
	fmt.Println("Code page:", db.CodePage())

	// File structure
	for _, f := range db.DecodedFields() {
		fmt.Println(f.Name, f.Type, f.Len, f.Dec)
	}
}

Append Record

package main

import (
	"fmt"
	"github.com/tsingsun/xbase"
	"time"
)

func main() {
	db,err := xbase.Open("persons.dbf", false)
	if err != nil {
        fmt.Println(err)
        return
    }
	defer db.Close()

	db.Add()
	db.SetFieldValue(1, "John Smith")
	db.SetFieldValue(2, 1234.56)
	db.SetFieldValue(3, time.Date(1998, 2, 20, 0, 0, 0, 0, time.UTC))
	db.Save()
	if db.Error() != nil {
		fmt.Println("Code page:",db.Error())
	}
}

Serialization and Deserialization

package main

import (
	"fmt"
	"github.com/tsingsun/xbase"
	"time"
)

type Person struct {
	Name   string  `dbf:"name,len=30"`
	Salary float64 `dbf:"salary,len=9,dec=2"`
	BDate  time.Time `dbf:"bdate"`
}

func main() {
	db, err := xbase.Open("persons.dbf", false)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer db.Close()

	p := Person{
		Name:   "John Smith", 
		Salary: 1234.56,
		BDate:  time.Date(1998, 2, 20, 0, 0, 0, 0, time.UTC),
	}
	// append record by struct
	err = db.Append(p)
	if err != nil {
		fmt.Println(err)
	}

	// Deserialize to struct
	var p2 Person
	err = db.DecodeRecord(&p2)
	if err != nil {
		fmt.Println(err)
	}
}