2017-01-03 01:20:28 -07:00
|
|
|
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2021-08-12 17:11:42 -06:00
|
|
|
"strings"
|
2017-01-03 01:20:28 -07:00
|
|
|
|
2020-03-22 09:12:55 -06:00
|
|
|
"xorm.io/xorm/core"
|
2017-01-03 01:20:28 -07:00
|
|
|
)
|
|
|
|
|
2017-09-12 23:18:22 -06:00
|
|
|
func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) {
|
|
|
|
for _, filter := range session.engine.dialect.Filters() {
|
2020-03-22 09:12:55 -06:00
|
|
|
*sqlStr = filter.Do(*sqlStr)
|
2017-01-03 01:20:28 -07:00
|
|
|
}
|
2017-09-12 23:18:22 -06:00
|
|
|
|
|
|
|
session.lastSQL = *sqlStr
|
|
|
|
session.lastSQLArgs = paramStr
|
2017-01-03 01:20:28 -07:00
|
|
|
}
|
|
|
|
|
2017-09-12 23:18:22 -06:00
|
|
|
func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Rows, error) {
|
|
|
|
defer session.resetStatement()
|
2020-03-22 09:12:55 -06:00
|
|
|
if session.statement.LastError != nil {
|
|
|
|
return nil, session.statement.LastError
|
|
|
|
}
|
2017-01-03 01:20:28 -07:00
|
|
|
|
2017-09-12 23:18:22 -06:00
|
|
|
session.queryPreprocess(&sqlStr, args...)
|
2017-01-03 01:20:28 -07:00
|
|
|
|
2020-03-22 09:12:55 -06:00
|
|
|
session.lastSQL = sqlStr
|
|
|
|
session.lastSQLArgs = args
|
2017-09-12 23:18:22 -06:00
|
|
|
|
|
|
|
if session.isAutoCommit {
|
2018-01-27 08:20:59 -07:00
|
|
|
var db *core.DB
|
2021-08-12 17:11:42 -06:00
|
|
|
if session.sessionType == groupSession && strings.EqualFold(sqlStr[:6], "select") {
|
2018-01-27 08:20:59 -07:00
|
|
|
db = session.engine.engineGroup.Slave().DB()
|
|
|
|
} else {
|
|
|
|
db = session.DB()
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:18:22 -06:00
|
|
|
if session.prepareStmt {
|
|
|
|
// don't clear stmt since session will cache them
|
2018-01-27 08:20:59 -07:00
|
|
|
stmt, err := session.doPrepare(db, sqlStr)
|
2017-01-03 01:20:28 -07:00
|
|
|
if err != nil {
|
2017-09-12 23:18:22 -06:00
|
|
|
return nil, err
|
2017-01-03 01:20:28 -07:00
|
|
|
}
|
2017-09-12 23:18:22 -06:00
|
|
|
|
2019-06-23 09:22:43 -06:00
|
|
|
rows, err := stmt.QueryContext(session.ctx, args...)
|
2017-01-03 01:20:28 -07:00
|
|
|
if err != nil {
|
2017-09-12 23:18:22 -06:00
|
|
|
return nil, err
|
2017-01-03 01:20:28 -07:00
|
|
|
}
|
2017-09-12 23:18:22 -06:00
|
|
|
return rows, nil
|
2017-01-03 01:20:28 -07:00
|
|
|
}
|
2017-09-12 23:18:22 -06:00
|
|
|
|
2019-06-23 09:22:43 -06:00
|
|
|
rows, err := db.QueryContext(session.ctx, sqlStr, args...)
|
2017-09-12 23:18:22 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-01-03 01:20:28 -07:00
|
|
|
}
|
2017-09-12 23:18:22 -06:00
|
|
|
return rows, nil
|
2017-01-03 01:20:28 -07:00
|
|
|
}
|
|
|
|
|
2019-06-23 09:22:43 -06:00
|
|
|
rows, err := session.tx.QueryContext(session.ctx, sqlStr, args...)
|
2017-08-22 05:39:52 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:18:22 -06:00
|
|
|
return rows, nil
|
|
|
|
}
|
2017-08-22 05:39:52 -06:00
|
|
|
|
2017-09-12 23:18:22 -06:00
|
|
|
func (session *Session) queryRow(sqlStr string, args ...interface{}) *core.Row {
|
|
|
|
return core.NewRow(session.queryRows(sqlStr, args...))
|
2017-08-22 05:39:52 -06:00
|
|
|
}
|
|
|
|
|
2017-09-12 23:18:22 -06:00
|
|
|
func (session *Session) queryBytes(sqlStr string, args ...interface{}) ([]map[string][]byte, error) {
|
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
2017-08-22 05:39:52 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2021-08-12 17:11:42 -06:00
|
|
|
return rows2maps(rows)
|
2017-08-22 05:39:52 -06:00
|
|
|
}
|
|
|
|
|
2017-09-12 23:18:22 -06:00
|
|
|
func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, error) {
|
2017-04-06 19:47:25 -06:00
|
|
|
defer session.resetStatement()
|
|
|
|
|
2017-05-01 18:50:33 -06:00
|
|
|
session.queryPreprocess(&sqlStr, args...)
|
2017-01-03 01:20:28 -07:00
|
|
|
|
2020-03-22 09:12:55 -06:00
|
|
|
session.lastSQL = sqlStr
|
|
|
|
session.lastSQLArgs = args
|
2017-09-12 23:18:22 -06:00
|
|
|
|
|
|
|
if !session.isAutoCommit {
|
2019-06-23 09:22:43 -06:00
|
|
|
return session.tx.ExecContext(session.ctx, sqlStr, args...)
|
2017-01-03 01:20:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if session.prepareStmt {
|
2018-01-27 08:20:59 -07:00
|
|
|
stmt, err := session.doPrepare(session.DB(), sqlStr)
|
2017-01-03 01:20:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-23 09:22:43 -06:00
|
|
|
res, err := stmt.ExecContext(session.ctx, args...)
|
2017-01-03 01:20:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2019-06-23 09:22:43 -06:00
|
|
|
return session.DB().ExecContext(session.ctx, sqlStr, args...)
|
2017-01-03 01:20:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exec raw sql
|
2019-06-23 09:22:43 -06:00
|
|
|
func (session *Session) Exec(sqlOrArgs ...interface{}) (sql.Result, error) {
|
2017-08-22 05:39:52 -06:00
|
|
|
if session.isAutoClose {
|
2017-01-03 01:20:28 -07:00
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
2019-06-23 09:22:43 -06:00
|
|
|
if len(sqlOrArgs) == 0 {
|
2018-12-11 18:01:41 -07:00
|
|
|
return nil, ErrUnSupportedType
|
|
|
|
}
|
|
|
|
|
2020-03-22 09:12:55 -06:00
|
|
|
sqlStr, args, err := session.statement.ConvertSQLOrArgs(sqlOrArgs...)
|
2018-12-11 18:01:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-03 01:20:28 -07:00
|
|
|
return session.exec(sqlStr, args...)
|
|
|
|
}
|