111 lines
3.4 KiB
Go
Raw Normal View History

2023-10-05 18:08:44 +02:00
package db
import (
"bufio"
"database/sql"
"fmt"
"os"
"strings"
"syscall"
"streifling.com/jason/sicherheitsunterweisung/packages/types"
"github.com/go-sql-driver/mysql"
"golang.org/x/term"
)
type DB struct {
*sql.DB
Name string
}
func getCredentials() (string, string, error) {
fmt.Printf("DB Benutzer: ")
user, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", "", fmt.Errorf("getCredentials: bufio.NewReader(os.Stdin).ReadString('\n'): %v", err)
}
fmt.Printf("DB Passwort: ")
bytePass, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", "", fmt.Errorf("getCredentials: term.ReadPassword(int(syscall.Stdin)): %v", err)
}
fmt.Println()
pass := string(bytePass)
return strings.TrimSpace(user), strings.TrimSpace(pass), nil
}
func OpenDB(dbName string) (*DB, error) {
var db DB
var err error
cfg := mysql.NewConfig()
cfg.DBName = dbName
cfg.User, cfg.Passwd, err = getCredentials()
if err != nil {
return nil, fmt.Errorf("OpenDB: getCredentials(): %v\n", err)
}
db.Name = dbName
db.DB, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
return nil, fmt.Errorf("newDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err)
}
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("newDB: db.Ping(): %v\n", err)
}
return &db, nil
}
func (db *DB) WriteBriefing(b *types.Briefing) error {
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"+
"\"+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
}