Insert and Update statement improvements.
This commit is contained in:
parent
038a4b9dd0
commit
a4feb66692
22 changed files with 660 additions and 453 deletions
|
|
@ -2,7 +2,6 @@ package tests
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
. "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"
|
||||
|
|
@ -26,16 +25,32 @@ func TestAllTypesSelect(t *testing.T) {
|
|||
assert.DeepEqual(t, dest[1], allTypesRow1)
|
||||
}
|
||||
|
||||
func TestAllTypesInsert(t *testing.T) {
|
||||
query := AllTypes.INSERT(AllTypes.AllColumns...).
|
||||
MODEL(allTypesRow0).
|
||||
MODEL(&allTypesRow1).
|
||||
func TestAllTypesInsertModel(t *testing.T) {
|
||||
query := AllTypes.INSERT(AllTypes.AllColumns).
|
||||
USING(allTypesRow0).
|
||||
USING(&allTypesRow1).
|
||||
RETURNING(AllTypes.AllColumns)
|
||||
|
||||
dest := []model.AllTypes{}
|
||||
err := query.Query(db, &dest)
|
||||
|
||||
spew.Dump(dest[0])
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(dest), 2)
|
||||
assert.DeepEqual(t, dest[0], allTypesRow0)
|
||||
assert.DeepEqual(t, dest[1], allTypesRow1)
|
||||
}
|
||||
|
||||
func TestAllTypesInsertQuery(t *testing.T) {
|
||||
query := AllTypes.INSERT(AllTypes.AllColumns).
|
||||
QUERY(
|
||||
AllTypes.
|
||||
SELECT(AllTypes.AllColumns).
|
||||
LIMIT(2),
|
||||
).
|
||||
RETURNING(AllTypes.AllColumns)
|
||||
|
||||
dest := []model.AllTypes{}
|
||||
err := query.Query(db, &dest)
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(dest), 2)
|
||||
|
|
|
|||
|
|
@ -149,6 +149,9 @@ CREATE TABLE IF NOT EXISTS test_sample.link (
|
|||
rel VARCHAR (50)
|
||||
);
|
||||
|
||||
INSERT INTO test_sample.link (ID, url, name, description) VALUES
|
||||
(0, 'http://www.youtube.com', 'Youtube' , '');
|
||||
|
||||
|
||||
-- Employee table ---------------
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
package tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
. "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"
|
||||
|
|
@ -11,61 +9,79 @@ import (
|
|||
)
|
||||
|
||||
func TestInsertValues(t *testing.T) {
|
||||
insertQuery := Link.INSERT(Link.URL, Link.Name, Link.Rel).
|
||||
VALUES("http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
VALUES("http://www.google.com", "Google", DEFAULT).
|
||||
VALUES("http://www.yahoo.com", "Yahoo", DEFAULT).
|
||||
VALUES("http://www.bing.com", "Bing", DEFAULT).
|
||||
RETURNING(Link.ID)
|
||||
|
||||
insertQueryStr, args, err := insertQuery.Sql()
|
||||
cleanUpLinkTable(t)
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(args), 8)
|
||||
var expectedSql = `
|
||||
INSERT INTO test_sample.link (id, url, name, rel) VALUES
|
||||
(100, 'http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT),
|
||||
(101, 'http://www.google.com', 'Google', DEFAULT),
|
||||
(102, 'http://www.yahoo.com', 'Yahoo', NULL)
|
||||
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";
|
||||
`
|
||||
|
||||
fmt.Println(insertQueryStr)
|
||||
insertQuery := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Rel).
|
||||
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
VALUES(101, "http://www.google.com", "Google", DEFAULT).
|
||||
VALUES(102, "http://www.yahoo.com", "Yahoo", nil).
|
||||
RETURNING(Link.AllColumns)
|
||||
|
||||
assert.Equal(t, insertQueryStr, `
|
||||
INSERT INTO test_sample.link (url,name,rel) VALUES
|
||||
($1, $2, DEFAULT),
|
||||
($3, $4, DEFAULT),
|
||||
($5, $6, DEFAULT),
|
||||
($7, $8, DEFAULT)
|
||||
RETURNING link.id AS "link.id";
|
||||
`)
|
||||
res, err := insertQuery.Execute(db)
|
||||
assertStatementSql(t, insertQuery, expectedSql,
|
||||
100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial",
|
||||
101, "http://www.google.com", "Google",
|
||||
102, "http://www.yahoo.com", "Yahoo", nil)
|
||||
|
||||
insertedLinks := []model.Link{}
|
||||
|
||||
err := insertQuery.Query(db, &insertedLinks)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
rowsAffected, err := res.RowsAffected()
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(insertedLinks), 3)
|
||||
|
||||
assert.Equal(t, rowsAffected, int64(4))
|
||||
|
||||
link := []model.Link{}
|
||||
|
||||
err = Link.SELECT(Link.AllColumns).Query(db, &link)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.Equal(t, len(link), 4)
|
||||
|
||||
assert.DeepEqual(t, link[0], model.Link{
|
||||
ID: 1,
|
||||
assert.DeepEqual(t, insertedLinks[0], model.Link{
|
||||
ID: 100,
|
||||
URL: "http://www.postgresqltutorial.com",
|
||||
Name: "PostgreSQL Tutorial",
|
||||
Rel: nil,
|
||||
})
|
||||
|
||||
assert.DeepEqual(t, link[3], model.Link{
|
||||
ID: 4,
|
||||
URL: "http://www.bing.com",
|
||||
Name: "Bing",
|
||||
assert.DeepEqual(t, insertedLinks[1], model.Link{
|
||||
ID: 101,
|
||||
URL: "http://www.google.com",
|
||||
Name: "Google",
|
||||
Rel: nil,
|
||||
})
|
||||
|
||||
assert.DeepEqual(t, insertedLinks[2], model.Link{
|
||||
ID: 102,
|
||||
URL: "http://www.yahoo.com",
|
||||
Name: "Yahoo",
|
||||
Rel: nil,
|
||||
})
|
||||
|
||||
allLinks := []model.Link{}
|
||||
|
||||
err = Link.SELECT(Link.AllColumns).
|
||||
WHERE(Link.ID.GT_EQ(Int(100))).
|
||||
ORDER_BY(Link.ID).
|
||||
Query(db, &allLinks)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.DeepEqual(t, insertedLinks, allLinks)
|
||||
}
|
||||
|
||||
func TestInsertDataObject(t *testing.T) {
|
||||
var expectedSql = `
|
||||
INSERT INTO test_sample.link (url, name) VALUES
|
||||
('http://www.duckduckgo.com', 'Duck Duck go');
|
||||
`
|
||||
|
||||
linkData := model.Link{
|
||||
URL: "http://www.duckduckgo.com",
|
||||
Name: "Duck Duck go",
|
||||
|
|
@ -74,47 +90,62 @@ func TestInsertDataObject(t *testing.T) {
|
|||
|
||||
query := Link.
|
||||
INSERT(Link.URL, Link.Name).
|
||||
MODEL(linkData)
|
||||
USING(linkData)
|
||||
|
||||
queryStr, args, err := query.Sql()
|
||||
assertStatementSql(t, query, expectedSql, "http://www.duckduckgo.com", "Duck Duck go")
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(args), 2)
|
||||
|
||||
fmt.Println(queryStr)
|
||||
|
||||
result, err := query.Execute(db)
|
||||
result, err := query.Exec(db)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
fmt.Println(result)
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
|
||||
assert.Equal(t, rowsAffected, int64(1))
|
||||
}
|
||||
|
||||
func TestInsertQuery(t *testing.T) {
|
||||
|
||||
_, err := Link.INSERT(Link.URL, Link.Name).
|
||||
VALUES("http://www.postgresqltutorial.com", "PostgreSQL Tutorial").Execute(db)
|
||||
|
||||
_, err := Link.DELETE().
|
||||
WHERE(Link.ID.NOT_EQ(Int(0)).AND(Link.Name.EQ(String("Youtube")))).
|
||||
Exec(db)
|
||||
assert.NilError(t, err)
|
||||
|
||||
var expectedSql = `
|
||||
INSERT INTO test_sample.link (url, name) (
|
||||
SELECT link.url AS "link.url",
|
||||
link.name AS "link.name"
|
||||
FROM test_sample.link
|
||||
WHERE link.id = 0
|
||||
)
|
||||
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";
|
||||
`
|
||||
|
||||
query := Link.
|
||||
INSERT(Link.URL, Link.Name).
|
||||
QUERY(Link.SELECT(Link.URL, Link.Name))
|
||||
QUERY(
|
||||
SELECT(Link.URL, Link.Name).
|
||||
FROM(Link).
|
||||
WHERE(Link.ID.EQ(Int(0))),
|
||||
).
|
||||
RETURNING(Link.AllColumns)
|
||||
|
||||
queryStr, args, err := query.Sql()
|
||||
assertStatementSql(t, query, expectedSql, int64(0))
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(args), 0)
|
||||
dest := []model.Link{}
|
||||
|
||||
fmt.Println(queryStr)
|
||||
|
||||
_, err = query.Execute(db)
|
||||
err = query.Query(db, &dest)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
allLinks := []model.Link{}
|
||||
err = Link.SELECT(Link.AllColumns).Query(db, &allLinks)
|
||||
assert.NilError(t, err)
|
||||
youtubeLinks := []model.Link{}
|
||||
err = Link.
|
||||
SELECT(Link.AllColumns).
|
||||
WHERE(Link.Name.EQ(String("Youtube"))).
|
||||
Query(db, &youtubeLinks)
|
||||
|
||||
spew.Dump(allLinks)
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(youtubeLinks), 2)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ WHERE actor.actor_id = 1;
|
|||
SELECT(Actor.AllColumns).
|
||||
WHERE(Actor.ActorID.EQ(Int(1)))
|
||||
|
||||
assertQuery(t, query, expectedSql, int64(1))
|
||||
assertStatementSql(t, query, expectedSql, int64(1))
|
||||
|
||||
actor := model.Actor{}
|
||||
err := query.Query(db, &actor)
|
||||
|
|
@ -73,7 +73,7 @@ LIMIT 30;
|
|||
ORDER_BY(Payment.PaymentID.ASC()).
|
||||
LIMIT(30)
|
||||
|
||||
assertQuery(t, query, expectedSql, int64(30))
|
||||
assertStatementSql(t, query, expectedSql, int64(30))
|
||||
|
||||
dest := []model.Payment{}
|
||||
|
||||
|
|
@ -102,7 +102,7 @@ ORDER BY customer.customer_id ASC;
|
|||
|
||||
query := Customer.SELECT(Customer.AllColumns).ORDER_BY(Customer.CustomerID.ASC())
|
||||
|
||||
assertQuery(t, query, expectedSql)
|
||||
assertStatementSql(t, query, expectedSql)
|
||||
|
||||
err := query.Query(db, &customers)
|
||||
assert.NilError(t, err)
|
||||
|
|
@ -156,7 +156,7 @@ LIMIT 12;
|
|||
LIMIT(12)
|
||||
|
||||
fmt.Println(query.Sql())
|
||||
assertQuery(t, query, expectedSql, int64(1), int64(1), int64(10), int64(1), int64(2), int64(1), int64(12))
|
||||
assertStatementSql(t, query, expectedSql, int64(1), int64(1), int64(10), int64(1), int64(2), int64(1), int64(12))
|
||||
}
|
||||
|
||||
func TestJoinQueryStruct(t *testing.T) {
|
||||
|
|
@ -224,7 +224,7 @@ LIMIT 500;
|
|||
ORDER_BY(Film.FilmID.ASC()).
|
||||
LIMIT(500)
|
||||
|
||||
assertQuery(t, query, expectedSql, int64(500))
|
||||
assertStatementSql(t, query, expectedSql, int64(500))
|
||||
|
||||
var languageActorFilm []struct {
|
||||
model.Language
|
||||
|
|
@ -291,7 +291,7 @@ LIMIT 15;
|
|||
WHERE(Film.Rating.EQ(enum.MpaaRating.NC17)).
|
||||
LIMIT(15)
|
||||
|
||||
assertQuery(t, query, expectedSql, int64(15))
|
||||
assertStatementSql(t, query, expectedSql, int64(15))
|
||||
|
||||
err := query.Query(db, &filmsPerLanguage)
|
||||
|
||||
|
|
@ -422,7 +422,7 @@ ORDER BY customer.customer_id ASC;
|
|||
SELECT(Customer.AllColumns, Address.AllColumns).
|
||||
ORDER_BY(Customer.CustomerID.ASC())
|
||||
|
||||
assertQuery(t, query, expectedSql)
|
||||
assertStatementSql(t, query, expectedSql)
|
||||
|
||||
allCustomersAndAddress := []struct {
|
||||
Address *model.Address
|
||||
|
|
@ -475,7 +475,7 @@ LIMIT 1000;
|
|||
ORDER_BY(Customer.CustomerID.ASC()).
|
||||
LIMIT(1000)
|
||||
|
||||
assertQuery(t, query, expectedSql, int64(1000))
|
||||
assertStatementSql(t, query, expectedSql, int64(1000))
|
||||
|
||||
var customerAddresCrosJoined []struct {
|
||||
model.Customer
|
||||
|
|
@ -513,7 +513,7 @@ ORDER BY employee.employee_id;
|
|||
SELECT(Employee.AllColumns, manager.AllColumns).
|
||||
ORDER_BY(Employee.EmployeeID)
|
||||
|
||||
assertQuery(t, query, expectedSql)
|
||||
assertStatementSql(t, query, expectedSql)
|
||||
|
||||
var dest []struct {
|
||||
model2.Employee
|
||||
|
|
@ -585,7 +585,7 @@ ORDER BY f1.film_id ASC;
|
|||
SELECT(f1.AllColumns, f2.AllColumns).
|
||||
ORDER_BY(f1.FilmID.ASC())
|
||||
|
||||
assertQuery(t, query, expectedSql)
|
||||
assertStatementSql(t, query, expectedSql)
|
||||
|
||||
type F1 model.Film
|
||||
type F2 model.Film
|
||||
|
|
@ -627,7 +627,7 @@ LIMIT 1000;
|
|||
ORDER_BY(f1.Length.ASC(), f1.Title.ASC(), f2.Title.ASC()).
|
||||
LIMIT(1000)
|
||||
|
||||
assertQuery(t, query, expectedSql, int64(1000))
|
||||
assertStatementSql(t, query, expectedSql, int64(1000))
|
||||
|
||||
type thesameLengthFilms struct {
|
||||
Title1 string
|
||||
|
|
@ -691,7 +691,7 @@ LIMIT 1000;
|
|||
// SELECT(Staff.StaffID, Staff.FirstName, Staff.LastName, Address.AllColumns, manager.StaffID, manager.FirstName).
|
||||
// DISTINCT()
|
||||
//
|
||||
// assertQuery(t, query, expectedSql)
|
||||
// assertStatementSql(t, query, expectedSql)
|
||||
//
|
||||
// staffs := []staff{}
|
||||
//
|
||||
|
|
@ -747,7 +747,7 @@ FROM dvds.actor
|
|||
|
||||
fmt.Println(query.Sql())
|
||||
|
||||
assertQuery(t, query, expectedQuery)
|
||||
assertStatementSql(t, query, expectedQuery)
|
||||
|
||||
dest := []model.Actor{}
|
||||
|
||||
|
|
@ -765,7 +765,7 @@ FROM dvds.film;
|
|||
MAXf(Film.RentalRate).AS("max_film_rate"),
|
||||
)
|
||||
|
||||
assertQuery(t, query, expectedQuery)
|
||||
assertStatementSql(t, query, expectedQuery)
|
||||
|
||||
ret := struct {
|
||||
MaxFilmRate float64
|
||||
|
|
@ -808,7 +808,7 @@ ORDER BY film.film_id ASC;
|
|||
ORDER_BY(Film.FilmID.ASC())
|
||||
|
||||
fmt.Println(query.Sql())
|
||||
assertQuery(t, query, expectedSql)
|
||||
assertStatementSql(t, query, expectedSql)
|
||||
|
||||
maxRentalRateFilms := []model.Film{}
|
||||
err := query.Query(db, &maxRentalRateFilms)
|
||||
|
|
@ -866,7 +866,7 @@ ORDER BY SUM(payment.amount) ASC;
|
|||
SUMf(Payment.Amount).GT(Float(100)),
|
||||
)
|
||||
|
||||
assertQuery(t, customersPaymentQuery, expectedSql, float64(100))
|
||||
assertStatementSql(t, customersPaymentQuery, expectedSql, float64(100))
|
||||
|
||||
type CustomerPaymentSum struct {
|
||||
CustomerID int16
|
||||
|
|
@ -936,7 +936,7 @@ ORDER BY customer_payment_sum.amount_sum ASC;
|
|||
).
|
||||
ORDER_BY(amountSum.ASC())
|
||||
|
||||
assertQuery(t, query, expectedSql)
|
||||
assertStatementSql(t, query, expectedSql)
|
||||
|
||||
type CustomerWithAmounts struct {
|
||||
Customer *model.Customer
|
||||
|
|
@ -992,7 +992,7 @@ ORDER BY payment.payment_date ASC;
|
|||
WHERE(Payment.PaymentDate.LT(Timestamp(2007, 02, 14, 22, 16, 01, 0))).
|
||||
ORDER_BY(Payment.PaymentDate.ASC())
|
||||
|
||||
assertQuery(t, query, expectedSql, "2007-02-14 22:16:01.000")
|
||||
assertStatementSql(t, query, expectedSql, "2007-02-14 22:16:01.000")
|
||||
|
||||
payments := []model.Payment{}
|
||||
|
||||
|
|
@ -1049,7 +1049,7 @@ OFFSET 20;
|
|||
queryStr, _, _ := query.Sql()
|
||||
|
||||
fmt.Println("-" + queryStr + "-")
|
||||
assertQuery(t, query, expectedQuery, float64(100), float64(200), int64(10), int64(20))
|
||||
assertStatementSql(t, query, expectedQuery, float64(100), float64(200), int64(10), int64(20))
|
||||
|
||||
dest := []model.Payment{}
|
||||
|
||||
|
|
@ -1088,7 +1088,7 @@ LIMIT 20;
|
|||
ORDER_BY(Payment.PaymentID.ASC()).
|
||||
LIMIT(20)
|
||||
|
||||
assertQuery(t, query, expectedQuery, int64(1), "ONE", int64(2), "TWO", int64(3), "THREE", "OTHER", int64(20))
|
||||
assertStatementSql(t, query, expectedQuery, int64(1), "ONE", int64(2), "TWO", int64(3), "THREE", "OTHER", int64(20))
|
||||
|
||||
dest := []struct {
|
||||
StaffIdNum string
|
||||
|
|
@ -1111,11 +1111,11 @@ LOCK TABLE dvds.address IN EXCLUSIVE MODE NOWAIT;
|
|||
querySql, _, _ := query.Sql()
|
||||
fmt.Println("-" + querySql + "-")
|
||||
|
||||
assertQuery(t, query, expectedSql)
|
||||
assertStatementSql(t, query, expectedSql)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
|
||||
_, err := query.Execute(tx)
|
||||
_, err := query.Exec(tx)
|
||||
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func assertQuery(t *testing.T, query sqlbuilder.Statement, expectedQuery string, expectedArgs ...interface{}) {
|
||||
func assertStatementSql(t *testing.T, query sqlbuilder.Statement, expectedQuery string, expectedArgs ...interface{}) {
|
||||
_, args, err := query.Sql()
|
||||
assert.NilError(t, err)
|
||||
//assert.Equal(t, queryStr, expectedQuery)
|
||||
|
|
@ -21,6 +21,20 @@ func assertQuery(t *testing.T, query sqlbuilder.Statement, expectedQuery string,
|
|||
assert.Equal(t, debuqSql, expectedQuery)
|
||||
}
|
||||
|
||||
func assertExec(t *testing.T, stmt sqlbuilder.Statement, rowsAffected int64) {
|
||||
res, err := stmt.Exec(db)
|
||||
|
||||
assert.NilError(t, err)
|
||||
rows, err := res.RowsAffected()
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, rows, rowsAffected)
|
||||
}
|
||||
|
||||
func assertExecErr(t *testing.T, stmt sqlbuilder.Statement, errorStr string) {
|
||||
_, err := stmt.Exec(db)
|
||||
|
||||
assert.Equal(t, err.Error(), errorStr)
|
||||
}
|
||||
func boolPtr(b bool) *bool {
|
||||
return &b
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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