diff --git a/example-sqlc/authors/mysql/db_test.go b/example-sqlc/authors/mysql/db_test.go new file mode 100644 index 0000000..08c1122 --- /dev/null +++ b/example-sqlc/authors/mysql/db_test.go @@ -0,0 +1,47 @@ +// +build examples + +package authors + +import ( + "context" + "database/sql" + "testing" + + "github.com/kyleconroy/sqlc/internal/sqltest" +) + +func TestAuthors(t *testing.T) { + sdb, cleanup := sqltest.MySQL(t, []string{"schema.sql"}) + defer cleanup() + + ctx := context.Background() + db := New(sdb) + + // list all authors + authors, err := db.ListAuthors(ctx) + if err != nil { + t.Fatal(err) + } + t.Log(authors) + + // create an author + result, err := db.CreateAuthor(ctx, CreateAuthorParams{ + Name: "Brian Kernighan", + Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true}, + }) + if err != nil { + t.Fatal(err) + } + authorID, err := result.LastInsertId() + if err != nil { + t.Fatal(err) + } + t.Log(authorID) + + // get the author we just inserted + fetchedAuthor, err := db.GetAuthor(ctx, authorID) + if err != nil { + t.Fatal(err) + } + t.Log(fetchedAuthor) +} diff --git a/example-sqlc/authors/mysql/query.sql b/example-sqlc/authors/mysql/query.sql new file mode 100644 index 0000000..c3b5866 --- /dev/null +++ b/example-sqlc/authors/mysql/query.sql @@ -0,0 +1,18 @@ +/* name: GetAuthor :one */ +SELECT * FROM authors +WHERE id = ? LIMIT 1; + +/* name: ListAuthors :many */ +SELECT * FROM authors +ORDER BY name; + +/* name: CreateAuthor :execresult */ +INSERT INTO authors ( + name, bio +) VALUES ( + ?, ? +); + +/* name: DeleteAuthor :exec */ +DELETE FROM authors +WHERE id = ?; diff --git a/example-sqlc/authors/mysql/schema.sql b/example-sqlc/authors/mysql/schema.sql new file mode 100644 index 0000000..581ecfe --- /dev/null +++ b/example-sqlc/authors/mysql/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE authors ( + id BIGINT PRIMARY KEY AUTO_INCREMENT, + name text NOT NULL, + bio text +); diff --git a/example-sqlc/authors/postgresql/db_test.go b/example-sqlc/authors/postgresql/db_test.go new file mode 100644 index 0000000..45f3188 --- /dev/null +++ b/example-sqlc/authors/postgresql/db_test.go @@ -0,0 +1,43 @@ +// +build examples + +package authors + +import ( + "context" + "database/sql" + "testing" + + "github.com/kyleconroy/sqlc/internal/sqltest" +) + +func TestAuthors(t *testing.T) { + sdb, cleanup := sqltest.PostgreSQL(t, []string{"schema.sql"}) + defer cleanup() + + ctx := context.Background() + db := New(sdb) + + // list all authors + authors, err := db.ListAuthors(ctx) + if err != nil { + t.Fatal(err) + } + t.Log(authors) + + // create an author + insertedAuthor, err := db.CreateAuthor(ctx, CreateAuthorParams{ + Name: "Brian Kernighan", + Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true}, + }) + if err != nil { + t.Fatal(err) + } + t.Log(insertedAuthor) + + // get the author we just inserted + fetchedAuthor, err := db.GetAuthor(ctx, insertedAuthor.ID) + if err != nil { + t.Fatal(err) + } + t.Log(fetchedAuthor) +} diff --git a/example-sqlc/authors/postgresql/query.sql b/example-sqlc/authors/postgresql/query.sql new file mode 100644 index 0000000..75e38b2 --- /dev/null +++ b/example-sqlc/authors/postgresql/query.sql @@ -0,0 +1,19 @@ +-- name: GetAuthor :one +SELECT * FROM authors +WHERE id = $1 LIMIT 1; + +-- name: ListAuthors :many +SELECT * FROM authors +ORDER BY name; + +-- name: CreateAuthor :one +INSERT INTO authors ( + name, bio +) VALUES ( + $1, $2 +) +RETURNING *; + +-- name: DeleteAuthor :exec +DELETE FROM authors +WHERE id = $1; diff --git a/example-sqlc/authors/postgresql/schema.sql b/example-sqlc/authors/postgresql/schema.sql new file mode 100644 index 0000000..b4fad78 --- /dev/null +++ b/example-sqlc/authors/postgresql/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE authors ( + id BIGSERIAL PRIMARY KEY, + name text NOT NULL, + bio text +); diff --git a/example-sqlc/authors/sqlc.json b/example-sqlc/authors/sqlc.json new file mode 100644 index 0000000..58feec2 --- /dev/null +++ b/example-sqlc/authors/sqlc.json @@ -0,0 +1,27 @@ +{ + "version": "2", + "sql": [ + { + "schema": "postgresql/schema.sql", + "queries": "postgresql/query.sql", + "engine": "postgresql", + "gen": { + "go": { + "package": "authors", + "out": "postgresql" + } + } + }, + { + "schema": "mysql/schema.sql", + "queries": "mysql/query.sql", + "engine": "mysql", + "gen": { + "go": { + "package": "authors", + "out": "mysql" + } + } + } + ] +}