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...)
|
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()
|
||||||
|
|
55
session.go
55
session.go
|
@ -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,20 +1196,24 @@ 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 session.Statement.JoinStr == "" {
|
if len(session.Statement.selectStr) > 0 {
|
||||||
if columnStr == "" {
|
columnStr = session.Statement.selectStr
|
||||||
if session.Statement.GroupByStr != "" {
|
|
||||||
columnStr = session.Statement.Engine.Quote(strings.Replace(session.Statement.GroupByStr, ",", session.Engine.Quote(","), -1))
|
|
||||||
} else {
|
|
||||||
columnStr = session.Statement.genColumnStr()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if columnStr == "" {
|
if session.Statement.JoinStr == "" {
|
||||||
if session.Statement.GroupByStr != "" {
|
if columnStr == "" {
|
||||||
columnStr = session.Statement.Engine.Quote(strings.Replace(session.Statement.GroupByStr, ",", session.Engine.Quote(","), -1))
|
if session.Statement.GroupByStr != "" {
|
||||||
} else {
|
columnStr = session.Statement.Engine.Quote(strings.Replace(session.Statement.GroupByStr, ",", session.Engine.Quote(","), -1))
|
||||||
columnStr = "*"
|
} 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 = "*"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
38
statement.go
38
statement.go
|
@ -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,20 +1154,24 @@ 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.JoinStr) == 0 {
|
if len(statement.selectStr) > 0 {
|
||||||
if len(columnStr) == 0 {
|
columnStr = statement.selectStr
|
||||||
if statement.GroupByStr != "" {
|
|
||||||
columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1))
|
|
||||||
} else {
|
|
||||||
columnStr = statement.genColumnStr()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if len(columnStr) == 0 {
|
if len(statement.JoinStr) == 0 {
|
||||||
if statement.GroupByStr != "" {
|
if len(columnStr) == 0 {
|
||||||
columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1))
|
if statement.GroupByStr != "" {
|
||||||
} else {
|
columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1))
|
||||||
columnStr = "*"
|
} 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 = "*"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue