utils.go 680 字节
Newer Older
Y
Your Name 已提交
1
package consolemysql
E
eoLinker API Management 已提交
2 3 4 5 6

import (
	SQL "database/sql"
	"fmt"

黄孟柱 已提交
7
	"github.com/eolinker/goku-api-gateway/common/database"
E
eoLinker API Management 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
)

func getCountSQL(sql string, args ...interface{}) int {
	var count int
	countSQL := fmt.Sprintf("SELECT COUNT(*) FROM (%s) A", sql)
	err := database.GetConnection().QueryRow(countSQL, args...).Scan(&count)
	if err != nil {
		panic(err)
	}
	return count
}

func getPageSQL(sql string, orderBy, orderType string, page, pageSize int, args ...interface{}) (*SQL.Rows, error) {
	pageSQL := fmt.Sprintf("%s ORDER BY %s %s LIMIT ?,?", sql, orderBy, orderType)
	args = append(args, (page-1)*pageSize, pageSize)
	return database.GetConnection().Query(pageSQL, args...)
}