<commit_before>package orm

import (
	"context"
	"database/sql"
	"github.com/pkg/errors"
	"github.com/rbastic/dyndao/object"
)

func (o ORM) FindOrCreateTx(ctx context.Context, tx *sql.Tx, obj *object.Object) (*object.Object, error) {
	obj, err := o.RetrieveTx(ctx, tx, obj.Type, obj.KV)
	if err != nil {
		return nil, err
	}

	if obj == nil {
		numRows, err := o.Insert(ctx, tx, obj)
		if err != nil {
			return nil, err
		}
		if numRows == 0 {
			return nil, errors.New("FindOrCreateTx: numRows was 0 when expecting Insert")
		}

		return obj, nil
	}

	return obj, nil
}

func (o ORM) FindOrCreate(ctx context.Context, obj *object.Object) (*object.Object, error) {
	return o.FindOrCreateTx(ctx, nil, obj)
}

func (o ORM) FindOrCreateKVTx(ctx context.Context, tx *sql.Tx, typ string, queryKV map[string]interface{}, createKV map[string]interface{}) (*object.Object, error) {
	obj, err := o.RetrieveTx(ctx, tx, typ, queryKV)
	if err != nil {
		return nil, err
	}

	if obj == nil {
		obj := object.New(typ)
		obj.KV = createKV

		numRows, err := o.Insert(ctx, tx, obj)
		if err != nil {
			return nil, err
		}
		if numRows == 0 {
			return nil, errors.New("FindOrCreateKVTx: numRows was 0 when expecting Insert")
		}

		return obj, nil
	}

	return obj, nil
}

func (o ORM) FindOrCreateKV(ctx context.Context, typ string, queryKV map[string]interface{}, createKV map[string]interface{}) (*object.Object, error) {
	return o.FindOrCreateKVTx(ctx, nil, typ, queryKV, createKV)
}
<commit_msg>add numRows to FindOrCreate return values<commit_after>package orm

import (
	"context"
	"database/sql"
	"github.com/pkg/errors"
	"github.com/rbastic/dyndao/object"
)

func (o ORM) FindOrCreateTx(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, *object.Object, error) {
	obj, err := o.RetrieveTx(ctx, tx, obj.Type, obj.KV)
	if err != nil {
		return 0, nil, err
	}

	if obj == nil {
		numRows, err := o.Insert(ctx, tx, obj)
		if err != nil {
			return numRows, nil, err
		}
		if numRows == 0 {
			return 0, nil, errors.New("FindOrCreateTx: numRows was 0 when expecting Insert")
		}

		return numRows, obj, nil
	}

	return 0, obj, nil
}

func (o ORM) FindOrCreate(ctx context.Context, obj *object.Object) (int64, *object.Object, error) {
	return o.FindOrCreateTx(ctx, nil, obj)
}

func (o ORM) FindOrCreateKVTx(ctx context.Context, tx *sql.Tx, typ string, queryKV map[string]interface{}, createKV map[string]interface{}) (int64, *object.Object, error) {
	obj, err := o.RetrieveTx(ctx, tx, typ, queryKV)
	if err != nil {
		return 0, nil, err
	}

	if obj == nil {
		obj := object.New(typ)
		obj.KV = createKV

		numRows, err := o.Insert(ctx, tx, obj)
		if err != nil {
			return numRows, nil, err
		}
		if numRows == 0 {
			return 0, nil, errors.New("FindOrCreateKVTx: numRows was 0 when expecting Insert")
		}

		return numRows, obj, nil
	}

	return 0, obj, nil
}

func (o ORM) FindOrCreateKV(ctx context.Context, typ string, queryKV map[string]interface{}, createKV map[string]interface{}) (int64, *object.Object, error) {
	return o.FindOrCreateKVTx(ctx, nil, typ, queryKV, createKV)
}

{'fraction_non_alphanumeric': 0.14211550940947437, 'fraction_numerical': 0.007138221933809215, 'mean_word_length': 3.9249201277955272, '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': '5663885', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1160, 'n_words': 333}