Skip to content

Commit

Permalink
Add complete example
Browse files Browse the repository at this point in the history
  • Loading branch information
pablojimpas committed Jul 18, 2024
1 parent 3c858b5 commit 817e170
Show file tree
Hide file tree
Showing 13 changed files with 644 additions and 7 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
examples/library/internal/gen
tmp

# Binaries for programs and plugins
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ sqlc: protoc

## push: push changes to the remote Git repository
.PHONY: push
push: clean audit test/cover no-dirty
push: clean sqlc audit test/cover no-dirty
git push

## clean: clean all generated artifacts
Expand Down
9 changes: 9 additions & 0 deletions examples/library/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
exampledb:
image: postgres
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=examplepassword
- POSTGRES_USER=exampleuser
- POSTGRES_DB=exampledb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- Code generated by protoc-gen-sqlc. DO NOT EDIT.
-- source:
-- examples/library/v1/author.proto
--
-- name: GetAuthor :one
SELECT * FROM Author
WHERE author_id = $1 LIMIT 1;

-- name: ListAuthor :many
SELECT * FROM Author
ORDER BY author_id;

-- name: CreateAuthor :one
INSERT INTO Author (
author_id, name, biography
) VALUES (
$1, $2, $3
)
RETURNING *;

-- name: UpdateAuthor :one
UPDATE Author SET
name = $2,
biography = $3
WHERE author_id = $1
RETURNING *;

-- name: DeleteAuthor :exec
DELETE FROM Author
WHERE author_id = $1;
37 changes: 37 additions & 0 deletions examples/library/internal/gen/pb/sqlc/examples/library/v1/book.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- Code generated by protoc-gen-sqlc. DO NOT EDIT.
-- source:
-- examples/library/v1/book.proto
--
-- name: GetBook :one
SELECT * FROM Book
WHERE book_id = $1 LIMIT 1;

-- name: ListBook :many
SELECT * FROM Book
ORDER BY book_id;

-- name: CreateBook :one
INSERT INTO Book (
book_id, author_id, isbn, book_type, title, year, available_time, tags, published, price
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
)
RETURNING *;

-- name: UpdateBook :one
UPDATE Book SET
author_id = $2,
isbn = $3,
book_type = $4,
title = $5,
year = $6,
available_time = $7,
tags = $8,
published = $9,
price = $10
WHERE book_id = $1
RETURNING *;

-- name: DeleteBook :exec
DELETE FROM Book
WHERE book_id = $1;
33 changes: 33 additions & 0 deletions examples/library/internal/gen/pb/sqlc/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- Code generated by protoc-gen-sqlc. DO NOT EDIT.
-- source:
--

CREATE TYPE BookType AS ENUM (
'BOOK_TYPE_UNSPECIFIED',
'BOOK_TYPE_FICTION',
'BOOK_TYPE_NONFICTION'
);

CREATE TABLE Author (
author_id INTEGER NOT NULL,
name TEXT NOT NULL DEFAULT 'Anonymous',
biography JSONB,
PRIMARY KEY(author_id)
);

CREATE TABLE Book (
book_id INTEGER NOT NULL,
author_id INTEGER NOT NULL,
isbn TEXT NOT NULL,
book_type BookType NOT NULL DEFAULT 'BOOK_TYPE_FICTION',
title TEXT NOT NULL DEFAULT 'Unknown',
year INTEGER NOT NULL DEFAULT 2000,
available_time TIMESTAMP NOT NULL DEFAULT 'NOW()',
tags TEXT[] NOT NULL DEFAULT '{}',
published BOOLEAN DEFAULT false,
price FLOAT,
PRIMARY KEY(book_id),
FOREIGN KEY(author_id) REFERENCES Author(author_id) ON DELETE CASCADE,
UNIQUE(isbn)
);

104 changes: 104 additions & 0 deletions examples/library/internal/gen/sqlc/author.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 817e170

Please sign in to comment.