2017-01-15 13:03:12 +03:00
|
|
|
package gorm
|
|
|
|
|
2019-04-19 14:48:52 +03:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
)
|
2017-01-15 13:03:12 +03:00
|
|
|
|
|
|
|
// Define callbacks for row query
|
|
|
|
func init() {
|
|
|
|
DefaultCallback.RowQuery().Register("gorm:row_query", rowQueryCallback)
|
|
|
|
}
|
|
|
|
|
|
|
|
type RowQueryResult struct {
|
|
|
|
Row *sql.Row
|
|
|
|
}
|
|
|
|
|
|
|
|
type RowsQueryResult struct {
|
|
|
|
Rows *sql.Rows
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
// queryCallback used to query data from database
|
|
|
|
func rowQueryCallback(scope *Scope) {
|
|
|
|
if result, ok := scope.InstanceGet("row_query_result"); ok {
|
|
|
|
scope.prepareQuerySQL()
|
2019-12-06 04:16:51 +03:00
|
|
|
|
|
|
|
if str, ok := scope.Get("gorm:query_hint"); ok {
|
|
|
|
scope.SQL = fmt.Sprint(str) + scope.SQL
|
|
|
|
}
|
|
|
|
|
2019-04-19 14:48:52 +03:00
|
|
|
if str, ok := scope.Get("gorm:query_option"); ok {
|
|
|
|
scope.SQL += addExtraSpaceIfExist(fmt.Sprint(str))
|
|
|
|
}
|
2017-01-15 13:03:12 +03:00
|
|
|
|
|
|
|
if rowResult, ok := result.(*RowQueryResult); ok {
|
|
|
|
rowResult.Row = scope.SQLDB().QueryRow(scope.SQL, scope.SQLVars...)
|
|
|
|
} else if rowsResult, ok := result.(*RowsQueryResult); ok {
|
|
|
|
rowsResult.Rows, rowsResult.Error = scope.SQLDB().Query(scope.SQL, scope.SQLVars...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|