Method Operations

Method operations are used for native SQL execution, which are more foundational compared to chain operations. When ORM chain operations are unable to handle overly complex SQL operations, method operations can be used instead.

API Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/database/gdb#DB

Common Methods:

The list of methods in this document may lag behind the code. For a detailed list of methods, please refer to the API documentation. The following methods are for reference only.

  1. // SQL operation methods, returning native standard library sql objects
  2. Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
  3. Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
  4. Prepare(ctx context.Context, query string) (*sql.Stmt, error)
  5. // Database record queries:
  6. // Query single record, query multiple records, get record object, query single field value (similar to chain operations)
  7. GetAll(ctx context.Context, sql string, args ...interface{}) (Result, error)
  8. GetOne(ctx context.Context, sql string, args ...interface{}) (Record, error)
  9. GetValue(ctx context.Context, sql string, args ...interface{}) (Value, error)
  10. GetArray(ctx context.Context, sql string, args ...interface{}) ([]Value, error)
  11. GetCount(ctx context.Context, sql string, args ...interface{}) (int, error)
  12. GetScan(ctx context.Context, objPointer interface{}, sql string, args ...interface{}) error
  13. // Single data operation
  14. Insert(ctx context.Context, table string, data interface{}, batch...int) (sql.Result, error)
  15. Replace(ctx context.Context, table string, data interface{}, batch...int) (sql.Result, error)
  16. Save(ctx context.Context, table string, data interface{}, batch...int) (sql.Result, error)
  17. // Data update/delete
  18. Update(ctx context.Context, table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error)
  19. Delete(ctx context.Context, table string, condition interface{}, args ...interface{}) (sql.Result, error)

Brief Description:

  1. Query is the original data query method that returns the native standard library’s result set object, requiring manual parsing. It is recommended to use the Get* methods, which automatically parse the results.
  2. Exec is used for SQL operations related to writing/updating.
  3. It is recommended to use the Get* series of query methods for data queries.
  4. The data parameter in the Insert/Replace/Save methods supports data types: string/map/slice/struct/*struct. When passed as a slice type, it is automatically recognized as a batch operation, and the batch parameter is effective.

Operation Examples

1. ORM Object

  1. // Get the database object with default configuration (configuration name is "default")
  2. db := g.DB()
  3. // Get the database object with the configuration group name "user-center"
  4. db := g.DB("user-center")
  5. // Use the native singleton management method to get the database object singleton
  6. db, err := gdb.Instance()
  7. db, err := gdb.Instance("user-center")
  8. // Note that there is no need to use the Close method to close the database connection when not in use (and gdb does not provide a Close method).
  9. // The underlying design of the database engine uses connection pooling, and the connection will automatically close when it is no longer in use.

2. Data Insertion

  1. r, err := db.Insert(ctx, "user", gdb.Map {
  2. "name": "john",
  3. })

3. Data Query (List)

  1. list, err := db.GetAll(ctx, "select * from user limit 2")
  2. list, err := db.GetAll(ctx, "select * from user where age > ? and name like ?", g.Slice{18, "%john%"})
  3. list, err := db.GetAll(ctx, "select * from user where status=?", g.Slice{1})

4. Data Query (Single Record)

  1. one, err := db.GetOne(ctx, "select * from user limit 2")
  2. one, err := db.GetOne(ctx, "select * from user where uid=1000")
  3. one, err := db.GetOne(ctx, "select * from user where uid=?", 1000)
  4. one, err := db.GetOne(ctx, "select * from user where uid=?", g.Slice{1000})

5. Data Save

  1. r, err := db.Save(ctx, "user", gdb.Map {
  2. "uid" : 1,
  3. "name" : "john",
  4. })

6. Batch Operations

The batch parameter is used to specify the number of records to be written in batches during batch operations (default is 10).

  1. _, err := db.Insert(ctx, "user", gdb.List {
  2. {"name": "john_1"},
  3. {"name": "john_2"},
  4. {"name": "john_3"},
  5. {"name": "john_4"},
  6. }, 10)

7. Data Update/Delete

  1. // db.Update/db.Delete likewise
  2. // UPDATE `user` SET `name`='john' WHERE `uid`=10000
  3. r, err := db.Update(ctx, "user", gdb.Map {"name": "john"}, "uid=?", 10000)
  4. // UPDATE `user` SET `name`='john' WHERE `uid`=10000
  5. r, err := db.Update(ctx, "user", "name='john'", "uid=10000")
  6. // UPDATE `user` SET `name`='john' WHERE `uid`=10000
  7. r, err := db.Update(ctx, "user", "name=?", "uid=?", "john", 10000)

Note that it is supported and recommended to use prepared statement mode (using ? placeholders) for input to avoid SQL injection risks.