Skip to content

Commit

Permalink
Merge pull request #41 from juaismar/juaismar/refactor/linted
Browse files Browse the repository at this point in the history
Juaismar/refactor/linted
  • Loading branch information
juaismar authored Nov 26, 2020
2 parents 8f6533c + 781495a commit ee28eb3
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 141 deletions.
19 changes: 11 additions & 8 deletions SSP.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package SSP
package ssp

import (
"database/sql"
Expand All @@ -13,24 +13,28 @@ import (

var dialect = ""

// Data is a line in map that link the database field with datatable field
type Data struct {
Db string //name of column
Dt interface{} //id of column in client (int or string)
Cs bool //case sensitive - optional default false
Formatter func(data interface{}, row map[string]interface{}) (interface{}, error) // - optional
}

// MessageDataTable is theresponse object
type MessageDataTable struct {
Draw int `json:"draw"`
RecordsTotal int `json:"recordsTotal"`
RecordsFiltered int `json:"recordsFiltered"`
Data []interface{} `json:"data,nilasempty"`
}

// Controller emulate the beego controller
type Controller interface {
GetString(string, ...string) string
}

// Simple is a main method, externally called
func Simple(c Controller, conn *gorm.DB,
table string,
columns map[int]Data) (responseJSON MessageDataTable, err error) {
Expand Down Expand Up @@ -78,6 +82,7 @@ func Simple(c Controller, conn *gorm.DB,
return
}

// Complex is a main method, externally called
func Complex(c Controller, conn *gorm.DB, table string, columns map[int]Data,
whereResult []string,
whereAll []string) (responseJSON MessageDataTable, err error) {
Expand Down Expand Up @@ -316,8 +321,6 @@ func order(c Controller, columns map[int]Data) func(db *gorm.DB) *gorm.DB {
order := "desc"
if requestColumnData == "asc" {
order = "asc"
} else {
order = "desc"
}

order = checkOrderDialect(order)
Expand All @@ -338,9 +341,8 @@ func checkOrderDialect(order string) string {
if dialect == "sqlite3" {
if order == "asc" {
return "desc"
} else {
return "asc"
}
return "asc"
}

return order
Expand Down Expand Up @@ -440,10 +442,8 @@ func regExp(columndb, value string) string {
if dialect == "sqlite3" {
//TODO make regexp
return fmt.Sprintf("Lower(%s) LIKE '%s'", columndb, "%"+strings.ToLower(value)+"%")
} else {
return fmt.Sprintf("%s ~* '%s'", columndb, value)
}

return fmt.Sprintf("%s ~* '%s'", columndb, value)
}

// https://github.com/jinzhu/gorm/issues/1167
Expand All @@ -458,6 +458,9 @@ func getFields(rows *sql.Rows) (map[string]interface{}, error) {
current := makeResultReceiver(length)

columnsType, err := rows.ColumnTypes()
if err != nil {
return nil, err
}

err = rows.Scan(current...)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion SSP_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package SSP_test
package ssp_test

import (
"testing"
Expand Down
10 changes: 5 additions & 5 deletions SSP_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package SSP
package ssp_test

import (
"github.com/juaismar/gormssp/test/dbs/postgres"
Expand All @@ -14,8 +14,8 @@ var _ = Describe("Test SQLITE", func() {
ComplexFunctionTest(db)
//TODO uncoment when work
//RegExpTest(db)
TestTypes(db)
SimplexFunctionTest(db)
Types(db)
SimpleFunctionTest(db)
//TODO test id "INTEGER" type
Errors(db)
})
Expand All @@ -25,8 +25,8 @@ var _ = Describe("Test POSTGRES", func() {

ComplexFunctionTest(db)
RegExpTest(db)
TestTypes(db)
SimplexFunctionTest(db)
Types(db)
SimpleFunctionTest(db)
Errors(db)
})

Expand Down
5 changes: 5 additions & 0 deletions exported_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ssp

// Export for testing.
var Search = search
var Flated = flated
Loading

0 comments on commit ee28eb3

Please sign in to comment.