2017-01-03 16:20:28 +08: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-13 07:11:42 +08:00
|
|
|
"strings"
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
"xorm.io/xorm/core"
|
2017-01-03 16:20:28 +08:00
|
|
|
)
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) {
|
|
|
|
for _, filter := range session.engine.dialect.Filters() {
|
2020-03-22 23:12:55 +08:00
|
|
|
*sqlStr = filter.Do(*sqlStr)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2017-09-13 13:18:22 +08:00
|
|
|
|
|
|
|
session.lastSQL = *sqlStr
|
|
|
|
session.lastSQLArgs = paramStr
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Rows, error) {
|
|
|
|
defer session.resetStatement()
|
2020-03-22 23:12:55 +08:00
|
|
|
if session.statement.LastError != nil {
|
|
|
|
return nil, session.statement.LastError
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
session.queryPreprocess(&sqlStr, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
session.lastSQL = sqlStr
|
|
|
|
session.lastSQLArgs = args
|
2017-09-13 13:18:22 +08:00
|
|
|
|
|
|
|
if session.isAutoCommit {
|
2018-01-27 23:20:59 +08:00
|
|
|
var db *core.DB
|
2021-08-13 07:11:42 +08:00
|
|
|
if session.sessionType == groupSession && strings.EqualFold(sqlStr[:6], "select") {
|
2018-01-27 23:20:59 +08:00
|
|
|
db = session.engine.engineGroup.Slave().DB()
|
|
|
|
} else {
|
|
|
|
db = session.DB()
|
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
if session.prepareStmt {
|
|
|
|
// don't clear stmt since session will cache them
|
2018-01-27 23:20:59 +08:00
|
|
|
stmt, err := session.doPrepare(db, sqlStr)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
2017-09-13 13:18:22 +08:00
|
|
|
return nil, err
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2017-09-13 13:18:22 +08:00
|
|
|
|
2019-06-23 23:22:43 +08:00
|
|
|
rows, err := stmt.QueryContext(session.ctx, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
2017-09-13 13:18:22 +08:00
|
|
|
return nil, err
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2017-09-13 13:18:22 +08:00
|
|
|
return rows, nil
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2017-09-13 13:18:22 +08:00
|
|
|
|
2019-06-23 23:22:43 +08:00
|
|
|
rows, err := db.QueryContext(session.ctx, sqlStr, args...)
|
2017-09-13 13:18:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2017-09-13 13:18:22 +08:00
|
|
|
return rows, nil
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
2019-06-23 23:22:43 +08:00
|
|
|
rows, err := session.tx.QueryContext(session.ctx, sqlStr, args...)
|
2017-08-22 19:39:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-13 13:18:22 +08:00
|
|
|
return rows, nil
|
|
|
|
}
|
2017-08-22 19:39:52 +08:00
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
func (session *Session) queryRow(sqlStr string, args ...interface{}) *core.Row {
|
|
|
|
return core.NewRow(session.queryRows(sqlStr, args...))
|
2017-08-22 19:39:52 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
func (session *Session) queryBytes(sqlStr string, args ...interface{}) ([]map[string][]byte, error) {
|
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
2017-08-22 19:39:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2021-08-13 07:11:42 +08:00
|
|
|
return rows2maps(rows)
|
2017-08-22 19:39:52 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, error) {
|
2017-04-07 09:47:25 +08:00
|
|
|
defer session.resetStatement()
|
|
|
|
|
2017-05-02 08:50:33 +08:00
|
|
|
session.queryPreprocess(&sqlStr, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
session.lastSQL = sqlStr
|
|
|
|
session.lastSQLArgs = args
|
2017-09-13 13:18:22 +08:00
|
|
|
|
|
|
|
if !session.isAutoCommit {
|
2019-06-23 23:22:43 +08:00
|
|
|
return session.tx.ExecContext(session.ctx, sqlStr, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if session.prepareStmt {
|
2018-01-27 23:20:59 +08:00
|
|
|
stmt, err := session.doPrepare(session.DB(), sqlStr)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-23 23:22:43 +08:00
|
|
|
res, err := stmt.ExecContext(session.ctx, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2019-06-23 23:22:43 +08:00
|
|
|
return session.DB().ExecContext(session.ctx, sqlStr, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exec raw sql
|
2019-06-23 23:22:43 +08:00
|
|
|
func (session *Session) Exec(sqlOrArgs ...interface{}) (sql.Result, error) {
|
2017-08-22 19:39:52 +08:00
|
|
|
if session.isAutoClose {
|
2017-01-03 16:20:28 +08:00
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
2019-06-23 23:22:43 +08:00
|
|
|
if len(sqlOrArgs) == 0 {
|
2018-12-12 09:01:41 +08:00
|
|
|
return nil, ErrUnSupportedType
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
sqlStr, args, err := session.statement.ConvertSQLOrArgs(sqlOrArgs...)
|
2018-12-12 09:01:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:20:28 +08:00
|
|
|
return session.exec(sqlStr, args...)
|
|
|
|
}
|