From 7a5c8ed20334610e9560e561a21ee4f3fc66bef4 Mon Sep 17 00:00:00 2001 From: biezhi Date: Thu, 24 Jan 2019 17:18:37 +0800 Subject: [PATCH] :bookmark: release 0.0.2 --- example/go.mod | 15 ++++++ example/go.sum | 23 +++++++++ example/main.go | 106 +++++++++++++++++++-------------------- pagination/pagination.go | 13 +++-- 4 files changed, 96 insertions(+), 61 deletions(-) create mode 100644 example/go.mod create mode 100644 example/go.sum diff --git a/example/go.mod b/example/go.mod new file mode 100644 index 0000000..a41ac04 --- /dev/null +++ b/example/go.mod @@ -0,0 +1,15 @@ +module example + +require ( + github.com/biezhi/gorm-paginator v0.0.0-20190124081913-54ada098dbc1 + github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 // indirect + github.com/gin-gonic/gin v1.3.0 + github.com/golang/protobuf v1.2.0 // indirect + github.com/jinzhu/gorm v1.9.2 + github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect + github.com/mattn/go-isatty v0.0.4 // indirect + github.com/mattn/go-sqlite3 v1.10.0 + github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 // indirect + gopkg.in/go-playground/validator.v8 v8.18.2 // indirect + gopkg.in/yaml.v2 v2.2.2 // indirect +) diff --git a/example/go.sum b/example/go.sum new file mode 100644 index 0000000..b8a3fdf --- /dev/null +++ b/example/go.sum @@ -0,0 +1,23 @@ +github.com/biezhi/gorm-paginator v0.0.0-20190124081913-54ada098dbc1 h1:NvwGCDDGv8Ax8SpwMhgujsrCr9gaSs5IWp90PP/PtSQ= +github.com/biezhi/gorm-paginator v0.0.0-20190124081913-54ada098dbc1/go.mod h1:Ojq6HkxEz6814vSw/+tYrn79UpRl5qH6CsA1qxl/CgI= +github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cougNAV9szl6CVoj2RYwzS3DpUQNtlY= +github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= +github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs= +github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/jinzhu/gorm v1.9.2 h1:lCvgEaqe/HVE+tjAR2mt4HbbHAZsQOv3XAZiEZV37iw= +github.com/jinzhu/gorm v1.9.2/go.mod h1:Vla75njaFJ8clLU1W44h34PjIkijhjHIYnZxMqCdxqo= +github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a h1:eeaG9XMUvRBYXJi4pg1ZKM7nxc5AfXfojeLLW7O5J3k= +github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= +github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o= +github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 h1:EICbibRW4JNKMcY+LsWmuwob+CRS1BmdRdjphAm9mH4= +github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= +gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/example/main.go b/example/main.go index 619969c..5f67a4e 100644 --- a/example/main.go +++ b/example/main.go @@ -1,73 +1,71 @@ package main import ( - "fmt" - "strconv" + "fmt" + "strconv" - "github.com/biezhi/gorm-paginator/pagination" - "github.com/gin-gonic/gin" - "github.com/jinzhu/gorm" - _ "github.com/mattn/go-sqlite3" + "github.com/biezhi/gorm-paginator/pagination" + "github.com/gin-gonic/gin" + "github.com/jinzhu/gorm" + _ "github.com/mattn/go-sqlite3" ) // User 用户 type User struct { - ID int - UserName string `gorm:"not null;size:100;unique"` + ID int + UserName string `gorm:"not null;size:100;unique"` } func main() { - db, err := gorm.Open("sqlite3", "example.db") - if err == nil { - db.AutoMigrate(&User{}) - count := 0 - db.Model(User{}).Count(&count) - if count == 0 { - db.Create(User{ID: 1, UserName: "biezhi"}) - db.Create(User{ID: 2, UserName: "rose"}) - db.Create(User{ID: 3, UserName: "jack"}) - db.Create(User{ID: 4, UserName: "lili"}) - db.Create(User{ID: 5, UserName: "bob"}) - db.Create(User{ID: 6, UserName: "tom"}) - db.Create(User{ID: 7, UserName: "anny"}) - db.Create(User{ID: 8, UserName: "wat"}) - fmt.Println("Insert OK!") - } - } else { - fmt.Println(err) - return - } + db, err := gorm.Open("sqlite3", "example.db") + if err == nil { + db.AutoMigrate(&User{}) + count := 0 + db.Model(User{}).Count(&count) + if count == 0 { + db.Create(User{ID: 1, UserName: "biezhi"}) + db.Create(User{ID: 2, UserName: "rose"}) + db.Create(User{ID: 3, UserName: "jack"}) + db.Create(User{ID: 4, UserName: "lili"}) + db.Create(User{ID: 5, UserName: "bob"}) + db.Create(User{ID: 6, UserName: "tom"}) + db.Create(User{ID: 7, UserName: "anny"}) + db.Create(User{ID: 8, UserName: "wat"}) + fmt.Println("Insert OK!") + } + } else { + fmt.Println(err) + return + } - var users []User + var users []User - pagination.Pagging(&pagination.Param{ - DB: db.Where("id > ?", 0), - Result: &users, - Page: 1, - Limit: 3, - OrderBy: []string{"id desc"}, - ShowSQL: true, - }) + pagination.Paging(&pagination.Param{ + DB: db.Where("id > ?", 0), + Page: 1, + Limit: 3, + OrderBy: []string{"id desc"}, + ShowSQL: true, + }, &users) - fmt.Println("users:", users) + fmt.Println("users:", users) - r := gin.Default() - r.GET("/", func(c *gin.Context) { - page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) - limit, _ := strconv.Atoi(c.DefaultQuery("limit", "3")) - var users []User + r := gin.Default() + r.GET("/", func(c *gin.Context) { + page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) + limit, _ := strconv.Atoi(c.DefaultQuery("limit", "3")) + var users []User - paginator := pagination.Pagging(&pagination.Param{ - DB: db, - Result: &users, - Page: page, - Limit: limit, - OrderBy: []string{"id desc"}, - ShowSQL: true, - }) - c.JSON(200, paginator) - }) + paginator := pagination.Paging(&pagination.Param{ + DB: db, + Page: page, + Limit: limit, + OrderBy: []string{"id desc"}, + ShowSQL: true, + }, &users) + c.JSON(200, paginator) + }) - r.Run() + r.Run() } diff --git a/pagination/pagination.go b/pagination/pagination.go index 11155cc..47d1690 100644 --- a/pagination/pagination.go +++ b/pagination/pagination.go @@ -9,7 +9,6 @@ import ( // Param 分页参数 type Param struct { DB *gorm.DB - Result *interface{} Page int Limit int OrderBy []string @@ -29,7 +28,7 @@ type Paginator struct { } // Paging 分页 -func Paging(p *Param) *Paginator { +func Paging(p *Param, result interface{}) *Paginator { db := p.DB if p.ShowSQL { @@ -52,7 +51,7 @@ func Paging(p *Param) *Paginator { var count int var offset int - go countRecords(db, p.Result, done, &count) + go countRecords(db, result, done, &count) if p.Page == 1 { offset = 0 @@ -60,11 +59,11 @@ func Paging(p *Param) *Paginator { offset = (p.Page - 1) * p.Limit } - db.Limit(p.Limit).Offset(offset).Find(p.Result) + db.Limit(p.Limit).Offset(offset).Find(result) <-done paginator.TotalRecord = count - paginator.Records = p.Result + paginator.Records = result paginator.Page = p.Page paginator.Offset = offset @@ -85,7 +84,7 @@ func Paging(p *Param) *Paginator { return &paginator } -func countRecords(db *gorm.DB, countDataSource interface{}, done chan bool, count *int) { - db.Model(countDataSource).Count(count) +func countRecords(db *gorm.DB, anyType interface{}, done chan bool, count *int) { + db.Model(anyType).Count(count) done <- true }