package main import ( "context" "database/sql" "log" sq "github.com/Masterminds/squirrel" _ "github.com/mattn/go-sqlite3" ) type Database interface { CreatePreRace(*Race) error DeletePreRace(int) error UpdatePreRace(int) error GetPreRaceByID(int) (*Race, error) GetPreRaces() ([]*Race, error) } type SQLiteDatabase struct { Db *sql.DB } func NewSQLiteDatabase(cfg Config) (*SQLiteDatabase, error) { db, err := sql.Open("sqlite3", cfg.DBName) if err != nil { return nil, err } log.Println("Opening SQLite database") _, err = db.ExecContext( context.Background(), `CREATE TABLE IF NOT EXISTS "races" ( "id" INTEGER NOT NULL UNIQUE, "torrentname" TEXT NOT NULL, "category" TEXT, "indexer" TEXT, "type" TEXT, "title" TEXT, "season" TEXT, "episode" TEXT, "year" TEXT, "resolution" TEXT, "source" TEXT, "torrentURL" TEXT, "won" INTEGER, "prerace" INTEGER, PRIMARY KEY("ID" AUTOINCREMENT) )`, ) if err != nil { log.Panicln("Cannot create table", err) } return &SQLiteDatabase{ Db: db, }, nil } func (s *SQLiteDatabase) CreatePreRace(r *Race) error { if r.Type == "movie" || r.Type == "episode" { queryBuilder := sq. Insert("races"). Columns("torrentname", "category", "indexer", "type", "title", "season", "episode", "year", "resolution", "source", "torrenturl", "won", "prerace"). Values(r.TorrentName, r.Category, r.Indexer, r.Type, r.Title, r.Season, r.Episode, r.Year, r.Resolution, r.Source, r.TorrentURL, r.Won, r.PreRace). Suffix("RETURNING id").RunWith(s.Db) err := queryBuilder.QueryRow().Scan(&r.ID) if err != nil { return err } log.Printf("Prerace *** %s *** from %s added to the database", r.TorrentName, r.Indexer) } return nil } func (s *SQLiteDatabase) DeletePreRace(*Race) error { return nil } func (s *SQLiteDatabase) UpdatePreRace(*Race) error { return nil } func (s *SQLiteDatabase) GetPreRaceByID(*Race) error { return nil } func (s *SQLiteDatabase) GetPreRaces(*Race) error { return nil }