add sqlx example

This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-08-30 18:24:24 +03:00
parent 77ce225c2f
commit b9f38be9ea
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
3 changed files with 115 additions and 1 deletions

View File

@ -621,7 +621,7 @@ New Context Methods:
- `Context.IsGRPC() bool` reports whether the request came from a gRPC client
- `Context.UpsertCookie(*http.Cookie, cookieOptions ...context.CookieOption)` upserts a cookie, fixes [#1485](https://github.com/kataras/iris/issues/1485) too
- `Context.StopWithStatus(int)` stops the handlers chain and writes the status code
- `Context.StopWithText(int, string)` stops the handlers chain, writes thre status code and a plain text message
- `StopWithText(statusCode int, format string, args ...interface{})` stops the handlers chain, writes thre status code and a plain text message
- `Context.StopWithError(int, error)` stops the handlers chain, writes thre status code and the error's message
- `Context.StopWithJSON(int, interface{})` stops the handlers chain, writes the status code and sends a JSON response
- `Context.StopWithProblem(int, iris.Problem)` stops the handlers, writes the status code and sends an `application/problem+json` response

View File

@ -8,6 +8,7 @@
* Database
* [MySQL, Groupcache & Docker](database/mysql)
* [MongoDB](database/mongodb)
* [Sqlx](database/orm/sqlx/main.go)
* [Xorm](database/orm/xorm/main.go)
* [Gorm](database/orm/gorm/main.go)
* [Reform](database/orm/reform/main.go)

View File

@ -0,0 +1,113 @@
package main
import (
"github.com/kataras/iris/v12"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
/*
go get -u github.com/mattn/go-sqlite3
go get -u github.com/jmoiron/sqlx
If you're on win64 and you can't install go-sqlite3:
1. Download: https://sourceforge.net/projects/mingw-w64/files/latest/download
2. Select "x86_x64" and "posix"
3. Add C:\Program Files\mingw-w64\x86_64-7.1.0-posix-seh-rt_v5-rev1\mingw64\bin
to your PATH env variable.
Docs: https://github.com/jmoiron/sqlx
*/
// Person is our person table structure.
type Person struct {
ID int64 `db:"person_id"`
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string
}
const schema = `
CREATE TABLE IF NOT EXISTS person (
person_id INTEGER PRIMARY KEY,
first_name text,
last_name text,
email text
);`
func main() {
app := iris.New()
db, err := sqlx.Connect("sqlite3", "./test.db")
if err != nil {
app.Logger().Fatalf("db failed to initialized: %v", err)
}
iris.RegisterOnInterrupt(func() {
db.Close()
})
db.MustExec(schema)
app.Get("/insert", func(ctx iris.Context) {
res, err := db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`,
map[string]interface{}{
"first": "John",
"last": "Doe",
"email": "johndoe@example.com",
})
if err != nil {
// Note: on production, don't give the error back to the user.
// However for the sake of the example we do:
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
id, err := res.LastInsertId()
if err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
ctx.Writef("person inserted: id: %d", id)
})
app.Get("/get", func(ctx iris.Context) {
// Select all persons.
people := []Person{}
db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
if err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
if len(people) == 0 {
ctx.Writef("no persons found, use /insert first.")
return
}
ctx.Writef("persons found: %#v", people)
/* Select a single or more with a first name of John from the database:
person := Person{FirstName: "John"}
rows, err := db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, person)
if err != nil { ... }
defer rows.Close()
for rows.Next() {
if err := rows.StructScan(&person); err != nil {
if err == sql.ErrNoRows {
ctx.StopWithText(iris.StatusNotFound, "Person: %s not found", person.FirstName)
} else {
ctx.StopWithError(iris.StatusInternalServerError, err)
}
return
}
}
*/
})
// http://localhost:8080/insert
// http://localhost:8080/get
app.Listen(":8080")
}