OSDN Git Service

Merge pull request #201 from Bytom/v0.1
[bytom/vapor.git] / vendor / github.com / jinzhu / gorm / callback_row_query.go
diff --git a/vendor/github.com/jinzhu/gorm/callback_row_query.go b/vendor/github.com/jinzhu/gorm/callback_row_query.go
new file mode 100755 (executable)
index 0000000..c2ff4a0
--- /dev/null
@@ -0,0 +1,30 @@
+package gorm
+
+import "database/sql"
+
+// 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()
+
+               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...)
+               }
+       }
+}