sqlbuilder

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2022 License: MIT Imports: 3 Imported by: 1

README

A simple package for building sql queries in Go.

Go Reference

Usage

Basic queries
Select
// query: SELECT * FROM products
query, _ := sqlbuilder.Select("*").From("products").
		Query()

// query: SELECT id, title FROM products
query, _ := sqlbuilder.Select("id", "title").From("products").
		Query()
Update
// query: UPDATE products SET title = $1
// args: []interface{}{"new title"}
query, args := sqlbuilder.Update("products").
		Set("title", "new title").
		Query()
Insert
// query: INSERT INTO products (title, description)
// 		  VALUES ($1, $2)
// args: []interface{}{"new title", "new description"}
query, args := sqlbuilder.Insert("products").
		Columns("title", "description").
		Values("new title", "new desscription").
		Query()
Delete
// query: DELETE FROM products
query, _ := sqlbuilder.DeleteFrom("products").Query()

Documentation

Overview

package sqlbuilder provides a simple sql query builder

Index

Constants

View Source
const (
	Postgres = "postgres"
	Sqlite   = "sqlite"
	Mysql    = "mysql"
)

Dialect names

Variables

This section is empty.

Functions

This section is empty.

Types

type BuildFunc

type BuildFunc func(*Builder)

the BuildFunc type is used to add sql expressions to the provided builder

func And

func And(fns ...BuildFunc) BuildFunc

And builds an `AND` operator

func Eq

func Eq(column string, value interface{}) BuildFunc

Eq builds a `=` operator

func Expr

func Expr(expr string, args ...interface{}) BuildFunc

Expr adds a raw sql expression to the query builder

func Gt

func Gt(column string, value interface{}) BuildFunc

Gt builds a `>` operator

func Gte

func Gte(column string, value interface{}) BuildFunc

Gte builds a `>=` operator

func In

func In(col string, values ...interface{}) BuildFunc

In builds an `IN` operator

func IsNull

func IsNull(col string) BuildFunc

IsNull builds an `IS NULL` operator

func Like

func Like(col, pattern string) BuildFunc

Like builds a `LIKE` operator

func Lt

func Lt(column string, value interface{}) BuildFunc

Lt builds a `<` operator

func Lte

func Lte(column string, value interface{}) BuildFunc

Lte builds a `<=` operator

func Neq

func Neq(column string, value interface{}) BuildFunc

Neq builds a `<>` operator

func Not

func Not(fn BuildFunc) BuildFunc

Not builds a `NOT` operator

func NotIn

func NotIn(col string, values ...interface{}) BuildFunc

NotIn builds a `NOT IN` operator

func NotNull

func NotNull(col string) BuildFunc

NotNull builds an `IS NOT NULL` operator

func Or

func Or(fns ...BuildFunc) BuildFunc

Or builds an `OR` operator

type Builder

type Builder struct {
	*strings.Builder
	// contains filtered or unexported fields
}

Builder is where we put our built sql query

func NewBuilder

func NewBuilder() *Builder

NewBuilder creates a builder

func (*Builder) WriteArg

func (b *Builder) WriteArg(arg interface{})

WriteArg adds an input argument to builder

type DeleteBuilder

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

DeleteBuilder is a builder for delete queries

func DeleteFrom

func DeleteFrom(table string) *DeleteBuilder

DeleteFrom create a builder for delete queries

func (*DeleteBuilder) Query

func (db *DeleteBuilder) Query() (string, []interface{})

Query builds the sql query and returns it along with its arguments

func (*DeleteBuilder) Where

func (db *DeleteBuilder) Where(cond BuildFunc) *DeleteBuilder

Where adds a where condition to the delete query

type DialectBuilder added in v0.2.0

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

DialectBuilder is for building queries for a custom sql dialect

func Dialect added in v0.2.0

func Dialect(dialect string) DialectBuilder

Dialect creates a custom builder for the given sql dialect

func (DialectBuilder) DeleteFrom added in v0.2.0

func (db DialectBuilder) DeleteFrom(table string) *DeleteBuilder

DeleteFrom creates a DeleteBuilder for the configured dialect

func (DialectBuilder) Insert added in v0.2.0

func (db DialectBuilder) Insert(table string) *InsertBuilder

Insert creates an InsertBuilder for the configured dialect

func (DialectBuilder) Select added in v0.2.0

func (db DialectBuilder) Select(columns ...string) *SelectBuilder

Select creates a SelectBuilder for the configured dialect

func (DialectBuilder) Update added in v0.2.0

func (db DialectBuilder) Update(table string) *UpdateBuilder

Update creates an UpdateBuilder for the configured dialect

type InsertBuilder

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

InsertBuilder is a builder for insert queries

func Insert

func Insert(table string) *InsertBuilder

Insert creates a builder for insert queries

func (*InsertBuilder) Columns

func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder

Columns adds a list of columns to the insert query

func (*InsertBuilder) Query

func (b *InsertBuilder) Query() (string, []interface{})

Query builds the sql query and returns it along with its arguments

func (*InsertBuilder) Returning added in v0.2.0

func (b *InsertBuilder) Returning(columns ...string) *InsertBuilder

func (*InsertBuilder) Values

func (b *InsertBuilder) Values(values ...interface{}) *InsertBuilder

Values adds column values to the insert query

type Op

type Op int

an Op represents a sql operator

const (
	// Comparison Operators
	OpEQ  Op = iota // =
	OpNEQ           // <>
	OpGT            // >
	OpGTE           // >=
	OpLT            // <
	OpLTE           // <=

	// Logical operation
	OpAND // AND
	OpOR  // OR
	OpNot // NOT

	OpIN      // IN
	OpNotIN   // NOT IN
	OpIsNULL  // IS NULL
	OpNotNULL // IS NOT NULL
	OpLIKE    // LIKE
)

type SelectBuilder

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

SelectBuilder is a builder for select queries

func Select

func Select(columns ...string) *SelectBuilder

Select creates a builder for select queries

func (*SelectBuilder) From

func (sb *SelectBuilder) From(table string) *SelectBuilder

From sets the table that we are selecting from

func (*SelectBuilder) GroupBy

func (sb *SelectBuilder) GroupBy(columns ...string) *SelectBuilder

GroupBy adds a `GROUP BY` clause to the query

func (*SelectBuilder) Having

func (sb *SelectBuilder) Having(cond BuildFunc) *SelectBuilder

Having adds a `HAVING` condition to the query

func (*SelectBuilder) Join

func (sb *SelectBuilder) Join(table string, on BuildFunc) *SelectBuilder

Join adds an `INNER JOIN` clause to the query

func (*SelectBuilder) LeftJoin

func (sb *SelectBuilder) LeftJoin(table string, on BuildFunc) *SelectBuilder

Join adds a `LEFT JOIN` clause to the query

func (*SelectBuilder) Limit

func (sb *SelectBuilder) Limit(limit int) *SelectBuilder

Limit adds a `LIMIT` clause to the query

func (*SelectBuilder) Offset

func (sb *SelectBuilder) Offset(offset int) *SelectBuilder

Offset adds an `OFFSET` clause to the query

func (*SelectBuilder) OrderBy

func (sb *SelectBuilder) OrderBy(columns ...string) *SelectBuilder

OrderBy adds an `ORDER BY` clause to the query

func (*SelectBuilder) Query

func (sb *SelectBuilder) Query() (string, []interface{})

Query builds the sql query and returns it along with its arguments

func (*SelectBuilder) RightJoin

func (sb *SelectBuilder) RightJoin(table string, on BuildFunc) *SelectBuilder

Join adds a `RIGHT JOIN` clause to the query

func (*SelectBuilder) Where

func (sb *SelectBuilder) Where(cond BuildFunc) *SelectBuilder

Where adds a where condition to the select query

type UpdateBuilder

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

UpdateBuilder is a builder for update queries

func Update

func Update(table string) *UpdateBuilder

Update creates a builder for update queries

func (*UpdateBuilder) Query

func (b *UpdateBuilder) Query() (string, []interface{})

Query builds the sql query and returns it along with its arguments

func (*UpdateBuilder) Set

func (b *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder

Set sets the column to the provided value

func (*UpdateBuilder) Where

func (b *UpdateBuilder) Where(cond BuildFunc) *UpdateBuilder

Where adds a where condition to the update query

Jump to

Keyboard shortcuts

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