-
Notifications
You must be signed in to change notification settings - Fork 9
/
create.go
106 lines (92 loc) · 2.15 KB
/
create.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
package main
import (
"fmt"
"io"
"os"
"github.com/Valentin-Kaiser/go-dbase/dbase"
"golang.org/x/text/encoding/charmap"
)
type Test struct {
ID int32 `dbase:"ID"`
Name string `dbase:"NAME"`
Memo string `dbase:"MEMO"`
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))
// When creating a new table you need to define table type
// For more information about table types see the constants.go file
file, err := dbase.NewTable(
dbase.FoxProVar,
&dbase.Config{
Filename: "test.dbf",
Converter: dbase.NewDefaultConverter(charmap.Windows1250),
TrimSpaces: true,
},
columns(),
64,
nil,
)
if err != nil {
panic(err)
}
defer file.Close()
fmt.Printf(
"Last modified: %v Columns count: %v Record count: %v File size: %v \n",
file.Header().Modified(0),
file.Header().ColumnsCount(),
file.Header().RecordsCount(),
file.Header().FileSize(),
)
// Print all database column infos.
for _, column := range file.Columns() {
fmt.Printf("Name: %v - Type: %v \n", column.Name(), column.Type())
}
row, err := file.RowFromStruct(&Test{
ID: 1,
Name: "Test",
Memo: "Memo",
Var: "Var",
})
if err != nil {
panic(err)
}
err = row.Add()
if err != nil {
panic(err)
}
}
func columns() []*dbase.Column {
// Integer are allways 4 bytes long
idCol, err := dbase.NewColumn("ID", dbase.Integer, 0, 0, false)
if err != nil {
panic(err)
}
// Field name are always saved uppercase
nameCol, err := dbase.NewColumn("Name", dbase.Character, 20, 0, false)
if err != nil {
panic(err)
}
// Memo fields need no length the memo block size is defined as last parameter when calling New()
memoCol, err := dbase.NewColumn("Memo", dbase.Memo, 0, 0, false)
if err != nil {
panic(err)
}
// Some fields can be null this is defined by the last parameter
varCol, err := dbase.NewColumn("Var", dbase.Varchar, 64, 0, true)
if err != nil {
panic(err)
}
return []*dbase.Column{
idCol,
nameCol,
memoCol,
varCol,
}
}