Add support for offset in table rows endpoint, dry up code
This commit is contained in:
parent
9b5764d4fb
commit
61523e33df
@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -109,27 +108,21 @@ func GetTable(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetTableRows(c *gin.Context) {
|
func GetTableRows(c *gin.Context) {
|
||||||
limit := 1000 // Number of rows to fetch
|
offset, err := parseIntFormValue(c, "offset", 0)
|
||||||
limitVal := c.Request.FormValue("limit")
|
|
||||||
|
|
||||||
if limitVal != "" {
|
|
||||||
num, err := strconv.Atoi(limitVal)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, Error{"Invalid limit value"})
|
c.JSON(400, NewError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if num <= 0 {
|
limit, err := parseIntFormValue(c, "limit", 100)
|
||||||
c.JSON(400, Error{"Limit should be greater than 0"})
|
if err != nil {
|
||||||
|
c.JSON(400, NewError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
limit = num
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := client.RowsOptions{
|
opts := client.RowsOptions{
|
||||||
Limit: limit,
|
Limit: limit,
|
||||||
|
Offset: offset,
|
||||||
SortColumn: c.Request.FormValue("sort_column"),
|
SortColumn: c.Request.FormValue("sort_column"),
|
||||||
SortOrder: c.Request.FormValue("sort_order"),
|
SortOrder: c.Request.FormValue("sort_order"),
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"mime"
|
"mime"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sosedoff/pgweb/pkg/data"
|
"github.com/sosedoff/pgweb/pkg/data"
|
||||||
@ -33,6 +35,25 @@ func getQueryParam(c *gin.Context, name string) string {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseIntFormValue(c *gin.Context, name string, defValue int) (int, error) {
|
||||||
|
val := c.Request.FormValue(name)
|
||||||
|
|
||||||
|
if val == "" {
|
||||||
|
return defValue, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
num, err := strconv.Atoi(val)
|
||||||
|
if err != nil {
|
||||||
|
return defValue, fmt.Errorf("%s must be a number", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if num < 1 {
|
||||||
|
return defValue, fmt.Errorf("%s must be greated than 0", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return num, nil
|
||||||
|
}
|
||||||
|
|
||||||
func assetContentType(name string) string {
|
func assetContentType(name string) string {
|
||||||
ext := filepath.Ext(name)
|
ext := filepath.Ext(name)
|
||||||
result := mime.TypeByExtension(ext)
|
result := mime.TypeByExtension(ext)
|
||||||
|
@ -21,6 +21,7 @@ type Client struct {
|
|||||||
|
|
||||||
// Struct to hold table rows browsing options
|
// Struct to hold table rows browsing options
|
||||||
type RowsOptions struct {
|
type RowsOptions struct {
|
||||||
|
Offset int // Number of rows to skip
|
||||||
Limit int // Number of rows to fetch
|
Limit int // Number of rows to fetch
|
||||||
SortColumn string // Column to sort by
|
SortColumn string // Column to sort by
|
||||||
SortOrder string // Sort direction (ASC, DESC)
|
SortOrder string // Sort direction (ASC, DESC)
|
||||||
@ -110,6 +111,10 @@ func (client *Client) TableRows(table string, opts RowsOptions) (*Result, error)
|
|||||||
sql += fmt.Sprintf(" LIMIT %d", opts.Limit)
|
sql += fmt.Sprintf(" LIMIT %d", opts.Limit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opts.Offset > 0 {
|
||||||
|
sql += fmt.Sprintf(" OFFSET %d", opts.Offset)
|
||||||
|
}
|
||||||
|
|
||||||
return client.query(sql)
|
return client.query(sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user