2020-07-19 19:32:22 +02: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-30 19:45:06 +02:00
|
|
|
"strings"
|
2020-07-19 19:32:22 +02:00
|
|
|
|
|
|
|
"xorm.io/xorm/core"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) {
|
|
|
|
for _, filter := range session.engine.dialect.Filters() {
|
2023-10-01 12:09:07 +02:00
|
|
|
*sqlStr = filter.Do(session.ctx, *sqlStr)
|
2020-07-19 19:32:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
session.lastSQL = *sqlStr
|
|
|
|
session.lastSQLArgs = paramStr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Rows, error) {
|
|
|
|
defer session.resetStatement()
|
|
|
|
if session.statement.LastError != nil {
|
|
|
|
return nil, session.statement.LastError
|
|
|
|
}
|
|
|
|
|
|
|
|
session.queryPreprocess(&sqlStr, args...)
|
|
|
|
|
|
|
|
session.lastSQL = sqlStr
|
|
|
|
session.lastSQLArgs = args
|
|
|
|
|
|
|
|
if session.isAutoCommit {
|
|
|
|
var db *core.DB
|
2022-07-16 11:43:41 +02:00
|
|
|
if session.sessionType == groupSession && strings.EqualFold(strings.TrimSpace(sqlStr)[:6], "select") && !session.statement.IsForUpdate {
|
2020-07-19 19:32:22 +02:00
|
|
|
db = session.engine.engineGroup.Slave().DB()
|
|
|
|
} else {
|
|
|
|
db = session.DB()
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.prepareStmt {
|
|
|
|
// don't clear stmt since session will cache them
|
|
|
|
stmt, err := session.doPrepare(db, sqlStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-16 11:43:41 +02:00
|
|
|
return stmt.QueryContext(session.ctx, args...)
|
2020-07-19 19:32:22 +02:00
|
|
|
}
|
|
|
|
|
2022-07-16 11:43:41 +02:00
|
|
|
return db.QueryContext(session.ctx, sqlStr, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.prepareStmt {
|
|
|
|
stmt, err := session.doPrepareTx(sqlStr)
|
2020-07-19 19:32:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-16 11:43:41 +02:00
|
|
|
|
|
|
|
return stmt.QueryContext(session.ctx, args...)
|
2020-07-19 19:32:22 +02:00
|
|
|
}
|
|
|
|
|
2022-07-16 11:43:41 +02:00
|
|
|
return session.tx.QueryContext(session.ctx, sqlStr, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) queryRow(sqlStr string, args ...interface{}) *core.Row {
|
|
|
|
return core.NewRow(session.queryRows(sqlStr, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query runs a raw sql and return records as []map[string][]byte
|
|
|
|
func (session *Session) Query(sqlOrArgs ...interface{}) ([]map[string][]byte, error) {
|
|
|
|
if session.isAutoClose {
|
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlStr, args, err := session.statement.GenQuerySQL(sqlOrArgs...)
|
2020-07-19 19:32:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-16 11:43:41 +02:00
|
|
|
|
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
return session.engine.scanByteMaps(rows)
|
2020-07-19 19:32:22 +02:00
|
|
|
}
|
|
|
|
|
2022-07-16 11:43:41 +02:00
|
|
|
// QueryString runs a raw sql and return records as []map[string]string
|
|
|
|
func (session *Session) QueryString(sqlOrArgs ...interface{}) ([]map[string]string, error) {
|
|
|
|
if session.isAutoClose {
|
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlStr, args, err := session.statement.GenQuerySQL(sqlOrArgs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
return session.engine.ScanStringMaps(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
// QuerySliceString runs a raw sql and return records as [][]string
|
|
|
|
func (session *Session) QuerySliceString(sqlOrArgs ...interface{}) ([][]string, error) {
|
|
|
|
if session.isAutoClose {
|
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlStr, args, err := session.statement.GenQuerySQL(sqlOrArgs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
return session.engine.ScanStringSlices(rows)
|
2020-07-19 19:32:22 +02:00
|
|
|
}
|
|
|
|
|
2022-07-16 11:43:41 +02:00
|
|
|
// QueryInterface runs a raw sql and return records as []map[string]interface{}
|
|
|
|
func (session *Session) QueryInterface(sqlOrArgs ...interface{}) ([]map[string]interface{}, error) {
|
|
|
|
if session.isAutoClose {
|
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlStr, args, err := session.statement.GenQuerySQL(sqlOrArgs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-19 19:32:22 +02:00
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2022-07-16 11:43:41 +02:00
|
|
|
return session.engine.ScanInterfaceMaps(rows)
|
2020-07-19 19:32:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, error) {
|
|
|
|
defer session.resetStatement()
|
|
|
|
|
|
|
|
session.queryPreprocess(&sqlStr, args...)
|
|
|
|
|
|
|
|
session.lastSQL = sqlStr
|
|
|
|
session.lastSQLArgs = args
|
|
|
|
|
|
|
|
if !session.isAutoCommit {
|
2022-07-16 11:43:41 +02:00
|
|
|
if session.prepareStmt {
|
|
|
|
stmt, err := session.doPrepareTx(sqlStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stmt.ExecContext(session.ctx, args...)
|
|
|
|
}
|
2020-07-19 19:32:22 +02:00
|
|
|
return session.tx.ExecContext(session.ctx, sqlStr, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.prepareStmt {
|
|
|
|
stmt, err := session.doPrepare(session.DB(), sqlStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-16 11:43:41 +02:00
|
|
|
return stmt.ExecContext(session.ctx, args...)
|
2020-07-19 19:32:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return session.DB().ExecContext(session.ctx, sqlStr, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec raw sql
|
|
|
|
func (session *Session) Exec(sqlOrArgs ...interface{}) (sql.Result, error) {
|
|
|
|
if session.isAutoClose {
|
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sqlOrArgs) == 0 {
|
|
|
|
return nil, ErrUnSupportedType
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlStr, args, err := session.statement.ConvertSQLOrArgs(sqlOrArgs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.exec(sqlStr, args...)
|
|
|
|
}
|