sqlbuilder

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2016 License: MIT Imports: 2 Imported by: 0

README

sqlbuilder

Travis CI status

sqlbuilder is a Go library for building SQL queries.

Installation

go get github.com/thcyron/sqlbuilder

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 := sqlbuilder.Insert().
        Into("customers").
        Set("name", "John").
        Set("phone", "555").
        Build()
err := db.Exec(query, args...)

UPDATE

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

Supported DBMS

sqlbuilder supports building queries for MySQL and Postgres databases. You can set the default DBMS used by the package-level Select, Update and Insert functions with:

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

or you can specify the DBMS explicitly:

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

You typically set the default DBMS in init():

func init() {
        sqlbuilder.DefaultDBMS = sqlbuilder.Postgres
}

Documentation

Documentation is available at Godoc.

Versioning

sqlbuilder follows Semantic Versioning. The current version is 1.0.1.

License

sqlbuilder is licensed under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MySQL    = DBMS{/* contains filtered or unexported fields */}
	Postgres = DBMS{/* contains filtered or unexported fields */}
)
View Source
var DefaultDBMS = MySQL

DefaultDBMS is the DBMS used by the package-level Select, Insert, and Update functions.

Functions

This section is empty.

Types

type DBMS

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

DBMS represents a DBMS.

func (DBMS) Insert

func (dbms DBMS) Insert() InsertStatement

Insert returns a new INSERT statement.

func (DBMS) Placeholder

func (dbms DBMS) Placeholder(idx int) string

Placeholder returns the placeholder string for the given index.

func (DBMS) Quote

func (dbms DBMS) Quote(s string) string

Quote returns s quoted.

func (DBMS) Select

func (dbms DBMS) Select() SelectStatement

Select returns a new SELECT statement.

func (DBMS) Update

func (dbms DBMS) Update() UpdateStatement

Update returns a new UPDATE statement.

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 using the default DBMS.

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) 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) ReturnSQL

func (s InsertStatement) ReturnSQL(sql string, dest interface{}) InsertStatement

ReturnSQL is Return without quoting the argument.

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 using the default DBMS.

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) 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) MapSQL

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

MapSQL is Map without quoting col.

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 using the default DBMS.

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) 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.

Jump to

Keyboard shortcuts

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