# README
sorting
import "github.com/greenbone/opensight-golang-libraries/pkg/query/sorting"
Index
- Constants
- func AddRequest(transaction *gorm.DB, params Params) *gorm.DB
- func NewSortingError(format string, value ...any) error
- func ValidateSortingRequest(req *Request) error
- type Error
- type Params
- type Request
- type Response
- type SortDefault
- type SortDirection
- type SortableColumn
- type SortingSettingsInterface
Constants
const (
// SortColumnTag is a tag to define the field name to be used for sorting
SortColumnTag = "sortColumn"
/*SortColumnOverrideTag is a tag to override the field name to be used for sorting.
An example use case is the following:
If you take a column to order by, which is part of the original table (like hostname) the SQL syntax is fine - see the generated GORM query below.´.
SELECT "asset"."id","asset"."created_at","asset"."updated_at","asset"."greenbone_agent_id","asset"."last_authenticated_scan_at","asset"."mac_address","asset"."net_bios_name","asset"."ssh_fingerprint","asset"."has_agent","asset"."has_vt_result","asset"."hostname","asset"."ip","asset"."last_scan_at","asset"."operating_system","asset"."deleted_at","asset"."deleted_by","asset"."source_id","asset"."appliance_id" FROM "asset" LEFT JOIN "appliance" "Appliance" ON "asset"."appliance_id" = "Appliance"."id" LEFT JOIN "installed_software" "InstalledSoftwares" ON "asset"."id" = "InstalledSoftwares"."asset_id" WHERE ("Appliance"."name" ILIKE '%Example%') ORDER BY hostname ASC LIMIT 10
If we now change to sort by a column of a joined table, the SQL syntax is "TABLENAME"."Columnname". Now see the
LEFT JOIN "appliance" "Appliance" which means to access the table appliance we now need to use "Appliance" instead of "appliance".
Therefore its necassary to change the SQL syntax for the foreign table sort.
SELECT "asset"."id","asset"."created_at","asset"."updated_at","asset"."greenbone_agent_id","asset"."last_authenticated_scan_at","asset"."mac_address","asset"."net_bios_name","asset"."ssh_fingerprint","asset"."has_agent","asset"."has_vt_result","asset"."hostname","asset"."ip","asset"."last_scan_at","asset"."operating_system","asset"."deleted_at","asset"."deleted_by","asset"."source_id","asset"."appliance_id" FROM "asset" LEFT JOIN "appliance" "Appliance" ON "asset"."appliance_id" = "Appliance"."id" LEFT JOIN "installed_software" "InstalledSoftwares" ON "asset"."id" = "InstalledSoftwares"."asset_id" ORDER BY "Appliance"."name" DESC LIMIT 20
If we query for the appliance name GORM changes it correctly to "Appliance"."name" - see the query below. And the same we now do in our code for the sorting - we change the JOINED field from appliance.name to "Appliance"."name".
SELECT "asset"."id","asset"."created_at","asset"."updated_at","asset"."greenbone_agent_id","asset"."last_authenticated_scan_at","asset"."mac_address","asset"."net_bios_name","asset"."ssh_fingerprint","asset"."has_agent","asset"."has_vt_result","asset"."hostname","asset"."ip","asset"."last_scan_at","asset"."operating_system","asset"."deleted_at","asset"."deleted_by","asset"."source_id","asset"."appliance_id" FROM "asset" LEFT JOIN "appliance" "Appliance" ON "asset"."appliance_id" = "Appliance"."id" LEFT JOIN "installed_software" "InstalledSoftwares" ON "asset"."id" = "InstalledSoftwares"."asset_id" WHERE ("Appliance"."name" ILIKE '%Example%') ORDER BY hostname ASC LIMIT 10
*/
SortColumnOverrideTag = "sortColumnOverride"
// SortDirectionTag is a tag that defines the sort (i.e. direction of sorting); must be a SortDirection
SortDirectionTag = "sortDirection"
)
func AddRequest
func AddRequest(transaction *gorm.DB, params Params) *gorm.DB
func NewSortingError
func NewSortingError(format string, value ...any) error
func ValidateSortingRequest
func ValidateSortingRequest(req *Request) error
ValidateSortingRequest validates a sorting request.
type Error
type Error struct {
Msg string
}
func (*Error) Error
func (e *Error) Error() string
type Params
type Params struct {
OriginalSortColumn string
SortDirection SortDirection
EffectiveSortColumn string
}
func DetermineEffectiveSortingParams
func DetermineEffectiveSortingParams(model SortingSettingsInterface, sortingReq *Request) (Params, error)
DetermineEffectiveSortingParams checks the requested sorting and sets the defaults in case of an error. If a SortColumnOverrideTag (sortColumnOverride) is given, it's value will be used for sorting instead of SortColumnTag (sortColumn). For a detailed explanation see SortColumnOverrideTag
type Request
Request represents a sorting request with a specified sort column and sort direction.
Fields: - SortColumn: the column to sort on - SortDirection: the direction of sorting (asc or desc)
type Request struct {
SortColumn string `json:"column"`
SortDirection SortDirection `json:"direction"`
}
type Response
Response represents the response structure for sorting column and direction. SortingColumn stores the name of the column which was used for sorting. SortingDirection stores the direction which was applied by the sorting.
type Response struct {
SortingColumn string `json:"column"`
SortingDirection SortDirection `json:"direction"`
}
type SortDefault
SortDefault holds the default for sort direction and sorting field.
type SortDefault struct {
Column string
Direction SortDirection
}
func GetSortDefaults
func GetSortDefaults(model SortingSettingsInterface) (result SortDefault, err error)
GetSortDefaults returns the sortable fields based on the struct provided.
type SortDirection
type SortDirection string
const (
DirectionDescending SortDirection = "desc"
DirectionAscending SortDirection = "asc"
NoDirection SortDirection = ""
)
func SortDirectionFromString
func SortDirectionFromString(str string) SortDirection
func (SortDirection) String
func (s SortDirection) String() string
type SortableColumn
SortableColumn is a struct to hold the fields which the paging can sort by.
type SortableColumn struct {
Column string
ColumnOverride string
}
func GetSortableColumns
func GetSortableColumns(model SortingSettingsInterface) (sortables []SortableColumn)
GetSortableColumns returns a list of sortable fields
type SortingSettingsInterface
type SortingSettingsInterface interface {
GetSortDefault() SortDefault
GetSortingMap() map[string]string
GetOverrideSortColumn(string) string
}
Generated by gomarkdoc
License
Copyright (C) 2022-2023 [Greenbone AG][Greenbone AG]
Licensed under the GNU General Public License v3.0 or later.