Skip to content

thcyron/sqlbuilder

Repository files navigation

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.

About

sqlbuilder is a Go library for building SQL queries.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages