sqlc-go-one-or-fail

command module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2023 License: MIT Imports: 1 Imported by: 0

README

sqlc-go-one-or-fail

sqlc-go-one-or-fail modifies the Go code generated by sqlc to fail if more than one record is retrieved in the :one command.

Usage

$ sqlc generate
$ sqlc-go-one-or-fail path/to/generated_by_sqlc/*.go
Before
const getAuthorByName = `-- name: GetAuthorByName :one
SELECT id, name, bio FROM authors
WHERE name = ?
`

func (q *Queries) GetAuthorByName(ctx context.Context, name string) (Author, error) {
	row := q.db.QueryRowContext(ctx, getAuthorByName, name)
	var i Author
	err := row.Scan(&i.ID, &i.Name, &i.Bio)
	return i, err
}
After
const getAuthorByName = `-- name: GetAuthorByName :one
SELECT id, name, bio FROM authors
WHERE name = ?
`

func (q *Queries) GetAuthorByName(ctx context.Context, name string) (Author, error) {
	rows, err := q.db.QueryContext(ctx, getAuthorByName, name)
	if err != nil {
		return Author{}, err
	}
	defer rows.Close()
	if !rows.Next() {
		if err := rows.Err(); err != nil {
			return Author{}, err
		}
		return Author{}, sql.ErrNoRows
	}
	var i Author
	if err := rows.Scan(&i.ID, &i.Name, &i.Bio); err != nil {
		return Author{}, err
	}
	if rows.Next() {
		return Author{}, fmt.Errorf("multiple records were retrieved when the following query was executed: %q", getAuthorByName)
	}
	if err := rows.Close(); err != nil {
		return Author{}, err
	}
	if err := rows.Err(); err != nil {
		return Author{}, err
	}
	return i, err

}

Install

deb:

$ export SQLC_GO_ONE_OE_FAIL_VERSION=X.X.X
$ curl -o sqlc-go-one-or-fail.deb -L https://github.com/k1LoW/sqlc-go-one-or-fail/releases/download/v$SQLC_GO_ONE_OE_FAIL_VERSION/sqlc-go-one-or-fail_$SQLC_GO_ONE_OE_FAIL_VERSION-1_amd64.deb
$ dpkg -i sqlc-go-one-or-fail.deb

RPM:

$ export SQLC_GO_ONE_OE_FAIL_VERSION=X.X.X
$ yum install https://github.com/k1LoW/sqlc-go-one-or-fail/releases/download/v$SQLC_GO_ONE_OE_FAIL_VERSION/sqlc-go-one-or-fail_$SQLC_GO_ONE_OE_FAIL_VERSION-1_amd64.rpm

apk:

$ export SQLC_GO_ONE_OE_FAIL_VERSION=X.X.X
$ curl -o sqlc-go-one-or-fail.apk -L https://github.com/k1LoW/sqlc-go-one-or-fail/releases/download/v$SQLC_GO_ONE_OE_FAIL_VERSION/runn_$SQLC_GO_ONE_OE_FAIL_VERSION-1_amd64.apk
$ apk add sqlc-go-one-or-fail.apk

homebrew tap:

$ brew install k1LoW/tap/sqlc-go-one-or-fail

manually:

Download binary from releases page

go install:

$ go install github.com/k1LoW/sqlc-go-one-or-fail@latest

Documentation

Overview

Copyright © 2023 Ken'ichiro Oyama <k1lowxb@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL