...
|
...
|
@@ -14,17 +14,17 @@ func PrintLogSql(sql string, param ...interface{}) { |
|
|
log.Debug(format, sql, fmt.Sprint(param...))
|
|
|
}
|
|
|
|
|
|
//ExcuteSql 执行原生sql语句
|
|
|
func ExcuteSql(result interface{}, sqlstr string, param ...interface{}) error {
|
|
|
//ExcuteQueryOne 执行原生sql查询单条记录;结果用结构体接收
|
|
|
func ExcuteQueryOne(result interface{}, sqlstr string, param ...interface{}) error {
|
|
|
PrintLogSql(sqlstr, param...)
|
|
|
var err error
|
|
|
o := orm.NewOrm()
|
|
|
err = ExcuteSqlWithOrmer(o, result, sqlstr, param)
|
|
|
err = ExcuteQueryOneWithOrmer(o, result, sqlstr, param)
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
//ExcuteSqlWithOrmer 执行原生sql语句
|
|
|
func ExcuteSqlWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param ...interface{}) error {
|
|
|
//ExcuteQueryOneWithOrmer 执行原生sql查询单条
|
|
|
func ExcuteQueryOneWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param ...interface{}) error {
|
|
|
PrintLogSql(sqlstr, param...)
|
|
|
var err error
|
|
|
err = o.Raw(sqlstr, param).QueryRow(result)
|
...
|
...
|
@@ -34,6 +34,28 @@ func ExcuteSqlWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param .. |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
//ExcuteQuerySql 执行原生sql查询多条记录
|
|
|
func ExcuteQueryAll(result interface{}, sqlstr string, param ...interface{}) error {
|
|
|
PrintLogSql(sqlstr, param...)
|
|
|
var err error
|
|
|
o := orm.NewOrm()
|
|
|
err = ExcuteQueryOneWithOrmer(o, result, sqlstr, param)
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
//ExcuteQueryOneWithOrmer 执行原生sql查询多条记录
|
|
|
func ExcuteQueryAllWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param ...interface{}) error {
|
|
|
PrintLogSql(sqlstr, param...)
|
|
|
var (
|
|
|
err error
|
|
|
)
|
|
|
_, err = o.Raw(sqlstr, param).QueryRows(result)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("SQL EXCUTE err:%s", err)
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
type QueryDataByPage struct {
|
|
|
CountSql string
|
|
|
DataSql string
|
...
|
...
|
@@ -49,6 +71,7 @@ func NewQueryDataByPage(countsql, datasql string) *QueryDataByPage { |
|
|
}
|
|
|
}
|
|
|
|
|
|
//AddParam 添加条件参数
|
|
|
func (q *QueryDataByPage) AddParam(param ...interface{}) {
|
|
|
q.Param = param
|
|
|
}
|
...
|
...
|
@@ -58,6 +81,7 @@ func (q *QueryDataByPage) LimitPage(offset, num int) { |
|
|
q.num = num
|
|
|
}
|
|
|
|
|
|
//Query 执行分页查询
|
|
|
func (q *QueryDataByPage) Query(result interface{}) (pageinfo protocol.ResponsePageInfo, err error) {
|
|
|
pagebegin := (q.offset - 1) * q.num
|
|
|
if pagebegin < 0 {
|
...
|
...
|
@@ -67,7 +91,7 @@ func (q *QueryDataByPage) Query(result interface{}) (pageinfo protocol.ResponseP |
|
|
total int
|
|
|
)
|
|
|
o := orm.NewOrm()
|
|
|
err = ExcuteSqlWithOrmer(o, &total, q.CountSql, q.Param...)
|
|
|
err = ExcuteQueryOneWithOrmer(o, &total, q.CountSql, q.Param...)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
...
|
...
|
@@ -75,7 +99,7 @@ func (q *QueryDataByPage) Query(result interface{}) (pageinfo protocol.ResponseP |
|
|
return protocol.ResponsePageInfo{CurrentPage: q.offset, TotalPage: total}, nil
|
|
|
}
|
|
|
q.DataSql = fmt.Sprintf("%s limit %d,%d", q.DataSql, pagebegin, q.num)
|
|
|
err = ExcuteSqlWithOrmer(o, result, q.DataSql, q.Param...)
|
|
|
err = ExcuteQueryAllWithOrmer(o, result, q.DataSql, q.Param...)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
...
|
...
|
|