mirror of
https://github.com/kataras/iris.git
synced 2025-03-13 21:36:28 +01:00
add sqlx example
This commit is contained in:
parent
77ce225c2f
commit
b9f38be9ea
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
113
_examples/database/orm/sqlx/main.go
Normal file
113
_examples/database/orm/sqlx/main.go
Normal 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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user