query

package
v1.31.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2026 License: GPL-3.0 Imports: 13 Imported by: 1

README

query

import "github.com/greenbone/opensight-golang-libraries/pkg/postgres/query"

Package query facilitates the translation of a result selector into a PostgresSQL conditional query string, incorporating sorting and paging functionalities.

Index

type Builder

Builder represents a query builder used to construct PostgresSQL conditional query strings with sorting and paging functionalities.

type Builder struct {
    // contains filtered or unexported fields
}

func NewPostgresQueryBuilder
func NewPostgresQueryBuilder(querySetting *Settings) *Builder

NewPostgresQueryBuilder creates a new instance of the query builder with the provided settings.

func (*Builder) Build
func (qb *Builder) Build(resultSelector query.ResultSelector) (query string, args []any, err error)

Build generates the complete SQL query based on the provided result selector. It constructs the query by adding filter, sorting, and paging conditions. It returns the constructed query string, and all the individual filter fields values (args) in a single list If any error occurs during the construction, it returns an empty string.

func (*Builder) BuildQueryConditions
func (qb *Builder) BuildQueryConditions(request *filter.Request) (args []any, err error)

BuildQueryConditions builds and appends filter conditions to the query builder based on the provided filter request. It constructs conditional clauses using the logic operator specified in the request. It uses the `?` query placeholder, so you can pass your parameter separately It returns all individual field values in a single list BuildQueryConditions can be used as a standalone function with Gorm

type Settings

Settings is a configuration struct used to customize the behavior of the query builder.

type Settings struct {
    // FilterFieldMapping is the mapping of filter fields for query customization
    // also serves as safeguard against sql injection
    FilterFieldMapping map[string]string
}

Generated by gomarkdoc

Documentation

Overview

Package query facilitates the translation of a result selector into a PostgresSQL conditional query string, incorporating sorting and paging functionalities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder represents a query builder used to construct PostgresSQL conditional query strings with sorting and paging functionalities.

func NewPostgresQueryBuilder

func NewPostgresQueryBuilder(querySetting Settings) (*Builder, error)

NewPostgresQueryBuilder creates a new instance of the query builder with the provided settings.

func (*Builder) Build

func (qb *Builder) Build(resultSelector query.ResultSelector) (query string, args []any, err error)

Build generates the complete postgres SQL query based on the provided result selector. It constructs the query by adding filter, sorting, and paging conditions. It returns the constructed query string, and all the individual filter fields values (args) in a single list

type Settings

type Settings struct {
	// FilterFieldMapping is the mapping of filter (or sorting) fields to columns in the database.
	// The columns will be part of the `WHERE` or `ORDER BY` clause, so depending on the query the entry
	// needs to be prefixed with the table name or alias used in the query.
	// It also serves as safeguard against sql injection.
	FilterFieldMapping map[string]string
	// Column used as tie breaker when sorting results. It should have a unique value for each row.
	// It will be part of the `ORDER BY` clause, so depending on the query the entry needs to be prefixed
	// with the table name or alias used in the query.
	SortingTieBreakerColumn string
}

Settings is a configuration struct used to customize the behavior of the query builder.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL