2020-09-30 08:28:49 +00:00
|
|
|
package database
|
2020-09-28 20:37:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-10-01 15:45:57 +00:00
|
|
|
"fmt"
|
2020-09-28 20:37:02 +00:00
|
|
|
"log"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
func InitDB(filepath string) (db *sql.DB, err error) {
|
2020-09-28 20:37:02 +00:00
|
|
|
log.Printf("Init DB")
|
2020-10-01 15:45:57 +00:00
|
|
|
db, err = sql.Open("sqlite3", filepath)
|
2020-09-28 20:37:02 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if db == nil {
|
2020-09-29 07:44:35 +00:00
|
|
|
panic("DB couldn't be initialized")
|
2020-09-28 20:37:02 +00:00
|
|
|
}
|
2020-09-29 07:44:35 +00:00
|
|
|
|
2020-09-30 08:28:49 +00:00
|
|
|
handleError(initDeviceTable(db))
|
2020-10-01 15:45:57 +00:00
|
|
|
handleError(initKeyTable(db))
|
2020-09-30 08:28:49 +00:00
|
|
|
handleError(initEventTable(db))
|
|
|
|
handleError(initRoomTable(db))
|
|
|
|
handleError(initTransactionTable(db))
|
|
|
|
handleError(initUserTable(db))
|
2020-09-29 07:44:35 +00:00
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
return
|
2020-09-28 20:37:02 +00:00
|
|
|
}
|
2020-09-29 07:44:35 +00:00
|
|
|
|
2020-09-30 08:28:49 +00:00
|
|
|
func handleError(err error) {
|
|
|
|
if err != nil {
|
2020-10-01 15:45:57 +00:00
|
|
|
panic(fmt.Sprintf("Could not execute Database Query: %s", err))
|
2020-09-30 08:28:49 +00:00
|
|
|
}
|
|
|
|
}
|