diff options
Diffstat (limited to 'crawler/database.go')
| -rw-r--r-- | crawler/database.go | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/crawler/database.go b/crawler/database.go index e47f723..6e08b78 100644 --- a/crawler/database.go +++ b/crawler/database.go @@ -4,8 +4,6 @@ import ( "database/sql" "fmt" "strings" - - "github.com/jmoiron/sqlx" ) func (app *App) createTables() error { @@ -91,13 +89,13 @@ func (app *App) createTables() error { } /** - * Saves scrapped offers in database. Detects which offers are new. Runs inside tx. + * Saves scrapped offers in database. Detects which offers are new. */ -func (app *App) save_offer(Tx *sqlx.Tx, W []Angebot) error { +func (app *App) save_offer(W []Angebot) error { query := `INSERT INTO angebot (shop, name, url, abv, volume, age, original_price, discounted_price, base_price, valid_until, image_url, website_raw, spirit_type, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` - stmt, err := Tx.Prepare(query) + stmt, err := app.DB.Prepare(query) if err != nil { Warn(err, "Save Offer: Preparing query failed") return err @@ -114,7 +112,7 @@ func (app *App) save_offer(Tx *sqlx.Tx, W []Angebot) error { // resembles UNIQUE constraint detect_duplicate_query := fmt.Sprintf(`SELECT 1 FROM _intern_view WHERE name = ? AND shop_id = %d AND volume = %4.2f AND abv = %4.2f AND original_price = %d AND discounted_price = %d AND valid_until = %d`, o.Shop, o.Volume, o.Abv, o.Original_price, o.Discounted_price, o.Valid_until) - err := Tx.QueryRow(detect_duplicate_query, o.Name).Scan(&found) + err := app.DB.QueryRow(detect_duplicate_query, o.Name).Scan(&found) if err == sql.ErrNoRows { @@ -144,14 +142,14 @@ func (app *App) save_offer(Tx *sqlx.Tx, W []Angebot) error { } /** - * Invalidates expired offers by updating 'valid_until' column. Detects which offers are expired. Runs inside Tx. + * Invalidates expired offers by updating 'valid_until' column. Detects which offers are expired. */ -func (app *App) remove_expired(Tx *sqlx.Tx, W []Angebot, shop Shop) error { +func (app *App) remove_expired(W []Angebot, shop Shop) error { query := `SELECT id, name, shop, volume, abv, original_price, discounted_price FROM angebot WHERE shop = ? AND created_at < ? AND (valid_until = 0 OR valid_until > ?)` - rows, err := Tx.Queryx(query, shop.Id, app.Now, app.Now) + rows, err := app.DB.Queryx(query, shop.Id, app.Now, app.Now) if err != nil { Warn(err, "Remove expired: Query failed") return err @@ -170,7 +168,7 @@ func (app *App) remove_expired(Tx *sqlx.Tx, W []Angebot, shop Shop) error { if !app.offer_contains(W, offer_db) { DebugOffer(offer_db, "Contains not - Set to expire") expire_query := `UPDATE angebot SET valid_until = ? WHERE id = ?` - _, err = Tx.Exec(expire_query, app.Now, offer_db.Id) + _, err = app.DB.Exec(expire_query, app.Now, offer_db.Id) if err != nil { offer_db.error_msg = err.Error() offer_db.error_ctx = fmt.Sprintf("UPDATE angebot SET valid_until = %d WHERE id = %d", app.Now, offer_db.Id) |
