<commit_before>package qbs

import (
	"fmt"
	"reflect"
	"strings"
	"time"
)

type base struct {
	Dialect Dialect
}

func (d base) substituteMarkers(query string) string {
	return query
}

func (d base) quote(s string) string {
	sep := "."
	a := []string{}
	c := strings.Split(s, sep)
	for _, v := range c {
		a = append(a, fmt.Sprintf("`%s`", v))
	}
	return strings.Join(a, sep)
}

func (d base) parseBool(value reflect.Value) bool {
	return value.Bool()
}

func (d base) setModelValue(driverValue, fieldValue reflect.Value) error {
	switch fieldValue.Type().Kind() {
	case reflect.Bool:
		fieldValue.SetBool(d.Dialect.parseBool(driverValue.Elem()))
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		fieldValue.SetInt(driverValue.Elem().Int())
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		// reading uint from int value causes panic
		switch driverValue.Elem().Kind() {
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			fieldValue.SetUint(uint64(driverValue.Elem().Int()))
		default:
			fieldValue.SetUint(driverValue.Elem().Uint())
		}
	case reflect.Float32, reflect.Float64:
		fieldValue.SetFloat(driverValue.Elem().Float())
	case reflect.String:
		fieldValue.SetString(string(driverValue.Elem().Bytes()))
	case reflect.Slice:
		if reflect.TypeOf(driverValue.Interface()).Elem().Kind() == reflect.Uint8 {
			fieldValue.SetBytes(driverValue.Elem().Bytes())
		}
	case reflect.Struct:
		if _, ok := fieldValue.Interface().(time.Time); ok {
			fieldValue.Set(driverValue.Elem())
		}
	}
	return nil
}

func (d base) querySql(criteria *criteria) (string, []interface{}) {
	query := make([]string, 0, 20)
	args := make([]interface{}, 0, 20)

	table := d.Dialect.quote(criteria.model.table)
	columns := []string{}
	tables := []string{table}
	hasJoin := len(criteria.model.refs) > 0
	for _, v := range criteria.model.fields {
		colName := d.Dialect.quote(v.name)
		if hasJoin {
			colName = d.Dialect.quote(criteria.model.table) + "." + colName
		}
		columns = append(columns, colName)
	}
	for k, v := range criteria.model.refs {
		tableAlias := toSnake(k)
		quotedTableAlias := d.Dialect.quote(tableAlias)
		quotedParentTable := d.Dialect.quote(v.model.table)
		leftKey := table + "." + d.Dialect.quote(v.refKey)
		parentPrimary := quotedTableAlias + "." + d.Dialect.quote(v.model.pk.name)
		joinClause := fmt.Sprintf("LEFT JOIN %v AS %v ON %v = %v", quotedParentTable, quotedTableAlias, leftKey, parentPrimary)
		tables = append(tables, joinClause)
		for _, f := range v.model.fields {
			alias := tableAlias + "___" + f.name
			columns = append(columns, d.Dialect.quote(tableAlias+"."+f.name)+" AS "+alias)
		}
	}
	query = append(query, "SELECT", strings.Join(columns, ", "), "FROM", strings.Join(tables, " "))

	if criteria.condition != nil {
		cexpr, cargs := criteria.condition.Merge()
		query = append(query, "WHERE", cexpr)
		args = append(args, cargs...)
	}
	orderByLen := len(criteria.orderBys)
	if orderByLen > 0 {
		query = append(query, "ORDER BY")
		for i, order := range criteria.orderBys {
			query = append(query, order.path)
			if order.desc {
				query = append(query, "DESC")
			}
			if i < orderByLen-1 {
				query = append(query, ",")
			}
		}
	}

	if x := criteria.limit; x > 0 {
		query = append(query, "LIMIT ?")
		args = append(args, criteria.limit)
	}
	if x := criteria.offset; x > 0 {
		query = append(query, "OFFSET ?")
		args = append(args, criteria.offset)
	}
	return d.Dialect.substituteMarkers(strings.Join(query, " ")), args
}

func (d base) insert(q *Qbs) (int64, error) {
	sql, args := d.Dialect.insertSql(q.criteria)
	result, err := q.Exec(sql, args...)
	if err != nil {
		return -1, err
	}
	id, err := result.LastInsertId()
	if err != nil {
		return -1, err
	}
	return id, nil
}

func (d base) insertSql(criteria *criteria) (string, []interface{}) {
	columns, values := criteria.model.columnsAndValues(false)
	quotedColumns := make([]string, 0, len(columns))
	markers := make([]string, 0, len(columns))
	for _, c := range columns {
		quotedColumns = append(quotedColumns, d.Dialect.quote(c))
		markers = append(markers, "?")
	}
	sql := fmt.Sprintf(
		"INSERT INTO %v (%v) VALUES (%v)",
		d.Dialect.quote(criteria.model.table),
		strings.Join(quotedColumns, ", "),
		strings.Join(markers, ", "),
	)
	return sql, values
}

func (d base) update(q *Qbs) (int64, error) {
	sql, args := d.Dialect.updateSql(q.criteria)
	result, err := q.Exec(sql, args...)
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

func (d base) updateSql(criteria *criteria) (string, []interface{}) {
	columns, values := criteria.model.columnsAndValues(true)
	pairs := make([]string, 0, len(columns))
	for _, column := range columns {
		pairs = append(pairs, fmt.Sprintf("%v = ?", d.Dialect.quote(column)))
	}
	conditionSql, args := criteria.condition.Merge()
	sql := fmt.Sprintf(
		"UPDATE %v SET %v WHERE %v",
		d.Dialect.quote(criteria.model.table),
		strings.Join(pairs, ", "),
		conditionSql,
	)
	values = append(values, args...)
	return sql, values
}

func (d base) delete(q *Qbs) (int64, error) {
	sql, args := d.Dialect.deleteSql(q.criteria)
	result, err := q.Exec(sql, args...)
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	if err != nil {
		return 0, err
	}
	return affected, err<commit_msg>base delete: remove redundant code<commit_after>package qbs

import (
	"fmt"
	"reflect"
	"strings"
	"time"
)

type base struct {
	Dialect Dialect
}

func (d base) substituteMarkers(query string) string {
	return query
}

func (d base) quote(s string) string {
	sep := "."
	a := []string{}
	c := strings.Split(s, sep)
	for _, v := range c {
		a = append(a, fmt.Sprintf("`%s`", v))
	}
	return strings.Join(a, sep)
}

func (d base) parseBool(value reflect.Value) bool {
	return value.Bool()
}

func (d base) setModelValue(driverValue, fieldValue reflect.Value) error {
	switch fieldValue.Type().Kind() {
	case reflect.Bool:
		fieldValue.SetBool(d.Dialect.parseBool(driverValue.Elem()))
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		fieldValue.SetInt(driverValue.Elem().Int())
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		// reading uint from int value causes panic
		switch driverValue.Elem().Kind() {
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			fieldValue.SetUint(uint64(driverValue.Elem().Int()))
		default:
			fieldValue.SetUint(driverValue.Elem().Uint())
		}
	case reflect.Float32, reflect.Float64:
		fieldValue.SetFloat(driverValue.Elem().Float())
	case reflect.String:
		fieldValue.SetString(string(driverValue.Elem().Bytes()))
	case reflect.Slice:
		if reflect.TypeOf(driverValue.Interface()).Elem().Kind() == reflect.Uint8 {
			fieldValue.SetBytes(driverValue.Elem().Bytes())
		}
	case reflect.Struct:
		if _, ok := fieldValue.Interface().(time.Time); ok {
			fieldValue.Set(driverValue.Elem())
		}
	}
	return nil
}

func (d base) querySql(criteria *criteria) (string, []interface{}) {
	query := make([]string, 0, 20)
	args := make([]interface{}, 0, 20)

	table := d.Dialect.quote(criteria.model.table)
	columns := []string{}
	tables := []string{table}
	hasJoin := len(criteria.model.refs) > 0
	for _, v := range criteria.model.fields {
		colName := d.Dialect.quote(v.name)
		if hasJoin {
			colName = d.Dialect.quote(criteria.model.table) + "." + colName
		}
		columns = append(columns, colName)
	}
	for k, v := range criteria.model.refs {
		tableAlias := toSnake(k)
		quotedTableAlias := d.Dialect.quote(tableAlias)
		quotedParentTable := d.Dialect.quote(v.model.table)
		leftKey := table + "." + d.Dialect.quote(v.refKey)
		parentPrimary := quotedTableAlias + "." + d.Dialect.quote(v.model.pk.name)
		joinClause := fmt.Sprintf("LEFT JOIN %v AS %v ON %v = %v", quotedParentTable, quotedTableAlias, leftKey, parentPrimary)
		tables = append(tables, joinClause)
		for _, f := range v.model.fields {
			alias := tableAlias + "___" + f.name
			columns = append(columns, d.Dialect.quote(tableAlias+"."+f.name)+" AS "+alias)
		}
	}
	query = append(query, "SELECT", strings.Join(columns, ", "), "FROM", strings.Join(tables, " "))

	if criteria.condition != nil {
		cexpr, cargs := criteria.condition.Merge()
		query = append(query, "WHERE", cexpr)
		args = append(args, cargs...)
	}
	orderByLen := len(criteria.orderBys)
	if orderByLen > 0 {
		query = append(query, "ORDER BY")
		for i, order := range criteria.orderBys {
			query = append(query, order.path)
			if order.desc {
				query = append(query, "DESC")
			}
			if i < orderByLen-1 {
				query = append(query, ",")
			}
		}
	}

	if x := criteria.limit; x > 0 {
		query = append(query, "LIMIT ?")
		args = append(args, criteria.limit)
	}
	if x := criteria.offset; x > 0 {
		query = append(query, "OFFSET ?")
		args = append(args, criteria.offset)
	}
	return d.Dialect.substituteMarkers(strings.Join(query, " ")), args
}

func (d base) insert(q *Qbs) (int64, error) {
	sql, args := d.Dialect.insertSql(q.criteria)
	result, err := q.Exec(sql, args...)
	if err != nil {
		return -1, err
	}
	id, err := result.LastInsertId()
	if err != nil {
		return -1, err
	}
	return id, nil
}

func (d base) insertSql(criteria *criteria) (string, []interface{}) {
	columns, values := criteria.model.columnsAndValues(false)
	quotedColumns := make([]string, 0, len(columns))
	markers := make([]string, 0, len(columns))
	for _, c := range columns {
		quotedColumns = append(quotedColumns, d.Dialect.quote(c))
		markers = append(markers, "?")
	}
	sql := fmt.Sprintf(
		"INSERT INTO %v (%v) VALUES (%v)",
		d.Dialect.quote(criteria.model.table),
		strings.Join(quotedColumns, ", "),
		strings.Join(markers, ", "),
	)
	return sql, values
}

func (d base) update(q *Qbs) (int64, error) {
	sql, args := d.Dialect.updateSql(q.criteria)
	result, err := q.Exec(sql, args...)
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

func (d base) updateSql(criteria *criteria) (string, []interface{}) {
	columns, values := criteria.model.columnsAndValues(true)
	pairs := make([]string, 0, len(columns))
	for _, column := range columns {
		pairs = append(pairs, fmt.Sprintf("%v = ?", d.Dialect.quote(column)))
	}
	conditionSql, args := criteria.condition.Merge()
	sql := fmt.Sprintf(
		"UPDATE %v SET %v WHERE %v",
		d.Dialect.quote(criteria.model.table),
		strings.Join(pairs, ", "),
		conditionSql,
	)
	values = append(values, args...)
	return sql, values
}

func (d base) delete(q *Qbs) (int64, error) {
	sql, args := d.Dialect.deleteSql(q.criteria)
	result, err := q.Exec(sql, args...)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
{'fraction_non_alphanumeric': 0.16954210575398676, 'fraction_numerical': 0.009792035810873822, 'mean_word_length': 4.096958174904943, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '6823190', 'n_tokens_mistral': 4119, 'n_tokens_neox': 3852, 'n_words': 879}