added Select method for special select column express
This commit is contained in:
parent
fb995894f0
commit
439cc27466
|
@ -510,6 +510,12 @@ func (engine *Engine) Distinct(columns ...string) *Session {
|
|||
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
|
||||
func (engine *Engine) Cols(columns ...string) *Session {
|
||||
session := engine.NewSession()
|
||||
|
|
29
session.go
29
session.go
|
@ -172,6 +172,12 @@ func (session *Session) SetExpr(column string, expression string) *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
|
||||
func (session *Session) Cols(columns ...string) *Session {
|
||||
session.Statement.Cols(columns...)
|
||||
|
@ -621,12 +627,20 @@ func (statement *Statement) convertIdSql(sqlStr string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interface{}) (has bool, err error) {
|
||||
// if has no reftable, then don't use cache currently
|
||||
func (session *Session) canCache() bool {
|
||||
if session.Statement.RefTable == nil ||
|
||||
session.Statement.JoinStr != "" ||
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
if session.Statement.RefTable == nil ||
|
||||
if !session.canCache() ||
|
||||
indexNoCase(sqlStr, "having") != -1 ||
|
||||
indexNoCase(sqlStr, "group by") != -1 ||
|
||||
session.Tx != nil {
|
||||
indexNoCase(sqlStr, "group by") != -1 {
|
||||
return ErrCacheFailed
|
||||
}
|
||||
|
||||
|
@ -1183,6 +1196,9 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
|||
var args []interface{}
|
||||
if session.Statement.RawSQL == "" {
|
||||
var columnStr string = session.Statement.ColumnStr
|
||||
if len(session.Statement.selectStr) > 0 {
|
||||
columnStr = session.Statement.selectStr
|
||||
} else {
|
||||
if session.Statement.JoinStr == "" {
|
||||
if columnStr == "" {
|
||||
if session.Statement.GroupByStr != "" {
|
||||
|
@ -1200,6 +1216,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
session.Statement.attachInSql()
|
||||
|
||||
|
|
12
statement.go
12
statement.go
|
@ -49,6 +49,7 @@ type Statement struct {
|
|||
GroupByStr string
|
||||
HavingStr string
|
||||
ColumnStr string
|
||||
selectStr string
|
||||
columnMap map[string]bool
|
||||
useAllCols bool
|
||||
OmitStr string
|
||||
|
@ -100,6 +101,7 @@ func (statement *Statement) Init() {
|
|||
statement.UseAutoTime = true
|
||||
statement.IsDistinct = false
|
||||
statement.TableAlias = ""
|
||||
statement.selectStr = ""
|
||||
statement.allUseBool = false
|
||||
statement.useAllCols = false
|
||||
statement.mustColumnMap = make(map[string]bool)
|
||||
|
@ -862,6 +864,12 @@ func (statement *Statement) Distinct(columns ...string) *Statement {
|
|||
return statement
|
||||
}
|
||||
|
||||
// replace select
|
||||
func (s *Statement) Select(str string) *Statement {
|
||||
s.selectStr = str
|
||||
return s
|
||||
}
|
||||
|
||||
// Generate "col1, col2" statement
|
||||
func (statement *Statement) Cols(columns ...string) *Statement {
|
||||
newColumns := col2NewCols(columns...)
|
||||
|
@ -1146,6 +1154,9 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
|
|||
statement.BeanArgs = args
|
||||
|
||||
var columnStr string = statement.ColumnStr
|
||||
if len(statement.selectStr) > 0 {
|
||||
columnStr = statement.selectStr
|
||||
} else {
|
||||
if len(statement.JoinStr) == 0 {
|
||||
if len(columnStr) == 0 {
|
||||
if statement.GroupByStr != "" {
|
||||
|
@ -1163,6 +1174,7 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
statement.attachInSql() // !admpub! fix bug:Iterate func missing "... IN (...)"
|
||||
return statement.genSelectSql(columnStr), append(statement.Params, statement.BeanArgs...)
|
||||
|
|
Loading…
Reference in New Issue