Matrix/utils/database/databaseConnector.go
2020-09-30 10:28:49 +02:00

63 lines
1.1 KiB
Go

package database
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
func InitDB(filepath string) *sql.DB {
log.Printf("Init DB")
db, err := sql.Open("sqlite3", filepath)
if err != nil {
panic(err)
}
if db == nil {
panic("DB couldn't be initialized")
}
handleError(initDeviceTable(db))
handleError(initEventTable(db))
handleError(initRoomTable(db))
handleError(initTransactionTable(db))
handleError(initUserTable(db))
return db
}
func handleError(err error) {
if err != nil {
// TODO: Add Error Message
panic("Could not execute Database Query")
}
}
/*
sqlStmt := fmt.Sprintf(`INSERT INTO data
(id, content)
VALUES
(?, ?)`)
tx, err := db.Begin()
if err != nil {
log.Panic(err)
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return err
}
defer stmt.Close()
for i := 1; i < 10; i++ {
id := fmt.Sprintf("%d", i)
content := fmt.Sprintf("content #%d %s", i, shortuuid.New())
log.Printf("Inserting %s: %s", id, content)
_, err := stmt.Exec(id, content)
if err != nil {
log.Panic(err)
}
}
tx.Commit()
*/