package table import ( "fmt" "strings" "github.com/jmoiron/sqlx" "git.amok.space/yevhen/resource-scraper/types/constant" "git.amok.space/yevhen/resource-scraper/types/model" ) type ExternalSources struct { Columns []string } func (f *ExternalSources) InsertOnDuplicate(es model.ExternalSources, db *sqlx.DB) model.ExternalSources { stmt := "INSERT INTO %s (%s) VALUES (%s) ON DUPLICATE KEY UPDATE title=:title, created=:created RETURNING id" placeholders := strings.Join(f.Columns, ", :") placeholders = ":" + strings.Replace(placeholders, "`", "", -1) query := fmt.Sprintf(stmt, constant.ExternalSourcesTable, strings.Join(f.Columns, ", "), placeholders) //fmt.Printf("%s\n", query) if rows, err := db.NamedQuery(query, &es); err == nil { for rows.Next() { es.Error = rows.StructScan(&es) } } else { es.Error = err } return es }