Skip to content

Commit

Permalink
add unsafe conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoLiberali committed Aug 23, 2023
1 parent b1778eb commit 4215ea3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
38 changes: 38 additions & 0 deletions orm/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,44 @@ func divideConditionsByType[T any](
return
}

// Condition that can be used to express conditions that are not supported (yet?) by BaDORM
// Example: table1.columnX = table2.columnY
type UnsafeCondition[T any] struct {
SQLCondition string
Values []any
}

//nolint:unused // see inside
func (condition UnsafeCondition[T]) interfaceVerificationMethod(_ T) {
// This method is necessary to get the compiler to verify
// that an object is of type Condition[T]
}

func (condition UnsafeCondition[T]) ApplyTo(query *gorm.DB, tableName string) (*gorm.DB, error) {
return applyWhereCondition[T](condition, query, tableName)
}

func (condition UnsafeCondition[T]) GetSQL(_ *gorm.DB, tableName string) (string, []any, error) {
return fmt.Sprintf(
condition.SQLCondition,
tableName,
), condition.Values, nil
}

//nolint:unused // is used
func (condition UnsafeCondition[T]) affectsDeletedAt() bool {
return false
}

// Condition that can be used to express conditions that are not supported (yet?) by BaDORM
// Example: table1.columnX = table2.columnY
func NewUnsafeCondition[T any](condition string, values []any) UnsafeCondition[T] {
return UnsafeCondition[T]{
SQLCondition: condition,
Values: values,
}
}

// Condition used to returns an error when the query is executed
type InvalidCondition[T any] struct {
Err error
Expand Down
25 changes: 25 additions & 0 deletions testintegration/join_conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,31 @@ func (ts *JoinConditionsIntTestSuite) TestConditionThatJoinsMultipleTimes() {
EqualList(&ts.Suite, []*models.Sale{match}, entities)
}

func (ts *WhereConditionsIntTestSuite) TestJoinWithUnsafeCondition() {
product1 := ts.createProduct("", 0, 0.0, false, nil)
product2 := ts.createProduct("", 0, 0.0, false, nil)

company1 := ts.createCompany("ditrit")
company2 := ts.createCompany("orness")

seller1 := ts.createSeller("ditrit", company1)
seller2 := ts.createSeller("agustin", company2)

match := ts.createSale(0, product1, seller1)
ts.createSale(0, product2, seller2)

entities, err := ts.crudSaleService.Query(
conditions.SaleSeller(
conditions.SellerCompany(
orm.NewUnsafeCondition[models.Company]("%s.name = sellers_sales.name", []any{}),
),
),
)
ts.Nil(err)

EqualList(&ts.Suite, []*models.Sale{match}, entities)
}

func (ts *WhereConditionsIntTestSuite) TestJoinWithEmptyConnectionConditionMakesNothing() {
product1 := ts.createProduct("", 1, 0.0, false, nil)
product2 := ts.createProduct("", 2, 0.0, false, nil)
Expand Down
14 changes: 14 additions & 0 deletions testintegration/where_conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,20 @@ func (ts *WhereConditionsIntTestSuite) TestMultipleConditionsDifferentOperators(
EqualList(&ts.Suite, []*models.Product{match1, match2}, entities)
}

func (ts *WhereConditionsIntTestSuite) TestUnsafeCondition() {
match1 := ts.createProduct("match", 1, 0.0, true, nil)
match2 := ts.createProduct("match", 1, 0.0, true, nil)

ts.createProduct("not_match", 2, 0.0, true, nil)

entities, err := ts.crudProductService.Query(
orm.NewUnsafeCondition[models.Product]("%s.int = ?", []any{1}),
)
ts.Nil(err)

EqualList(&ts.Suite, []*models.Product{match1, match2}, entities)
}

func (ts *WhereConditionsIntTestSuite) TestEmptyConnectionConditionMakesNothing() {
match1 := ts.createProduct("match", 1, 0.0, true, nil)
match2 := ts.createProduct("match", 1, 0.0, true, nil)
Expand Down

0 comments on commit 4215ea3

Please sign in to comment.