Skip to content

Commit 0dc2c81

Browse files
authored
feat: More SQL Syntax Support for SQLite (#1687)
* add test helper for SQLite * [sqlite] support simple insert and select where clause * [sqlite] Improve simple select syntax support * Add support `where` and `join` clause * Add support table alias * [sqlite] add support simple update statement * add booktest example for SQLite * [sqlite] support add column with NOT NULL constraint * add ondeck example for SQLite * regenerate example by sqlc 1.14 * [sqlite] add endtoend test for alias * [sqlite] support coalesce function * [sqlite] support 'bool' type * [sqlite] support create/drop view * add endtoend test for SQLite * column_as * comment_syntax * comparisons * create_view * [sqlite] update datatype mapping * [sqlite] support insert_select * add endtoend test for SQLite * identical_tables * inflection * insert_select * insert_select_invalid * insert_values * [sqlite] better select statement support * support `GROUP BY` and `HAVING` * support `ORDER BY` * support wildcard(`*`) column with table name * add endtoend test for SQLite * invalid_group_by_reference * invalid_table_alias * join_alias * join_from * join_left * join_left_same_table * join_table_name * join_two_tables * join_where_clause * [sqlite] support LIMIT and OFFSET * add endtoend test for SQLite * limit * limit_offset * add endtoend test for SQLite * select_limit * limit_offset * single_param_conflict * update_set * update_set_multiple * [sqlite] support nested SELECT
1 parent dff26be commit 0dc2c81

File tree

175 files changed

+5868
-188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+5868
-188
lines changed

examples/authors/sqlc.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@
2222
"out": "mysql"
2323
}
2424
}
25+
},
26+
{
27+
"schema": "sqlite/schema.sql",
28+
"queries": "sqlite/query.sql",
29+
"engine": "_lemon",
30+
"gen": {
31+
"go": {
32+
"package": "authors",
33+
"out": "sqlite"
34+
}
35+
}
2536
}
2637
]
27-
}
38+
}

examples/authors/sqlite/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/authors/sqlite/db_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//go:build examples
2+
// +build examples
3+
4+
package authors
5+
6+
import (
7+
"context"
8+
"database/sql"
9+
"testing"
10+
11+
"github.com/kyleconroy/sqlc/internal/sqltest"
12+
)
13+
14+
func TestAuthors(t *testing.T) {
15+
sdb, cleanup := sqltest.SQLite(t, []string{"schema.sql"})
16+
defer cleanup()
17+
18+
ctx := context.Background()
19+
db := New(sdb)
20+
21+
// list all authors
22+
authors, err := db.ListAuthors(ctx)
23+
if err != nil {
24+
t.Fatal(err)
25+
}
26+
t.Log(authors)
27+
28+
// create an author
29+
result, err := db.CreateAuthor(ctx, CreateAuthorParams{
30+
Name: "Brian Kernighan",
31+
Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
32+
})
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
authorID, err := result.LastInsertId()
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
t.Log(authorID)
41+
42+
// get the author we just inserted
43+
fetchedAuthor, err := db.GetAuthor(ctx, authorID)
44+
if err != nil {
45+
t.Fatal(err)
46+
}
47+
t.Log(fetchedAuthor)
48+
}

examples/authors/sqlite/models.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/authors/sqlite/query.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* name: GetAuthor :one */
2+
SELECT * FROM authors
3+
WHERE id = ? LIMIT 1;
4+
5+
/* name: ListAuthors :many */
6+
SELECT * FROM authors
7+
ORDER BY name;
8+
9+
/* name: CreateAuthor :execresult */
10+
INSERT INTO authors (
11+
name, bio
12+
) VALUES (
13+
?, ?
14+
);
15+
16+
/* name: DeleteAuthor :exec */
17+
DELETE FROM authors
18+
WHERE id = ?;

examples/authors/sqlite/query.sql.go

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/authors/sqlite/schema.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE authors (
2+
id integer PRIMARY KEY AUTOINCREMENT,
3+
name text NOT NULL,
4+
bio text
5+
);

examples/booktest/sqlc.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
"schema": "mysql/schema.sql",
1515
"queries": "mysql/query.sql",
1616
"engine": "mysql"
17+
},
18+
{
19+
"name": "booktest",
20+
"path": "sqlite",
21+
"schema": "sqlite/schema.sql",
22+
"queries": "sqlite/query.sql",
23+
"engine": "_lemon"
1724
}
1825
]
19-
}
26+
}

examples/booktest/sqlite/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)