forked from Valentin-Kaiser/go-dbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
128 lines (106 loc) · 3.21 KB
/
read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"io"
"os"
"time"
"github.com/Valentin-Kaiser/go-dbase/dbase"
)
type Product struct {
ID int32 `dbase:"PRODUCTID"`
Name string `dbase:"PRODNAME"`
Price float64 `dbase:"PRICE"`
Double float64 `dbase:"DOUBLE"`
Date time.Time `dbase:"DATE"`
DateTime time.Time `dbase:"DATETIME"`
Integer int32 `dbase:"INTEGER"`
Float float64 `dbase:"FLOAT"`
Active bool `dbase:"ACTIVE"`
Description string `dbase:"DESC"`
Tax float64 `dbase:"TAX"`
Stock int64 `dbase:"INSTOCK"`
Blob []byte `dbase:"BLOB"`
Varbinary []byte `dbase:"VARBIN_NIL"`
Varchar string `dbase:"VAR_NIL"`
Var string `dbase:"VAR"`
}
func main() {
// Open debug log file so we see what's going on
f, err := os.OpenFile("debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
return
}
dbase.Debug(true, io.MultiWriter(os.Stdout, f))
// Open the example database table.
table, err := dbase.OpenTable(&dbase.Config{
Filename: "../test_data/table/TEST.DBF",
TrimSpaces: true,
})
if err != nil {
panic(dbase.GetErrorTrace(err))
}
defer table.Close()
fmt.Printf(
"Last modified: %v Columns count: %v Record count: %v File size: %v \n",
table.Header().Modified(),
table.Header().ColumnsCount(),
table.Header().RecordsCount(),
table.Header().FileSize(),
)
// Print all database column infos.
for _, column := range table.Columns() {
fmt.Printf("Name: %v - Type: %v \n", column.Name(), column.Type())
}
// Loop through all rows using rowPointer in DBF struct.
for !table.EOF() {
row, err := table.Row()
if err != nil {
panic(dbase.GetErrorTrace(err))
}
// Increment the row pointer.
table.Skip(1)
// Skip deleted rows.
if row.Deleted {
fmt.Printf("Deleted row at position: %v \n", row.Position)
continue
}
// Get the first field by column position
field := row.Field(0)
if field == nil {
panic("Field not found")
}
// Print the field value.
fmt.Printf("Field: %v [%v] => %v \n", field.Name(), field.Type(), field.GetValue())
// Get value by column name
field = row.FieldByName("PRODNAME")
if field == nil {
panic("Field not found")
}
// Print the field value.
fmt.Printf("Field: %v [%v] => %v \n", field.Name(), field.Type(), field.GetValue())
// === Modifications ===
// Disable space trimming for the company name
err = table.SetColumnModificationByName("PRODNAME", &dbase.Modification{TrimSpaces: false})
if err != nil {
panic(dbase.GetErrorTrace(err))
}
// Add a column modification to switch the names of "INTEGER" and "Float" to match the data types
err = table.SetColumnModificationByName("INTEGER", &dbase.Modification{TrimSpaces: true, ExternalKey: "FLOAT"})
if err != nil {
panic(dbase.GetErrorTrace(err))
}
err = table.SetColumnModificationByName("FLOAT", &dbase.Modification{TrimSpaces: true, ExternalKey: "INTEGER"})
if err != nil {
panic(dbase.GetErrorTrace(err))
}
// === Struct Conversion ===
// Read the row into a struct.
p := &Product{}
err = row.ToStruct(p)
if err != nil {
panic(dbase.GetErrorTrace(err))
}
fmt.Printf("Product: %+v \n", p)
}
}