+1
Daily
17,742
stars
1,152
forks
188
watchers
299
prs
702
issues
1
releases
README
sqlx
sqlx is a library which provides a set of extensions on go's standard
database/sql library. The sqlx versions of sql.DB, sql.TX, sql.Stmt,
et al. all leave the underlying interfaces untouched, so that their interfaces
are a superset on the standard ones. This makes it relatively painless to
integrate existing codebases using database/sql with sqlx.
Major additional concepts are:
Marshal rows into structs (with embedded struct support), maps, and slices
Named parameter support including prepared statements
Get and Select to go quickly from query to struct/slice
In addition to the godoc API documentation,
there is also some user documentation that
explains how to use database/sql along with sqlx.
Recent Changes
1.3.0:
sqlx.DB.Connx(context.Context) sqlx.Conn
sqlx.BindDriver(driverName, bindType)
support for []map[string]interface{} to do "batch" insertions
allocation & perf improvements for sqlx.In
DB.Connx returns an sqlx.Conn, which is an sql.Conn-alike consistent with
sqlx's wrapping of other types.
BindDriver allows users to control the bindvars that sqlx will use for drivers,
and add new drivers at runtime. This results in a very slight performance hit
when resolving the driver into a bind type (~40ns per call), but it allows users
to specify what bindtype their driver uses even when sqlx has not been updated
to know about it by default.
Backwards Compatibility
Compatibility with the most recent two versions of Go is a requirement for any
new changes. Compatibility beyond that is not guaranteed.
Versioning is done with Go modules. Breaking changes (eg. removing deprecated API)
will get major version number bumps.
install
go get github.com/jmoiron/sqlx
issues
Row headers can be ambiguous (SELECT 1 AS a, 2 AS a), and the result of
Columns() does not fully qualify column names in queries like:
sql
SELECT a.id, a.name, b.id, b.name FROM foos AS a JOIN foos AS b ON a.parent = b.id;
making a struct or map destination ambiguous. Use AS in your queries
to give columns distinct names, rows.Scan to scan them manually, or
SliceScan to get a slice of results.
usage
Below is an example which shows some common use cases for sqlx. Check
sqlx_test.go for more
usage.
go
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq"
"github.com/jmoiron/sqlx"
)
var schema =
CREATE TABLE person (
first_name text,
last_name text,
email text
);
CREATE TABLE place (
country text,
city text NULL,
telcode integer
)
type Person struct {
FirstName string db:"first_name"
LastName string db:"last_name"
Email string
}
type Place struct {
Country string
City sql.NullString
TelCode int
}
func main() {
// this Pings the database trying to connect
// use sqlx.Open() for sql.Open() semantics
db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
if err != nil {
log.Fatalln(err)
}Top issues
#131better tools for join queries28 comments · 2015-04-07#225MapScan values are garbled21 comments · 2016-05-29#162StructScan rows after outer join17 comments · 2015-08-25#447Missing NamedQueryContext on *sqlx.Tx12 comments · 2018-09-03#88Using Multi Statement Files with MySQL & LoadFile12 comments · 2014-09-24#381Queryx not returning error if there is no result set11 comments · 2018-01-19#230Reflection when scanning and getting values seems to not be selecting the shallowest depth value10 comments · 2016-06-14#227*sql.DB as an interface9 comments · 2016-06-01
Latest release
Update dependencies to their latest stable versions v1.4.0 2024-04-23This release has updated all dependencies to their latest stable version. And now, for the auto-generated description: --- What's Changed Introduce CircleCI by @dlsniper in https://github.com/jmoiron/sqlx/pull/922 Upgrade dependencies by @dlsniper in https://github.com/jmoiron/sqlx/pull/909 New Contributors @dlsniper made their first contribution at https://github.com/jmoiron/sqlx/pull/922 Full Changelog: https://github.com/jmoiron/sqlx/compare/v1.3.5...v1.4.0