-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_to_database.go
248 lines (215 loc) · 7.15 KB
/
json_to_database.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"
"github.com/emculber/database_access/postgresql"
"../JTDB/postgresql"
)
type Configuration struct {
Database []database
}
type database struct {
Default string
Name string
Connection Connection
Users Users
Tables []Table
}
type Connection struct {
Host string
Port int
}
type Users struct {
DefaultUser User `json:"Default User"`
User []User
}
type User struct {
Username string
Password string
Role string
}
type Table struct {
Name string
Columns []Column
}
type Column struct {
Name string
Datatype string
Constraints []string
}
func GetDatabaseConnection(database_name string, user User, connection Connection) *sql.DB {
fmt.Println("Parsing Database connection information")
dbname := strings.Split(database_name, "{")
dbuser := strings.Split(user.Username, "{")
fmt.Println("Connecting to default database:",
"\nDatabase Name=", dbname[0],
"\nDatabase Host=", connection.Host,
"\nDatabase Port=", connection.Port,
"\nDatabase User=", dbuser[0],
"\nDatabase Password=", user.Password)
db := postgresql.ConnectToDatabase(
dbname[0],
connection.Host,
connection.Port,
dbuser[0],
user.Password,
)
return db
}
func CreateTableSQL(table_struct Table) string {
create_table := "CREATE TABLE " + table_struct.Name + " ("
for i, column_struct := range table_struct.Columns {
//fmt.Println(column_struct)
//TODO: if columns dont exits create column
create_table += column_struct.Name + " " + strings.ToLower(column_struct.Datatype)
for _, constrant := range column_struct.Constraints {
create_table += " " + strings.ToLower(constrant)
}
if i != len(table_struct.Columns)-1 {
create_table += ", "
}
}
create_table += ");"
return create_table
}
func JsonToDatabse(configuration Configuration) Configuration {
fmt.Println("Traversing database structure")
for i, database_struct := range configuration.Database {
fmt.Println("Getting database connection")
var db *sql.DB
fmt.Println("Checking if Default Database is set:", database_struct.Default)
if database_struct.Default != "" {
db = GetDatabaseConnection(database_struct.Default, database_struct.Users.DefaultUser, database_struct.Connection)
} else {
db = GetDatabaseConnection(database_struct.Name, database_struct.Users.DefaultUser, database_struct.Connection)
}
fmt.Println("Checking if Roles exist")
for u_index, user := range database_struct.Users.User {
user_name := strings.Split(user.Username, "{")
user_roles := strings.Replace(user.Role, " ", "", -1)
user_roles = strings.Replace(user_roles, ",", " ", -1)
fmt.Println("Checking if user exitst:", user_name[0])
user_exist, _ := postgresql.CheckIfRoleExists(db, user_name[0])
//TODO: Check if roles match
if !user_exist {
fmt.Println("User does not exits, Creating User")
postgresql.CreateUser(db, user_name[0], user.Password, user_roles)
}
if strings.Contains(user_name[1], "Default") {
fmt.Println("Starting Proccess of setting user to default")
defaut_user_name := strings.Split(database_struct.Users.DefaultUser.Username, "{")
if strings.Contains(defaut_user_name[1], "Remove") {
fmt.Println("Current default user is set to be removed")
configuration.Database[i].Users.DefaultUser.Username = user_name[0]
configuration.Database[i].Users.DefaultUser.Password = user.Password
configuration.Database[i].Users.DefaultUser.Role = user.Role
fmt.Println("New default",
"\nusername:", configuration.Database[i].Users.DefaultUser.Username,
"\nPassword:", configuration.Database[i].Users.DefaultUser.Password,
"\nRole:", configuration.Database[i].Users.DefaultUser.Role)
fmt.Println("Deleting Old User")
configuration.Database[i].Users.User = append(configuration.Database[i].Users.User[:u_index], configuration.Database[i].Users.User[u_index+1:]...)
} else {
fmt.Println("default user is not set to be removed no action will be taken")
}
}
}
if database_struct.Default != "" {
fmt.Println("Closing database connection for new connection")
db.Close()
fmt.Println("Openning new connection")
db = GetDatabaseConnection(configuration.Database[i].Default, configuration.Database[i].Users.DefaultUser, configuration.Database[i].Connection)
fmt.Println("Checking if Database:", database_struct.Name, "Exists")
exist, _ := postgresql.CheckIfDatabaseExists(db, database_struct.Name)
if !exist {
fmt.Println("Database does not exits, Creating database")
postgresql.CreateDatabase(db, database_struct.Name)
}
}
fmt.Println("Closing database connection for new connection")
db.Close()
fmt.Println("Openning new connection")
db = GetDatabaseConnection(configuration.Database[i].Name, configuration.Database[i].Users.DefaultUser, configuration.Database[i].Connection)
fmt.Println("Checking and creating tables")
for _, table_struct := range database_struct.Tables {
table_exist, _ := postgresql.CheckIfTableExists(db, table_struct.Name)
if !table_exist {
fmt.Println("Table does not exits, Creating User")
statement := CreateTableSQL(table_struct)
fmt.Println("Created statment to create table:", statement)
err := postgresql_access.CreateDatabaseTable(db, statement)
if err != nil {
fmt.Println(err)
}
}
}
}
return configuration
}
func main() {
path := "/home/erik/programming/golang/src/Scheduler"
fmt.Println("Running Json to Database")
fmt.Println("Loading Json Configuration")
configuration := testJson(path)
new_configuration := JsonToDatabse(testJson(path))
if !reflect.DeepEqual(configuration, new_configuration) {
fmt.Println("Configurations are not equal")
err := os.Mkdir(path+"/PastConfig", 0711)
if err != nil {
fmt.Println(err)
}
files, _ := ioutil.ReadDir(path + "/PastConfig")
f, err := os.Create(path + "/PastConfig/db.conf_v" + strconv.Itoa(len(files)) + ".json")
if err != nil {
fmt.Println(err)
}
defer f.Close()
configuration_json, err := json.Marshal(configuration)
if err != nil {
fmt.Println(err)
}
f.Write(configuration_json)
err = os.Remove(path + "/db.conf.json")
if err != nil {
fmt.Println(err)
}
conf, err := os.Create(path + "/db.conf.json")
if err != nil {
fmt.Println(err)
}
defer conf.Close()
new_configuration_json, err := json.Marshal(new_configuration)
if err != nil {
fmt.Println(err)
}
conf.Write(new_configuration_json)
} else {
fmt.Println("Configurations are equal")
}
}
func testJson(path string) Configuration {
fmt.Println("Loading test Json Configuration at path: ", path)
config_file, err := os.Open(path + "/db.conf.json")
if err != nil {
fmt.Println("Test Json Configuration failed to loaded successfully")
fmt.Println(err)
}
fmt.Println("Test Json Configuration was loaded successfully")
var configuration Configuration
fmt.Println("Decoding json")
decoder := json.NewDecoder(config_file)
err = decoder.Decode(&configuration)
if err != nil {
fmt.Println("Failed to Decoding json")
fmt.Println(err)
}
fmt.Println("Decoding json was sucessful")
//fmt.Println(configuration)
return configuration
}