sqlbuilder

package module
v4.0.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2020 License: MIT Imports: 4 Imported by: 0

README

sqlbuilder

go.dev reference CI status

sqlbuilder is a Go library for building SQL queries.

The latest stable version is 4.0.0. Version 4 is identical to version 3 with added support for Go modules.

sqlbuilder follows Semantic Versioning.

Usage

import "github.com/thcyron/sqlbuilder/v4"

Examples

SELECT

query, args, dest := sqlbuilder.Select().
        From("customers").
        Map("id", &customer.ID).
        Map("name", &customer.Name).
        Map("phone", &customer.Phone).
        Order("id DESC").
        Limit(1).
        Build()

err := db.QueryRow(query, args...).Scan(dest...)

INSERT

query, args, dest := sqlbuilder.Insert().
        Into("customers").
        Set("name", "John").
        Set("phone", "555").
        Build()
res, err := db.Exec(query, args...)

UPDATE

query, args := sqlbuilder.Update().
        Table("customers").
        Set("name", "John").
        Set("phone", "555").
        Where("id = ?", 1).
        Build()
res, err := db.Exec(query, args...)

DELETE

query, args := sqlbuilder.Delete().
    From("customers").
    Where("name = ?", "John").
    Build()
res, err := db.Exec(query, args...)

Dialects

sqlbuilder supports building queries for MySQL, SQLite, and Postgres databases. You can set the default dialect with:

sqlbuilder.DefaultDialect = sqlbuilder.Postgres
sqlbuilder.Select().From("...")...

Or you can specify the dialect explicitly:

sqlbuilder.Select().Dialect(sqlbuilder.Postgres).From("...")...

License

sqlbuilder is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MySQL    mysql.Dialect    // MySQL
	SQLite   mysql.Dialect    // SQLite (same as MySQL)
	Postgres postgres.Dialect // Postgres
)
View Source
var DefaultDialect = MySQL // Default dialect

Functions

This section is empty.

Types

type DeleteStatement

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

DeleteStatement represents a DELETE statement.

func Delete

func Delete() DeleteStatement

Delete returns a new DELETE statement with the default dialect.

func (DeleteStatement) Build

func (s DeleteStatement) Build() (query string, args []interface{})

Build builds the SQL query. It returns the query and the argument slice.

func (DeleteStatement) Dialect

func (s DeleteStatement) Dialect(dialect Dialect) DeleteStatement

Dialect returns a new statement with dialect set to 'dialect'.

func (DeleteStatement) From

func (s DeleteStatement) From(table string) DeleteStatement

From returns a new statement with the table to delete from set to 'table'.

func (DeleteStatement) Where

func (s DeleteStatement) Where(cond string, args ...interface{}) DeleteStatement

Where returns a new statement with condition 'cond'. Multiple Where() are combined with AND.

type Dialect

type Dialect interface {
	// Placeholder returns the placeholder binding string for parameter at index idx.
	Placeholder(idx int) string
}

Dialect represents a SQL dialect.

type InsertStatement

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

InsertStatement represents an INSERT statement.

func Insert

func Insert() InsertStatement

Insert returns a new INSERT statement with the default dialect.

func (InsertStatement) Build

func (s InsertStatement) Build() (query string, args []interface{}, dest []interface{})

Build builds the SQL query. It returns the SQL query and the argument slice.

func (InsertStatement) Dialect

func (s InsertStatement) Dialect(dialect Dialect) InsertStatement

Dialect returns a new statement with dialect set to 'dialect'.

func (InsertStatement) Into

func (s InsertStatement) Into(table string) InsertStatement

Into returns a new statement with the table to insert into set to 'table'.

func (InsertStatement) Return

func (s InsertStatement) Return(col string, dest interface{}) InsertStatement

Return returns a new statement with a RETURNING clause.

func (InsertStatement) Set

func (s InsertStatement) Set(col string, val interface{}) InsertStatement

Set returns a new statement with column 'col' set to value 'val'.

func (InsertStatement) SetSQL

func (s InsertStatement) SetSQL(col, sql string) InsertStatement

SetSQL returns a new statement with column 'col' set to the raw SQL expression 'sql'.

type SelectStatement

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

SelectStatement represents a SELECT statement.

func Select

func Select() SelectStatement

Select returns a new SELECT statement with the default dialect.

func (SelectStatement) Build

func (s SelectStatement) Build() (query string, args []interface{}, dest []interface{})

Build builds the SQL query. It returns the query, the argument slice, and the destination slice.

func (SelectStatement) Dialect

func (s SelectStatement) Dialect(dialect Dialect) SelectStatement

Dialect returns a new statement with dialect set to 'dialect'.

func (SelectStatement) From

func (s SelectStatement) From(table string) SelectStatement

From returns a new statement with the table to select from set to 'table'.

func (SelectStatement) Group

func (s SelectStatement) Group(group string) SelectStatement

Group returns a new statement with grouping 'group'. Only the last Group() is used.

func (SelectStatement) Having

func (s SelectStatement) Having(having string) SelectStatement

Having returns a new statement with HAVING condition 'having'. Only the last Having() is used.

func (SelectStatement) Join

func (s SelectStatement) Join(sql string, args ...interface{}) SelectStatement

Join returns a new statement with JOIN expression 'sql'.

func (SelectStatement) Limit

func (s SelectStatement) Limit(limit int) SelectStatement

Limit returns a new statement with the limit set to 'limit'.

func (SelectStatement) Lock

Lock returns a new statement with FOR UPDATE locking.

func (SelectStatement) Map

func (s SelectStatement) Map(col string, dest interface{}) SelectStatement

Map returns a new statement with column 'col' selected and scanned into 'dest'. 'dest' may be nil if the value should not be scanned.

func (SelectStatement) Offset

func (s SelectStatement) Offset(offset int) SelectStatement

Offset returns a new statement with the offset set to 'offset'.

func (SelectStatement) Order

func (s SelectStatement) Order(order string) SelectStatement

Order returns a new statement with ordering 'order'. Only the last Order() is used.

func (SelectStatement) Where

func (s SelectStatement) Where(cond string, args ...interface{}) SelectStatement

Where returns a new statement with condition 'cond'. Multiple conditions are combined with AND.

type UpdateStatement

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

UpdateStatement represents an UPDATE statement.

func Update

func Update() UpdateStatement

Update returns a new UPDATE statement with the default dialect.

func (UpdateStatement) Build

func (s UpdateStatement) Build() (query string, args []interface{})

Build builds the SQL query. It returns the query and the argument slice.

func (UpdateStatement) Dialect

func (s UpdateStatement) Dialect(dialect Dialect) UpdateStatement

Dialect returns a new statement with dialect set to 'dialect'.

func (UpdateStatement) Set

func (s UpdateStatement) Set(col string, val interface{}) UpdateStatement

Set returns a new statement with column 'col' set to value 'val'.

func (UpdateStatement) SetSQL

func (s UpdateStatement) SetSQL(col string, sql string) UpdateStatement

SetSQL returns a new statement with column 'col' set to SQL expression 'sql'.

func (UpdateStatement) Table

func (s UpdateStatement) Table(table string) UpdateStatement

Table returns a new statement with the table to update set to 'table'.

func (UpdateStatement) Where

func (s UpdateStatement) Where(cond string, args ...interface{}) UpdateStatement

Where returns a new statement with condition 'cond'. Multiple Where() are combined with AND.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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