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