Skip to content

Commit 44d04b5

Browse files
authored
Add some real documentation (sqlc-dev#25)
1 parent d16768c commit 44d04b5

File tree

5 files changed

+367
-109
lines changed

5 files changed

+367
-109
lines changed

README.md

Lines changed: 225 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,238 @@
1-
# 🚨 NOT PRODUCTION READY 🚨
1+
> 🚨
2+
> DinoSQL is **very new** and is under rapid development. The command line
3+
> interface and generated code will change. There are currently no guarantees
4+
> around stability or compatibility.
5+
> 🚨
26
3-
DinoSQL is **very new** and in no way production ready. So far it's been tested
4-
in only three (!!) small applications. The interface and command line tool are
5-
under heavy developement and **will change**. The documentation below isn't
6-
even correct!
7+
# Dino: A SQL Compiler
78

8-
## DinoSQL
9+
> And lo, the Great One looked down upon the people and proclaimed:
10+
>
11+
> "SQL is actually pretty great"
912
10-
> SQL is actually pretty great
13+
DinoSQL *generates idiomatic Go code from SQL*. Save yourself the pain of
14+
writing boilerplate `database/sql` code.
1115

12-
DinoSQL is inspired by [PugSQL](https://pugsql.org/) and [HugSQL](https://www.hugsql.org/).
16+
A quick `dinosql generate` turns the following SQL:
1317

14-
### Usage
18+
```sql
19+
CREATE TABLE authors (
20+
id SERIAL PRIMARY KEY,
21+
name text NOT NULL,
22+
bio text
23+
);
1524

25+
-- name: GetAuthor :one
26+
SELECT * FROM authors
27+
WHERE id = $1;
28+
29+
-- name: ListAuthors :many
30+
SELECT * FROM authors
31+
ORDER BY name;
32+
33+
-- name: CreateAuthor :one
34+
INSERT INTO authors (
35+
name, bio
36+
) VALUES (
37+
$1, $2
38+
)
39+
RETURNING *;
40+
41+
-- name: DeleteAuthor :exec
42+
DELETE FROM authors
43+
WHERE id = $1;
1644
```
17-
$ dinosql -h
18-
DinoSQL is a tool for generating Go source code from SQL.
1945

46+
Into Go you'd have to write yourself:
47+
48+
```go
49+
package db
50+
51+
import (
52+
"context"
53+
"database/sql"
54+
)
55+
56+
type Author struct {
57+
ID int
58+
Name string
59+
Bio sql.NullString
60+
}
61+
62+
type dbtx interface {
63+
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
64+
PrepareContext(context.Context, string) (*sql.Stmt, error)
65+
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
66+
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
67+
}
68+
69+
func New(db dbtx) *Queries {
70+
return &Queries{db: db}
71+
}
72+
73+
type Queries struct {
74+
db dbtx
75+
}
76+
77+
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
78+
return &Queries{
79+
db: tx,
80+
}
81+
}
82+
83+
const createAuthor = `-- name: CreateAuthor :one
84+
INSERT INTO authors (
85+
name, bio
86+
) VALUES (
87+
$1, $2
88+
)
89+
RETURNING id, name, bio
90+
`
91+
92+
type CreateAuthorParams struct {
93+
Name string
94+
Bio sql.NullString
95+
}
96+
97+
func (q *Queries) CreateAuthor(ctx context.Context, arg CreateAuthorParams) (Author, error) {
98+
row := q.db.QueryRowContext(ctx, createAuthor, arg.Name, arg.Bio)
99+
var i Author
100+
err := row.Scan(&i.ID, &i.Name, &i.Bio)
101+
return i, err
102+
}
103+
104+
const deleteAuthor = `-- name: DeleteAuthor :exec
105+
DELETE FROM authors
106+
WHERE id = $1
107+
`
108+
109+
func (q *Queries) DeleteAuthor(ctx context.Context, id int) error {
110+
_, err := q.db.ExecContext(ctx, deleteAuthor, id)
111+
return err
112+
}
113+
114+
const getAuthor = `-- name: GetAuthor :one
115+
SELECT id, name, bio FROM authors
116+
WHERE id = $1
117+
`
118+
119+
func (q *Queries) GetAuthor(ctx context.Context, id int) (Author, error) {
120+
row := q.db.QueryRowContext(ctx, getAuthor, id)
121+
var i Author
122+
err := row.Scan(&i.ID, &i.Name, &i.Bio)
123+
return i, err
124+
}
125+
126+
const listAuthors = `-- name: ListAuthors :many
127+
SELECT id, name, bio FROM authors
128+
ORDER BY name
129+
`
130+
131+
func (q *Queries) ListAuthors(ctx context.Context) ([]Author, error) {
132+
rows, err := q.db.QueryContext(ctx, listAuthors)
133+
if err != nil {
134+
return nil, err
135+
}
136+
defer rows.Close()
137+
var items []Author
138+
for rows.Next() {
139+
var i Author
140+
if err := rows.Scan(&i.ID, &i.Name, &i.Bio); err != nil {
141+
return nil, err
142+
}
143+
items = append(items, i)
144+
}
145+
if err := rows.Close(); err != nil {
146+
return nil, err
147+
}
148+
if err := rows.Err(); err != nil {
149+
return nil, err
150+
}
151+
return items, nil
152+
}
153+
```
154+
155+
## Limitations
156+
157+
DinoSQL currently only supports PostgreSQL. There are no plans to add support
158+
for other databases.
159+
160+
## Examples
161+
162+
Your favorite PostgreSQL / Go features are supported:
163+
- SQL
164+
- [SELECT](./examples/query_one.md)
165+
- [NULL](./examples/null.md)
166+
- [COUNT](./examples/query_count.md)
167+
- [INSERT](./examples/insert.md)
168+
- [UPDATE](./examples/update.md)
169+
- [DELETE](./examples/delete.md)
170+
- [RETURNING](./examples/returning.md)
171+
- [Transactions](./examples/transactions.md)
172+
- [Prepared queries](./examples/prepared_query.md)
173+
- PostgreSQL Types
174+
- [enum](./examples/enums.md)
175+
- [timestamp](./examples/time.md)
176+
- [uuid](./examples/uuid.md)
177+
- DDL
178+
- [CREATE TABLE](./examples/table.md)
179+
- [ALTER TABLE](./examples/alter_table.md)
180+
- Go
181+
- [JSON struct tags](./examples/json_tags.md)
182+
- [Goose migrations](./examples/goose.md)
183+
184+
A full, end-to-end example can be found in the sample
185+
[`ondeck`](./internal/dinosql/testdata/ondeck) package.
186+
187+
## Usage
188+
189+
```
20190
Usage:
191+
dinosql [command]
192+
193+
Available Commands:
194+
compile Statically check SQL for syntax and type errors
195+
generate Generate Go code from SQL
196+
help Help about any command
197+
init Create an empty dinosql.json settings file
198+
version Print the DinoSQL version number
199+
200+
Flags:
201+
-h, --help help for dinosql
202+
203+
Use "dinosql [command] --help" for more information about a command.
204+
```
21205

22-
dinosql <command> [arguments]
206+
## Settings
23207

24-
The commands are:
208+
The `dinosql` tool is configured via a `dinosql.json` file. This file must be
209+
in the directory where the `dinosql` command is run.
25210

26-
check verify SQL
27-
gen generate Go files by processing SQL
211+
```json
212+
{
213+
"package": "db",
214+
"emit_json_tags": true,
215+
"emit_prepared_queries": false,
216+
"out": "internal/db/db.go",
217+
"queries": "./sql/query/",
218+
"schema": "./sql/schema/"
219+
}
28220
```
221+
222+
- `package`:
223+
- The package name to use for the generated code
224+
- `emit_json_tags`:
225+
- If true, add JSON tags to generated structs. Defaults to `false`.
226+
- `emit_prepared_queries`:
227+
- If true, include support for prepared queries. Defaults to `false`.
228+
- `out`:
229+
- Filename for generated code
230+
- `queries`:
231+
- Directory of SQL queries stored in `.sql` files
232+
- `schema`:
233+
- Directory of SQL migrations, stored in `.sql` files
234+
235+
## Acknowledgements
236+
237+
DinoSQL was inspired by [PugSQL](https://pugsql.org/) and
238+
[HugSQL](https://www.hugsql.org/).

examples/query_column.md

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)