-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriteData.go
165 lines (133 loc) · 5.01 KB
/
writeData.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
var insertIntoStudents = `INSERT INTO student(national_code, student_no, full_name_fa, full_name_en, father_name, birth_date, mobile, major) VALUES `
var insertIntoProfessors = `INSERT INTO professor(national_code, professor_no, full_name_fa, full_name_en, father_name, birth_date, mobile, department, title) VALUES `
var insertIntoCourses = `INSERT INTO course(course_id, course_name, professor_no) VALUES `
var insertIntoCourseTakes = `INSERT INTO course_takes(student_no, course_id) VALUES `
func readJsonData() map[string]interface{} {
// Open our jsonFile
pwd, _ := os.Getwd()
fmt.Println(pwd + "/data/data.json")
jsonFile, err := os.Open(pwd + "/data/data.json")
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened data.json", jsonFile)
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
// https://stackoverflow.com/questions/31398044/got-error-invalid-character-%C3%AF-looking-for-beginning-of-value-from-json-unmar
byteValue = bytes.TrimPrefix(byteValue, []byte("\xef\xbb\xbf"))
var result map[string]interface{}
err = json.Unmarshal([]byte(byteValue), &result)
if err != nil {
fmt.Println(err)
}
return result
}
func writeStudentsData(data interface{}, db *sql.DB) {
students := data.([]interface{})
vals := []interface{}{}
for _, s := range students {
student := s.(map[string]interface{})
national_code := student["national_code"].(string)
student_no := student["student_no"].(string)
name_fa := student["name_fa"].(string)
name_en := student["name_en"].(string)
father_name := student["father_name"].(string)
birth_date := student["birth_date"].(string)
mobile := student["mobile"].(string)
major := student["major"].(string)
//INSERT INTO student(national_code, student_no, full_name_fa, full_name_en, father_name, birth_date, mobile, major) `
insertIntoStudents += " (?, ?, ?, ?, ?, ?, ?, ?),"
vals = append(vals, national_code, student_no, name_fa, name_en, father_name, birth_date, mobile, major)
}
// trim last colon
insertIntoStudents = insertIntoStudents[0 : len(insertIntoStudents)-1]
stmt, _ := db.Prepare(insertIntoStudents)
res, err := stmt.Exec(vals...)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("result after inserting student", res)
}
func writeProfessorsData(data interface{}, db *sql.DB) {
professors := data.([]interface{})
vals := []interface{}{}
for _, p := range professors {
professor := p.(map[string]interface{})
national_code := professor["national_code"].(string)
professor_no := professor["professor_no"].(string)
name_fa := professor["name_fa"].(string)
name_en := professor["name_en"].(string)
father_name := professor["father_name"].(string)
birth_date := professor["birth_date"].(string)
mobile := professor["mobile"].(string)
department := professor["department"].(string)
title := professor["title"].(string)
insertIntoProfessors += " (?, ?, ?, ?, ?, ?, ?, ?, ?),"
vals = append(vals, national_code, professor_no, name_fa, name_en, father_name, birth_date, mobile, department, title)
}
// trim last colon
insertIntoProfessors = insertIntoProfessors[0 : len(insertIntoProfessors)-1]
stmt, _ := db.Prepare(insertIntoProfessors)
res, err := stmt.Exec(vals...)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("result after inserting professors", res)
}
func writeCoursesData(data interface{}, db *sql.DB) {
courses := data.([]interface{})
vals := []interface{}{}
for _, c := range courses {
course := c.(map[string]interface{})
course_id := course["id"].(string)
course_name := course["name"].(string)
professor_no := course["professor_no"].(string)
insertIntoCourses += " (?, ?, ?),"
vals = append(vals, course_id, course_name, professor_no)
}
// trim last colon
insertIntoCourses = insertIntoCourses[0 : len(insertIntoCourses)-1]
stmt, _ := db.Prepare(insertIntoCourses)
res, err := stmt.Exec(vals...)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("result after inserting courses", res)
}
func writeCourseTakesData(data interface{}, db *sql.DB) {
takes := data.([]interface{})
vals := []interface{}{}
for _, t := range takes {
studentTakes := t.(map[string]interface{})
student_no := studentTakes["student_no"].(string)
course_id := studentTakes["course_id"].(string)
insertIntoCourseTakes += " (?, ?),"
vals = append(vals, student_no, course_id)
}
// trim last colon
insertIntoCourseTakes = insertIntoCourseTakes[0 : len(insertIntoCourseTakes)-1]
stmt, _ := db.Prepare(insertIntoCourseTakes)
res, err := stmt.Exec(vals...)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("result after inserting course_takes", res)
}
func WriteDataToDatabsae(db *sql.DB) {
data := readJsonData()
// fmt.Println(data["students"])
writeStudentsData(data["students"], db)
writeProfessorsData(data["faculty"], db)
writeCoursesData(data["courses"], db)
writeCourseTakesData(data["classrooms"], db)
}