<commit_before>package db

import (
	"fmt"
	"strings"

	"github.com/pborman/uuid"
)

type Store struct {
	UUID     string `json:"uuid"`
	Name     string `json:"name"`
	Summary  string `json:"summary"`
	Plugin   string `json:"plugin"`
	Endpoint string `json:"endpoint"`
}

type StoreFilter struct {
	SkipUsed   bool
	SkipUnused bool
	SearchName string
	ForPlugin  string
}

func (f *StoreFilter) Query() (string, []interface{}) {
	var wheres []string = []string{"s.uuid = s.uuid"}
	args := []interface{}{}
	n := 1
	if f.SearchName != "" {
		wheres = append(wheres, fmt.Sprintf("s.name LIKE $%d", n))
		args = append(args, Pattern(f.SearchName))
		n++
	}
	if f.ForPlugin != "" {
		wheres = append(wheres, fmt.Sprintf("s.plugin = $%d", n))
		args = append(args, f.ForPlugin)
		n++
	}

	if !f.SkipUsed && !f.SkipUnused {
		return `
			SELECT s.uuid, s.name, s.summary, s.plugin, s.endpoint, -1 AS n
				FROM stores s
				WHERE ` + strings.Join(wheres, " AND ") + `
				ORDER BY s.name, s.uuid ASC
		`, args
	}

	// by default, show stores with no attached jobs (unused)
	having := `HAVING COUNT(j.uuid) = 0`
	if f.SkipUnused {
		// otherwise, only show stores that have attached jobs
		having = `HAVING COUNT(j.uuid) > 0`
	}

	return `
		SELECT DISTINCT s.uuid, s.name, s.summary, s.plugin, s.endpoint, COUNT(j.uuid) AS n
			FROM stores s
				LEFT JOIN jobs j
					ON j.store_uuid = s.uuid
			WHERE ` + strings.Join(wheres, " AND ") + `
			GROUP BY s.uuid
			` + having + `
			ORDER BY s.name, s.uuid ASC
	`, args
}

func (db *DB) GetAllStores(filter *StoreFilter) ([]*Store, error) {
	l := []*Store{}
	query, args := filter.Query()
	r, err := db.Query(query, args...)
	if err != nil {
		return l, err
	}
	defer r.Close()

	for r.Next() {
		ann := &Store{}
		var n int

		if err = r.Scan(&ann.UUID, &ann.Name, &ann.Summary, &ann.Plugin, &ann.Endpoint, &n); err != nil {
			return l, err
		}

		l = append(l, ann)
	}

	return l, nil
}

func (db *DB) GetStore(id uuid.UUID) (*Store, error) {
	r, err := db.Query(`
		SELECT s.uuid, s.name, s.summary, s.plugin, s.endpoint
			FROM stores s
				LEFT JOIN jobs j
					ON j.store_uuid = s.uuid
			WHERE s.uuid = $1`, id.String())
	if err != nil {
		return nil, err
	}
	defer r.Close()

	if !r.Next() {
		return nil, nil
	}

	ann := &Store{}

	if err = r.Scan(&ann.UUID, &ann.Name, &ann.Summary, &ann.Plugin, &ann.Endpoint); err != nil {
		return nil, err
	}

	return ann, nil
}

func (db *DB) AnnotateStore(id uuid.UUID, name string, summary string) error {
	return db.Exec(
		`UPDATE stores SET name = $1, summary = $2 WHERE uuid = $3`,
		name, summary, id.String(),
	)
}

func (db *DB) CreateStore(plugin string, endpoint interface{}) (uuid.UUID, error) {
	id := uuid.NewRandom()
	return id, db.Exec(
		`INSERT INTO stores (uuid, plugin, endpoint) VALUES ($1, $2, $3)`,
		id.String(), plugin, endpoint,
	)
}
<commit_msg>Make db.Store UUIDs real UUIDs<commit_after>package db

import (
	"fmt"
	"strings"

	"github.com/pborman/uuid"
)

type Store struct {
	UUID     uuid.UUID `json:"uuid"`
	Name     string    `json:"name"`
	Summary  string    `json:"summary"`
	Plugin   string    `json:"plugin"`
	Endpoint string    `json:"endpoint"`
}

type StoreFilter struct {
	SkipUsed   bool
	SkipUnused bool
	SearchName string
	ForPlugin  string
}

func (f *StoreFilter) Query() (string, []interface{}) {
	var wheres []string = []string{"s.uuid = s.uuid"}
	args := []interface{}{}
	n := 1
	if f.SearchName != "" {
		wheres = append(wheres, fmt.Sprintf("s.name LIKE $%d", n))
		args = append(args, Pattern(f.SearchName))
		n++
	}
	if f.ForPlugin != "" {
		wheres = append(wheres, fmt.Sprintf("s.plugin = $%d", n))
		args = append(args, f.ForPlugin)
		n++
	}

	if !f.SkipUsed && !f.SkipUnused {
		return `
			SELECT s.uuid, s.name, s.summary, s.plugin, s.endpoint, -1 AS n
				FROM stores s
				WHERE ` + strings.Join(wheres, " AND ") + `
				ORDER BY s.name, s.uuid ASC
		`, args
	}

	// by default, show stores with no attached jobs (unused)
	having := `HAVING COUNT(j.uuid) = 0`
	if f.SkipUnused {
		// otherwise, only show stores that have attached jobs
		having = `HAVING COUNT(j.uuid) > 0`
	}

	return `
		SELECT DISTINCT s.uuid, s.name, s.summary, s.plugin, s.endpoint, COUNT(j.uuid) AS n
			FROM stores s
				LEFT JOIN jobs j
					ON j.store_uuid = s.uuid
			WHERE ` + strings.Join(wheres, " AND ") + `
			GROUP BY s.uuid
			` + having + `
			ORDER BY s.name, s.uuid ASC
	`, args
}

func (db *DB) GetAllStores(filter *StoreFilter) ([]*Store, error) {
	l := []*Store{}
	query, args := filter.Query()
	r, err := db.Query(query, args...)
	if err != nil {
		return l, err
	}
	defer r.Close()

	for r.Next() {
		ann := &Store{}
		var n int
		var thisUUID string
		if err = r.Scan(&thisUUID, &ann.Name, &ann.Summary, &ann.Plugin, &ann.Endpoint, &n); err != nil {
			return l, err
		}
		ann.UUID = uuid.Parse(thisUUID)

		l = append(l, ann)
	}

	return l, nil
}

func (db *DB) GetStore(id uuid.UUID) (*Store, error) {
	r, err := db.Query(`
		SELECT s.uuid, s.name, s.summary, s.plugin, s.endpoint
			FROM stores s
				LEFT JOIN jobs j
					ON j.store_uuid = s.uuid
			WHERE s.uuid = $1`, id.String())
	if err != nil {
		return nil, err
	}
	defer r.Close()

	if !r.Next() {
		return nil, nil
	}

	ann := &Store{}
	var thisUUID string
	if err = r.Scan(&thisUUID, &ann.Name, &ann.Summary, &ann.Plugin, &ann.Endpoint); err != nil {
		return nil, err
	}
	ann.UUID = uuid.Parse(thisUUID)

	return ann, nil
}

func (db *DB) AnnotateStore(id uuid.UUID, name string, summary string) error {
	return db.Exec(
		`UPDATE stores SET name = $1, summary = $2 WHERE uuid = $3`,
		name, summary, id.String(),
	)
}

func (db *DB) CreateStore(plugin string, endpoint interface{}) (uuid.UUID, error) {
	id := uuid.NewRandom()
	return id, db.Exec(
		`INSERT INTO stores (uuid, plugin, endpoint) VALUES ($1, $2, $3)`,
		id.String(), plugin, endpoint,
	)
}

{'fraction_non_alphanumeric': 0.16410781303309452, 'fraction_numerical': 0.0037529853292391675, 'mean_word_length': 3.1201686577652845, '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': '6905426', 'n_tokens_mistral': 2411, 'n_tokens_neox': 2225, 'n_words': 656}