Skip to content

Commit

Permalink
♻️ refactor(paginate.go): altera query para usar parametros dinamicos
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinícius Boscardin committed Jun 26, 2023
1 parent 6f6918c commit 9c42732
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
11 changes: 4 additions & 7 deletions paginate/paginate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package paginate

import (
"fmt"
"log"
"reflect"
"strings"
)
Expand Down Expand Up @@ -115,15 +114,13 @@ func (pagination Pagination) Select() (*string, *string) {
if pagination.search != "" && len(pagination.searchFields) > 0 {
for i, p := range pagination.searchFields {
if p != "" {
log.Println("suoo")
log.Println(p)
p = getFieldName(p, "json", pagination.structType, "paginate")
if i == 0 {
countQuery += "and ((" + p + "::TEXT ilike '%" + pagination.search + "%') "
query += "and ((" + p + "::TEXT ilike '%" + pagination.search + "%') "
countQuery += "and ((" + p + "::TEXT ilike $1) "
query += "and ((" + p + "::TEXT ilike $1) "
} else {
countQuery += "or (" + p + "::TEXT ilike '%" + pagination.search + "%') "
query += "or (" + p + "::TEXT ilike '%" + pagination.search + "%') "
countQuery += "or (" + p + "::TEXT ilike $1) "
query += "or (" + p + "::TEXT ilike $1) "
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions paginate/paginate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package paginate_test

import (
"fmt"
"testing"

"github.com/booscaaa/go-paginate/paginate"
Expand All @@ -12,8 +13,8 @@ type Test struct {
}

func TestPaginate(t *testing.T) {
queryString := "SELECT t.* FROM test t WHERE 1=1 and ((test.name::TEXT ilike '%vinicius%') ) ORDER BY name DESC, last_name ASC LIMIT 50 OFFSET 100;"
queryCountString := "SELECT COUNT(t.id) FROM test t WHERE 1=1 and ((test.name::TEXT ilike '%vinicius%') ) "
queryString := "SELECT t.* FROM test t WHERE 1=1 and ((test.name::TEXT ilike $1) ) ORDER BY name DESC, last_name ASC LIMIT 50 OFFSET 100;"
queryCountString := "SELECT COUNT(t.id) FROM test t WHERE 1=1 and ((test.name::TEXT ilike $1) ) "

pagin := paginate.Instance(Test{})
query, queryCount := pagin.
Expand All @@ -25,6 +26,8 @@ func TestPaginate(t *testing.T) {
SearchBy("vinicius", "name").
Select()

fmt.Println(*query)

if queryString != *query {
t.Errorf("Wrong query")
return
Expand All @@ -36,8 +39,8 @@ func TestPaginate(t *testing.T) {
}

func TestPaginateWithArgs(t *testing.T) {
queryString := "SELECT t.* FROM test t WHERE test.name = 'jhon' and ((test.last_name::TEXT ilike '%vinicius%') ) ORDER BY name DESC, last_name ASC LIMIT 50 OFFSET 100;"
queryCountString := "SELECT COUNT(t.id) FROM test t WHERE test.name = 'jhon' and ((test.last_name::TEXT ilike '%vinicius%') ) "
queryString := "SELECT t.* FROM test t WHERE test.name = 'jhon' and ((test.last_name::TEXT ilike $1) ) ORDER BY name DESC, last_name ASC LIMIT 50 OFFSET 100;"
queryCountString := "SELECT COUNT(t.id) FROM test t WHERE test.name = 'jhon' and ((test.last_name::TEXT ilike $1) ) "

pagin := paginate.Instance(Test{})

Expand Down

0 comments on commit 9c42732

Please sign in to comment.