Пример #1
0
func (d *Postgres) getLogTags(tx *sql.Tx, log *models.Log) error {
	var (
		tag  string
		tags []string
	)

	query, args, dest := sqlbuilder.Select().
		Dialect(sqlbuilder.Postgres).
		From(`"log_tag"`).
		Map(`"tag"`, &tag).
		Where(`"log_id" = ?`, log.ID).
		Build()

	rows, err := tx.Query(query, args...)
	if err != nil {
		return err
	}
	for rows.Next() {
		if err := rows.Scan(dest...); err != nil {
			return err
		}
		tags = append(tags, tag)
	}
	if err := rows.Err(); err != nil {
		return err
	}
	log.Tags = tags
	return nil
}
Пример #2
0
func (d *Postgres) getLogTracks(tx *sql.Tx, log *models.Log) error {
	var (
		track  models.Track
		tracks []*models.Track
	)

	query, args, dest := sqlbuilder.Select().
		Dialect(sqlbuilder.Postgres).
		From(`"track"`).
		Map(`"id"`, &track.ID).
		Map(`"log_id"`, &track.LogID).
		Map(`COALESCE("name", '')`, &track.Name).
		Map(`"start"`, &track.Start).
		Map(`"end"`, &track.End).
		Map(`"duration"`, &track.Duration).
		Map(`"distance"`, &track.Distance).
		Where(`"log_id" = ?`, log.ID).
		Build()

	rows, err := tx.Query(query, args...)
	if err != nil {
		return err
	}
	for rows.Next() {
		if err := rows.Scan(dest...); err != nil {
			return err
		}
		t := new(models.Track)
		*t = track
		tracks = append(tracks, t)
	}
	if err := rows.Err(); err != nil {
		return err
	}

	for _, track := range tracks {
		if err := d.getTrackPoints(tx, track); err != nil {
			return err
		}
	}

	log.Tracks = tracks
	return nil
}