func (g *NgGrid) GenerateSql(tx gorp.SqlExecutor, fields []string) error {
	var sq bytes.Buffer
	sq.WriteString("select count(*) ")
	sq.WriteString("from ( %s ) as t")
	query := fmt.Sprintf(sq.String(), g.MainQuery)
	totalCount, err := tx.SelectInt(query)
	g.TotalCount = totalCount
	g.FilterCount = g.TotalCount
	if g.TotalCount == 0 {
		return err
	}
	sq.Reset()
	if fields == nil {
		sq.WriteString("select * ")
	} else {
		sq.WriteString(fmt.Sprintf("select %s ", strings.Join(fields, ",")))
	}
	sq.WriteString("from ")
	sq.WriteString("(select row_number() over(order by %s %s) as rownum, ")
	sq.WriteString("t.* ")
	sq.WriteString("from ")
	sq.WriteString("( %s ) as t) as t ")
	sq.WriteString("where t.rownum between %d and %d ")
	if g.SortField == "" {
		return errors.New("Query sortField parameter is missing")
	}
	sortDirection := "asc"
	if strings.ToLower(g.SortDirection) == "desc" {
		sortDirection = "desc"
	}
	g.SortDirection = sortDirection
	sf := strings.Split(g.SortField, ".")
	g.SortField = sf[len(sf)-1]
	g.GeneratedQuery = fmt.Sprintf(sq.String(), strings.ToLower(g.SortField), g.SortDirection, g.MainQuery, g.FromRow(), g.ToRow())
	glog.V(4).Infoln(g.GeneratedQuery)
	return err
}
Generates the sql and calculates the number of rows from the result query
package tecgrid
import (
	"bytes"
	"errors"
	"fmt"
	"strconv"
	"strings"
	"github.com/mauleyzaola/glog"
	"github.com/mauleyzaola/gorp"
)
type NgGrid struct {
	PageNumber     int64       `json:"pageNumber"`
	PageSize       int64       `json:"pageSize"`
	Query          string      `json:"query"`
	SortDirection  string      `json:"sortDirection"`
	SortField      string      `json:"sortField"`
	TotalCount     int64       `json:"totalCount"`
	FilterCount    int64       `json:"filterCount"`
	Rows           interface{} `json:"rows"`
	GeneratedQuery string      `json:"-"`
	MainQuery      string      `json:"-"`
}
const (
	QS_SORT_DIRECTION = "sortDirection"
	QS_SORT_FIELD     = "sortField"
	QS_PAGE_NUMBER    = "pageNumber"
	QS_PAGE_SIZE      = "pageSize"
	QS_QUERY          = "query"
)
func ParseQueryString(values map[string][]string) *NgGrid {
	g := &NgGrid{}
	if value := values[QS_SORT_DIRECTION]; len(value) != 0 {
		g.SortDirection = value[0]
	}
	if value := values[QS_SORT_FIELD]; len(value) != 0 {
		g.SortField = value[0]
	}
	if value := values[QS_QUERY]; len(value) != 0 {
		g.Query = value[0]
	}
	if value := values[QS_PAGE_NUMBER]; len(value) != 0 {
		pn, err := strconv.Atoi(value[0])
		if err == nil {
			g.PageNumber = int64(pn)
		}
	}
	if value := values[QS_PAGE_SIZE]; len(value) != 0 {
		pn, err := strconv.Atoi(value[0])
		if err == nil {
			g.PageSize = int64(pn)
		}
	}
	if g.PageNumber < 1 {
		g.PageNumber = 1
	}
	return g
}
func (g *NgGrid) GetQuery() string {
	return strings.ToLower(g.Query)
}
func (g *NgGrid) GetPageNumber() int64 {
	if g.PageNumber == 0 {
		g.PageNumber = 1
	}
	return g.PageNumber
}
func (g *NgGrid) GetPageSize() int64 {
	if g.PageSize <= 0 {
		g.PageSize = 10
	}
	return g.PageSize
}
func (g *NgGrid) Skip() int64 {
	return (g.GetPageNumber() - 1) * g.GetPageSize()
}
func (g *NgGrid) Limit() int64 {
	return g.GetPageSize()
}
func (g *NgGrid) FromRow() int64 {
	return g.ToRow() - g.PageSize + 1
}
func (g *NgGrid) ToRow() int64 {
	return g.GetPageNumber() * g.GetPageSize()
}