<commit_before>package db

import (
	"database/sql"
	"fmt"
	"log"
	"strconv"
	"strings"

	_ "github.com/lib/pq"
)

var _ DBAdapter = &postgresqlAdapter{}

type postgresqlAdapter struct{}

func (a *postgresqlAdapter) Open(dataSourceName string) (DB, error) {
	db, err := sql.Open(a.Name(), dataSourceName)
	if err != nil {
		return nil, err
	}
	if err := db.Ping(); err != nil {
		if err2 := db.Close(); err2 != nil {
			log.Println(err2)
		}
		return nil, err
	}
	// TODO: make this configurable
	db.SetMaxOpenConns(20)
	return dbWrapper{
		db:   db,
		adap: a,
		pp:   fixLibPqQuery,
	}, nil
}

func (a *postgresqlAdapter) OpenForTest() (DB, error) {
	panic("no implemented")
}

func (_ *postgresqlAdapter) Name() string {
	return "postgres"
}

func (_ *postgresqlAdapter) SingleTx() bool {
	return false
}

func (_ *postgresqlAdapter) Quote(ident string) string {
	if strings.ContainsAny(ident, "\"\x00") {
		panic(fmt.Sprintf("Invalid identifier %#v", ident))
	}
	return `"` + ident + `"`
}

func (a *postgresqlAdapter) BlobIdxQuote(ident string) string {
	return a.Quote(ident)
}

func (_ *postgresqlAdapter) BoolType() string {
	return "bool"
}

func (_ *postgresqlAdapter) IntType() string {
	return "integer"
}

func (_ *postgresqlAdapter) BigIntType() string {
	return "bigint"
}

func (_ *postgresqlAdapter) BlobType() string {
	return "bytea"
}

func (_ *postgresqlAdapter) LockStmt(buf *strings.Builder, lock Lock) {
	switch lock {
	case LockNone:
	case LockRead:
		buf.WriteString(" FOR SHARE")
	case LockWrite:
		buf.WriteString(" FOR UPDATE")
	default:
		panic(fmt.Errorf("Unknown lock %v", lock))
	}
}

func fixLibPqQuery(query string) string {
	parts := strings.Split(query, "?")
	var b strings.Builder
	b.WriteString(parts[0])
	for i := 1; i < len(parts); i++ {
		b.WriteRune('$')
		b.WriteString(strconv.Itoa(i))
		b.WriteString(parts[i])
	}
	return b.String()
}

func init() {
	RegisterAdapter(new(postgresqlAdapter))
}<commit_msg>be/schema: rename postgresqlAdapter to postgresAdapter<commit_after>package db

import (
	"database/sql"
	"fmt"
	"log"
	"strconv"
	"strings"

	_ "github.com/lib/pq"
)

var _ DBAdapter = &postgresAdapter{}

type postgresAdapter struct{}

func (a *postgresAdapter) Open(dataSourceName string) (DB, error) {
	db, err := sql.Open(a.Name(), dataSourceName)
	if err != nil {
		return nil, err
	}
	if err := db.Ping(); err != nil {
		if err2 := db.Close(); err2 != nil {
			log.Println(err2)
		}
		return nil, err
	}
	// TODO: make this configurable
	db.SetMaxOpenConns(20)
	return dbWrapper{
		db:   db,
		adap: a,
		pp:   fixLibPqQuery,
	}, nil
}

func (a *postgresAdapter) OpenForTest() (DB, error) {
	panic("no implemented")
}

func (_ *postgresAdapter) Name() string {
	return "postgres"
}

func (_ *postgresAdapter) SingleTx() bool {
	return false
}

func (_ *postgresAdapter) Quote(ident string) string {
	if strings.ContainsAny(ident, "\"\x00") {
		panic(fmt.Sprintf("Invalid identifier %#v", ident))
	}
	return `"` + ident + `"`
}

func (a *postgresAdapter) BlobIdxQuote(ident string) string {
	return a.Quote(ident)
}

func (_ *postgresAdapter) BoolType() string {
	return "bool"
}

func (_ *postgresAdapter) IntType() string {
	return "integer"
}

func (_ *postgresAdapter) BigIntType() string {
	return "bigint"
}

func (_ *postgresAdapter) BlobType() string {
	return "bytea"
}

func (_ *postgresAdapter) LockStmt(buf *strings.Builder, lock Lock) {
	switch lock {
	case LockNone:
	case LockRead:
		buf.WriteString(" FOR SHARE")
	case LockWrite:
		buf.WriteString(" FOR UPDATE")
	default:
		panic(fmt.Errorf("Unknown lock %v", lock))
	}
}

func fixLibPqQuery(query string) string {
	parts := strings.Split(query, "?")
	var b strings.Builder
	b.WriteString(parts[0])
	for i := 1; i < len(parts); i++ {
		b.WriteRune('$')
		b.WriteString(strconv.Itoa(i))
		b.WriteString(parts[i])
	}
	return b.String()
}

func init() {
	RegisterAdapter(new(postgresAdapter))
}
{'fraction_non_alphanumeric': 0.1518016866854076, 'fraction_numerical': 0.004600051111679018, 'mean_word_length': 4.376373626373627, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5968342', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1465, 'n_words': 354}