boulder/vendor/github.com/jmoiron/sqlx
Daniel McCarney 200e31b9d5 Update CFSSL to tip of master (ed5223a). (#3408)
Update CFSSL to get upstream ocsp changes required to minimize log
volume.

Confirmed that unit tests pass:

```
$ git rev-parse HEAD
ed5223a490ece4d66899bbb292e3e46c0677cb86

$> go test ./...
ok      github.com/cloudflare/cfssl/api 0.009s
ok      github.com/cloudflare/cfssl/api/bundle  0.811s
ok      github.com/cloudflare/cfssl/api/certadd 6.735s
?       github.com/cloudflare/cfssl/api/certinfo        [no test files]
ok      github.com/cloudflare/cfssl/api/client  0.069s
ok      github.com/cloudflare/cfssl/api/crl     0.103s
ok      github.com/cloudflare/cfssl/api/gencrl  0.008s
ok      github.com/cloudflare/cfssl/api/generator       0.051s
ok      github.com/cloudflare/cfssl/api/info    0.027s
ok      github.com/cloudflare/cfssl/api/initca  0.022s
ok      github.com/cloudflare/cfssl/api/ocsp    0.026s
ok      github.com/cloudflare/cfssl/api/revoke  0.614s
ok      github.com/cloudflare/cfssl/api/scan    51.888s
ok      github.com/cloudflare/cfssl/api/sign    0.329s
ok      github.com/cloudflare/cfssl/api/signhandler     0.056s
ok      github.com/cloudflare/cfssl/auth        0.002s
ok      github.com/cloudflare/cfssl/bundler     7.864s
?       github.com/cloudflare/cfssl/certdb      [no test files]
ok      github.com/cloudflare/cfssl/certdb/dbconf       0.003s
ok      github.com/cloudflare/cfssl/certdb/ocspstapling 1.103s
ok      github.com/cloudflare/cfssl/certdb/sql  0.369s
?       github.com/cloudflare/cfssl/certdb/testdb       [no test files]
?       github.com/cloudflare/cfssl/certinfo    [no test files]
ok      github.com/cloudflare/cfssl/cli 0.003s
ok      github.com/cloudflare/cfssl/cli/bundle  0.003s [no tests to run]
?       github.com/cloudflare/cfssl/cli/certinfo        [no test files]
ok      github.com/cloudflare/cfssl/cli/crl     0.061s
ok      github.com/cloudflare/cfssl/cli/gencert 1.518s
ok      github.com/cloudflare/cfssl/cli/gencrl  0.011s
ok      github.com/cloudflare/cfssl/cli/gencsr  0.010s
ok      github.com/cloudflare/cfssl/cli/genkey  0.583s
?       github.com/cloudflare/cfssl/cli/info    [no test files]
?       github.com/cloudflare/cfssl/cli/ocspdump        [no test files]
ok      github.com/cloudflare/cfssl/cli/ocsprefresh     0.068s
?       github.com/cloudflare/cfssl/cli/ocspserve       [no test files]
?       github.com/cloudflare/cfssl/cli/ocspsign        [no test files]
?       github.com/cloudflare/cfssl/cli/printdefault    [no test files]
ok      github.com/cloudflare/cfssl/cli/revoke  0.092s
ok      github.com/cloudflare/cfssl/cli/scan    0.003s
ok      github.com/cloudflare/cfssl/cli/selfsign        0.648s
ok      github.com/cloudflare/cfssl/cli/serve   0.016s
ok      github.com/cloudflare/cfssl/cli/sign    0.041s
ok      github.com/cloudflare/cfssl/cli/version 0.003s
ok      github.com/cloudflare/cfssl/cmd/cfssl   0.005s [no tests to run]
?       github.com/cloudflare/cfssl/cmd/cfssl-bundle    [no test files]
?       github.com/cloudflare/cfssl/cmd/cfssl-certinfo  [no test files]
?       github.com/cloudflare/cfssl/cmd/cfssl-newkey    [no test files]
?       github.com/cloudflare/cfssl/cmd/cfssl-scan      [no test files]
ok      github.com/cloudflare/cfssl/cmd/cfssljson       0.012s
ok      github.com/cloudflare/cfssl/cmd/mkbundle        0.011s [no tests
to run]
?       github.com/cloudflare/cfssl/cmd/multirootca     [no test files]
ok      github.com/cloudflare/cfssl/config      0.004s
ok      github.com/cloudflare/cfssl/crl 0.013s
?       github.com/cloudflare/cfssl/crypto      [no test files]
?       github.com/cloudflare/cfssl/crypto/pkcs7        [no test files]
ok      github.com/cloudflare/cfssl/csr 4.836s
ok      github.com/cloudflare/cfssl/errors      0.004s
ok      github.com/cloudflare/cfssl/helpers     0.037s
?       github.com/cloudflare/cfssl/helpers/derhelpers  [no test files]
ok      github.com/cloudflare/cfssl/helpers/testsuite   4.830s
?       github.com/cloudflare/cfssl/info        [no test files]
ok      github.com/cloudflare/cfssl/initca      17.794s
ok      github.com/cloudflare/cfssl/log 0.002s
ok      github.com/cloudflare/cfssl/multiroot/config    0.022s
ok      github.com/cloudflare/cfssl/ocsp        0.119s
?       github.com/cloudflare/cfssl/ocsp/config [no test files]
?       github.com/cloudflare/cfssl/ocsp/universal      [no test files]
ok      github.com/cloudflare/cfssl/revoke      2.172s
ok      github.com/cloudflare/cfssl/scan        0.003s
?       github.com/cloudflare/cfssl/scan/vendor/crypto  [no test files]
?       github.com/cloudflare/cfssl/scan/vendor/crypto/md5      [no test
files]
?       github.com/cloudflare/cfssl/scan/vendor/crypto/rsa      [no test
files]
?       github.com/cloudflare/cfssl/scan/vendor/crypto/sha1     [no test
files]
?       github.com/cloudflare/cfssl/scan/vendor/crypto/sha256   [no test
files]
?       github.com/cloudflare/cfssl/scan/vendor/crypto/sha512   [no test
files]
?       github.com/cloudflare/cfssl/scan/vendor/crypto/tls      [no test
files]
ok      github.com/cloudflare/cfssl/selfsign    0.011s
ok      github.com/cloudflare/cfssl/signer      0.003s
ok      github.com/cloudflare/cfssl/signer/local        0.419s
ok      github.com/cloudflare/cfssl/signer/remote       0.341s
ok      github.com/cloudflare/cfssl/signer/universal    0.262s
ok      github.com/cloudflare/cfssl/transport   0.017s
?       github.com/cloudflare/cfssl/transport/ca        [no test files]
ok      github.com/cloudflare/cfssl/transport/ca/localca        0.020s
ok      github.com/cloudflare/cfssl/transport/core      0.021s
?       github.com/cloudflare/cfssl/transport/example/exlib     [no test
files]
?       github.com/cloudflare/cfssl/transport/example/maclient  [no test
files]
?       github.com/cloudflare/cfssl/transport/example/maserver  [no test
files]
ok      github.com/cloudflare/cfssl/transport/kp        0.021s
?       github.com/cloudflare/cfssl/transport/roots     [no test files]
?       github.com/cloudflare/cfssl/transport/roots/system      [no test
files]
ok      github.com/cloudflare/cfssl/ubiquity    0.012s
ok      github.com/cloudflare/cfssl/whitelist   0.086s
?       github.com/cloudflare/cfssl/whitelist/example   [no test files]

```
2018-01-31 12:50:33 -08:00
..
reflectx Update CFSSL to tip of master (ed5223a). (#3408) 2018-01-31 12:50:33 -08:00
.gitignore Update CFSSL to tip of master (ed5223a). (#3408) 2018-01-31 12:50:33 -08:00
LICENSE Update CFSSL to tip of master (ed5223a). (#3408) 2018-01-31 12:50:33 -08:00
README.md Update CFSSL to tip of master (ed5223a). (#3408) 2018-01-31 12:50:33 -08:00
bind.go Update CFSSL to tip of master (ed5223a). (#3408) 2018-01-31 12:50:33 -08:00
doc.go Update CFSSL to tip of master (ed5223a). (#3408) 2018-01-31 12:50:33 -08:00
named.go Update CFSSL to tip of master (ed5223a). (#3408) 2018-01-31 12:50:33 -08:00
named_context.go Update CFSSL to tip of master (ed5223a). (#3408) 2018-01-31 12:50:33 -08:00
sqlx.go Update CFSSL to tip of master (ed5223a). (#3408) 2018-01-31 12:50:33 -08:00
sqlx_context.go Update CFSSL to tip of master (ed5223a). (#3408) 2018-01-31 12:50:33 -08:00

README.md

sqlx

Build Status Godoc license

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 standard documentation that explains how to use database/sql along with sqlx.

Recent Changes

  • sqlx/types.JsonText has been renamed to JSONText to follow Go naming conventions.

This breaks backwards compatibility, but it's in a way that is trivially fixable (s/JsonText/JSONText/g). The types package is both experimental and not in active development currently.

  • Using Go 1.6 and below with types.JSONText and types.GzippedText can be potentially unsafe, especially when used with common auto-scan sqlx idioms like Select and Get. See golang bug #13905.

Backwards Compatibility

There is no Go1-like promise of absolute stability, but I take the issue seriously and will maintain the library in a compatible state unless vital bugs prevent me from doing so. Since #59 and #60 necessitated breaking behavior, a wider API cleanup was done at the time of fixing. It's possible this will happen in future; if it does, a git tag will be provided for users requiring the old behavior to continue to use it until such a time as they can migrate.

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:

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.

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, panics on error
    // use sqlx.Open() for sql.Open() semantics
    db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
    if err != nil {
        log.Fatalln(err)
    }

    // exec the schema or fail; multi-statement Exec behavior varies between
    // database drivers;  pq will exec them all, sqlite3 won't, ymmv
    db.MustExec(schema)
    
    tx := db.MustBegin()
    tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Jason", "Moiron", "jmoiron@jmoiron.net")
    tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "John", "Doe", "johndoeDNE@gmail.net")
    tx.MustExec("INSERT INTO place (country, city, telcode) VALUES ($1, $2, $3)", "United States", "New York", "1")
    tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Hong Kong", "852")
    tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Singapore", "65")
    // Named queries can use structs, so if you have an existing struct (i.e. person := &Person{}) that you have populated, you can pass it in as &person
    tx.NamedExec("INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Jane", "Citizen", "jane.citzen@example.com"})
    tx.Commit()

    // Query the database, storing results in a []Person (wrapped in []interface{})
    people := []Person{}
    db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
    jason, john := people[0], people[1]

    fmt.Printf("%#v\n%#v", jason, john)
    // Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}
    // Person{FirstName:"John", LastName:"Doe", Email:"johndoeDNE@gmail.net"}

    // You can also get a single result, a la QueryRow
    jason = Person{}
    err = db.Get(&jason, "SELECT * FROM person WHERE first_name=$1", "Jason")
    fmt.Printf("%#v\n", jason)
    // Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}

    // if you have null fields and use SELECT *, you must use sql.Null* in your struct
    places := []Place{}
    err = db.Select(&places, "SELECT * FROM place ORDER BY telcode ASC")
    if err != nil {
        fmt.Println(err)
        return
    }
    usa, singsing, honkers := places[0], places[1], places[2]
    
    fmt.Printf("%#v\n%#v\n%#v\n", usa, singsing, honkers)
    // Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
    // Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}
    // Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}

    // Loop through rows using only one struct
    place := Place{}
    rows, err := db.Queryx("SELECT * FROM place")
    for rows.Next() {
        err := rows.StructScan(&place)
        if err != nil {
            log.Fatalln(err)
        } 
        fmt.Printf("%#v\n", place)
    }
    // Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
    // Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}
    // Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}

    // Named queries, using `:name` as the bindvar.  Automatic bindvar support
    // which takes into account the dbtype based on the driverName on sqlx.Open/Connect
    _, err = db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`, 
        map[string]interface{}{
            "first": "Bin",
            "last": "Smuth",
            "email": "bensmith@allblacks.nz",
    })

    // Selects Mr. Smith from the database
    rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:fn`, map[string]interface{}{"fn": "Bin"})

    // Named queries can also use structs.  Their bind names follow the same rules
    // as the name -> db mapping, so struct fields are lowercased and the `db` tag
    // is taken into consideration.
    rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, jason)
}