Skip to content

Commit

Permalink
🔖 release 0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Jan 24, 2019
1 parent fe4e33e commit 7a5c8ed
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 61 deletions.
15 changes: 15 additions & 0 deletions example/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
23 changes: 23 additions & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
@@ -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=
106 changes: 52 additions & 54 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -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()

}
13 changes: 6 additions & 7 deletions pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
// Param 分页参数
type Param struct {
DB *gorm.DB
Result *interface{}
Page int
Limit int
OrderBy []string
Expand All @@ -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 {
Expand All @@ -52,19 +51,19 @@ 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
} else {
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
Expand All @@ -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
}

0 comments on commit 7a5c8ed

Please sign in to comment.