diff --git a/engine.go b/engine.go index fa852bad..bbe8eb47 100644 --- a/engine.go +++ b/engine.go @@ -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() diff --git a/session.go b/session.go index bb30ab97..f4c56a49 100644 --- a/session.go +++ b/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,20 +1196,24 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) var args []interface{} if session.Statement.RawSQL == "" { var columnStr string = session.Statement.ColumnStr - if session.Statement.JoinStr == "" { - if columnStr == "" { - if session.Statement.GroupByStr != "" { - columnStr = session.Statement.Engine.Quote(strings.Replace(session.Statement.GroupByStr, ",", session.Engine.Quote(","), -1)) - } else { - columnStr = session.Statement.genColumnStr() - } - } + if len(session.Statement.selectStr) > 0 { + columnStr = session.Statement.selectStr } else { - if columnStr == "" { - if session.Statement.GroupByStr != "" { - columnStr = session.Statement.Engine.Quote(strings.Replace(session.Statement.GroupByStr, ",", session.Engine.Quote(","), -1)) - } else { - columnStr = "*" + if session.Statement.JoinStr == "" { + if columnStr == "" { + if session.Statement.GroupByStr != "" { + columnStr = session.Statement.Engine.Quote(strings.Replace(session.Statement.GroupByStr, ",", session.Engine.Quote(","), -1)) + } else { + columnStr = session.Statement.genColumnStr() + } + } + } else { + if columnStr == "" { + if session.Statement.GroupByStr != "" { + columnStr = session.Statement.Engine.Quote(strings.Replace(session.Statement.GroupByStr, ",", session.Engine.Quote(","), -1)) + } else { + columnStr = "*" + } } } } diff --git a/statement.go b/statement.go index 9a62d6f0..e55b5349 100644 --- a/statement.go +++ b/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,20 +1154,24 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) statement.BeanArgs = args var columnStr string = statement.ColumnStr - if len(statement.JoinStr) == 0 { - if len(columnStr) == 0 { - if statement.GroupByStr != "" { - columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1)) - } else { - columnStr = statement.genColumnStr() - } - } + if len(statement.selectStr) > 0 { + columnStr = statement.selectStr } else { - if len(columnStr) == 0 { - if statement.GroupByStr != "" { - columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1)) - } else { - columnStr = "*" + if len(statement.JoinStr) == 0 { + if len(columnStr) == 0 { + if statement.GroupByStr != "" { + columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1)) + } else { + columnStr = statement.genColumnStr() + } + } + } else { + if len(columnStr) == 0 { + if statement.GroupByStr != "" { + columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1)) + } else { + columnStr = "*" + } } } } diff --git a/xorm.go b/xorm.go index bb7d6461..d9c8330c 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.4.3.0520" + Version string = "0.4.3.0524" ) func regDrvsNDialects() bool {