<commit_before>{{- $type := .Data -}}
{{- $short := (shortname $type.Name "err" "res" "sqlstr" "db" "logf") -}}
{{- $table := (schema $type.Table.TableName) -}}
{{- if $type.Comment -}}
// {{ $type.Comment }}
{{- else -}}
// {{ $type.Name }} represents a row from '{{ $table }}'.
{{- end }}
type {{ $type.Name }} struct {
{{ range $type.Fields -}}
	{{ .Name }} {{ retype .Type }} {{ fieldtag . }} // {{ .Col.ColumnName }}
{{ end }}{{ if $type.PrimaryKey -}}
	// xo fields
	_exists, _deleted bool
{{ end -}}
}

{{ if $type.PrimaryKey -}}
// Exists returns true when the {{ $type.Name }} exists in the database.
func ({{ $short }} *{{ $type.Name }}) Exists() bool {
	return {{ $short }}._exists
}

// Deleted returns true when the {{ $type.Name }} has been marked for deletion from
// the database.
func ({{ $short }} *{{ $type.Name }}) Deleted() bool {
	return {{ $short }}._deleted
}

// Insert{{ if context_both }}Context{{ end }} inserts the {{ $type.Name }} to the database.
func ({{ $short }} *{{ $type.Name }}) Insert{{ if context_both }}Context{{ end }}({{ if context }}ctx context.Context, {{ end }}db DB) error {
	switch {
	case {{ $short }}._exists: // already exists
		return logerror(&ErrInsertFailed{ErrAlreadyExists})
	case {{ $short }}._deleted: // deleted
		return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
	}
{{ if $type.Table.ManualPk -}}
	// insert (basic)
	const sqlstr = `INSERT INTO {{ $table }} (` +
		`{{ colnames $type.Fields }}` +
		`) VALUES (` +
		`{{ colvals $type.Fields }}` +
		`)`
	// run
	logf(sqlstr, {{ fieldnames $type.Fields $short }})
	if err := db.QueryRow{{ if context }}Context{{ end }}({{ if context }}ctx, {{ end }}sqlstr, {{ fieldnames $type.Fields $short }}).Scan(&{{ $short }}.{{ $type.PrimaryKey.Name }}); err != nil {
		return logerror(err)
	}
{{- else -}}
	// insert (primary key generated and returned by database)
	const sqlstr = `INSERT INTO {{ $table }} (` +
		`{{ colnames $type.Fields $type.PrimaryKey.Name }}` +
		`) VALUES (` +
		`{{ colvals $type.Fields $type.PrimaryKey.Name }}` +
		`){{ if (driver "postgres" "oracle") }} RETURNING {{ colname $type.PrimaryKey.Col }}{{ end }}{{ if (driver "oracle") }} /*LASTINSERTID*/ INTO :pk{{ end }}{{ if (driver "sqlserver") }}; select ID = convert(bigint, SCOPE_IDENTITY()){{ end }}`
	// run
	logf(sqlstr, {{ fieldnames $type.Fields $short $type.PrimaryKey.Name }}{{ if (driver "oracle") }}, nil{{ end }})
{{ if (driver "postgres") -}}
	if err := db.QueryRow{{ if context }}Context{{ end }}({{ if context }}ctx, {{ end }}sqlstr, {{ fieldnames $type.Fields $short $type.PrimaryKey.Name }}).Scan(&{{ $short }}.{{ $type.PrimaryKey.Name }}); err != nil {
		return logerror(err)
	}
{{- else if (driver "sqlserver") -}}
	rows, err := db.Query{{ if context }}Context{{ end }}({{ if context }}ctx, {{ end }}sqlstr, {{ fieldnames $type.Fields $short $type.PrimaryKey.Name }})
	if err != nil {
		return logerror(err)<commit_msg>Fixing a minor issue with the Go typedef template<commit_after>{{- $type := .Data -}}
{{- $short := (shortname $type.Name "err" "res" "sqlstr" "db" "logf") -}}
{{- $table := (schema $type.Table.TableName) -}}
{{- if $type.Comment -}}
// {{ $type.Comment }}
{{- else -}}
// {{ $type.Name }} represents a row from '{{ $table }}'.
{{- end }}
type {{ $type.Name }} struct {
{{ range $type.Fields -}}
	{{ .Name }} {{ retype .Type }} {{ fieldtag . }} // {{ .Col.ColumnName }}
{{ end }}{{ if $type.PrimaryKey -}}
	// xo fields
	_exists, _deleted bool
{{ end -}}
}

{{ if $type.PrimaryKey -}}
// Exists returns true when the {{ $type.Name }} exists in the database.
func ({{ $short }} *{{ $type.Name }}) Exists() bool {
	return {{ $short }}._exists
}

// Deleted returns true when the {{ $type.Name }} has been marked for deletion from
// the database.
func ({{ $short }} *{{ $type.Name }}) Deleted() bool {
	return {{ $short }}._deleted
}

// Insert{{ if context_both }}Context{{ end }} inserts the {{ $type.Name }} to the database.
func ({{ $short }} *{{ $type.Name }}) Insert{{ if context_both }}Context{{ end }}({{ if context }}ctx context.Context, {{ end }}db DB) error {
	switch {
	case {{ $short }}._exists: // already exists
		return logerror(&ErrInsertFailed{ErrAlreadyExists})
	case {{ $short }}._deleted: // deleted
		return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
	}
{{ if $type.Table.ManualPk -}}
	// insert (basic)
	const sqlstr = `INSERT INTO {{ $table }} (` +
		`{{ colnames $type.Fields }}` +
		`) VALUES (` +
		`{{ colvals $type.Fields }}` +
		`)`
	// run
	logf(sqlstr, {{ fieldnames $type.Fields $short }})
	if _, err := db.Exec{{ if context }}Context{{ end }}({{ if context }}ctx, {{ end }}sqlstr, {{ fieldnames $type.Fields $short }}); err != nil {
		return logerror(err)
	}
{{- else -}}
	// insert (primary key generated and returned by database)
	const sqlstr = `INSERT INTO {{ $table }} (` +
		`{{ colnames $type.Fields $type.PrimaryKey.Name }}` +
		`) VALUES (` +
		`{{ colvals $type.Fields $type.PrimaryKey.Name }}` +
		`){{ if (driver "postgres" "oracle") }} RETURNING {{ colname $type.PrimaryKey.Col }}{{ end }}{{ if (driver "oracle") }} /*LASTINSERTID*/ INTO :pk{{ end }}{{ if (driver "sqlserver") }}; select ID = convert(bigint, SCOPE_IDENTITY()){{ end }}`
	// run
	logf(sqlstr, {{ fieldnames $type.Fields $short $type.PrimaryKey.Name }}{{ if (driver "oracle") }}, nil{{ end }})
{{ if (driver "postgres") -}}
	if err := db.QueryRow{{ if context }}Context{{ end }}({{ if context }}ctx, {{ end }}sqlstr, {{ fieldnames $type.Fields $short $type.PrimaryKey.Name }}).Scan(&{{ $short }}.{{ $type.PrimaryKey.Name }}); err != nil {
		return logerror(err)
	}
{{- else if (driver "sqlserver") -}}
	rows, err := db.Query{{ if context }}Context{{ end }}({{ if context }}ctx, {{ end }}sqlstr, {{ fieldnames $type.Fields $short $type.PrimaryKey.Name }})
	if err != nil {
		return logerror(err)
{'fraction_non_alphanumeric': 0.2280130293159609, 'fraction_numerical': 0.0, 'mean_word_length': 3.7976973684210527, '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': '6258470', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1969, 'n_words': 552}