-
Notifications
You must be signed in to change notification settings - Fork 7
/
init_dbs.go
83 lines (71 loc) · 1.81 KB
/
init_dbs.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
package main
import (
"fmt"
"log"
handler "mobile-attribution/handler"
"os"
)
// var Phones = []string{"{number}:{type}:{region}"}
// var Regions = []string{"{province}:{city}:{zip_code}:{area_code}"}
var Phones = []string{
"1300000:1:123",
"1300001:1:123",
}
const DBFILE = "./dbs/phones.go"
const TEMP = `package dbs
// var Phones = []string{"{number}:{type}:{region}"}
// var Regions = []string{"{province}:{city}:{zip_code}:{area_code}"}
`
// WriteHead2File write head to file
func WriteHead2File(f *os.File) {
_, err := f.WriteString(TEMP)
if err != nil {
log.Fatal("write temp error: %v", err)
}
}
// WriteNumbers2File write numbers to file
func WriteNumbers2File(f *os.File) {
fmt.Fprintln(f, "var Phones = []string{")
phones := handler.Phones{}
numbers, err := phones.Fetch()
if err != nil {
log.Fatal(err)
}
for numbers.Next() {
numbers.Scan(&phones.ID, &phones.Number, &phones.Type, &phones.RegionID)
line := fmt.Sprintf(`"%d:%d:%d",`, phones.Number, phones.Type, phones.RegionID)
fmt.Fprintln(f, line)
}
fmt.Fprintln(f, "}")
}
// WriteRegions2File write regions to file
func WriteRegions2File(f *os.File) {
fmt.Fprintln(f, "var Regions = []string{")
region := handler.Region{}
regions, err := region.Fetch()
if err != nil {
log.Fatal(err)
}
for regions.Next() {
regions.Scan(®ion.ID, ®ion.Province, ®ion.City, ®ion.ZipCode, ®ion.AreaCode)
line := fmt.Sprintf(`"%s:%s:%s:%s",`, region.Province, region.City, region.ZipCode, region.AreaCode)
fmt.Fprintln(f, line)
}
fmt.Fprintln(f, "}")
}
func InitDB() {
// open file
f, err := os.Create(DBFILE)
if err != nil {
panic(err)
}
// close fo on exit and check for its returned error
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
WriteHead2File(f)
WriteNumbers2File(f)
WriteRegions2File(f)
}