server/database.go: remove unnecessary function/code
This commit is contained in:
parent
a4bce8a07b
commit
4fda36f75e
@ -3,9 +3,7 @@ package main
|
||||
import (
|
||||
"asink"
|
||||
"database/sql"
|
||||
"errors"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func GetAndInitDB() (*sql.DB, error) {
|
||||
@ -38,11 +36,19 @@ func GetAndInitDB() (*sql.DB, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func DatabaseAddEvent(db *sql.DB, e *asink.Event) error {
|
||||
func DatabaseAddEvent(db *sql.DB, e *asink.Event) (err error) {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//make sure the transaction gets rolled back on error, and the database gets unlocked
|
||||
defer func() {
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
result, err := tx.Exec("INSERT INTO events (localid, type, status, path, hash, timestamp, permissions) VALUES (?,?,?,?,?,?,?);", e.LocalId, e.Type, e.Status, e.Path, e.Hash, e.Timestamp, e.Permissions)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -61,43 +67,8 @@ func DatabaseAddEvent(db *sql.DB, e *asink.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func DatabaseUpdateEvent(db *sql.DB, e *asink.Event) error {
|
||||
if !e.InDB {
|
||||
return errors.New("Attempting to update an event in the database which hasn't been previously added.")
|
||||
}
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := tx.Exec("UPDATE events SET id=?, type=?, status=?, path=?, hash=?, timestamp=?, permissions=? WHERE id=?;", e.Id, e.Type, e.Status, e.Path, e.Hash, e.Timestamp, e.Permissions, e.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rows != 1 {
|
||||
return errors.New("Updated " + strconv.Itoa(int(rows)) + " row(s) when intending to update 1 event row.")
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func DatabaseRetrieveEvents(db *sql.DB, firstId uint64, maxEvents uint) ([]*asink.Event, error) {
|
||||
var events []*asink.Event
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, err := tx.Query("SELECT id, localid, type, status, path, hash, timestamp, permissions FROM events WHERE id >= ? ORDER BY id ASC LIMIT ?;", firstId, maxEvents)
|
||||
func DatabaseRetrieveEvents(db *sql.DB, firstId uint64, maxEvents uint) (events []*asink.Event, err error) {
|
||||
rows, err := db.Query("SELECT id, localid, type, status, path, hash, timestamp, permissions FROM events WHERE id >= ? ORDER BY id ASC LIMIT ?;", firstId, maxEvents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -110,10 +81,5 @@ func DatabaseRetrieveEvents(db *sql.DB, firstId uint64, maxEvents uint) ([]*asin
|
||||
events = append(events, &event)
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user