Skip to content

Instantly share code, notes, and snippets.

@Streppel
Last active May 28, 2021 22:32
Show Gist options
  • Save Streppel/f1a441b08a225508b8933a13c184097b to your computer and use it in GitHub Desktop.
Save Streppel/f1a441b08a225508b8933a13c184097b to your computer and use it in GitHub Desktop.
// db.go
// Code generated by sqlc. DO NOT EDIT.
package db
import (
"context"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}
}
// models.go
// Code generated by sqlc. DO NOT EDIT.
package db
import (
"database/sql"
)
type Author struct {
ID int64 `json:"id"`
Name string `json:"name"`
Bio sql.NullString `json:"bio"`
Amigos []string `json:"amigos"`
}
// schema.sql.go
// Code generated by sqlc. DO NOT EDIT.
// source: schema.sql
package db
import (
"context"
"database/sql"
"github.com/jackc/pgconn"
)
const createAuthor = `-- name: CreateAuthor :execresult
INSERT INTO authors (name, bio, amigos)
VALUES ($1, $2, $3)
`
type CreateAuthorParams struct {
Name string `json:"name"`
Bio sql.NullString `json:"bio"`
Amigos []string `json:"amigos"`
}
func (q *Queries) CreateAuthor(ctx context.Context, arg CreateAuthorParams) (pgconn.CommandTag, error) {
return q.db.Exec(ctx, q.createAuthorStmt, createAuthor, arg.Name, arg.Bio, arg.Amigos)
}
const deleteAuthor = `-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1
`
func (q *Queries) DeleteAuthor(ctx context.Context, id int64) error {
_, err := q.db.Exec(ctx, deleteAuthor, id)
return err
}
const getAmigosFromID = `-- name: GetAmigosFromID :many
SELECT id, name, bio, amigos from authors where amigos = ANY($1)
`
func (q *Queries) GetAmigosFromID(ctx context.Context, amigos []string) ([]Author, error) {
rows, err := q.db.Query(ctx, getAmigosFromID, amigos)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Author
for rows.Next() {
var i Author
if err := rows.Scan(
&i.ID,
&i.Name,
&i.Bio,
&i.Amigos,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getAuthor = `-- name: GetAuthor :one
SELECT id, name, bio, amigos FROM authors
WHERE id = $1 LIMIT 1
`
func (q *Queries) GetAuthor(ctx context.Context, id int64) (Author, error) {
row := q.db.QueryRow(ctx, getAuthor, id)
var i Author
err := row.Scan(
&i.ID,
&i.Name,
&i.Bio,
&i.Amigos,
)
return i, err
}
const listAuthors = `-- name: ListAuthors :many
SELECT id, name, bio, amigos FROM authors
ORDER BY name
`
func (q *Queries) ListAuthors(ctx context.Context) ([]Author, error) {
rows, err := q.db.Query(ctx, listAuthors)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Author
for rows.Next() {
var i Author
if err := rows.Scan(
&i.ID,
&i.Name,
&i.Bio,
&i.Amigos,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment