Merge branch 'master' of github.com:go-xorm/xorm

This commit is contained in:
Nash Tsai 2014-08-19 10:39:28 +08:00
commit d1e396ee3f
2 changed files with 48 additions and 6 deletions

View File

@ -219,12 +219,7 @@ func (session *Session) OrderBy(order string) *Session {
// Method Desc provide desc order by query condition, the input parameters are columns. // Method Desc provide desc order by query condition, the input parameters are columns.
func (session *Session) Desc(colNames ...string) *Session { func (session *Session) Desc(colNames ...string) *Session {
if session.Statement.OrderStr != "" { session.Statement.Desc(colNames...)
session.Statement.OrderStr += ", "
}
newColNames := col2NewCols(colNames...)
sqlStr := strings.Join(newColNames, session.Engine.Quote(" DESC, "))
session.Statement.OrderStr += session.Engine.Quote(sqlStr) + " DESC"
return session return session
} }

View File

@ -2,6 +2,7 @@ package xorm
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
@ -87,6 +88,7 @@ func (statement *Statement) Init() {
statement.UseAutoTime = true statement.UseAutoTime = true
statement.IsDistinct = false statement.IsDistinct = false
statement.allUseBool = false statement.allUseBool = false
statement.useAllCols = false
statement.mustColumnMap = make(map[string]bool) statement.mustColumnMap = make(map[string]bool)
statement.checkVersion = true statement.checkVersion = true
statement.inColumns = make(map[string]*inParam) statement.inColumns = make(map[string]*inParam)
@ -771,6 +773,27 @@ func col2NewCols(columns ...string) []string {
return newColumns return newColumns
} }
func (statement *Statement) col2NewColsWithQuote(columns ...string) []string {
newColumns := make([]string, 0)
for _, col := range columns {
strings.Replace(col, "`", "", -1)
strings.Replace(col, statement.Engine.QuoteStr(), "", -1)
ccols := strings.Split(col, ",")
for _, c := range ccols {
fields := strings.Split(strings.TrimSpace(c), ".")
if len(fields) == 1 {
newColumns = append(newColumns, statement.Engine.Quote(fields[0]))
} else if len(fields) == 2 {
newColumns = append(newColumns, statement.Engine.Quote(fields[0])+"."+
statement.Engine.Quote(fields[1]))
} else {
panic(errors.New("unwanted colnames"))
}
}
}
return newColumns
}
// Generate "Distince col1, col2 " statment // Generate "Distince col1, col2 " statment
func (statement *Statement) Distinct(columns ...string) *Statement { func (statement *Statement) Distinct(columns ...string) *Statement {
statement.IsDistinct = true statement.IsDistinct = true
@ -851,10 +874,34 @@ func (statement *Statement) Limit(limit int, start ...int) *Statement {
// Generate "Order By order" statement // Generate "Order By order" statement
func (statement *Statement) OrderBy(order string) *Statement { func (statement *Statement) OrderBy(order string) *Statement {
if statement.OrderStr != "" {
statement.OrderStr += ", "
}
statement.OrderStr = order statement.OrderStr = order
return statement return statement
} }
func (statement *Statement) Desc(colNames ...string) *Statement {
if statement.OrderStr != "" {
statement.OrderStr += ", "
}
newColNames := statement.col2NewColsWithQuote(colNames...)
sqlStr := strings.Join(newColNames, " DESC, ")
statement.OrderStr += sqlStr + " DESC"
return statement
}
// Method Asc provide asc order by query condition, the input parameters are columns.
func (statement *Statement) Asc(colNames ...string) *Statement {
if statement.OrderStr != "" {
statement.OrderStr += ", "
}
newColNames := statement.col2NewColsWithQuote(colNames...)
sqlStr := strings.Join(newColNames, " ASC, ")
statement.OrderStr += sqlStr + " ASC"
return statement
}
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN //The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
func (statement *Statement) Join(join_operator, tablename, condition string) *Statement { func (statement *Statement) Join(join_operator, tablename, condition string) *Statement {
if statement.JoinStr != "" { if statement.JoinStr != "" {