-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
566 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//go:build examples | ||
// +build examples | ||
|
||
package update | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/kyleconroy/sqlc/internal/sqltest" | ||
) | ||
|
||
func TestUpdate(t *testing.T) { | ||
sdb, cleanup := sqltest.MySQL(t, []string{"schema.sql"}) | ||
defer cleanup() | ||
|
||
ctx := context.Background() | ||
db := New(sdb) | ||
|
||
_, err := db.CreateT1(ctx, CreateT1Params{ | ||
UserID: int32(2), | ||
Name: "", | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// get the data we just inserted | ||
oldData, err := db.GetT1(ctx, int32(2)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if oldData.Name != "" { | ||
t.Fatal("create fail") | ||
} | ||
|
||
_, err = db.CreateT2(ctx, CreateT2Params{ | ||
Email: "[email protected]", | ||
Name: "test", | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = db.CreateT3(ctx, CreateT3Params{ | ||
UserID: int32(2), | ||
Email: "[email protected]", | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = db.UpdateAll(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
newData, err := db.GetT1(ctx, int32(2)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if newData.Name != "test" { | ||
t.Fatal("update fail") | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* name: CreateT1 :execresult */ | ||
INSERT INTO | ||
t1 (user_id, name) | ||
VALUES | ||
(?, ?); | ||
|
||
/* name: CreateT2 :execresult */ | ||
INSERT INTO | ||
t2 (email, name) | ||
VALUES | ||
(?, ?); | ||
|
||
/* name: CreateT3 :execresult */ | ||
INSERT INTO | ||
t3 (user_id, email) | ||
VALUES | ||
(?, ?); | ||
|
||
/* name: UpdateAll :exec */ | ||
UPDATE | ||
t1 | ||
INNER JOIN t3 ON t3.user_id = t1.user_id | ||
INNER JOIN t2 ON t2.email = t3.email | ||
SET | ||
t1.name = t2.name | ||
WHERE | ||
t1.name = ''; | ||
|
||
/* name: GetT1 :one */ | ||
SELECT | ||
* | ||
FROM | ||
t1 | ||
WHERE | ||
user_id = ? | ||
LIMIT | ||
1; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CREATE TABLE t1 ( | ||
user_id int NOT NULL, | ||
name varchar(255) NOT NULL | ||
); | ||
|
||
CREATE TABLE t2 ( | ||
email varchar(255) NOT NULL, | ||
name varchar(255) NOT NULL | ||
); | ||
|
||
CREATE TABLE t3 ( | ||
user_id int NOT NULL, | ||
email varchar(255) NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"version": "2", | ||
"sql": [ | ||
{ | ||
"schema": "withparams/schema.sql", | ||
"queries": "withparams/query.sql", | ||
"engine": "mysql", | ||
"gen": { | ||
"go": { | ||
"package": "update", | ||
"out": "withparams" | ||
} | ||
} | ||
}, | ||
{ | ||
"schema": "noparams/schema.sql", | ||
"queries": "noparams/query.sql", | ||
"engine": "mysql", | ||
"gen": { | ||
"go": { | ||
"package": "update", | ||
"out": "noparams" | ||
} | ||
} | ||
} | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//go:build examples | ||
// +build examples | ||
|
||
package update | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/kyleconroy/sqlc/internal/sqltest" | ||
) | ||
|
||
func TestAuthor(t *testing.T) { | ||
sdb, cleanup := sqltest.MySQL(t, []string{"schema.sql"}) | ||
defer cleanup() | ||
|
||
ctx := context.Background() | ||
db := New(sdb) | ||
|
||
// create an author | ||
result, err := db.CreateAuthor(ctx, CreateAuthorParams{ | ||
Name: "Brian Kernighan", | ||
DeletedAt: time.Now(), | ||
UpdatedAt: time.Now(), | ||
}) | ||
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) | ||
} | ||
|
||
// create a book | ||
_, err = db.CreateBook(ctx, true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = db.DeleteAuthor(ctx, "Brian Kernighan") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// get the author we just inserted | ||
newFetchedAuthor, err := db.GetAuthor(ctx, authorID) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Log(fetchedAuthor) | ||
if newFetchedAuthor.DeletedAt.Unix() != fetchedAuthor.DeletedAt.Unix() && newFetchedAuthor.DeletedAt.Unix() != newFetchedAuthor.UpdatedAt.Unix() { | ||
t.Fatal("update fail") | ||
} | ||
} |
Oops, something went wrong.