2019-07-17 05:28:37 +00:00
|
|
|
package browsers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Database sql wrapper for operations with browser objects.
|
|
|
|
type Database struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes database.
|
|
|
|
func (db Database) Close() error {
|
|
|
|
return db.db.Close()
|
|
|
|
}
|
|
|
|
|
2019-07-25 05:35:09 +00:00
|
|
|
func NewDB(db *sql.DB) *Database {
|
|
|
|
return &Database{db: db}
|
2019-07-17 05:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Browser struct {
|
|
|
|
ID string `json:"browser-id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Timestamp uint64 `json:"timestamp"`
|
|
|
|
Dapp bool `json:"dapp?"`
|
2019-07-27 05:48:23 +00:00
|
|
|
HistoryIndex int `json:"history-index"`
|
2019-07-17 05:28:37 +00:00
|
|
|
History []string `json:"history,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) InsertBrowser(browser Browser) (err error) {
|
2020-05-14 10:51:32 +00:00
|
|
|
tx, err := db.db.Begin()
|
2019-07-17 05:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
2020-05-14 10:51:32 +00:00
|
|
|
|
|
|
|
bInsert, err := tx.Prepare("INSERT OR REPLACE INTO browsers(id, name, timestamp, dapp, historyIndex) VALUES(?, ?, ?, ?, ?)")
|
2019-07-17 05:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
_, err = bInsert.Exec(browser.ID, browser.Name, browser.Timestamp, browser.Dapp, browser.HistoryIndex)
|
|
|
|
bInsert.Close()
|
2019-07-17 05:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
|
2019-07-17 05:28:37 +00:00
|
|
|
if len(browser.History) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
bhInsert, err := tx.Prepare("INSERT INTO browsers_history(browser_id, history) VALUES(?, ?)")
|
2019-07-17 05:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
defer bhInsert.Close()
|
2019-07-17 05:28:37 +00:00
|
|
|
for _, history := range browser.History {
|
2020-05-14 10:51:32 +00:00
|
|
|
_, err = bhInsert.Exec(browser.ID, history)
|
2019-07-17 05:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
|
2019-07-17 05:28:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetBrowsers() (rst []*Browser, err error) {
|
2020-05-14 10:51:32 +00:00
|
|
|
tx, err := db.db.Begin()
|
2019-07-17 05:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
2020-05-14 10:51:32 +00:00
|
|
|
|
2019-07-17 05:28:37 +00:00
|
|
|
// FULL and RIGHT joins are not supported
|
2020-05-14 10:51:32 +00:00
|
|
|
bRows, err := tx.Query("SELECT id, name, timestamp, dapp, historyIndex FROM browsers ORDER BY timestamp DESC")
|
2019-07-17 05:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
defer bRows.Close()
|
2019-07-17 05:28:37 +00:00
|
|
|
browsers := map[string]*Browser{}
|
2020-05-14 10:51:32 +00:00
|
|
|
for bRows.Next() {
|
2019-07-17 05:28:37 +00:00
|
|
|
browser := Browser{}
|
2020-05-14 10:51:32 +00:00
|
|
|
err = bRows.Scan(&browser.ID, &browser.Name, &browser.Timestamp, &browser.Dapp, &browser.HistoryIndex)
|
2019-07-17 05:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
browsers[browser.ID] = &browser
|
|
|
|
rst = append(rst, &browser)
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
|
|
|
|
bhRows, err := tx.Query("SELECT browser_id, history from browsers_history")
|
2019-07-17 05:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
defer bhRows.Close()
|
2019-07-17 05:28:37 +00:00
|
|
|
var (
|
|
|
|
id string
|
|
|
|
history string
|
|
|
|
)
|
2020-05-14 10:51:32 +00:00
|
|
|
for bhRows.Next() {
|
|
|
|
err = bhRows.Scan(&id, &history)
|
2019-07-17 05:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
browsers[id].History = append(browsers[id].History, history)
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
|
2019-07-17 05:28:37 +00:00
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) DeleteBrowser(id string) error {
|
|
|
|
_, err := db.db.Exec("DELETE from browsers WHERE id = ?", id)
|
|
|
|
return err
|
|
|
|
}
|