QueryString and QueryInterface supports composite conditions (#784)
This commit is contained in:
parent
936cca7c69
commit
a6cc098689
|
@ -1376,17 +1376,17 @@ func (engine *Engine) Query(sqlorArgs ...interface{}) (resultsSlice []map[string
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryString runs a raw sql and return records as []map[string]string
|
// QueryString runs a raw sql and return records as []map[string]string
|
||||||
func (engine *Engine) QueryString(sqlStr string, args ...interface{}) ([]map[string]string, error) {
|
func (engine *Engine) QueryString(sqlorArgs ...interface{}) ([]map[string]string, error) {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
return session.QueryString(sqlStr, args...)
|
return session.QueryString(sqlorArgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryInterface runs a raw sql and return records as []map[string]interface{}
|
// QueryInterface runs a raw sql and return records as []map[string]interface{}
|
||||||
func (engine *Engine) QueryInterface(sqlStr string, args ...interface{}) ([]map[string]interface{}, error) {
|
func (engine *Engine) QueryInterface(sqlorArgs ...interface{}) ([]map[string]interface{}, error) {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
return session.QueryInterface(sqlStr, args...)
|
return session.QueryInterface(sqlorArgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert one or more records
|
// Insert one or more records
|
||||||
|
|
|
@ -48,8 +48,8 @@ type Interface interface {
|
||||||
OrderBy(order string) *Session
|
OrderBy(order string) *Session
|
||||||
Ping() error
|
Ping() error
|
||||||
Query(sqlOrAgrs ...interface{}) (resultsSlice []map[string][]byte, err error)
|
Query(sqlOrAgrs ...interface{}) (resultsSlice []map[string][]byte, err error)
|
||||||
QueryInterface(sqlStr string, args ...interface{}) ([]map[string]interface{}, error)
|
QueryInterface(sqlorArgs ...interface{}) ([]map[string]interface{}, error)
|
||||||
QueryString(sqlStr string, args ...interface{}) ([]map[string]string, error)
|
QueryString(sqlorArgs ...interface{}) ([]map[string]string, error)
|
||||||
Rows(bean interface{}) (*Rows, error)
|
Rows(bean interface{}) (*Rows, error)
|
||||||
SetExpr(string, string) *Session
|
SetExpr(string, string) *Session
|
||||||
SQL(interface{}, ...interface{}) *Session
|
SQL(interface{}, ...interface{}) *Session
|
||||||
|
|
|
@ -15,21 +15,17 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Query runs a raw sql and return records as []map[string][]byte
|
func (session *Session) genQuerySQL(sqlorArgs ...interface{}) (string, []interface{}, error) {
|
||||||
func (session *Session) Query(sqlorArgs ...interface{}) ([]map[string][]byte, error) {
|
if len(sqlorArgs) > 0 {
|
||||||
if session.isAutoClose {
|
return sqlorArgs[0].(string), sqlorArgs[1:], nil
|
||||||
defer session.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var sqlStr string
|
|
||||||
var args []interface{}
|
|
||||||
if len(sqlorArgs) == 0 {
|
|
||||||
if session.statement.RawSQL != "" {
|
if session.statement.RawSQL != "" {
|
||||||
sqlStr = session.statement.RawSQL
|
return session.statement.RawSQL, session.statement.RawParams, nil
|
||||||
args = session.statement.RawParams
|
}
|
||||||
} else {
|
|
||||||
if len(session.statement.TableName()) <= 0 {
|
if len(session.statement.TableName()) <= 0 {
|
||||||
return nil, ErrTableNotFound
|
return "", nil, ErrTableNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
var columnStr = session.statement.ColumnStr
|
var columnStr = session.statement.ColumnStr
|
||||||
|
@ -60,23 +56,32 @@ func (session *Session) Query(sqlorArgs ...interface{}) ([]map[string][]byte, er
|
||||||
|
|
||||||
condSQL, condArgs, err := builder.ToSQL(session.statement.cond)
|
condSQL, condArgs, err := builder.ToSQL(session.statement.cond)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
args = append(session.statement.joinArgs, condArgs...)
|
args := append(session.statement.joinArgs, condArgs...)
|
||||||
sqlStr, err = session.statement.genSelectSQL(columnStr, condSQL)
|
sqlStr, err := session.statement.genSelectSQL(columnStr, condSQL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
// for mssql and use limit
|
// for mssql and use limit
|
||||||
qs := strings.Count(sqlStr, "?")
|
qs := strings.Count(sqlStr, "?")
|
||||||
if len(args)*2 == qs {
|
if len(args)*2 == qs {
|
||||||
args = append(args, args...)
|
args = append(args, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return sqlStr, args, nil
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
sqlStr = sqlorArgs[0].(string)
|
// Query runs a raw sql and return records as []map[string][]byte
|
||||||
args = sqlorArgs[1:]
|
func (session *Session) Query(sqlorArgs ...interface{}) ([]map[string][]byte, error) {
|
||||||
|
if session.isAutoClose {
|
||||||
|
defer session.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlStr, args, err := session.genQuerySQL(sqlorArgs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return session.queryBytes(sqlStr, args...)
|
return session.queryBytes(sqlStr, args...)
|
||||||
|
@ -174,11 +179,16 @@ func rows2Strings(rows *core.Rows) (resultsSlice []map[string]string, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryString runs a raw sql and return records as []map[string]string
|
// QueryString runs a raw sql and return records as []map[string]string
|
||||||
func (session *Session) QueryString(sqlStr string, args ...interface{}) ([]map[string]string, error) {
|
func (session *Session) QueryString(sqlorArgs ...interface{}) ([]map[string]string, error) {
|
||||||
if session.isAutoClose {
|
if session.isAutoClose {
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sqlStr, args, err := session.genQuerySQL(sqlorArgs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
rows, err := session.queryRows(sqlStr, args...)
|
rows, err := session.queryRows(sqlStr, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -222,11 +232,16 @@ func rows2Interfaces(rows *core.Rows) (resultsSlice []map[string]interface{}, er
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryInterface runs a raw sql and return records as []map[string]interface{}
|
// QueryInterface runs a raw sql and return records as []map[string]interface{}
|
||||||
func (session *Session) QueryInterface(sqlStr string, args ...interface{}) ([]map[string]interface{}, error) {
|
func (session *Session) QueryInterface(sqlorArgs ...interface{}) ([]map[string]interface{}, error) {
|
||||||
if session.isAutoClose {
|
if session.isAutoClose {
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sqlStr, args, err := session.genQuerySQL(sqlorArgs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
rows, err := session.queryRows(sqlStr, args...)
|
rows, err := session.queryRows(sqlStr, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue