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