<commit_before>package sql

import (
	"database/sql"
	"errors"
	"fmt"
	"reflect"
	"strconv"
)

func NewPool(sourceStrings []string) (pool *Pool, err error) {
	queue := make(chan *sql.DB, len(sourceStrings))
	var conn *sql.DB
	for _, sourceStr := range sourceStrings {
		conn, err = sql.Open("mysql", sourceStr)
		if err != nil {
			return
		}
		err = conn.Ping()
		if err != nil {
			return
		}

		queue <- conn
	}
	pool = &Pool{queue}
	return
}

type Pool struct {
	queue chan *sql.DB
}

func (p *Pool) Transact(txFun func(tx *Pool) error) (err error) {
	conn := <-p.queue
	defer func() { p.queue <- conn }()

	txPool := &Pool{queue: make(chan *sql.DB, 1)}
	_, err = conn.Exec("START TRANSACTION")
	if err != nil {
		return
	}

	err = txFun(txPool)
	if err != nil {
		return rollback(conn, err)
	}

	conn.Exec("COMMIT")

	return err
}

func rollback(conn *sql.DB, err error) error {
	_, rbErr := conn.Exec("ROLLBACK")
	if rbErr != nil {
		err = errors.New("Rollback error: " + rbErr.Error() + " Query error: " + err.Error())
	}
	return err
}

// Query with fixed args
func (p *Pool) Query(query string, args ...interface{}) (rows *sql.Rows, err error) {
	conn := <-p.queue
	defer func() { p.queue <- conn }()

	FixArgs(args)
	rows, err = conn.Query(query, args...)
	if err != nil {
		err = errors.New("sql.Query Error: " + err.Error() + ". Query: " + query + " Args: " + fmt.Sprint(args))
	}
	return
}

// Execute with fixed args
func (p *Pool) Exec(query string, args ...interface{}) (res sql.Result, err error) {
	conn := <-p.queue
	defer func() { p.queue <- conn }()

	FixArgs(args)
	res, err = conn.Exec(query, args...)
	if err != nil {
		err = errors.New("sql.Exec Error: " + err.Error() + ". Query: " + query + " Args: " + fmt.Sprint(args))
	}
	return
}

/*
Fix args by converting them to values of their underlying kind.
This avoids problems in database/sql with e.g custom string types.
Without FixArgs, the following code:

	type Foo string
	...
	pool.Query("SELECT * WHERE Foo=?", Foo("bar"))

would give you the error:

	sql: converting Exec argument #1's type: unsupported type Foo, a string
*/
func FixArgs(args []interface{}) {
	for i, arg := range args {
		vArg := reflect.ValueOf(arg)
		if vArg.Kind() == reflect.String {
			args[i] = vArg.String()
		}
	}
}

func (p *Pool) SelectInt(query string, args ...interface{}) (num int, found bool, err error) {
	found, err = p.queryOne(query, args, &num)
	return
}

func (p *Pool) SelectString(query string, args ...interface{}) (str string, found bool, err error) {
	found, err = p.queryOne(query, args, &str)
	return
}

func (p *Pool) SelectUInt(query string, args ...interface{}) (num uint, found bool, err error) {
	found, err = p.queryOne(query, args, &num)
	return
}

func (p *Pool) queryOne(query string, args []interface{}, out interface{}) (found bool, err error) {
	rows, err := p.Query(query, args...)
	if err != nil {
		return
	}

	if rows.Next() {
		found = true
		err = rows.Scan(out)
		if err != nil {
			return
		}
	}

	if rows.Next() {
		err = errors.New("Query returned too many columns")
		return
	}

	return
}

// type TransactFunc func() error

// func (p *Pool) Transact(f TransactFunc) (err error) {
// 	conn := <-p.queue
// 	defer func() { p.queue <- conn }()

// 	_, err = conn.Exec("START TRANSACTION")
// 	if err != nil {
// 		return
// 	}

// 	err = f(conn)
// 	if err != nil {
// 		_, rollbackError := conn.Exec("ROLLBACK")
// 		if rollbackError != nil {
// 			panic("Could not rollback transaction. TransactionFunc error: " + err.Error() + ". Rollback error:" + rollbackError.Error())
// 		}

// 	} else {
// 		_, err = conn.Exec("COMMIT")
// 	}

// 	return<commit_msg>If not found, return error<commit_after>package sql

import (
	"database/sql"
	"errors"
	"fmt"
	"reflect"
	"strconv"
)

func NewPool(sourceStrings []string) (pool *Pool, err error) {
	queue := make(chan *sql.DB, len(sourceStrings))
	var conn *sql.DB
	for _, sourceStr := range sourceStrings {
		conn, err = sql.Open("mysql", sourceStr)
		if err != nil {
			return
		}
		err = conn.Ping()
		if err != nil {
			return
		}

		queue <- conn
	}
	pool = &Pool{queue}
	return
}

type Pool struct {
	queue chan *sql.DB
}

func (p *Pool) Transact(txFun func(tx *Pool) error) (err error) {
	conn := <-p.queue
	defer func() { p.queue <- conn }()

	txPool := &Pool{queue: make(chan *sql.DB, 1)}
	_, err = conn.Exec("START TRANSACTION")
	if err != nil {
		return
	}

	err = txFun(txPool)
	if err != nil {
		return rollback(conn, err)
	}

	conn.Exec("COMMIT")

	return err
}

func rollback(conn *sql.DB, err error) error {
	_, rbErr := conn.Exec("ROLLBACK")
	if rbErr != nil {
		err = errors.New("Rollback error: " + rbErr.Error() + " Query error: " + err.Error())
	}
	return err
}

// Query with fixed args
func (p *Pool) Query(query string, args ...interface{}) (rows *sql.Rows, err error) {
	conn := <-p.queue
	defer func() { p.queue <- conn }()

	FixArgs(args)
	rows, err = conn.Query(query, args...)
	if err != nil {
		err = errors.New("sql.Query Error: " + err.Error() + ". Query: " + query + " Args: " + fmt.Sprint(args))
	}
	return
}

// Execute with fixed args
func (p *Pool) Exec(query string, args ...interface{}) (res sql.Result, err error) {
	conn := <-p.queue
	defer func() { p.queue <- conn }()

	FixArgs(args)
	res, err = conn.Exec(query, args...)
	if err != nil {
		err = errors.New("sql.Exec Error: " + err.Error() + ". Query: " + query + " Args: " + fmt.Sprint(args))
	}
	return
}

/*
Fix args by converting them to values of their underlying kind.
This avoids problems in database/sql with e.g custom string types.
Without FixArgs, the following code:

	type Foo string
	...
	pool.Query("SELECT * WHERE Foo=?", Foo("bar"))

would give you the error:

	sql: converting Exec argument #1's type: unsupported type Foo, a string
*/
func FixArgs(args []interface{}) {
	for i, arg := range args {
		vArg := reflect.ValueOf(arg)
		if vArg.Kind() == reflect.String {
			args[i] = vArg.String()
		}
	}
}

func (p *Pool) SelectInt(query string, args ...interface{}) (num int, err error) {
	err = p.queryOne(query, args, &num)
	return
}

func (p *Pool) SelectString(query string, args ...interface{}) (str string, err error) {
	err = p.queryOne(query, args, &str)
	return
}

func (p *Pool) SelectUInt(query string, args ...interface{}) (num uint, err error) {
	err = p.queryOne(query, args, &num)
	return
}

func (p *Pool) queryOne(query string, args []interface{}, out interface{}) (err error) {
	rows, err := p.Query(query, args...)
	if err != nil {
		return
	}

	if rows.Next() {
		err = rows.Scan(out)
		if err != nil {
			return
		}
	} else {
		err = errors.New("Query returned no rows")
		return
	}

	if rows.Next() {
		err = errors.New("Query returned too many rows")
		return
	}

	return
}

// type TransactFunc func() error

// func (p *Pool) Transact(f TransactFunc) (err error) {
// 	conn := <-p.queue
// 	defer func() { p.queue <- conn }()

// 	_, err = conn.Exec("START TRANSACTION")
// 	if err != nil {
// 		return
// 	}

// 	err = f(conn)
// 	if err != nil {
// 		_, rollbackError := conn.Exec("ROLLBACK")
// 		if rollbackError != nil {
// 			panic("Could not rollback transaction. TransactionFunc error: " + err.Error() + ". Rollback error:" + rollbackError.Error())
// 		}

// 	} else {
// 		_, err = conn.Exec("COMMIT")
// 	}

// 	return
{'fraction_non_alphanumeric': 0.1741926655719759, 'fraction_numerical': 0.0005473453749315818, 'mean_word_length': 3.127046866177301, 'pattern_counts': {'":': 0, '<': 21, '<?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': '5844866', 'n_tokens_mistral': 2797, 'n_tokens_neox': 2651, 'n_words': 787}