-
Notifications
You must be signed in to change notification settings - Fork 11
/
write.go
140 lines (121 loc) · 3.16 KB
/
write.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
129
130
131
132
133
134
135
136
137
138
139
140
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,
WriteLock: true,
})
if err != nil {
panic(err)
}
defer table.Close()
fmt.Printf(
"Last modified: %v Columns count: %v Record count: %v File size: %v \n",
table.Header().Modified(0),
table.Header().ColumnsCount(),
table.Header().RecordsCount(),
table.Header().FileSize(),
)
// Read the first row (rowPointer start at the first row).
row, err := table.Row()
if err != nil {
panic(err)
}
// Get the company name field by column name.
err = row.FieldByName("PRODNAME").SetValue("CHANGED_PRODUCT_NAME")
if err != nil {
panic(err)
}
// Change a memo field value.
err = row.FieldByName("DESC").SetValue("MEMO_TEST_VALUE")
if err != nil {
panic(err)
}
// Write the changed row to the database table.
err = row.Write()
if err != nil {
panic(err)
}
// === Modifications ===
// 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(err)
}
err = table.SetColumnModificationByName("FLOAT", &dbase.Modification{TrimSpaces: true, ExternalKey: "INTEGER"})
if err != nil {
panic(err)
}
// Create a new row with the same structure as the database table.
p := Product{
ID: 99,
Name: "NEW_PRODUCT",
Price: 99.99,
Tax: 19.99,
Stock: 999,
Date: time.Now(),
DateTime: time.Now(),
Description: "NEW_PRODUCT_DESCRIPTION",
Active: true,
Float: 105.67,
Integer: 104,
Double: 103.45,
Varchar: "VARCHAR",
}
row, err = table.RowFromStruct(p)
if err != nil {
panic(err)
}
// Add the new row to the database table.
err = row.Write()
if err != nil {
panic(err)
}
// Print all rows.
for !table.EOF() {
row, err := table.Next()
if err != nil {
panic(err)
}
// Skip deleted rows.
if row.Deleted {
fmt.Printf("Deleted row at position: %v \n", row.Position)
continue
}
// Print the current row values.
fmt.Println(row.Values()...)
}
}