Skip to content

Commit

Permalink
wrap around TRX
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Oct 7, 2024
1 parent c30a350 commit 32d5bf4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
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
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
38 changes: 38 additions & 0 deletions tests/postgres/alltypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgres
import (
"database/sql"
"github.com/go-jet/jet/v2/internal/utils/ptr"
"github.com/stretchr/testify/assert"
"testing"
"time"

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

func TestIntervalUpsert(t *testing.T) {
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
stmt := SELECT(Employee.AllColumns).FROM(Employee).
WHERE(Employee.EmployeeID.EQ(Int(1)))

//Validate initial dataset
var windy model.Employee
err := stmt.Query(db, &windy)
assert.Equal(t, windy.EmployeeID, int32(1))
assert.Equal(t, windy.FirstName, "Windy")
assert.Equal(t, windy.LastName, "Hays")
assert.Equal(t, *windy.PtoAccrual, "22:00:00")
assert.Nil(t, err)
windy.PtoAccrual = ptr.Of("3h")
//Update data
updateStmt := Employee.UPDATE(Employee.PtoAccrual).SET(
Employee.PtoAccrual.SET(INTERVAL(3, HOUR)),
).WHERE(Employee.EmployeeID.EQ(Int(1))).RETURNING(Employee.AllColumns)

err = updateStmt.Query(db, &windy)
err = stmt.Query(db, &windy)
assert.Nil(t, err)
assert.Equal(t, *windy.PtoAccrual, "03:00:00")
//Upsert dataset with a different value
windy.PtoAccrual = ptr.Of("5h")
insertStmt := Employee.INSERT(Employee.AllColumns).
MODEL(&windy).
ON_CONFLICT(Employee.EmployeeID).
DO_UPDATE(SET(
Employee.PtoAccrual.SET(Employee.EXCLUDED.PtoAccrual),
)).RETURNING(Employee.AllColumns)
err = insertStmt.Query(db, &windy)
assert.Nil(t, err)
assert.Equal(t, *windy.PtoAccrual, "05:00:00")
})
}

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

Expand Down
2 changes: 1 addition & 1 deletion tests/testdata

0 comments on commit 32d5bf4

Please sign in to comment.