kbuilder

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: Unlicense Imports: 9 Imported by: 0

README

Welcome to the Keep It Simple Query Builder

This is the Keep It Simple query builder created to work either in conjunction or separated from the KSQL package.

Warning: Experimental package

kbuilder is new and might be subject to breaking changes, so if you want to experiment with this tool know that updating it might require manual changes to the code

TODO List

  • Add support to Update and Delete operations
  • Improve support to JOINs by adding the tablename tag to the structs
  • Add error check for when the Select, Insert and Update attrs are all empty

Documentation

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 is the basic container for injecting query builder configurations.

All the Query structs can also be called directly without this builder, but we kept it here for convenience.

func New

func New(driver string) (Builder, error)

New creates a new Builder container.

func (*Builder) Build

func (builder *Builder) Build(query queryBuilder) (sqlQuery string, params []interface{}, _ error)

Build receives a query builder struct, injects it with the configurations build the query according to its arguments.

func (*Builder) RunAndCount

func (builder *Builder) RunAndCount(ctx context.Context, db ksql.Provider, targetPtr interface{}, queryBuilder Query) (count int, err error)

RunAndCount runs a paginated input query once returning the values and then runs it again just for counting the total number of results.

type Insert

type Insert struct {
	// Into expects a table name, e.g. "users"
	Into string

	// Data expected either a single record annotated with `ksql` tags
	// or a list of records annotated likewise.
	Data interface{}

	// OmitColumns informs kbuilder of a set of columns not to use during the insertion
	OmitColumns []string

	// Returning causes the query to be built in a way that the selected attributes
	// will be returned after the insertion.
	Returning []string
}

Insert is the struct template for building INSERT queries

Experimental warning: This package is still experimental and might be subject to breaking changes in future versions.

func (Insert) Build

func (i Insert) Build(driver string) (sqlQuery string, params []interface{}, _ error)

Build is a utility function for finding the dialect based on the driver and then calling BuildQuery(dialect)

func (Insert) BuildQuery

func (i Insert) BuildQuery(dialect sqldialect.Provider) (sqlQuery string, params []interface{}, _ error)

BuildQuery implements the queryBuilder interface

type Query

type Query struct {
	// Select expects either a struct using the `ksql` tags
	// or a string listing the column names using SQL syntax,
	// e.g.: `id, username, address`
	Select interface{}

	// From expects the FROM clause from an SQL query, e.g. `users JOIN posts USING(post_id)`
	From string

	// Where expects a list of WhereQuery instances built
	// by the public Where() function.
	Where WhereQueries

	Limit   int
	Offset  int
	OrderBy string
}

Query is is the struct template for building SELECT queries.

func (Query) Build

func (q Query) Build(driver string) (sqlQuery string, params []interface{}, _ error)

Build is a utility function for finding the dialect based on the driver and then calling BuildQuery(dialect)

func (Query) BuildQuery

func (q Query) BuildQuery(dialect sqldialect.Provider) (sqlQuery string, params []interface{}, _ error)

BuildQuery implements the queryBuilder interface

type WhereQueries

type WhereQueries []WhereQuery

WhereQueries is the helper for creating complex WHERE queries in a dynamic way.

func Where

func Where(cond string, params ...interface{}) WhereQueries

Where adds a new boolean condition to an existing WhereQueries helper.

func WhereIf

func WhereIf(ifCond bool, cond string, params ...interface{}) WhereQueries

WhereIf conditionally adds a new boolean expression to the WhereQueries helper

func (WhereQueries) Where

func (w WhereQueries) Where(cond string, params ...interface{}) WhereQueries

Where adds a new boolean condition to an existing WhereQueries helper.

func (WhereQueries) WhereIf

func (w WhereQueries) WhereIf(ifCond bool, cond string, params ...interface{}) WhereQueries

WhereIf conditionally adds a new boolean expression to the WhereQueries helper.

type WhereQuery

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

WhereQuery represents a single condition in a WHERE expression.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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