Skip to content

Instantly share code, notes, and snippets.

@johanbrandhorst
Last active August 29, 2021 16:25
Show Gist options
  • Save johanbrandhorst/85d0c0db77cd42779c29e21aa3fd5137 to your computer and use it in GitHub Desktop.
Save johanbrandhorst/85d0c0db77cd42779c29e21aa3fd5137 to your computer and use it in GitHub Desktop.
Missing nullable enums in sqlc v1.9.0
//// Code generated by sqlc. DO NOT EDIT.
package tutorial
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}
// Code generated by sqlc. DO NOT EDIT.
package tutorial
import (
"database/sql"
"fmt"
)
type BookType string
const (
BookTypeHardback BookType = "hardback"
BookTypeSoftback BookType = "softback"
BookTypeEbook BookType = "ebook"
)
func (e *BookType) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = BookType(s)
case string:
*e = BookType(s)
default:
return fmt.Errorf("unsupported scan type for BookType: %T", src)
}
return nil
}
type Author struct {
ID int64
Name string
}
type Book struct {
ID int64
Title string
BookType BookType
AuthorID sql.NullInt64
}
-- name: ListBooks :many
SELECT authors.name, books.title, books.book_type FROM authors
LEFT JOIN books ON authors.id = books.author_id;
// Code generated by sqlc. DO NOT EDIT.
// source: query.sql
package tutorial
import (
"context"
"database/sql"
)
const listBooks = `-- name: ListBooks :many
SELECT authors.name, books.title, books.book_type FROM authors
LEFT JOIN books ON authors.id = books.author_id
`
type ListBooksRow struct {
Name string
Title sql.NullString
BookType BookType
}
func (q *Queries) ListBooks(ctx context.Context) ([]ListBooksRow, error) {
rows, err := q.db.QueryContext(ctx, listBooks)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListBooksRow
for rows.Next() {
var i ListBooksRow
if err := rows.Scan(&i.Name, &i.Title, &i.BookType); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TYPE book_type AS ENUM ('hardback', 'softback', 'ebook');
CREATE TABLE books (
id BIGSERIAL PRIMARY KEY,
title TEXT NOT NULL,
book_type book_type NOT NULL,
author_id BIGINT NULL REFERENCES authors(id)
);
version: 1
packages:
- path: "tutorial"
name: "tutorial"
engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment