Skip to content

Commit

Permalink
Merge pull request #406 from safaci2000/feature/intervalSubmission
Browse files Browse the repository at this point in the history
Improved support for intervals in postgres
  • Loading branch information
go-jet authored Oct 10, 2024
2 parents c30a350 + c0a0b45 commit f9358ca
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 15 deletions.
7 changes: 7 additions & 0 deletions internal/jet/column_assigment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ type columnAssigmentImpl struct {
expression Expression
}

func NewColumnAssignment(serializer ColumnSerializer, expression Expression) ColumnAssigment {
return &columnAssigmentImpl{
column: serializer,
expression: expression,
}
}

func (a columnAssigmentImpl) isColumnAssigment() {}

func (a columnAssigmentImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
Expand Down
3 changes: 1 addition & 2 deletions internal/testutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/go-jet/jet/v2/internal/jet"
"github.com/go-jet/jet/v2/internal/utils/throw"
"github.com/go-jet/jet/v2/qrm"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -17,8 +18,6 @@ import (
"runtime"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

// UnixTimeComparer will compare time equality while ignoring time zone
Expand Down
7 changes: 7 additions & 0 deletions postgres/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,20 @@ type ColumnInterval interface {
jet.Column

From(subQuery SelectTable) ColumnInterval
SET(intervalExp IntervalExpression) ColumnAssigment
}

//------------------------------------------------------//

type intervalColumnImpl struct {
jet.ColumnExpressionImpl
intervalInterfaceImpl
}

func (i *intervalColumnImpl) SET(intervalExp IntervalExpression) ColumnAssigment {
return jet.NewColumnAssignment(i, intervalExp)
}

func (i *intervalColumnImpl) From(subQuery SelectTable) ColumnInterval {
newIntervalColumn := IntervalColumn(i.Name())
jet.SetTableName(newIntervalColumn, i.TableName())
Expand Down
64 changes: 64 additions & 0 deletions tests/postgres/alltypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package postgres
import (
"database/sql"
"github.com/go-jet/jet/v2/internal/utils/ptr"
"github.com/stretchr/testify/assert"
"log/slog"
"testing"
"time"

Expand Down Expand Up @@ -931,6 +933,68 @@ func TestTimeExpression(t *testing.T) {
require.NoError(t, err)
}

func TestIntervalSetFunctionality(t *testing.T) {

t.Run("updateQueryIntervalTest", func(t *testing.T) {
slog.Info("Running test", slog.Any("test", t.Name()))
expectedQuery := `
UPDATE test_sample.employee
SET pto_accrual = INTERVAL '3 HOUR'
WHERE employee.employee_id = $1
RETURNING employee.employee_id AS "employee.employee_id",
employee.first_name AS "employee.first_name",
employee.last_name AS "employee.last_name",
employee.employment_date AS "employee.employment_date",
employee.manager_id AS "employee.manager_id",
employee.pto_accrual AS "employee.pto_accrual";
`
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
var windy model.Employee
windy.PtoAccrual = ptr.Of("3h")
stmt := Employee.UPDATE(Employee.PtoAccrual).SET(
Employee.PtoAccrual.SET(INTERVAL(3, HOUR)),
).WHERE(Employee.EmployeeID.EQ(Int(1))).RETURNING(Employee.AllColumns)

testutils.AssertStatementSql(t, stmt, expectedQuery)
err := stmt.Query(tx, &windy)
assert.Nil(t, err)
assert.Equal(t, *windy.PtoAccrual, "03:00:00")

})
})

t.Run("upsertQueryIntervalTest", func(t *testing.T) {
expectedQuery := `
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id, pto_accrual)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (employee_id) DO UPDATE
SET pto_accrual = excluded.pto_accrual
RETURNING employee.employee_id AS "employee.employee_id",
employee.first_name AS "employee.first_name",
employee.last_name AS "employee.last_name",
employee.employment_date AS "employee.employment_date",
employee.manager_id AS "employee.manager_id",
employee.pto_accrual AS "employee.pto_accrual";
`
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
var employee model.Employee
employee.PtoAccrual = ptr.Of("5h")
stmt := Employee.INSERT(Employee.AllColumns).
MODEL(employee).
ON_CONFLICT(Employee.EmployeeID).
DO_UPDATE(SET(
Employee.PtoAccrual.SET(Employee.EXCLUDED.PtoAccrual),
)).RETURNING(Employee.AllColumns)

testutils.AssertStatementSql(t, stmt, expectedQuery)
err := stmt.Query(tx, &employee)
assert.Nil(t, err)
assert.Equal(t, *employee.PtoAccrual, "05:00:00")

})
})
}

func TestInterval(t *testing.T) {
skipForCockroachDB(t)

Expand Down
22 changes: 11 additions & 11 deletions tests/postgres/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ func TestInsertOnConflict(t *testing.T) {
ON_CONFLICT().DO_NOTHING()

testutils.AssertStatementSql(t, stmt, `
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id)
VALUES ($1, $2, $3, $4, $5),
($6, $7, $8, $9, $10)
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id, pto_accrual)
VALUES ($1, $2, $3, $4, $5, $6),
($7, $8, $9, $10, $11, $12)
ON CONFLICT DO NOTHING;
`)
testutils.AssertExecAndRollback(t, stmt, db, 1)
Expand All @@ -111,9 +111,9 @@ ON CONFLICT DO NOTHING;
ON_CONFLICT(Employee.EmployeeID).DO_NOTHING()

testutils.AssertStatementSql(t, stmt, `
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id)
VALUES ($1, $2, $3, $4, $5),
($6, $7, $8, $9, $10)
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id, pto_accrual)
VALUES ($1, $2, $3, $4, $5, $6),
($7, $8, $9, $10, $11, $12)
ON CONFLICT (employee_id) DO NOTHING;
`)
testutils.AssertExecAndRollback(t, stmt, db, 1)
Expand All @@ -130,9 +130,9 @@ ON CONFLICT (employee_id) DO NOTHING;
ON_CONFLICT().ON_CONSTRAINT("employee_pkey").DO_NOTHING()

testutils.AssertStatementSql(t, stmt, `
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id)
VALUES ($1, $2, $3, $4, $5),
($6, $7, $8, $9, $10)
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id, pto_accrual)
VALUES ($1, $2, $3, $4, $5, $6),
($7, $8, $9, $10, $11, $12)
ON CONFLICT ON CONSTRAINT employee_pkey DO NOTHING;
`)
testutils.AssertExecAndRollback(t, stmt, db, 1)
Expand Down Expand Up @@ -234,8 +234,8 @@ ON CONFLICT (id) WHERE (id * 2) > 10 DO UPDATE
ON_CONFLICT().DO_UPDATE(nil)

testutils.AssertStatementSql(t, stmt, `
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id)
VALUES ($1, $2, $3, $4, $5);
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id, pto_accrual)
VALUES ($1, $2, $3, $4, $5, $6);
`)
testutils.AssertExecAndRollback(t, stmt, db, 1)
requireLogged(t, stmt)
Expand Down
5 changes: 4 additions & 1 deletion tests/postgres/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,13 @@ SELECT employee.employee_id AS "employee.employee_id",
employee.last_name AS "employee.last_name",
employee.employment_date AS "employee.employment_date",
employee.manager_id AS "employee.manager_id",
employee.pto_accrual AS "employee.pto_accrual",
manager.employee_id AS "manager.employee_id",
manager.first_name AS "manager.first_name",
manager.last_name AS "manager.last_name",
manager.employment_date AS "manager.employment_date",
manager.manager_id AS "manager.manager_id"
manager.manager_id AS "manager.manager_id",
manager.pto_accrual AS "manager.pto_accrual"
FROM test_sample.employee
LEFT JOIN test_sample.employee AS manager ON (manager.employee_id = employee.manager_id)
ORDER BY employee.employee_id;
Expand Down Expand Up @@ -370,6 +372,7 @@ ORDER BY employee.employee_id;
LastName: "Hays",
EmploymentDate: testutils.TimestampWithTimeZone("1999-01-08 04:05:06.1 +0100 CET", 1),
ManagerID: nil,
PtoAccrual: ptr.Of("22:00:00"),
})

require.True(t, dest[0].Manager == nil)
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata

0 comments on commit f9358ca

Please sign in to comment.