<commit_before>package dao

import "database/sql"

type DAO struct {
	Db *sql.DB
}

func (d *DAO) Comments(threadHash string) ([]*Comment, error) {
	rows, err := d.Db.Query(`
    		select  coalesce(
    			(select name from addresses where address=sender), sender
    		), comment, received,
    		sender, body, pending, subject, extended
            from channels
            where thread_hash = ? and received > unix_timestamp() - 3*28*24*60*60
            order by received asc
            limit 1000
    	`, threadHash)
	var comments []*Comment
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	for rows.Next() {
		var comment Comment
		err := rows.Scan(
			&comment.SenderName,
			&comment.Text,
			&comment.Received,
			&comment.Sender,
			&comment.Body,
			&comment.Pending,
			&comment.Subject,
			&comment.IsExtended,
		)
		if err != nil {
			return nil, err
		}
		if comment.Subject == "" {
			comment.Subject = "(no subject)"
		}
		comments = append(comments, &comment)
	}
	return comments, nil
}

func (d *DAO) AllKeys() ([]*Key, error) {
	rows, err := d.Db.Query(`
        SELECT name, address, sigkey, deckey, enabled
        FROM keys2
        WHERE enabled=1
        ORDER BY name ASC
        LIMIT 10000;
    `)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	keys := make([]*Key, 0)
	for rows.Next() {
		var name, address, sigkey, deckey string
		var enabled bool
		err := rows.Scan(&name, &address, &sigkey, &deckey, &enabled)
		if err != nil {
			return nil, err
		}
		if len(address) < 3 || address[:3] != "BM-" {
			address = "BM-" + address
		}
		keys = append(keys, &Key{
			Name:    name,
			Address: address,
			Sigkey:  sigkey,
			Deckey:  deckey,
			Enabled: enabled,
		})
	}
	return keys, nil
}

func (d *DAO) KeyByName(name string) *Key {
	key := Key{
		Name: name,
	}
	_ = d.Db.QueryRow(`
		select address, sigkey, deckey, enabled
		from keys2
		where name = ?;
	`, key.Name).Scan(&key.Address, &key.Sigkey, &key.Deckey, &key.Enabled)
	if len(key.Address) < 3 || key.Address[:3] != "BM-" {
		key.Address = "BM-" + key.Address
	}
	return &key
}

func (d *DAO) ThreadsByChanName(name string) ([]*Thread, error) {
	rows, err := d.Db.Query(`
        select subject, count, last, hash
        from threads
        where name = ? and last > unix_timestamp() - 28*24*60*60<commit_msg>Small bug fix<commit_after>package dao

import "database/sql"

type DAO struct {
	Db *sql.DB
}

func (d *DAO) Comments(threadHash string) ([]*Comment, error) {
	rows, err := d.Db.Query(`
    		select  coalesce(
    			(select name from addresses where address=sender), sender
    		), comment, received,
    		sender, body, pending, subject, extended
            from channels
            where thread_hash = ? and received > unix_timestamp() - 3*28*24*60*60
            order by received asc
            limit 1000
    	`, threadHash)
	var comments []*Comment
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	for rows.Next() {
		var comment Comment
		err := rows.Scan(
			&comment.SenderName,
			&comment.Text,
			&comment.Received,
			&comment.Sender,
			&comment.Body,
			&comment.Pending,
			&comment.Subject,
			&comment.IsExtended,
		)
		if err != nil {
			return nil, err
		}
		if comment.Subject == "" {
			comment.Subject = "(no subject)"
		}
		comments = append(comments, &comment)
	}
	return comments, nil
}

func (d *DAO) AllKeys() ([]*Key, error) {
	rows, err := d.Db.Query(`
        SELECT name, address, sigkey, deckey, enabled
        FROM keys2
        WHERE enabled=1
        ORDER BY name ASC
        LIMIT 10000;
    `)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	keys := make([]*Key, 0)
	for rows.Next() {
		var name, address, sigkey, deckey string
		var enabled bool
		err := rows.Scan(&name, &address, &sigkey, &deckey, &enabled)
		if err != nil {
			return nil, err
		}
		if len(address) < 3 || address[:3] != "BM-" {
			address = "BM-" + address
		}
		keys = append(keys, &Key{
			Name:    name,
			Address: address,
			Sigkey:  sigkey,
			Deckey:  deckey,
			Enabled: enabled,
		})
	}
	return keys, nil
}

func (d *DAO) KeyByName(name string) *Key {
	key := Key{
		Name: name,
	}
	_ = d.Db.QueryRow(`
		select address, sigkey, deckey, enabled
		from keys2
		where name = ?;
	`, key.Name).Scan(&key.Address, &key.Sigkey, &key.Deckey, &key.Enabled)
	if key.Address != "" && (len(key.Address) < 3 || key.Address[:3] != "BM-") {
		key.Address = "BM-" + key.Address
	}
	return &key
}

func (d *DAO) ThreadsByChanName(name string) ([]*Thread, error) {
	rows, err := d.Db.Query(`
        select subject, count, last, hash
        from threads
        where name = ? and last > unix_timestamp() - 28*24*60*60
{'fraction_non_alphanumeric': 0.14426159435776875, 'fraction_numerical': 0.01453301987604189, 'mean_word_length': 2.7025316455696204, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5338828', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1646, 'n_words': 468}