jmoiron /sqlx

general purpose extensions to golang's database/sql

★ 17,742⑂ 1,152👁 188⑃ 299● 702▤ 1
GoMITCreated 2013-01-28jmoiron.github.io/sqlx/
下载:Update dependencies to their latest stable versionsUpdate-1.4.0 ↓1.4.02024-04-23
+1
日增长
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

Latest release

Update dependencies to their latest stable versions v1.4.0 2024-04-23
This 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

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON