<commit_before>package db

import (
	"github.com/go-errors/errors"
	"github.com/jmoiron/sqlx"
	sq "github.com/lann/squirrel"
	"github.com/stellar/horizon/log"
	"golang.org/x/net/context"
)

// SqlQuery helps facilitate queries against a postgresql database. See Select and Get for
// the main methods used by collaborators.
type SqlQuery struct {
	DB *sqlx.DB
}

// Select selects multiple rows returned by the provided sql builder into the provided dest.
// dest must be a slice of the correct record type.
func (q SqlQuery) Select(ctx context.Context, sql sq.SelectBuilder, dest interface{}) error {
	sql = sql.PlaceholderFormat(sq.Dollar)
	query, args, err := sql.ToSql()

	if err != nil {
		return errors.Wrap(err, 1)
	}

	return q.SelectRaw(ctx, query, args, dest)
}

// SelectRaw runs the provided postgres query and args against this sqlquery's db.
func (q SqlQuery) SelectRaw(ctx context.Context, query string, args []interface{}, dest interface{}) error {
	log.WithField(ctx, "sql", query).Info("query sql")
	log.WithField(ctx, "args", args).Debug("query args")

	err := q.DB.Select(dest, query, args...)
	if err != nil {
		err = errors.Wrap(err, 1)
	}
	return err
}

// Get gets a single row returned by the provided sql builder into the provided dest.
// dest must be a non-slice value of the correct record type.
func (q SqlQuery) Get(ctx context.Context, sql sq.SelectBuilder, dest interface{}) error {
	sql = sql.PlaceholderFormat(sq.Dollar)
	query, args, err := sql.ToSql()

	if err != nil {
		return errors.Wrap(err, 1)
	}

	return q.GetRaw(ctx, query, args, dest)
}

// GetRaw runs the provided postgres query and args against this sqlquery's db.
func (q SqlQuery) GetRaw(ctx context.Context, query string, args []interface{}, dest interface{}) error {
	log.WithField(ctx, "sql", query).Info("query sql")
	log.WithField(ctx, "args", args).Debug("query args")

	err := q.DB.Get(dest, query, args...)
	if err != nil {
		err = errors.Wrap(err, 1)
	}
	return err
}<commit_msg>Add Query and QueryRaw helpers<commit_after>package db

import (
	"github.com/go-errors/errors"
	"github.com/jmoiron/sqlx"
	sq "github.com/lann/squirrel"
	"github.com/stellar/horizon/log"
	"golang.org/x/net/context"
)

// SqlQuery helps facilitate queries against a postgresql database. See Select and Get for
// the main methods used by collaborators.
type SqlQuery struct {
	DB *sqlx.DB
}

// Select selects multiple rows returned by the provided sql builder into the provided dest.
// dest must be a slice of the correct record type.
func (q SqlQuery) Select(ctx context.Context, sql sq.SelectBuilder, dest interface{}) error {
	sql = sql.PlaceholderFormat(sq.Dollar)
	query, args, err := sql.ToSql()

	if err != nil {
		return errors.Wrap(err, 1)
	}

	return q.SelectRaw(ctx, query, args, dest)
}

// SelectRaw runs the provided postgres query and args against this sqlquery's db.
func (q SqlQuery) SelectRaw(ctx context.Context, query string, args []interface{}, dest interface{}) error {
	log.WithField(ctx, "sql", query).Info("query sql")
	log.WithField(ctx, "args", args).Debug("query args")

	err := q.DB.Select(dest, query, args...)
	if err != nil {
		err = errors.Wrap(err, 1)
	}
	return err
}

// Get gets a single row returned by the provided sql builder into the provided dest.
// dest must be a non-slice value of the correct record type.
func (q SqlQuery) Get(ctx context.Context, sql sq.SelectBuilder, dest interface{}) error {
	sql = sql.PlaceholderFormat(sq.Dollar)
	query, args, err := sql.ToSql()

	if err != nil {
		return errors.Wrap(err, 1)
	}

	return q.GetRaw(ctx, query, args, dest)
}

// GetRaw runs the provided postgres query and args against this sqlquery's db.
func (q SqlQuery) GetRaw(ctx context.Context, query string, args []interface{}, dest interface{}) error {
	log.WithField(ctx, "sql", query).Info("query sql")
	log.WithField(ctx, "args", args).Debug("query args")

	err := q.DB.Get(dest, query, args...)
	if err != nil {
		err = errors.Wrap(err, 1)
	}
	return err
}

func (q SqlQuery) Query(ctx context.Context, sql sq.SelectBuilder) (*sqlx.Rows, error) {
	sql = sql.PlaceholderFormat(sq.Dollar)
	query, args, err := sql.ToSql()

	if err != nil {
		return nil, errors.Wrap(err, 1)
	}

	return q.QueryRaw(ctx, query, args)
}

// QueryRaw runs the provided query and returns a *sqlx.Rows value to iterate through the response
func (q SqlQuery) QueryRaw(ctx context.Context, query string, args []interface{}) (*sqlx.Rows, error) {
	log.WithField(ctx, "sql", query).Info("query sql")
	log.WithField(ctx, "args", args).Debug("query args")

	rows, err := q.DB.Queryx(query, args...)
	if err != nil {
		err = errors.Wrap(err, 1)
	}
	return rows, err
}
{'fraction_non_alphanumeric': 0.13414634146341464, 'fraction_numerical': 0.002139495079161318, 'mean_word_length': 4.081521739130435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4144817', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1517, 'n_words': 527}