forked from SchumacherFM/OnixParser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
104 lines (88 loc) · 3.42 KB
/
main.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
//go:generate godep save -r ./...
/*
Copyright (C) 2014 Cyrill AT Schumacher dot fm
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contribute @ https://github.com/SchumacherFM/OnixParser
*/
package main
import (
"fmt"
"net/url"
"os"
"runtime"
"time"
_ "github.com/SchumacherFM/OnixParser/Godeps/_workspace/src/github.com/go-sql-driver/mysql"
"github.com/SchumacherFM/OnixParser/gonfig"
"github.com/SchumacherFM/OnixParser/onixml"
"github.com/SchumacherFM/OnixParser/sqlCreator"
)
var appConfig = gonfig.NewAppConfiguration()
func initDatabase() {
// Open doesn't open a connection. Validate DSN data:
err := appConfig.GetConnection().Ping()
appConfig.HandleErr(err)
// delete already created tables
// escape dbdb due to SQL injection
columnName := sqlCreator.QuoteInto("Tables_in_" + *appConfig.DbDb)
showQuery := "SHOW TABLES FROM " + sqlCreator.QuoteInto(*appConfig.DbDb) + " WHERE " + columnName + " LIKE '" + url.QueryEscape(*appConfig.TablePrefix) + "%'"
rows, err := appConfig.GetConnection().Query(showQuery)
appConfig.HandleErr(err)
defer rows.Close()
rowCount := 0
for rows.Next() { // just for learning purpose otherwise we can directly drop tables here
var tableName string
err = rows.Scan(&tableName)
appConfig.HandleErr(err)
_, err := appConfig.GetConnection().Exec("DROP TABLE " + sqlCreator.QuoteInto(tableName))
appConfig.HandleErr(err)
rowCount++
}
appConfig.Log("Dropped %d existing tables", rowCount)
// check for SELECT @@max_allowed_packet for reading LOAD DATA INFILE LOCAL
rows2, err2 := appConfig.GetConnection().Query("SELECT @@max_allowed_packet")
appConfig.HandleErr(err2)
defer rows2.Close()
if !rows2.Next() {
rows2.Close()
panic("crashed")
}
err = rows2.Scan(&appConfig.MaxPacketSize)
appConfig.MaxPacketSize = appConfig.MaxPacketSize - 40960*4 // just in case remove 160kb
appConfig.HandleErr(err)
}
func main() {
timeStart := time.Now()
if "" == os.Getenv("GOMAXPROCS") {
runtime.GOMAXPROCS(runtime.NumCPU())
}
appConfig.Init()
appConfig.NoExitCode = appConfig.NoExitCode == false
if appConfig.NoExitCode {
fmt.Println("OnixParser Copyright (C) 2014 Cyrill AT Schumacher dot fm")
fmt.Println("This program comes with ABSOLUTELY NO WARRANTY; License: http://www.gnu.org/copyleft/gpl.html")
}
initDatabase()
onixml.SetAppConfig(appConfig)
total, totalErr := onixml.OnixmlDecode()
appConfig.Log("Total products: %d \n", total)
appConfig.Log("Total errors: %d \n", totalErr)
onixml.ImportCsvIntoMysql()
appConfig.GetConnection().Close()
appConfig.CloseOutputFiles()
printDuration(timeStart)
}
func printDuration(timeStart time.Time) {
timeEnd := time.Now()
duration := timeEnd.Sub(timeStart)
appConfig.Log("XML Parser took %dh %dm %fs to run.\n", int(duration.Hours()), int(duration.Minutes()), duration.Seconds())
appConfig.Log("XML Parser took %v to run.\n", duration)
}