callback_row_query.go 730 B

123456789101112131415161718192021222324252627282930
  1. package gorm
  2. import "database/sql"
  3. // Define callbacks for row query
  4. func init() {
  5. DefaultCallback.RowQuery().Register("gorm:row_query", rowQueryCallback)
  6. }
  7. type RowQueryResult struct {
  8. Row *sql.Row
  9. }
  10. type RowsQueryResult struct {
  11. Rows *sql.Rows
  12. Error error
  13. }
  14. // queryCallback used to query data from database
  15. func rowQueryCallback(scope *Scope) {
  16. if result, ok := scope.InstanceGet("row_query_result"); ok {
  17. scope.prepareQuerySQL()
  18. if rowResult, ok := result.(*RowQueryResult); ok {
  19. rowResult.Row = scope.SQLDB().QueryRow(scope.SQL, scope.SQLVars...)
  20. } else if rowsResult, ok := result.(*RowsQueryResult); ok {
  21. rowsResult.Rows, rowsResult.Error = scope.SQLDB().Query(scope.SQL, scope.SQLVars...)
  22. }
  23. }
  24. }