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 c74ac36
Show file tree
Hide file tree
Showing 4 changed files with 299 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)
}

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 {

isValueWhitelist(value)

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

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

return q
}

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

isValueWhitelist(value)

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

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

return q
}

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
}

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

0 comments on commit c74ac36

Please sign in to comment.