Skip to content

Commit

Permalink
feat: add is and not
Browse files Browse the repository at this point in the history
  • Loading branch information
mul14 committed Oct 16, 2024
1 parent ad3d752 commit 280089d
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Query struct {
Relations []string
WhereAndList *[]string
WhereOrList *[]string
IsList *[]string
OrderList *[]string
LimitValue int
OffsetValue int
Expand Down Expand Up @@ -147,6 +148,11 @@ func buildQueryURI(q Query) string {
output += fmt.Sprintf("&or=(%s)", list)
}

if q.IsList != nil && len(*q.IsList) > 0 {
list := strings.Join(*q.IsList, ",")
output += fmt.Sprintf("&%s", list)

Check warning on line 153 in pkg/db/db.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/db.go#L152-L153

Added lines #L152 - L153 were not covered by tests
}

if q.OrderList != nil && len(*q.OrderList) > 0 {
orders := strings.Join(*q.OrderList, ",")
output += fmt.Sprintf("&order=%s", orders)
Expand Down
13 changes: 7 additions & 6 deletions pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ var mockRaidenContext = raiden.Ctx{
type ArticleMockModel struct {
ModelBase

Id int64 `json:"id,omitempty" column:"name:id;type:bigint;primaryKey;autoIncrement;nullable:false"`
UserId int64 `json:"user_id,omitempty" column:"name:user_id;type:bigint;nullable:false"`
Title string `json:"title,omitempty" column:"name:title;type:text;nullable:false"`
Body string `json:"body,omitempty" column:"name:body;type:text;nullable:true"`
Rating int64 `json:"rating,omitempty" column:"name:rating;type:bigint;nullable:false"`
CreatedAt time.Time `json:"created_at,omitempty" column:"name:created_at;type:timestampz;nullable:false;default:now()"`
Id int64 `json:"id,omitempty" column:"name:id;type:bigint;primaryKey;autoIncrement;nullable:false"`
UserId int64 `json:"user_id,omitempty" column:"name:user_id;type:bigint;nullable:false"`
Title string `json:"title,omitempty" column:"name:title;type:text;nullable:false"`
Body string `json:"body,omitempty" column:"name:body;type:text;nullable:true"`
Rating int64 `json:"rating,omitempty" column:"name:rating;type:bigint;nullable:false"`
IsFeatured bool `json:"is_featured,omitempty" column:"name:is_featured;type:bool;nullable:false"`
CreatedAt time.Time `json:"created_at,omitempty" column:"name:created_at;type:timestampz;nullable:false;default:now()"`

Metadata string `json:"-" schema:"public" tableName:"articles" rlsEnable:"true" rlsForced:"false"`

Expand Down
176 changes: 176 additions & 0 deletions pkg/db/where.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ func (q *Query) Eq(column string, value any) *Query {
return q
}

func (q *Query) NotEq(column string, value any) *Query {

if q.WhereAndList == nil {
q.WhereAndList = &[]string{}
}

*q.WhereAndList = append(
*q.WhereAndList,
fmt.Sprintf("%s=not.eq.%s", column, getStringValue(value)),
)

return q
}

func (q *Query) OrEq(column string, value any) *Query {

if q.WhereOrList == nil {
Expand Down Expand Up @@ -49,6 +63,20 @@ func (q *Query) Neq(column string, value any) *Query {
return q
}

func (q *Query) NotNeq(column string, value any) *Query {

if q.WhereAndList == nil {
q.WhereAndList = &[]string{}
}

*q.WhereAndList = append(
*q.WhereAndList,
fmt.Sprintf("%s=not.neq.%s", column, getStringValue(value)),
)

return q
}

func (q *Query) OrNeq(column string, value any) *Query {

if q.WhereOrList == nil {
Expand Down Expand Up @@ -77,6 +105,20 @@ func (q *Query) Lt(column string, value any) *Query {
return q
}

func (q *Query) NotLt(column string, value any) *Query {

if q.WhereAndList == nil {
q.WhereAndList = &[]string{}
}

*q.WhereAndList = append(
*q.WhereAndList,
fmt.Sprintf("%s=not.lt.%s", column, getStringValue(value)),
)

return q
}

func (q *Query) OrLt(column string, value any) *Query {

if q.WhereOrList == nil {
Expand Down Expand Up @@ -105,6 +147,20 @@ func (q *Query) Lte(column string, value any) *Query {
return q
}

func (q *Query) NotLte(column string, value any) *Query {

if q.WhereAndList == nil {
q.WhereAndList = &[]string{}
}

*q.WhereAndList = append(
*q.WhereAndList,
fmt.Sprintf("%s=not.lte.%s", column, getStringValue(value)),
)

return q
}

func (q *Query) OrLte(column string, value any) *Query {

if q.WhereOrList == nil {
Expand Down Expand Up @@ -133,6 +189,20 @@ func (q *Query) Gt(column string, value int) *Query {
return q
}

func (q *Query) NotGt(column string, value int) *Query {

if q.WhereAndList == nil {
q.WhereAndList = &[]string{}
}

*q.WhereAndList = append(
*q.WhereAndList,
fmt.Sprintf("%s=not.gt.%s", column, getStringValue(value)),
)

return q
}

func (q *Query) OrGt(column string, value int) *Query {

if q.WhereOrList == nil {
Expand Down Expand Up @@ -161,6 +231,20 @@ func (q *Query) Gte(column string, value any) *Query {
return q
}

func (q *Query) NotGte(column string, value any) *Query {

if q.WhereAndList == nil {
q.WhereAndList = &[]string{}
}

*q.WhereAndList = append(
*q.WhereAndList,
fmt.Sprintf("%s=not.gte.%s", column, getStringValue(value)),
)

return q
}

func (q *Query) OrGte(column string, value any) *Query {

if q.WhereOrList == nil {
Expand Down Expand Up @@ -191,6 +275,22 @@ func (q *Query) In(column string, value any) *Query {
return q
}

func (q *Query) NotIn(column string, value any) *Query {

if q.WhereAndList == nil {
q.WhereAndList = &[]string{}
}

strValues := strings.Join(SliceToStringSlice(value), ",")

*q.WhereAndList = append(
*q.WhereAndList,
fmt.Sprintf("%s=not.in.(%s)", column, strValues),
)

return q
}

func (q *Query) OrIn(column string, value any) *Query {

if q.WhereOrList == nil {
Expand Down Expand Up @@ -223,6 +323,22 @@ func (q *Query) Like(column string, value string) *Query {
return q
}

func (q *Query) NotLike(column string, value string) *Query {

if q.WhereAndList == nil {
q.WhereAndList = &[]string{}
}

value = strings.ReplaceAll(value, "%", "*")

*q.WhereAndList = append(
*q.WhereAndList,
fmt.Sprintf("%s=not.like.%s", column, getStringWithSpace(value)),
)

return q
}

func (q *Query) OrLike(column string, value string) *Query {

if q.WhereOrList == nil {
Expand Down Expand Up @@ -255,6 +371,22 @@ func (q *Query) Ilike(column string, value string) *Query {
return q
}

func (q *Query) NotIlike(column string, value string) *Query {

if q.WhereAndList == nil {
q.WhereAndList = &[]string{}
}

value = strings.ReplaceAll(value, "%", "*")

*q.WhereAndList = append(
*q.WhereAndList,
fmt.Sprintf("%s=not.ilike.%s", column, getStringWithSpace(value)),
)

return q
}

func (q *Query) OrIlike(column string, value string) *Query {

if q.WhereOrList == nil {
Expand All @@ -271,6 +403,38 @@ func (q *Query) OrIlike(column string, value string) *Query {
return q
}

func (q *Query) Is(column string, value any) *Query {

Check warning on line 406 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L406

Added line #L406 was not covered by tests

isValueWhitelist(value)

Check warning on line 408 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L408

Added line #L408 was not covered by tests

if q.IsList == nil {
q.IsList = &[]string{}

Check warning on line 411 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L410-L411

Added lines #L410 - L411 were not covered by tests
}

*q.IsList = append(
*q.IsList,
fmt.Sprintf("%s=is.%s", column, getStringValue(value)),
)

Check warning on line 417 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L414-L417

Added lines #L414 - L417 were not covered by tests

return q

Check warning on line 419 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L419

Added line #L419 was not covered by tests
}

func (q *Query) NotIs(column string, value string) *Query {

Check warning on line 422 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L422

Added line #L422 was not covered by tests

isValueWhitelist(value)

Check warning on line 424 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L424

Added line #L424 was not covered by tests

if q.IsList == nil {
q.IsList = &[]string{}

Check warning on line 427 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L426-L427

Added lines #L426 - L427 were not covered by tests
}

*q.IsList = append(
*q.IsList,
fmt.Sprintf("%s=not.is.%s", column, getStringValue(value)),
)

Check warning on line 433 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L430-L433

Added lines #L430 - L433 were not covered by tests

return q

Check warning on line 435 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L435

Added line #L435 was not covered by tests
}

func getStringValue(value any) string {
return fmt.Sprintf("%v", value)
}
Expand Down Expand Up @@ -304,3 +468,15 @@ func SliceToStringSlice(slice interface{}) []string {
}
return stringSlice
}

func isValueWhitelist(value any) bool {
switch getStringValue(value) {
case "true":
case "false":
case "null":
case "unknown":
return true

Check warning on line 478 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L472-L478

Added lines #L472 - L478 were not covered by tests
}

panic("isValueWhitelist: only \"true\", \"false\", \"null\", or \"unknown\" are allowed")

Check warning on line 481 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L481

Added line #L481 was not covered by tests
}
Loading

0 comments on commit 280089d

Please sign in to comment.