LinkinStars
cb851a2f95
All checks were successful
test tidb / test tidb (push) Successful in 5m10s
test postgres / test postgres (push) Successful in 5m41s
test mysql8 / test mysql8 (push) Successful in 4m57s
test sqlite / unit test & test sqlite (push) Successful in 8m42s
test mariadb / test mariadb (push) Successful in 4m36s
test cockroach / test cockroach (push) Successful in 10m5s
test mssql / test mssql (push) Successful in 5m52s
test mysql / test mysql (push) Successful in 4m47s
```diff // Filter is an interface to filter SQL type Filter interface { --- Do(sql string) string +++ Do(ctx context.Context, sql string) string } ``` ### Adds a `Context` parameter to the `Do` method of the Filter interface. Developers can rewrite SQL through the `Filter` `Do` method and **need to get the necessary data from the Context** to assist. For example, get user information through `Context`, so that different users can use different tables. Another example is to get the flags through `Context` to add annotations to the SQL, and use the annotations to let the subsequent `DB-Proxy` to achieve read/write separation. Reviewed-on: #2270 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: LinkinStars <linkinstar@foxmail.com> Co-committed-by: LinkinStars <linkinstar@foxmail.com>
198 lines
4.6 KiB
Go
198 lines
4.6 KiB
Go
// 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"
|
|
"strings"
|
|
|
|
"xorm.io/xorm/core"
|
|
)
|
|
|
|
func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) {
|
|
for _, filter := range session.engine.dialect.Filters() {
|
|
*sqlStr = filter.Do(session.ctx, *sqlStr)
|
|
}
|
|
|
|
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
|
|
if session.sessionType == groupSession && strings.EqualFold(strings.TrimSpace(sqlStr)[:6], "select") && !session.statement.IsForUpdate {
|
|
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
|
|
}
|
|
|
|
return stmt.QueryContext(session.ctx, args...)
|
|
}
|
|
|
|
return db.QueryContext(session.ctx, sqlStr, args...)
|
|
}
|
|
|
|
if session.prepareStmt {
|
|
stmt, err := session.doPrepareTx(sqlStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return stmt.QueryContext(session.ctx, args...)
|
|
}
|
|
|
|
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...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
return session.engine.scanByteMaps(rows)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
return session.engine.ScanInterfaceMaps(rows)
|
|
}
|
|
|
|
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 {
|
|
if session.prepareStmt {
|
|
stmt, err := session.doPrepareTx(sqlStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return stmt.ExecContext(session.ctx, args...)
|
|
}
|
|
return session.tx.ExecContext(session.ctx, sqlStr, args...)
|
|
}
|
|
|
|
if session.prepareStmt {
|
|
stmt, err := session.doPrepare(session.DB(), sqlStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return stmt.ExecContext(session.ctx, args...)
|
|
}
|
|
|
|
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...)
|
|
}
|