In Datenbank schreiben und nach Namen suchen implementiert

This commit is contained in:
Jason Streifling 2023-10-06 19:07:56 +02:00
parent 5749739761
commit f570950425
3 changed files with 51 additions and 10 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
create_tables.sql

12
main.go
View File

@ -9,6 +9,12 @@ import (
"streifling.com/jason/sicherheitsunterweisung/packages/types"
)
func writeBriefing(ch chan *types.Briefing, db *db.DB) {
for b := range ch {
db.WriteBriefing(b)
}
}
func main() {
var i, j int64
@ -26,11 +32,7 @@ func main() {
mux.HandleFunc("/add-participant/", server.AddParticipant(&i))
mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j))
go func() {
for b := range cb {
db.WriteBriefing(b)
}
}()
go writeBriefing(cb, db)
log.Fatalln(http.ListenAndServe(":8080", mux))
}

View File

@ -38,7 +38,7 @@ func getCredentials() (string, string, error) {
}
func OpenDB(dbName string) (*DB, error) {
var db *DB
var db DB
var err error
cfg := mysql.NewConfig()
@ -57,16 +57,54 @@ func OpenDB(dbName string) (*DB, error) {
return nil, fmt.Errorf("newDB: db.Ping(): %v\n", err)
}
return db, nil
return &db, nil
}
func (db *DB) WriteBriefing(b *types.Briefing) error {
for i := 1; i < len(b.Participants); i++ {
_, err := db.Exec("INSERT INTO ? (instructor_first, instructor_last, date, time, state, location, participant_first, participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", db.Name, b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, b.Participants[i].FirstName, b.Participants[i].LastName, b.Participants[i].Company)
for i := 0; i < len(b.Participants); i++ {
result, err := db.Exec("INSERT INTO "+db.Name+" (instructor_first,"+
"instructor_last, date, time, state, location, participant_first,"+
"participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location,
b.Participants[i].FirstName, b.Participants[i].LastName,
b.Participants[i].Company)
if err != nil {
return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO ? (instructor_first, instructor_last, date, time, state, location, participant_first, participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)\", db.Name, b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, b.Participants[i].FirstName, b.Participants[i].LastName, b.Participants[i].Company): %v\n", err)
return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO"+
"\"+db.Name+\" (instructor_first, instructor_last, date, time, state,"+
"location, participant_first, participant_last, company) VALUES (?, ?,"+
"?, ?, ?, ?, ?, ?, ?)\", b.FirstName, b.LastName, b.Date, b.Time,"+
"b.State, b.Location, b.Participants[i].FirstName,"+
"b.Participants[i].LastName, b.Participants[i].Company): %v\n", err)
}
_, err = result.LastInsertId()
if err != nil {
return fmt.Errorf("*DB.WriteBriefing: result.LastInsertId(): %v\n", err)
}
}
return nil
}
func (db *DB) ReadByName(name string) (*[]types.Briefing, error) {
var b types.Briefing
var bs []types.Briefing
rows, err := db.Query("SELECT * FROM " + db.Name + " WHERE" +
" instructor_first LIKE " + name + " OR instructor_last LIKE " + name +
" participant_first LIKE " + name + " participant_last LIKE " + name)
if err != nil {
return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT * FROM \"+db.Name+\" WHERE" +
" instructor_first LIKE \"+name+\" OR instructor_last LIKE \"+name+" +
" participant_first LIKE \"+name+\" participant_last LIKE \"+name)")
}
for rows.Next() {
if err := rows.Scan(&b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &b.Participants[0].FirstName, &b.Participants[0].LastName, &b.Participants[0].Company); err != nil {
return nil, fmt.Errorf("*DB.ReadByName: rows.Scan(&b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &b.Participants[0].FirstName, &b.Participants[0].LastName, &b.Participants[0].Company)")
}
bs = append(bs, b)
}
return &bs, nil
}