Skip to content

Commit db883d5

Browse files
authored
Merge pull request #6 from go-sqlx/chore/change-readme-file
chore: update readme.md
2 parents ecd341e + bd6871b commit db883d5

1 file changed

Lines changed: 13 additions & 207 deletions

File tree

README.md

Lines changed: 13 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -3,218 +3,24 @@ Hi there,
33

44
I am a bit sad that the original developer abandoned the project and does not merge any more fixes and stuff, so I try to bring it to at least some life again. I would love to get some support doing that, so if you're interested in becoming a maintainer, feel free to contact me [@uvulpos](https://github.com/uvulpos)
55

6-
---
7-
8-
# sqlx
9-
10-
[![Build Status](https://travis-ci.org/jmoiron/sqlx.svg?branch=master)](https://travis-ci.org/jmoiron/sqlx) [![Coverage Status](https://coveralls.io/repos/github/jmoiron/sqlx/badge.svg?branch=master)](https://coveralls.io/github/jmoiron/sqlx?branch=master) [![Godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/jmoiron/sqlx) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/jmoiron/sqlx/master/LICENSE)
11-
12-
sqlx is a library which provides a set of extensions on go's standard
13-
`database/sql` library. The sqlx versions of `sql.DB`, `sql.TX`, `sql.Stmt`,
14-
et al. all leave the underlying interfaces untouched, so that their interfaces
15-
are a superset on the standard ones. This makes it relatively painless to
16-
integrate existing codebases using database/sql with sqlx.
17-
18-
Major additional concepts are:
19-
20-
* Marshal rows into structs (with embedded struct support), maps, and slices
21-
* Named parameter support including prepared statements
22-
* `Get` and `Select` to go quickly from query to struct/slice
23-
24-
In addition to the [godoc API documentation](http://godoc.org/github.com/jmoiron/sqlx),
25-
there is also some [user documentation](http://jmoiron.github.io/sqlx/) that
26-
explains how to use `database/sql` along with sqlx.
27-
28-
## Recent Changes
29-
30-
1.3.0:
31-
32-
* `sqlx.DB.Connx(context.Context) *sqlx.Conn`
33-
* `sqlx.BindDriver(driverName, bindType)`
34-
* support for `[]map[string]interface{}` to do "batch" insertions
35-
* allocation & perf improvements for `sqlx.In`
6+
## 🚧 NOT READY TO USE YET
7+
Currently still work on the project setup so please come back later again :)
368

37-
DB.Connx returns an `sqlx.Conn`, which is an `sql.Conn`-alike consistent with
38-
sqlx's wrapping of other types.
39-
40-
`BindDriver` allows users to control the bindvars that sqlx will use for drivers,
41-
and add new drivers at runtime. This results in a very slight performance hit
42-
when resolving the driver into a bind type (~40ns per call), but it allows users
43-
to specify what bindtype their driver uses even when sqlx has not been updated
44-
to know about it by default.
45-
46-
### Backwards Compatibility
9+
---
4710

48-
Compatibility with the most recent two versions of Go is a requirement for any
49-
new changes. Compatibility beyond that is not guaranteed.
11+
# go/sqlx
5012

51-
Versioning is done with Go modules. Breaking changes (eg. removing deprecated API)
52-
will get major version number bumps.
13+
- **🧐 What is sqlx?**
14+
sqlx is a library which provides a set of extensions on go's standard
15+
`database/sql` library.
16+
- **🚀 Why use sqlx?**
17+
Marshal rows into Structs, Named Prepared Statements, `Get` and `Select` to go quickly from query to struct/slice
18+
- **🤝🏻 comapitble**
19+
sqlx leaves the underlying interfaces untouched and just superset on the standard ones
20+
5321

5422
## install
5523

56-
go get github.com/jmoiron/sqlx
57-
58-
## issues
59-
60-
Row headers can be ambiguous (`SELECT 1 AS a, 2 AS a`), and the result of
61-
`Columns()` does not fully qualify column names in queries like:
62-
63-
```sql
64-
SELECT a.id, a.name, b.id, b.name FROM foos AS a JOIN foos AS b ON a.parent = b.id;
65-
```
66-
67-
making a struct or map destination ambiguous. Use `AS` in your queries
68-
to give columns distinct names, `rows.Scan` to scan them manually, or
69-
`SliceScan` to get a slice of results.
70-
71-
## usage
72-
73-
Below is an example which shows some common use cases for sqlx. Check
74-
[sqlx_test.go](https://github.com/jmoiron/sqlx/blob/master/sqlx_test.go) for more
75-
usage.
76-
77-
78-
```go
79-
package main
80-
81-
import (
82-
"database/sql"
83-
"fmt"
84-
"log"
85-
86-
_ "github.com/lib/pq"
87-
"github.com/jmoiron/sqlx"
88-
)
89-
90-
var schema = `
91-
CREATE TABLE person (
92-
first_name text,
93-
last_name text,
94-
email text
95-
);
96-
97-
CREATE TABLE place (
98-
country text,
99-
city text NULL,
100-
telcode integer
101-
)`
102-
103-
type Person struct {
104-
FirstName string `db:"first_name"`
105-
LastName string `db:"last_name"`
106-
Email string
107-
}
108-
109-
type Place struct {
110-
Country string
111-
City sql.NullString
112-
TelCode int
113-
}
114-
115-
func main() {
116-
// this Pings the database trying to connect
117-
// use sqlx.Open() for sql.Open() semantics
118-
db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
119-
if err != nil {
120-
log.Fatalln(err)
121-
}
122-
123-
// exec the schema or fail; multi-statement Exec behavior varies between
124-
// database drivers; pq will exec them all, sqlite3 won't, ymmv
125-
db.MustExec(schema)
126-
127-
tx := db.MustBegin()
128-
tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Jason", "Moiron", "jmoiron@jmoiron.net")
129-
tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "John", "Doe", "johndoeDNE@gmail.net")
130-
tx.MustExec("INSERT INTO place (country, city, telcode) VALUES ($1, $2, $3)", "United States", "New York", "1")
131-
tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Hong Kong", "852")
132-
tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Singapore", "65")
133-
// 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
134-
tx.NamedExec("INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Jane", "Citizen", "jane.citzen@example.com"})
135-
tx.Commit()
136-
137-
// Query the database, storing results in a []Person (wrapped in []interface{})
138-
people := []Person{}
139-
db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
140-
jason, john := people[0], people[1]
141-
142-
fmt.Printf("%#v\n%#v", jason, john)
143-
// Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}
144-
// Person{FirstName:"John", LastName:"Doe", Email:"johndoeDNE@gmail.net"}
145-
146-
// You can also get a single result, a la QueryRow
147-
jason = Person{}
148-
err = db.Get(&jason, "SELECT * FROM person WHERE first_name=$1", "Jason")
149-
fmt.Printf("%#v\n", jason)
150-
// Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}
151-
152-
// if you have null fields and use SELECT *, you must use sql.Null* in your struct
153-
places := []Place{}
154-
err = db.Select(&places, "SELECT * FROM place ORDER BY telcode ASC")
155-
if err != nil {
156-
fmt.Println(err)
157-
return
158-
}
159-
usa, singsing, honkers := places[0], places[1], places[2]
160-
161-
fmt.Printf("%#v\n%#v\n%#v\n", usa, singsing, honkers)
162-
// Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
163-
// Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}
164-
// Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}
165-
166-
// Loop through rows using only one struct
167-
place := Place{}
168-
rows, err := db.Queryx("SELECT * FROM place")
169-
for rows.Next() {
170-
err := rows.StructScan(&place)
171-
if err != nil {
172-
log.Fatalln(err)
173-
}
174-
fmt.Printf("%#v\n", place)
175-
}
176-
// Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
177-
// Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}
178-
// Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}
179-
180-
// Named queries, using `:name` as the bindvar. Automatic bindvar support
181-
// which takes into account the dbtype based on the driverName on sqlx.Open/Connect
182-
_, err = db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`,
183-
map[string]interface{}{
184-
"first": "Bin",
185-
"last": "Smuth",
186-
"email": "bensmith@allblacks.nz",
187-
})
188-
189-
// Selects Mr. Smith from the database
190-
rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:fn`, map[string]interface{}{"fn": "Bin"})
191-
192-
// Named queries can also use structs. Their bind names follow the same rules
193-
// as the name -> db mapping, so struct fields are lowercased and the `db` tag
194-
// is taken into consideration.
195-
rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, jason)
196-
197-
198-
// batch insert
199-
200-
// batch insert with structs
201-
personStructs := []Person{
202-
{FirstName: "Ardie", LastName: "Savea", Email: "asavea@ab.co.nz"},
203-
{FirstName: "Sonny Bill", LastName: "Williams", Email: "sbw@ab.co.nz"},
204-
{FirstName: "Ngani", LastName: "Laumape", Email: "nlaumape@ab.co.nz"},
205-
}
206-
207-
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
208-
VALUES (:first_name, :last_name, :email)`, personStructs)
24+
go get github.com/go-sqlx/sqlx
20925

210-
// batch insert with maps
211-
personMaps := []map[string]interface{}{
212-
{"first_name": "Ardie", "last_name": "Savea", "email": "asavea@ab.co.nz"},
213-
{"first_name": "Sonny Bill", "last_name": "Williams", "email": "sbw@ab.co.nz"},
214-
{"first_name": "Ngani", "last_name": "Laumape", "email": "nlaumape@ab.co.nz"},
215-
}
21626

217-
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
218-
VALUES (:first_name, :last_name, :email)`, personMaps)
219-
}
220-
```

0 commit comments

Comments
 (0)