Giter VIP home page Giter VIP logo

sqlc-go-one-or-fail's Introduction

sqlc-go-one-or-fail

CI Coverage Code to Test Ratio Test Execution Time

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_OR_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_OR_FAIL_VERSION/sqlc-go-one-or-fail_$SQLC_GO_ONE_OR_FAIL_VERSION-1_amd64.deb
$ dpkg -i sqlc-go-one-or-fail.deb

RPM:

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

apk:

$ export SQLC_GO_ONE_OR_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_OR_FAIL_VERSION/runn_$SQLC_GO_ONE_OR_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

sqlc-go-one-or-fail's People

Contributors

github-actions[bot] avatar k1low avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.