Add /api/:table/rows endpoint to fetch table rows with sorting options

This commit is contained in:
Dan Sosedoff
2015-01-03 19:42:56 -06:00
parent 2e5a006846
commit 9b0b71363f
2 changed files with 63 additions and 0 deletions

View File

@@ -22,6 +22,13 @@ type Result struct {
Rows []Row `json:"rows"`
}
// Struct to hold table rows browsing options
type RowsOptions struct {
Limit int // Number of rows to fetch
SortColumn string // Column to sort by
SortOrder string // Sort direction (ASC, DESC)
}
func NewClient() (*Client, error) {
str, err := buildConnectionString(options)
@@ -76,6 +83,24 @@ func (client *Client) Table(table string) (*Result, error) {
return client.query(PG_TABLE_SCHEMA, table)
}
func (client *Client) TableRows(table string, opts RowsOptions) (*Result, error) {
sql := fmt.Sprintf(`SELECT * FROM "%s"`, table)
if opts.SortColumn != "" {
if opts.SortOrder == "" {
opts.SortOrder = "ASC"
}
sql += fmt.Sprintf(" ORDER BY %s %s", opts.SortColumn, opts.SortOrder)
}
if opts.Limit > 0 {
sql += fmt.Sprintf(" LIMIT %d", opts.Limit)
}
return client.query(sql)
}
func (client *Client) TableInfo(table string) (*Result, error) {
return client.query(PG_TABLE_INFO, table)
}