2019-08-11 09:52:02 +02:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
|
|
import (
|
2020-05-09 11:00:22 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-11 09:52:02 +02:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2019-08-12 12:27:33 +02:00
|
|
|
func TestInvalidInsert(t *testing.T) {
|
|
|
|
|
assertStatementSqlErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
|
|
|
|
|
}
|
2019-08-11 09:52:02 +02:00
|
|
|
|
|
|
|
|
func TestInsertNilValue(t *testing.T) {
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(nil), `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col1)
|
|
|
|
|
VALUES ($1);
|
2019-08-11 09:52:02 +02:00
|
|
|
`, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertSingleValue(t *testing.T) {
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(1), `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col1)
|
|
|
|
|
VALUES ($1);
|
2019-08-11 09:52:02 +02:00
|
|
|
`, int(1))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertWithColumnList(t *testing.T) {
|
2019-09-26 12:31:03 +02:00
|
|
|
columnList := ColumnList{table3ColInt, table3StrCol}
|
2019-08-11 09:52:02 +02:00
|
|
|
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, table3.INSERT(columnList).VALUES(1, 3), `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table3 (col_int, col2)
|
|
|
|
|
VALUES ($1, $2);
|
2019-08-11 09:52:02 +02:00
|
|
|
`, 1, 3)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertDate(t *testing.T) {
|
|
|
|
|
date := time.Date(1999, 1, 2, 3, 4, 5, 0, time.UTC)
|
|
|
|
|
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, table1.INSERT(table1ColTime).VALUES(date), `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col_time)
|
|
|
|
|
VALUES ($1);
|
2019-08-11 09:52:02 +02:00
|
|
|
`, date)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertMultipleValues(t *testing.T) {
|
2020-04-12 18:53:57 +02:00
|
|
|
assertStatementSql(t, table1.INSERT(table1Col1, table1ColFloat, table1ColBool).VALUES(1, 2, 3), `
|
|
|
|
|
INSERT INTO db.table1 (col1, col_float, col_bool)
|
|
|
|
|
VALUES ($1, $2, $3);
|
2019-08-11 09:52:02 +02:00
|
|
|
`, 1, 2, 3)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertMultipleRows(t *testing.T) {
|
|
|
|
|
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
|
|
|
|
VALUES(1, 2).
|
|
|
|
|
VALUES(11, 22).
|
|
|
|
|
VALUES(111, 222)
|
|
|
|
|
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, stmt, `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_float)
|
|
|
|
|
VALUES ($1, $2),
|
|
|
|
|
($3, $4),
|
|
|
|
|
($5, $6);
|
2019-08-11 09:52:02 +02:00
|
|
|
`, 1, 2, 11, 22, 111, 222)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertValuesFromModel(t *testing.T) {
|
|
|
|
|
type Table1Model struct {
|
|
|
|
|
Col1 *int
|
|
|
|
|
ColFloat float64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
one := 1
|
|
|
|
|
|
|
|
|
|
toInsert := Table1Model{
|
|
|
|
|
Col1: &one,
|
|
|
|
|
ColFloat: 1.11,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
|
|
|
|
MODEL(toInsert).
|
|
|
|
|
MODEL(&toInsert)
|
|
|
|
|
|
|
|
|
|
expectedSQL := `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_float)
|
|
|
|
|
VALUES ($1, $2),
|
|
|
|
|
($3, $4);
|
2019-08-11 09:52:02 +02:00
|
|
|
`
|
|
|
|
|
|
2020-04-12 18:53:57 +02:00
|
|
|
assertStatementSql(t, stmt, expectedSQL, 1, float64(1.11), 1, float64(1.11))
|
2019-08-11 09:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertValuesFromModelColumnMismatch(t *testing.T) {
|
|
|
|
|
defer func() {
|
|
|
|
|
r := recover()
|
2020-05-09 11:00:22 +02:00
|
|
|
require.Equal(t, r, "missing struct field for column : col1")
|
2019-08-11 09:52:02 +02:00
|
|
|
}()
|
|
|
|
|
type Table1Model struct {
|
|
|
|
|
Col1Prim int
|
|
|
|
|
Col2 string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newData := Table1Model{
|
|
|
|
|
Col1Prim: 1,
|
|
|
|
|
Col2: "one",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
table1.
|
|
|
|
|
INSERT(table1Col1, table1ColFloat).
|
|
|
|
|
MODEL(newData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertFromNonStructModel(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
r := recover()
|
2020-05-09 11:00:22 +02:00
|
|
|
require.Equal(t, r, "jet: data has to be a struct")
|
2019-08-11 09:52:02 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
table2.INSERT(table2ColInt).MODEL([]int{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertQuery(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
stmt := table1.INSERT(table1Col1).
|
|
|
|
|
QUERY(table1.SELECT(table1Col1))
|
|
|
|
|
|
|
|
|
|
var expectedSQL = `
|
|
|
|
|
INSERT INTO db.table1 (col1) (
|
|
|
|
|
SELECT table1.col1 AS "table1.col1"
|
|
|
|
|
FROM db.table1
|
|
|
|
|
);
|
|
|
|
|
`
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, stmt, expectedSQL)
|
2019-08-11 09:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertDefaultValue(t *testing.T) {
|
|
|
|
|
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
|
|
|
|
VALUES(DEFAULT, "two")
|
|
|
|
|
|
|
|
|
|
var expectedSQL = `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_float)
|
|
|
|
|
VALUES (DEFAULT, $1);
|
2019-08-11 09:52:02 +02:00
|
|
|
`
|
|
|
|
|
|
2019-08-12 12:11:16 +02:00
|
|
|
assertStatementSql(t, stmt, expectedSQL, "two")
|
2019-08-11 09:52:02 +02:00
|
|
|
}
|
2020-04-12 18:53:57 +02:00
|
|
|
|
|
|
|
|
func TestInsert_ON_CONFLICT(t *testing.T) {
|
|
|
|
|
stmt := table1.INSERT(table1Col1, table1ColBool).
|
|
|
|
|
VALUES("one", "two").
|
|
|
|
|
VALUES("1", "2").
|
|
|
|
|
VALUES("theta", "beta").
|
2024-10-17 14:12:21 +02:00
|
|
|
ON_CONFLICT(table1ColBool).WHERE(table1ColBool.IS_NOT_FALSE()).
|
|
|
|
|
DO_UPDATE(
|
|
|
|
|
SET(table1ColBool.SET(Bool(true)),
|
|
|
|
|
table2ColInt.SET(Int(1)),
|
|
|
|
|
ColumnList{table1Col1, table1ColBool}.SET(ROW(Int(2), String("two"))),
|
|
|
|
|
).WHERE(table1Col1.GT(Int(2))),
|
|
|
|
|
).
|
2020-04-12 18:53:57 +02:00
|
|
|
RETURNING(table1Col1, table1ColBool)
|
|
|
|
|
|
|
|
|
|
assertDebugStatementSql(t, stmt, `
|
|
|
|
|
INSERT INTO db.table1 (col1, col_bool)
|
|
|
|
|
VALUES ('one', 'two'),
|
|
|
|
|
('1', '2'),
|
|
|
|
|
('theta', 'beta')
|
|
|
|
|
ON CONFLICT (col_bool) WHERE col_bool IS NOT FALSE DO UPDATE
|
2021-12-26 17:15:43 +01:00
|
|
|
SET col_bool = TRUE::boolean,
|
2020-04-12 18:53:57 +02:00
|
|
|
col_int = 1,
|
2022-05-05 13:01:42 +02:00
|
|
|
(col1, col_bool) = ROW(2, 'two'::text)
|
2020-04-12 18:53:57 +02:00
|
|
|
WHERE table1.col1 > 2
|
|
|
|
|
RETURNING table1.col1 AS "table1.col1",
|
|
|
|
|
table1.col_bool AS "table1.col_bool";
|
|
|
|
|
`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsert_ON_CONFLICT_ON_CONSTRAINT(t *testing.T) {
|
2025-10-16 15:09:07 +02:00
|
|
|
stmt := table1.INSERT(table1Col1, table1ColBool).
|
|
|
|
|
VALUES("one", "two").
|
|
|
|
|
VALUES("1", "2").
|
|
|
|
|
ON_CONFLICT().ON_CONSTRAINT("idk_primary_key").
|
|
|
|
|
DO_UPDATE(
|
|
|
|
|
SET(table1ColBool.SET(Bool(false)),
|
|
|
|
|
table2ColInt.SET(Int(1)),
|
|
|
|
|
ColumnList{table1Col1, table1ColBool}.SET(ROW(Int(2), String("two"))),
|
|
|
|
|
).WHERE(table1Col1.GT(Int(2)))).
|
|
|
|
|
RETURNING(table1Col1, table1ColBool)
|
2020-04-12 18:53:57 +02:00
|
|
|
|
|
|
|
|
assertDebugStatementSql(t, stmt, `
|
2025-10-16 15:09:07 +02:00
|
|
|
INSERT INTO db.table1 (col1, col_bool)
|
|
|
|
|
VALUES ('one', 'two'),
|
|
|
|
|
('1', '2')
|
2020-04-12 18:53:57 +02:00
|
|
|
ON CONFLICT ON CONSTRAINT idk_primary_key DO UPDATE
|
2021-12-26 17:15:43 +01:00
|
|
|
SET col_bool = FALSE::boolean,
|
2020-04-12 18:53:57 +02:00
|
|
|
col_int = 1,
|
2025-10-16 15:09:07 +02:00
|
|
|
(col1, col_bool) = ROW(2, 'two'::text)
|
2020-04-12 18:53:57 +02:00
|
|
|
WHERE table1.col1 > 2
|
|
|
|
|
RETURNING table1.col1 AS "table1.col1",
|
2025-10-16 15:09:07 +02:00
|
|
|
table1.col_bool AS "table1.col_bool";
|
2020-04-12 18:53:57 +02:00
|
|
|
`)
|
|
|
|
|
}
|