xbase

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2021 License: MIT Imports: 11 Imported by: 1

README

xbase

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. If you call SetPanic(true), then when an error occurs, the methods will cause a panic. Use whichever is more convenient for you.

Limitations

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

Examples

File creation.

// Create file
db := xbase.New()
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 {
    return db.Error()
}
defer db.CloseFile()

// 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 {
    return db.Error()
}

Reading file.

db := xbase.New()
db.SetPanic(true)

db.OpenFile("persons.dbf", true)
defer db.CloseFile()

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.

db := xbase.New()
db.SetPanic(true)

db.OpenFile("persons.dbf", true)
defer db.CloseFile()

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

// File structure
for n := 1; n <= db.FieldCount(); n++ {
    name, typ, length, dec := db.FieldInfo(n)
    fmt.Println(name, typ, length, dec)
}

License

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

Documentation

Overview

dbf package implements functions for working with DBF files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type XBase

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

func New

func New() *XBase

New creates a XBase object to work with a DBF file.

func (*XBase) Add

func (db *XBase) Add()

Add adds a new empty record. To save the changes, you need to call the Save method.

func (*XBase) AddField

func (db *XBase) AddField(name string, typ string, opts ...int)

AddField adds a field to the structure of the DBF file. This method can only be used before creating a new file.

The following field types are supported: "C", "N", "L", "D".

The opts parameter contains optional parameters: field length and number of decimal places.

Examples:

db.AddField("NAME", "C", 24)
db.AddField("COUNT", "N", 8)
db.AddField("PRICE", "N", 12, 2)
db.AddField("FLAG", "L")
db.AddField("DATE", "D")

func (*XBase) BOF

func (db *XBase) BOF() bool

BOF returns true if the beginning of the file is reached or error.

func (*XBase) Clear

func (db *XBase) Clear()

Clear zeroes the field values ​​of the current record.

func (*XBase) CloseFile

func (db *XBase) CloseFile()

CloseFile closes a previously opened or created DBF file.

func (*XBase) CodePage

func (db *XBase) CodePage() int

CodePage returns the code page of a DBF file. Returns 0 if no code page is specified.

func (*XBase) CreateFile

func (db *XBase) CreateFile(name string)

CreateFile creates a new file in DBF format. If a file with that name exists, it will be overwritten.

func (*XBase) Del

func (db *XBase) Del()

Del marks the current record as "deleted". The record is not physically deleted from the file and can be subsequently restored.

func (*XBase) EOF

func (db *XBase) EOF() bool

EOF returns true if end of file is reached or error.

func (*XBase) Error

func (db *XBase) Error() error

Error returns an error when working with a DBF file.

func (*XBase) FieldCount

func (db *XBase) FieldCount() int

FieldCount returns the number of fields in the DBF file.

func (*XBase) FieldInfo

func (db *XBase) FieldInfo(fieldNo int) (name, typ string, length, dec int)

FieldInfo returns field attributes by number. Fields are numbered starting from 1.

func (*XBase) FieldNo

func (db *XBase) FieldNo(name string) int

FieldNo returns the number of the field by name. If name is not found returns 0. Fields are numbered starting from 1.

func (*XBase) FieldValueAsBool

func (db *XBase) FieldValueAsBool(fieldNo int) bool

FieldValueAsBool returns the boolean value of the field of the current record. Field type must be logical ("L"). Fields are numbered starting from 1.

func (*XBase) FieldValueAsDate

func (db *XBase) FieldValueAsDate(fieldNo int) time.Time

FieldValueAsDate returns the date value of the field of the current record. Field type must be date ("D"). Fields are numbered starting from 1.

func (*XBase) FieldValueAsFloat

func (db *XBase) FieldValueAsFloat(fieldNo int) float64

FieldValueAsFloat returns the float value of the field of the current record. Field type must be numeric ("N"). Fields are numbered starting from 1.

func (*XBase) FieldValueAsInt

func (db *XBase) FieldValueAsInt(fieldNo int) int64

FieldValueAsInt returns the integer value of the field of the current record. Field type must be numeric ("N"). Fields are numbered starting from 1.

func (*XBase) FieldValueAsString

func (db *XBase) FieldValueAsString(fieldNo int) string

FieldValueAsString returns the string value of the field of the current record. Fields are numbered starting from 1.

func (*XBase) First

func (db *XBase) First()

First positions the object to the first record.

func (*XBase) GoTo

func (db *XBase) GoTo(recNo int64)

GoTo allows you to go to a record by its ordinal number. Numbering starts from 1.

func (*XBase) IsPanic

func (db *XBase) IsPanic() bool

IsPanic returns true if panic mode is sedb.

func (*XBase) Last

func (db *XBase) Last()

Last positions the object to the last record.

func (*XBase) ModDate

func (db *XBase) ModDate() time.Time

ModDate returns the modification date of the DBF file.

func (*XBase) Next

func (db *XBase) Next()

Next positions the object to the next record.

func (*XBase) OpenFile

func (db *XBase) OpenFile(name string, readOnly bool)

OpenFile opens an existing DBF file.

func (*XBase) Prev

func (db *XBase) Prev()

Prev positions the object to the previous record.

func (*XBase) RecCount

func (db *XBase) RecCount() int64

RecCount returns the number of records in the DBF file.

func (*XBase) RecDeleted

func (db *XBase) RecDeleted() bool

RecDeleted returns the value of the delete flag for the current record.

func (*XBase) RecNo

func (db *XBase) RecNo() int64

RecNo returns the sequence number of the current record. Numbering starts from 1.

func (*XBase) Recall

func (db *XBase) Recall()

Recall removes the deletion mark from the current record.

func (*XBase) Save

func (db *XBase) Save()

Save writes changes to the file. Before calling it, all changes to the object were made only in memory and will be lost when you move to another record or close the file.

func (*XBase) SetCodePage

func (db *XBase) SetCodePage(cp int)

SetCodePage sets the encoding mode for reading and writing string field values. The default code page is 0.

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 (*XBase) SetFieldValue

func (db *XBase) SetFieldValue(fieldNo int, value interface{})

SetFieldValue sets the field value of the current record. The value must match the field type. To save the changes, you need to call the Save method.

func (*XBase) SetPanic

func (db *XBase) SetPanic(flag bool)

SetPanic sets panic mode on errors. By default, the object does not panic.

Jump to

Keyboard shortcuts

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