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"
|
2017-08-22 05:39:52 -06:00
|
|
|
"reflect"
|
|
|
|
"time"
|
2017-01-03 01:20:28 -07:00
|
|
|
|
2019-06-23 09:22:43 -06:00
|
|
|
"xorm.io/builder"
|
|
|
|
"xorm.io/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() {
|
|
|
|
*sqlStr = filter.Do(*sqlStr, session.engine.dialect, session.statement.RefTable)
|
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()
|
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
|
|
|
|
2017-09-12 23:18:22 -06:00
|
|
|
if session.engine.showSQL {
|
|
|
|
if session.engine.showExecTime {
|
|
|
|
b4ExecTime := time.Now()
|
|
|
|
defer func() {
|
|
|
|
execDuration := time.Since(b4ExecTime)
|
|
|
|
if len(args) > 0 {
|
|
|
|
session.engine.logger.Infof("[SQL] %s %#v - took: %v", sqlStr, args, execDuration)
|
|
|
|
} else {
|
|
|
|
session.engine.logger.Infof("[SQL] %s - took: %v", sqlStr, execDuration)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
if len(args) > 0 {
|
|
|
|
session.engine.logger.Infof("[SQL] %v %#v", sqlStr, args)
|
|
|
|
} else {
|
|
|
|
session.engine.logger.Infof("[SQL] %v", sqlStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.isAutoCommit {
|
2018-01-27 08:20:59 -07:00
|
|
|
var db *core.DB
|
2019-06-23 09:22:43 -06:00
|
|
|
if session.sessionType == groupSession {
|
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 value2Bytes(rawValue *reflect.Value) ([]byte, error) {
|
|
|
|
str, err := value2String(rawValue)
|
2017-08-22 05:39:52 -06:00
|
|
|
if err != nil {
|
2017-09-12 23:18:22 -06:00
|
|
|
return nil, err
|
2017-08-22 05:39:52 -06:00
|
|
|
}
|
2017-09-12 23:18:22 -06:00
|
|
|
return []byte(str), nil
|
2017-08-22 05:39:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func row2map(rows *core.Rows, fields []string) (resultsMap map[string][]byte, err error) {
|
|
|
|
result := make(map[string][]byte)
|
|
|
|
scanResultContainers := make([]interface{}, len(fields))
|
|
|
|
for i := 0; i < len(fields); i++ {
|
|
|
|
var scanResultContainer interface{}
|
|
|
|
scanResultContainers[i] = &scanResultContainer
|
|
|
|
}
|
|
|
|
if err := rows.Scan(scanResultContainers...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for ii, key := range fields {
|
|
|
|
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
|
|
|
//if row is null then ignore
|
|
|
|
if rawValue.Interface() == nil {
|
2017-09-12 23:18:22 -06:00
|
|
|
result[key] = []byte{}
|
2017-08-22 05:39:52 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if data, err := value2Bytes(&rawValue); err == nil {
|
|
|
|
result[key] = data
|
|
|
|
} else {
|
|
|
|
return nil, err // !nashtsai! REVIEW, should return err or just error log?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:18:22 -06:00
|
|
|
func rows2maps(rows *core.Rows) (resultsSlice []map[string][]byte, err error) {
|
2017-08-22 05:39:52 -06:00
|
|
|
fields, err := rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
2017-09-12 23:18:22 -06:00
|
|
|
result, err := row2map(rows, fields)
|
2017-08-22 05:39:52 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resultsSlice = append(resultsSlice, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultsSlice, nil
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2017-09-12 23:18:22 -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
|
|
|
|
2017-09-12 23:18:22 -06:00
|
|
|
if session.engine.showSQL {
|
|
|
|
if session.engine.showExecTime {
|
|
|
|
b4ExecTime := time.Now()
|
|
|
|
defer func() {
|
|
|
|
execDuration := time.Since(b4ExecTime)
|
|
|
|
if len(args) > 0 {
|
|
|
|
session.engine.logger.Infof("[SQL] %s %#v - took: %v", sqlStr, args, execDuration)
|
|
|
|
} else {
|
|
|
|
session.engine.logger.Infof("[SQL] %s - took: %v", sqlStr, execDuration)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
if len(args) > 0 {
|
|
|
|
session.engine.logger.Infof("[SQL] %v %#v", sqlStr, args)
|
|
|
|
} else {
|
|
|
|
session.engine.logger.Infof("[SQL] %v", sqlStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-23 09:22:43 -06:00
|
|
|
func convertSQLOrArgs(sqlOrArgs ...interface{}) (string, []interface{}, error) {
|
|
|
|
switch sqlOrArgs[0].(type) {
|
2018-12-11 18:01:41 -07:00
|
|
|
case string:
|
2019-06-23 09:22:43 -06:00
|
|
|
return sqlOrArgs[0].(string), sqlOrArgs[1:], nil
|
2018-12-11 18:01:41 -07:00
|
|
|
case *builder.Builder:
|
2019-06-23 09:22:43 -06:00
|
|
|
return sqlOrArgs[0].(*builder.Builder).ToSQL()
|
2018-12-11 18:01:41 -07:00
|
|
|
case builder.Builder:
|
2019-06-23 09:22:43 -06:00
|
|
|
bd := sqlOrArgs[0].(builder.Builder)
|
2018-12-11 18:01:41 -07:00
|
|
|
return bd.ToSQL()
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil, ErrUnSupportedType
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-23 09:22:43 -06:00
|
|
|
sqlStr, args, err := 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...)
|
|
|
|
}
|