kbuilder

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: MIT, Unlicense Imports: 7 Imported by: 0

README

Welcome to the KISS Query Builder

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

This package was started after ksql and while the ksql is already in a usable state I still don't recommend using this one since this being actively implemented and might change without further warning.

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.

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{}
}

Insert is the struct template for building INSERT queries

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 OrderByQuery

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

OrderByQuery represents the ORDER BY part of the query

func OrderBy

func OrderBy(fields string) OrderByQuery

OrderBy is a helper for building the ORDER BY part of the query.

func (OrderByQuery) Desc

func (o OrderByQuery) Desc() OrderByQuery

Desc is a setter function for configuring the ORDER BY part of the query as DESC

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 OrderByQuery
}

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(cond string, param 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(cond string, param 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.

Jump to

Keyboard shortcuts

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