OSDN Git Service

versoin1.1.9 (#594)
[bytom/vapor.git] / vendor / github.com / jinzhu / gorm / callback_row_query.go
1 package gorm
2
3 import "database/sql"
4
5 // Define callbacks for row query
6 func init() {
7         DefaultCallback.RowQuery().Register("gorm:row_query", rowQueryCallback)
8 }
9
10 type RowQueryResult struct {
11         Row *sql.Row
12 }
13
14 type RowsQueryResult struct {
15         Rows  *sql.Rows
16         Error error
17 }
18
19 // queryCallback used to query data from database
20 func rowQueryCallback(scope *Scope) {
21         if result, ok := scope.InstanceGet("row_query_result"); ok {
22                 scope.prepareQuerySQL()
23
24                 if rowResult, ok := result.(*RowQueryResult); ok {
25                         rowResult.Row = scope.SQLDB().QueryRow(scope.SQL, scope.SQLVars...)
26                 } else if rowsResult, ok := result.(*RowsQueryResult); ok {
27                         rowsResult.Rows, rowsResult.Error = scope.SQLDB().Query(scope.SQL, scope.SQLVars...)
28                 }
29         }
30 }