<commit_before>package pgproc

import (
	"database/sql"
	"errors"
	"fmt"
	"github.com/lib/pq"
	"reflect"
	"strings"
	"time"
)

type PgProc struct {
	db *sql.DB
}

type returnType struct {
	scalar         bool
	setof          bool
	scalarType     string
	compositeNames pq.StringArray
	compositeTypes pq.StringArray
}

var (
	DateMinusInfinity = time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC)
	DateInfinity      = time.Date(9999, 1, 1, 0, 0, 0, 0, time.UTC)
)

// NewPgProc creates a new connection to a PostgreSQL database
func NewPgProc(conninfo string) (*PgProc, error) {
	var pgproc = PgProc{}
	var err error
	pgproc.db, err = sql.Open("postgres", conninfo)
	if err != nil {
		return nil, err
	}
	pq.EnableInfinityTs(DateMinusInfinity, DateInfinity)
	return &pgproc, nil
}

// Call calls a PostgreSQL procedure and stores the result
func (p *PgProc) Call(result interface{}, schema string, proc string, params ...interface{}) error {

	if proc[0] == '_' {
		return errors.New("function not callable")
	}

	rt, err := p.getReturnType(schema, proc, len(params))
	if err != nil {
		return err
	}
	query := fmt.Sprintf("SELECT * FROM %s.%s(%s)",
		pq.QuoteIdentifier(schema),
		pq.QuoteIdentifier(proc),
		paramsString(len(params)))

	if rt.scalar {
		if !rt.setof {
			row := p.db.QueryRow(query, params...)
			err = row.Scan(result)
		} else {
			rows, _ := p.db.Query(query, params...)
			defer rows.Close()
			c := reflect.ValueOf(result) // the channel we have to send to
			// val is a zero element of the same type of the channel type
			val := reflect.Zero(reflect.TypeOf(result).Elem()).Interface()
			for rows.Next() {
				if err := rows.Scan(&val); err != nil {
					return err
				}
				c.Send(reflect.ValueOf(val))
			}
		}
	} else {
		if !rt.setof {
			row := p.db.QueryRow(query, params...)
			err = ScanCompositeRow(row, rt, result)
		} else {
			rows, _ := p.db.Query(query, params...)
			defer rows.Close()
			for rows.Next() {
				if err := ScanCompositeRows(rows, rt, result); err != nil {
					return err
				}
			}
		}
	}
	return err<commit_msg>call proc with void result<commit_after>package pgproc

import (
	"database/sql"
	"errors"
	"fmt"
	"github.com/lib/pq"
	"reflect"
	"strings"
	"time"
)

type PgProc struct {
	db *sql.DB
}

type returnType struct {
	scalar         bool
	setof          bool
	scalarType     string
	compositeNames pq.StringArray
	compositeTypes pq.StringArray
}

var (
	DateMinusInfinity = time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC)
	DateInfinity      = time.Date(9999, 1, 1, 0, 0, 0, 0, time.UTC)
)

// NewPgProc creates a new connection to a PostgreSQL database
func NewPgProc(conninfo string) (*PgProc, error) {
	var pgproc = PgProc{}
	var err error
	pgproc.db, err = sql.Open("postgres", conninfo)
	if err != nil {
		return nil, err
	}
	pq.EnableInfinityTs(DateMinusInfinity, DateInfinity)
	return &pgproc, nil
}

// Call calls a PostgreSQL procedure and stores the result
func (p *PgProc) Call(result interface{}, schema string, proc string, params ...interface{}) error {

	if proc[0] == '_' {
		return errors.New("function not callable")
	}

	rt, err := p.getReturnType(schema, proc, len(params))
	if err != nil {
		return err
	}
	query := fmt.Sprintf("SELECT * FROM %s.%s(%s)",
		pq.QuoteIdentifier(schema),
		pq.QuoteIdentifier(proc),
		paramsString(len(params)))

	if rt.scalar {
		if !rt.setof {
			if result != nil {
				row := p.db.QueryRow(query, params...)
				err = row.Scan(result)
			} else {
				_, err = p.db.Exec(query, params...)
			}
		} else {
			rows, _ := p.db.Query(query, params...)
			defer rows.Close()
			c := reflect.ValueOf(result) // the channel we have to send to
			// val is a zero element of the same type of the channel type
			val := reflect.Zero(reflect.TypeOf(result).Elem()).Interface()
			for rows.Next() {
				if err := rows.Scan(&val); err != nil {
					return err
				}
				c.Send(reflect.ValueOf(val))
			}
		}
	} else {
		if !rt.setof {
			row := p.db.QueryRow(query, params...)
			err = ScanCompositeRow(row, rt, result)
		} else {
			rows, _ := p.db.Query(query, params...)
			defer rows.Close()
			for rows.Next() {
				if err := ScanCompositeRows(rows, rt, result); err != nil {
					return err
				}
			}
		}
	}
	return err
{'fraction_non_alphanumeric': 0.14472123368920523, 'fraction_numerical': 0.008540925266903915, 'mean_word_length': 3.0421860019175457, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4243091', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1514, 'n_words': 418}