-
Notifications
You must be signed in to change notification settings - Fork 12
/
normative_document_type.go
94 lines (77 loc) · 2.38 KB
/
normative_document_type.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
package normative_document_type
import "encoding/xml"
// const dateformat = "2006-01-02"
// const tableName = "as_ndoctype"
// const elementName = "NormativeDocumentType"
// Статус центра
type XmlObject struct {
XMLName xml.Name `xml:"NormativeDocumentType" db:"as_ndoctype"`
NDTYPEID int `xml:"NDTYPEID,attr" db:"nd_type_id"`
NAME string `xml:"NAME,attr" db:"name"`
}
func Schema(tableName string) string {
return `CREATE TABLE ` + tableName + ` (
nd_type_id INT UNIQUE NOT NULL,
name VARCHAR(100) NOT NULL,
PRIMARY KEY (nd_type_id));`
}
/*
func Export(w *sync.WaitGroup, c chan string, db *sqlx.DB, format *string) {
defer w.Done()
helpers.DropAndCreateTable(schema, tableName, db)
var format2 string
format2 = *format
fileName, err2 := helpers.SearchFile(tableName, format2)
if err2 != nil {
fmt.Println("Error searching file:", err2)
return
}
pathToFile := format2 + "/" + fileName
// Подсчитываем, сколько элементов нужно обработать
//fmt.Println("Подсчет строк")
// _, err := helpers.CountElementsInXML(pathToFile, elementName)
// if err != nil {
// fmt.Println("Error counting elements in XML file:", err)
// return
// }
//fmt.Println("\nВ ", elementName, " содержится ", countedElements, " строк")
xmlFile, err := os.Open(pathToFile)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer xmlFile.Close()
decoder := xml.NewDecoder(xmlFile)
total := 0
var inElement string
for {
// Read tokens from the XML document in a stream.
t, _ := decoder.Token()
if t == nil {
break
}
// Inspect the type of the token just read.
switch se := t.(type) {
case xml.StartElement:
// If we just read a StartElement token
inElement = se.Name.Local
if inElement == elementName {
total++
var item XmlObject
// decode a whole chunk of following XML into the
// variable item which is a ActualStatus (se above)
err = decoder.DecodeElement(&item, &se)
if err != nil {
fmt.Println("Error in decode element:", err)
return
}
query := "INSERT INTO " + tableName + " (nd_type_id, name) VALUES ($1, $2)"
db.MustExec(query, item.NDTYPEID, item.NAME)
c <- helpers.PrintRowsAffected(elementName, total)
}
default:
}
}
//fmt.Printf("\nTotal processed items in "+elementName+": %d \n", total)
}
*/