summaryrefslogtreecommitdiff
path: root/crawler/database.go
diff options
context:
space:
mode:
authorhorus2018-02-20 01:45:46 +0100
committerhorus2018-02-20 01:45:46 +0100
commit7d1a61095f06b0a81b0f73c2ba0717fc8d9479df (patch)
tree37e2ff40f7053e3b81c94c24971a0e94e786f069 /crawler/database.go
parentf9557de3000ef36c97ba838bc926f0b12db59ba5 (diff)
downloadalkobote-7d1a61095f06b0a81b0f73c2ba0717fc8d9479df.tar.gz
Bugfix. (crawler)
Diffstat (limited to 'crawler/database.go')
-rw-r--r--crawler/database.go14
1 files changed, 8 insertions, 6 deletions
diff --git a/crawler/database.go b/crawler/database.go
index e11c80b..e47f723 100644
--- a/crawler/database.go
+++ b/crawler/database.go
@@ -4,6 +4,8 @@ import (
"database/sql"
"fmt"
"strings"
+
+ "github.com/jmoiron/sqlx"
)
func (app *App) createTables() error {
@@ -91,11 +93,11 @@ func (app *App) createTables() error {
/**
* Saves scrapped offers in database. Detects which offers are new. Runs inside tx.
*/
-func (app *App) save_offer(W []Angebot) error {
+func (app *App) save_offer(Tx *sqlx.Tx, 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 := app.Tx.Prepare(query)
+ stmt, err := Tx.Prepare(query)
if err != nil {
Warn(err, "Save Offer: Preparing query failed")
return err
@@ -112,7 +114,7 @@ func (app *App) save_offer(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 := app.Tx.QueryRow(detect_duplicate_query, o.Name).Scan(&found)
+ err := Tx.QueryRow(detect_duplicate_query, o.Name).Scan(&found)
if err == sql.ErrNoRows {
@@ -144,12 +146,12 @@ func (app *App) save_offer(W []Angebot) error {
/**
* Invalidates expired offers by updating 'valid_until' column. Detects which offers are expired. Runs inside Tx.
*/
-func (app *App) remove_expired(W []Angebot, shop Shop) error {
+func (app *App) remove_expired(Tx *sqlx.Tx, 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 := app.Tx.Queryx(query, shop.Id, app.Now, app.Now)
+ rows, err := Tx.Queryx(query, shop.Id, app.Now, app.Now)
if err != nil {
Warn(err, "Remove expired: Query failed")
return err
@@ -168,7 +170,7 @@ func (app *App) remove_expired(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 = app.Tx.Exec(expire_query, app.Now, offer_db.Id)
+ _, err = Tx.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)