Esempio n. 1
0
func (h Helper) CreateWithId(tx *sqlx.Tx, name string, args ...interface{}) (int64, error) {
	var id int64

	sql := h.SQL(name)
	if sql == "" {
		panic("No statement registered under " + name)
	}

	stmt, err := tx.Preparex(sql)
	if err != nil {
		return 0, err
	}
	defer stmt.Close()

	res, err := stmt.Exec(args...)
	if err != nil {
		return 0, err
	}

	id, err = res.LastInsertId()
	if err != nil {
		return 0, err
	}

	return id, nil
}
Esempio n. 2
0
func (db Database) UpdateAccountEmailConfirmed(tx *sqlx.Tx, account Account) error {
	stmt, err := tx.Preparex("UPDATE account SET email_confirmed = TRUE where id = $1")
	if err != nil {
		return err
	}
	_, err = stmt.Exec(account.Id)
	return err
}
Esempio n. 3
0
func (db Database) DeleteEmailConfirmation(tx *sqlx.Tx, account Account) error {
	stmt, err := tx.Preparex("DELETE FROM email_confirmation WHERE account_id=$1")
	if err != nil {
		return err
	}
	_, err = stmt.Exec(account.Id)
	return err
}
Esempio n. 4
0
func (db Database) DeleteAccountSession(tx *sqlx.Tx, key string) error {
	stmt, err := tx.Preparex("DELETE FROM account_session WHERE key=$1")
	if err != nil {
		return err
	}
	_, err = stmt.Exec(key)
	return err
}
Esempio n. 5
0
func (h Helper) CreateWithId(tx *sqlx.Tx, sql string, args ...interface{}) (int64, error) {
	var id int64

	sql += " RETURNING id"

	stmt, err := tx.Preparex(sql)
	if err != nil {
		return 0, err
	}
	defer stmt.Close()

	err = stmt.QueryRow(args...).Scan(&id)
	if err != nil {
		return 0, err
	}

	return id, nil
}
Esempio n. 6
0
func updateArticle(a content.Article, tx *sqlx.Tx, db *db.DB, logger webfw.Logger) {
	if a.HasErr() {
		return
	}

	if err := a.Validate(); err != nil {
		a.Err(err)
		return
	}

	logger.Infof("Updating article %s\n", a)

	d := a.Data()
	s := db.SQL()

	stmt, err := tx.Preparex(s.Article.Update)
	if err != nil {
		a.Err(err)
		return
	}
	defer stmt.Close()

	res, err := stmt.Exec(d.Title, d.Description, d.Date, d.Guid, d.Link, d.FeedId)
	if err != nil {
		a.Err(err)
		return
	}

	if num, err := res.RowsAffected(); err != nil && err == sql.ErrNoRows || num == 0 {
		logger.Infof("Creating article %s\n", a)

		aId, err := db.CreateWithId(tx, s.Article.Create, d.FeedId, d.Link, d.Guid,
			d.Title, d.Description, d.Date)

		if err != nil {
			a.Err(fmt.Errorf("Error updating article %s (guid - %v, link - %s): %v", a, d.Guid, d.Link, err))
			return
		}

		d.Id = data.ArticleId(aId)
		d.IsNew = true
		a.Data(d)
	}
}
Esempio n. 7
0
func (h Helper) CreateWithId(tx *sqlx.Tx, sql string, args ...interface{}) (int64, error) {
	var id int64

	stmt, err := tx.Preparex(sql)
	if err != nil {
		return 0, err
	}
	defer stmt.Close()

	res, err := stmt.Exec(args...)
	if err != nil {
		return 0, err
	}

	id, err = res.LastInsertId()
	if err != nil {
		return 0, err
	}

	return id, nil
}
Esempio n. 8
0
func (db DB) updateFeedArticles(tx *sqlx.Tx, f Feed, articles []Article) (bool, error) {
	if len(articles) == 0 {
		return false, nil
	}

	Debug.Println("Updating feed articles for " + f.Link)

	newArticles := []Article{}

	for _, a := range articles {
		if err := a.Validate(); err != nil {
			return false, err
		}

		stmt, err := tx.Preparex(db.NamedSQL("update_feed_article"))
		if err != nil {
			return false, err
		}
		defer stmt.Close()

		res, err := stmt.Exec(a.Title, a.Description, a.Link, a.Date, a.Id, f.Id)
		if err != nil {
			return false, err
		}

		if num, err := res.RowsAffected(); err != nil || num == 0 {
			newArticles = append(newArticles, a)
		}
	}

	if len(newArticles) == 0 {
		return false, nil
	}

	sql := `INSERT INTO articles(id, feed_id, title, description, link, date) `
	args := []interface{}{}
	index := 1

	for i, a := range newArticles {
		if err := a.Validate(); err != nil {
			return false, err
		}

		if i != 0 {
			sql += ` UNION `
		}

		sql += fmt.Sprintf(`SELECT $%d, $%d, $%d, $%d ,$%d, $%d EXCEPT SELECT id, feed_id, title, description, link, date FROM articles WHERE id = $%d AND feed_id = $%d`, index, index+1, index+2, index+3, index+4, index+5, index, index+1)
		args = append(args, a.Id, f.Id, a.Title, a.Description, a.Link, a.Date)
		index = len(args) + 1
	}

	stmt, err := tx.Preparex(sql)

	if err != nil {
		return false, err
	}
	defer stmt.Close()

	_, err = stmt.Exec(args...)
	if err != nil {
		return false, err
	}

	return true, nil
}