query

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 13 Imported by: 1

README

Query Package

The query package provides flexible SQL query execution with dynamic WHERE conditions and paging support.

Features

  • Raw SQL Queries: Execute raw SQL with parameter binding
  • Dynamic WHERE: Build WHERE conditions dynamically
  • Paging Support: Automatic pagination with total count
  • Preset Parameters: Support for default/fixed parameters
  • Configurable Queries: Define queries in configuration

Main Components

RawQuery

Query definition structure:

  • Sql: Raw SQL template
  • Params: Parameter names in order
  • Where: Map of WHERE conditions to parameter keys
  • Orderby: ORDER BY clause
  • Groupby: GROUP BY clause
  • Preset: Default parameter values
  • SumEnabled: Enable total count query
Query Functions
  • Query[T](db, rawQuery, data): Execute query and return typed results
  • RawQuery.Query(db, data): Query returning map results
  • PagingResult(): Get paginated results with totals
PagingResult[T]

Generic pagination result:

  • Page: Current page number
  • PageSize: Items per page
  • Total: Total record count
  • TotalPage: Total pages
  • Data: Result data

Usage

// Define query
query := &RawQuery{
    Sql: "SELECT * FROM users {{.where}}",
    Params: []string{"owner"},
    Where: map[string]string{
        "status": "status = ?",
        "role": "role LIKE ?",
    },
}

// Execute query
results, err := query.Query(db, map[string]any{
    "owner": "admin",
    "status": "active",
    "page": 0,
    "page_size": 100,
})

Special Parameters

  • page: Page number (0-indexed)
  • page_size: Items per page (-1 for all)
  • orderby: Dynamic ORDER BY
  • groupby: Dynamic GROUP BY
  • limit: Override limit
  • offset: Override offset

Dependencies

  • GORM for database access
  • Zap for logging

Documentation

Index

Constants

View Source
const (
	KeyWhere    = "{{.where}}"
	KeyPageSize = "page_size"
	KeyPage     = "page"
)

Variables

View Source
var (
	PageSize = 100 // default page size
)

Functions

func Query added in v0.10.14

func Query[T any](db *gorm.DB, r *RawQuery, data map[string]any) ([]T, error)

Types

type PagingResult added in v0.10.18

type PagingResult[T any] struct {
	Page      int
	PageSize  int
	TotalPage int64
	Total     int64
	Error     error `json:",omitempty"`
	Data      []T   `json:",omitempty"`
}

type RawQuery

type RawQuery struct {
	Connection string // which connection should be used for query.
	Sql        string
	SumRef     string // reference to another RawQuery define, not used yet.
	SumEnabled bool
	Preset     map[string]any
	Params     []string
	Where      map[string]string // key: where condition, value: param key, e.g. "id = ?", "id"
	Orderby    string
	Groupby    string
}

func (*RawQuery) PagingResult added in v0.10.18

func (r *RawQuery) PagingResult(db *gorm.DB, result []map[string]any, req map[string]any) (*PagingResult[map[string]any], error)

func (*RawQuery) Query

func (r *RawQuery) Query(db *gorm.DB, data map[string]any) ([]map[string]any, error)

func (*RawQuery) ShouldPagingResult added in v0.10.18

func (r *RawQuery) ShouldPagingResult(req map[string]any) bool

check should return Paging Result

type RawQuerySerice

type RawQuerySerice struct {
	Source      string
	EnabledAuth bool
	Base        string
	Items       []SerivceItem
	// contains filtered or unexported fields
}

type RawQueryWhere added in v0.10.14

type RawQueryWhere struct {
	Cond  string
	Param string
	Empty string // optional settings, where condition for params is empty
}

it's not enabled yet.

type SerivceItem

type SerivceItem struct {
	Uri     string
	Query   RawQuery  // header
	Details *RawQuery // details
}

Jump to

Keyboard shortcuts

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