added Select method for special select column express

This commit is contained in:
Lunny Xiao 2015-05-24 21:32:27 +08:00
parent fb995894f0
commit 439cc27466
4 changed files with 68 additions and 33 deletions

View File

@ -510,6 +510,12 @@ func (engine *Engine) Distinct(columns ...string) *Session {
return session.Distinct(columns...) return session.Distinct(columns...)
} }
func (engine *Engine) Select(str string) *Session {
session := engine.NewSession()
session.IsAutoClose = true
return session.Select(str)
}
// only use the paramters as select or update columns // only use the paramters as select or update columns
func (engine *Engine) Cols(columns ...string) *Session { func (engine *Engine) Cols(columns ...string) *Session {
session := engine.NewSession() session := engine.NewSession()

View File

@ -172,6 +172,12 @@ func (session *Session) SetExpr(column string, expression string) *Session {
return session return session
} }
// Method Cols provides some columns to special
func (session *Session) Select(str string) *Session {
session.Statement.Select(str)
return session
}
// Method Cols provides some columns to special // Method Cols provides some columns to special
func (session *Session) Cols(columns ...string) *Session { func (session *Session) Cols(columns ...string) *Session {
session.Statement.Cols(columns...) session.Statement.Cols(columns...)
@ -621,12 +627,20 @@ func (statement *Statement) convertIdSql(sqlStr string) string {
return "" return ""
} }
func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interface{}) (has bool, err error) { func (session *Session) canCache() bool {
// if has no reftable, then don't use cache currently
if session.Statement.RefTable == nil || if session.Statement.RefTable == nil ||
session.Statement.JoinStr != "" || session.Statement.JoinStr != "" ||
session.Statement.RawSQL != "" || session.Statement.RawSQL != "" ||
session.Tx != nil { session.Tx != nil ||
len(session.Statement.selectStr) > 0 {
return false
}
return true
}
func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interface{}) (has bool, err error) {
// if has no reftable, then don't use cache currently
if !session.canCache() {
return false, ErrCacheFailed return false, ErrCacheFailed
} }
@ -724,10 +738,9 @@ func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interf
} }
func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr interface{}, args ...interface{}) (err error) { func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr interface{}, args ...interface{}) (err error) {
if session.Statement.RefTable == nil || if !session.canCache() ||
indexNoCase(sqlStr, "having") != -1 || indexNoCase(sqlStr, "having") != -1 ||
indexNoCase(sqlStr, "group by") != -1 || indexNoCase(sqlStr, "group by") != -1 {
session.Tx != nil {
return ErrCacheFailed return ErrCacheFailed
} }
@ -1183,6 +1196,9 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
var args []interface{} var args []interface{}
if session.Statement.RawSQL == "" { if session.Statement.RawSQL == "" {
var columnStr string = session.Statement.ColumnStr var columnStr string = session.Statement.ColumnStr
if len(session.Statement.selectStr) > 0 {
columnStr = session.Statement.selectStr
} else {
if session.Statement.JoinStr == "" { if session.Statement.JoinStr == "" {
if columnStr == "" { if columnStr == "" {
if session.Statement.GroupByStr != "" { if session.Statement.GroupByStr != "" {
@ -1200,6 +1216,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
} }
} }
} }
}
session.Statement.attachInSql() session.Statement.attachInSql()

View File

@ -49,6 +49,7 @@ type Statement struct {
GroupByStr string GroupByStr string
HavingStr string HavingStr string
ColumnStr string ColumnStr string
selectStr string
columnMap map[string]bool columnMap map[string]bool
useAllCols bool useAllCols bool
OmitStr string OmitStr string
@ -100,6 +101,7 @@ func (statement *Statement) Init() {
statement.UseAutoTime = true statement.UseAutoTime = true
statement.IsDistinct = false statement.IsDistinct = false
statement.TableAlias = "" statement.TableAlias = ""
statement.selectStr = ""
statement.allUseBool = false statement.allUseBool = false
statement.useAllCols = false statement.useAllCols = false
statement.mustColumnMap = make(map[string]bool) statement.mustColumnMap = make(map[string]bool)
@ -862,6 +864,12 @@ func (statement *Statement) Distinct(columns ...string) *Statement {
return statement return statement
} }
// replace select
func (s *Statement) Select(str string) *Statement {
s.selectStr = str
return s
}
// Generate "col1, col2" statement // Generate "col1, col2" statement
func (statement *Statement) Cols(columns ...string) *Statement { func (statement *Statement) Cols(columns ...string) *Statement {
newColumns := col2NewCols(columns...) newColumns := col2NewCols(columns...)
@ -1146,6 +1154,9 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
statement.BeanArgs = args statement.BeanArgs = args
var columnStr string = statement.ColumnStr var columnStr string = statement.ColumnStr
if len(statement.selectStr) > 0 {
columnStr = statement.selectStr
} else {
if len(statement.JoinStr) == 0 { if len(statement.JoinStr) == 0 {
if len(columnStr) == 0 { if len(columnStr) == 0 {
if statement.GroupByStr != "" { if statement.GroupByStr != "" {
@ -1163,6 +1174,7 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
} }
} }
} }
}
statement.attachInSql() // !admpub! fix bug:Iterate func missing "... IN (...)" statement.attachInSql() // !admpub! fix bug:Iterate func missing "... IN (...)"
return statement.genSelectSql(columnStr), append(statement.Params, statement.BeanArgs...) return statement.genSelectSql(columnStr), append(statement.Params, statement.BeanArgs...)

View File

@ -17,7 +17,7 @@ import (
) )
const ( const (
Version string = "0.4.3.0520" Version string = "0.4.3.0524"
) )
func regDrvsNDialects() bool { func regDrvsNDialects() bool {