Insert and Update statement improvements.
This commit is contained in:
parent
038a4b9dd0
commit
a4feb66692
22 changed files with 660 additions and 453 deletions
|
|
@ -4,80 +4,261 @@ import (
|
|||
"fmt"
|
||||
. "github.com/go-jet/jet/sqlbuilder"
|
||||
"github.com/go-jet/jet/tests/.test_files/dvd_rental/test_sample/model"
|
||||
"github.com/go-jet/jet/tests/.test_files/dvd_rental/test_sample/table"
|
||||
. "github.com/go-jet/jet/tests/.test_files/dvd_rental/test_sample/table"
|
||||
"gotest.tools/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUpdateValues(t *testing.T) {
|
||||
_, err := table.Link.INSERT(table.Link.URL, table.Link.Name, table.Link.Rel).
|
||||
VALUES("http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
VALUES("http://www.yahoo.com", "Yahoo", DEFAULT).
|
||||
VALUES("http://www.bing.com", "Bing", DEFAULT).
|
||||
RETURNING(table.Link.ID).Execute(db)
|
||||
setupLinkTableForUpdateTest(t)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
query := table.Link.
|
||||
UPDATE(table.Link.Name, table.Link.URL).
|
||||
query := Link.
|
||||
UPDATE(Link.Name, Link.URL).
|
||||
SET("Bong", "http://bong.com").
|
||||
WHERE(table.Link.Name.EQ(String("Bing")))
|
||||
WHERE(Link.Name.EQ(String("Bing")))
|
||||
|
||||
queryStr, args, err := query.Sql()
|
||||
fmt.Println(query.DebugSql())
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(args), 3)
|
||||
fmt.Println(queryStr)
|
||||
var expectedSql = `
|
||||
UPDATE test_sample.link
|
||||
SET (name, url) = ('Bong', 'http://bong.com')
|
||||
WHERE link.name = 'Bing';
|
||||
`
|
||||
fmt.Println(query.Sql())
|
||||
|
||||
res, err := query.Execute(db)
|
||||
assertStatementSql(t, query, expectedSql, "Bong", "http://bong.com", "Bing")
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
fmt.Println(res)
|
||||
assertExec(t, query, 1)
|
||||
|
||||
links := []model.Link{}
|
||||
|
||||
err = table.Link.SELECT(table.Link.AllColumns).
|
||||
WHERE(table.Link.Name.EQ(String("Bong"))).
|
||||
err := Link.
|
||||
SELECT(Link.AllColumns).
|
||||
WHERE(Link.Name.EQ(String("Bong"))).
|
||||
Query(db, &links)
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(links), 1)
|
||||
assert.DeepEqual(t, links[0], model.Link{
|
||||
ID: 204,
|
||||
URL: "http://bong.com",
|
||||
Name: "Bong",
|
||||
})
|
||||
}
|
||||
|
||||
//spew.Dump(links)
|
||||
func TestUpdateWithSubQueries(t *testing.T) {
|
||||
setupLinkTableForUpdateTest(t)
|
||||
|
||||
query := Link.
|
||||
UPDATE(Link.Name, Link.URL).
|
||||
SET(
|
||||
SELECT(String("Bong")),
|
||||
SELECT(Link.URL).
|
||||
FROM(Link).
|
||||
WHERE(Link.Name.EQ(String("Bing"))),
|
||||
).
|
||||
WHERE(Link.Name.EQ(String("Bing")))
|
||||
|
||||
expectedSql := `
|
||||
UPDATE test_sample.link
|
||||
SET (name, url) = ((
|
||||
SELECT 'Bong'
|
||||
), (
|
||||
SELECT link.url AS "link.url"
|
||||
FROM test_sample.link
|
||||
WHERE link.name = 'Bing'
|
||||
))
|
||||
WHERE link.name = 'Bing';
|
||||
`
|
||||
|
||||
assertStatementSql(t, query, expectedSql, "Bong", "Bing", "Bing")
|
||||
|
||||
assertExec(t, query, 1)
|
||||
}
|
||||
|
||||
func TestUpdateAndReturning(t *testing.T) {
|
||||
_, err := table.Link.INSERT(table.Link.URL, table.Link.Name, table.Link.Rel).
|
||||
VALUES("http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
VALUES("http://www.ask.com", "Ask", DEFAULT).
|
||||
VALUES("http://www.ask.com", "Ask", DEFAULT).
|
||||
VALUES("http://www.yahoo.com", "Yahoo", DEFAULT).
|
||||
VALUES("http://www.bing.com", "Bing", DEFAULT).
|
||||
RETURNING(table.Link.ID).Execute(db)
|
||||
setupLinkTableForUpdateTest(t)
|
||||
|
||||
assert.NilError(t, err)
|
||||
expectedSql := `
|
||||
UPDATE test_sample.link
|
||||
SET (name, url) = ('DuckDuckGo', 'http://www.duckduckgo.com')
|
||||
WHERE link.name = 'Ask'
|
||||
RETURNING link.id AS "link.id",
|
||||
link.url AS "link.url",
|
||||
link.name AS "link.name",
|
||||
link.description AS "link.description",
|
||||
link.rel AS "link.rel";
|
||||
`
|
||||
|
||||
stmt := table.Link.
|
||||
UPDATE(table.Link.Name, table.Link.URL).
|
||||
stmt := Link.
|
||||
UPDATE(Link.Name, Link.URL).
|
||||
SET("DuckDuckGo", "http://www.duckduckgo.com").
|
||||
WHERE(table.Link.Name.EQ(String("Ask"))).
|
||||
RETURNING(table.Link.AllColumns)
|
||||
WHERE(Link.Name.EQ(String("Ask"))).
|
||||
RETURNING(Link.AllColumns)
|
||||
|
||||
stmtStr, args, err := stmt.Sql()
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(args), 3)
|
||||
fmt.Println(stmtStr)
|
||||
assertStatementSql(t, stmt, expectedSql, "DuckDuckGo", "http://www.duckduckgo.com", "Ask")
|
||||
|
||||
links := []model.Link{}
|
||||
|
||||
err = stmt.Query(db, &links)
|
||||
err := stmt.Query(db, &links)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.Equal(t, len(links), 2)
|
||||
|
||||
assert.Equal(t, links[0].Name, "DuckDuckGo")
|
||||
|
||||
assert.Equal(t, links[1].Name, "DuckDuckGo")
|
||||
}
|
||||
|
||||
func TestUpdateWithSelect(t *testing.T) {
|
||||
|
||||
stmt := Link.UPDATE(Link.AllColumns).
|
||||
SET(
|
||||
Link.
|
||||
SELECT(Link.AllColumns).
|
||||
WHERE(Link.ID.EQ(Int(0))),
|
||||
).
|
||||
WHERE(Link.ID.EQ(Int(0)))
|
||||
|
||||
expectedSql := `
|
||||
UPDATE test_sample.link
|
||||
SET (id, url, name, description, rel) = (
|
||||
SELECT link.id AS "link.id",
|
||||
link.url AS "link.url",
|
||||
link.name AS "link.name",
|
||||
link.description AS "link.description",
|
||||
link.rel AS "link.rel"
|
||||
FROM test_sample.link
|
||||
WHERE link.id = 0
|
||||
)
|
||||
WHERE link.id = 0;
|
||||
`
|
||||
assertStatementSql(t, stmt, expectedSql, int64(0), int64(0))
|
||||
|
||||
assertExec(t, stmt, 1)
|
||||
}
|
||||
|
||||
func TestUpdateWithInvalidSelect(t *testing.T) {
|
||||
|
||||
stmt := Link.UPDATE(Link.AllColumns).
|
||||
SET(
|
||||
Link.
|
||||
SELECT(Link.ID, Link.Name).
|
||||
WHERE(Link.ID.EQ(Int(0))),
|
||||
).
|
||||
WHERE(Link.ID.EQ(Int(0)))
|
||||
|
||||
var expectedSql = `
|
||||
UPDATE test_sample.link
|
||||
SET (id, url, name, description, rel) = (
|
||||
SELECT link.id AS "link.id",
|
||||
link.name AS "link.name"
|
||||
FROM test_sample.link
|
||||
WHERE link.id = 0
|
||||
)
|
||||
WHERE link.id = 0;
|
||||
`
|
||||
assertStatementSql(t, stmt, expectedSql, int64(0), int64(0))
|
||||
|
||||
assertExecErr(t, stmt, "pq: number of columns does not match number of values")
|
||||
}
|
||||
|
||||
func TestUpdateWithModelData(t *testing.T) {
|
||||
setupLinkTableForUpdateTest(t)
|
||||
|
||||
link := model.Link{
|
||||
ID: 201,
|
||||
URL: "http://www.duckduckgo.com",
|
||||
Name: "DuckDuckGo",
|
||||
}
|
||||
|
||||
stmt := Link.
|
||||
UPDATE(Link.AllColumns).
|
||||
USING(link).
|
||||
WHERE(Link.ID.EQ(Int(int64(link.ID))))
|
||||
|
||||
expectedSql := `
|
||||
UPDATE test_sample.link
|
||||
SET (id, url, name, description, rel) = (201, 'http://www.duckduckgo.com', 'DuckDuckGo', NULL, NULL)
|
||||
WHERE link.id = 201;
|
||||
`
|
||||
assertStatementSql(t, stmt, expectedSql, int32(201), "http://www.duckduckgo.com", "DuckDuckGo", nil, nil, int64(201))
|
||||
|
||||
assertExec(t, stmt, 1)
|
||||
}
|
||||
|
||||
func TestUpdateWithModelDataAndPredefinedColumnList(t *testing.T) {
|
||||
|
||||
setupLinkTableForUpdateTest(t)
|
||||
|
||||
link := model.Link{
|
||||
ID: 201,
|
||||
URL: "http://www.duckduckgo.com",
|
||||
Name: "DuckDuckGo",
|
||||
}
|
||||
|
||||
updateColumnList := ColumnList{Link.Rel, Link.Name, Link.URL}
|
||||
|
||||
stmt := Link.
|
||||
UPDATE(updateColumnList).
|
||||
USING(link).
|
||||
WHERE(Link.ID.EQ(Int(int64(link.ID))))
|
||||
|
||||
var expectedSql = `
|
||||
UPDATE test_sample.link
|
||||
SET (rel, name, url) = (NULL, 'DuckDuckGo', 'http://www.duckduckgo.com')
|
||||
WHERE link.id = 201;
|
||||
`
|
||||
assertStatementSql(t, stmt, expectedSql, nil, "DuckDuckGo", "http://www.duckduckgo.com", int64(201))
|
||||
|
||||
assertExec(t, stmt, 1)
|
||||
}
|
||||
|
||||
func TestUpdateWithInvalidModelData(t *testing.T) {
|
||||
|
||||
setupLinkTableForUpdateTest(t)
|
||||
|
||||
link := struct {
|
||||
Ident int
|
||||
URL string
|
||||
Name string
|
||||
Description *string
|
||||
Rel *string
|
||||
}{
|
||||
Ident: 201,
|
||||
URL: "http://www.duckduckgo.com",
|
||||
Name: "DuckDuckGo",
|
||||
}
|
||||
|
||||
stmt := Link.
|
||||
UPDATE(Link.AllColumns).
|
||||
USING(link).
|
||||
WHERE(Link.ID.EQ(Int(int64(link.Ident))))
|
||||
|
||||
var expectedSql = `
|
||||
UPDATE test_sample.link
|
||||
SET (id, url, name, description, rel) = ('http://www.duckduckgo.com', 'DuckDuckGo', NULL, NULL)
|
||||
WHERE link.id = 201;
|
||||
`
|
||||
assertStatementSql(t, stmt, expectedSql, "http://www.duckduckgo.com", "DuckDuckGo", nil, nil, int64(201))
|
||||
|
||||
assertExecErr(t, stmt, "pq: number of columns does not match number of values")
|
||||
}
|
||||
|
||||
func setupLinkTableForUpdateTest(t *testing.T) {
|
||||
|
||||
cleanUpLinkTable(t)
|
||||
|
||||
_, err := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Rel).
|
||||
VALUES(200, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
VALUES(201, "http://www.ask.com", "Ask", DEFAULT).
|
||||
VALUES(202, "http://www.ask.com", "Ask", DEFAULT).
|
||||
VALUES(203, "http://www.yahoo.com", "Yahoo", DEFAULT).
|
||||
VALUES(204, "http://www.bing.com", "Bing", DEFAULT).
|
||||
Exec(db)
|
||||
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
||||
func cleanUpLinkTable(t *testing.T) {
|
||||
_, err := Link.DELETE().WHERE(Link.ID.GT(Int(0))).Exec(db)
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue