<commit_before>package cockroach

import (
	"context"
	"fmt"
	"time"

	"github.com/coreos/go-semver/semver"

	dsserr "github.com/interuss/dss/pkg/errors"
	"github.com/interuss/dss/pkg/geo"
	dssmodels "github.com/interuss/dss/pkg/models"
	ridmodels "github.com/interuss/dss/pkg/rid/models"

	"github.com/golang/geo/s2"
	repos "github.com/interuss/dss/pkg/rid/repos"
	dssql "github.com/interuss/dss/pkg/sql"
	"github.com/lib/pq"
	"github.com/palantir/stacktrace"
	"go.uber.org/zap"
)

const (
	isaFields       = "id, owner, url, cells, starts_at, ends_at, writer, updated_at"
	updateISAFields = "id, url, cells, starts_at, ends_at, writer, updated_at"
)

var (
	v310 = *semver.New("3.1.0")
)

func NewISARepo(ctx context.Context, db dssql.Queryable, dbVersion semver.Version, logger *zap.Logger) repos.ISA {
	if dbVersion.Compare(v310) >= 0 {
		return &isaRepo{
			Queryable: db,
			logger:    logger,
		}
	}
	return &isaRepoV3{
		Queryable: db,
		logger:    logger,
	}
}

// isaRepo is an implementation of the ISARepo for CRDB.
type isaRepo struct {
	dssql.Queryable

	logger *zap.Logger
}

func (c *isaRepo) process(ctx context.Context, query string, args ...interface{}) ([]*ridmodels.IdentificationServiceArea, error) {
	rows, err := c.QueryContext(ctx, query, args...)
	if err != nil {
		return nil, stacktrace.Propagate(err, fmt.Sprintf("Error in query: %s", query))
	}
	defer rows.Close()

	var payload []*ridmodels.IdentificationServiceArea
	cids := pq.Int64Array{}

	for rows.Next() {
		i := new(ridmodels.IdentificationServiceArea)

		err := rows.Scan(
			&i.ID,
			&i.Owner,
			&i.URL,
			&cids,
			&i.StartTime,
			&i.EndTime,
			&i.Writer,
			&i.Version,
		)
		if err != nil {
			return nil, stacktrace.Propagate(err, "Error scanning ISA row")
		}
		i.SetCells(cids)
		payload = append(payload, i)
	}
	if err := rows.Err(); err != nil {
		return nil, stacktrace.Propagate(err, "Error in rows query result")<commit_msg>fix bug GetISA fail when writer=NULL (#402)<commit_after>package cockroach

import (
	"context"
	"database/sql"
	"fmt"
	"time"

	"github.com/coreos/go-semver/semver"

	dsserr "github.com/interuss/dss/pkg/errors"
	"github.com/interuss/dss/pkg/geo"
	dssmodels "github.com/interuss/dss/pkg/models"
	ridmodels "github.com/interuss/dss/pkg/rid/models"

	"github.com/golang/geo/s2"
	repos "github.com/interuss/dss/pkg/rid/repos"
	dssql "github.com/interuss/dss/pkg/sql"
	"github.com/lib/pq"
	"github.com/palantir/stacktrace"
	"go.uber.org/zap"
)

const (
	isaFields       = "id, owner, url, cells, starts_at, ends_at, writer, updated_at"
	updateISAFields = "id, url, cells, starts_at, ends_at, writer, updated_at"
)

var (
	v310 = *semver.New("3.1.0")
)

func NewISARepo(ctx context.Context, db dssql.Queryable, dbVersion semver.Version, logger *zap.Logger) repos.ISA {
	if dbVersion.Compare(v310) >= 0 {
		return &isaRepo{
			Queryable: db,
			logger:    logger,
		}
	}
	return &isaRepoV3{
		Queryable: db,
		logger:    logger,
	}
}

// isaRepo is an implementation of the ISARepo for CRDB.
type isaRepo struct {
	dssql.Queryable

	logger *zap.Logger
}

func (c *isaRepo) process(ctx context.Context, query string, args ...interface{}) ([]*ridmodels.IdentificationServiceArea, error) {
	rows, err := c.QueryContext(ctx, query, args...)
	if err != nil {
		return nil, stacktrace.Propagate(err, fmt.Sprintf("Error in query: %s", query))
	}
	defer rows.Close()

	var payload []*ridmodels.IdentificationServiceArea
	cids := pq.Int64Array{}

	var writer sql.NullString
	for rows.Next() {
		i := new(ridmodels.IdentificationServiceArea)

		err := rows.Scan(
			&i.ID,
			&i.Owner,
			&i.URL,
			&cids,
			&i.StartTime,
			&i.EndTime,
			&writer,
			&i.Version,
		)
		if err != nil {
			return nil, stacktrace.Propagate(err, "Error scanning ISA row")
		}
		if writer.Valid {
			i.Writer = writer.String
		} else {
			i.Writer = ""
		}
		i.SetCells(cids)
		payload = append(payload, i)
	}
	if err := rows.Err(); err != nil {
		return nil, stacktrace.Propagate(err, "Error in rows query result")
{'fraction_non_alphanumeric': 0.1493393168785839, 'fraction_numerical': 0.007728745948641236, 'mean_word_length': 3.7367178276269186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5624674', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1580, 'n_words': 331}