原生 SQL

原生查询 SQL 和 Scan

  1. type Result struct {
  2. ID int
  3. Name string
  4. Age int
  5. }
  6. var result Result
  7. db.Raw("SELECT id, name, age FROM users WHERE name = ?", 3).Scan(&result)
  8. db.Raw("SELECT id, name, age FROM users WHERE name = ?", 3).Scan(&result)
  9. var age int
  10. db.Raw("SELECT SUM(age) FROM users WHERE role = ?", "admin").Scan(&age)
  11. var users []User
  12. db.Raw("UPDATE users SET name = ? WHERE age = ? RETURNING id, name", "jinzhu", 20).Scan(&users)

Exec 原生 SQL

  1. db.Exec("DROP TABLE users")
  2. db.Exec("UPDATE orders SET shipped_at = ? WHERE id IN ?", time.Now(), []int64{1, 2, 3})
  3. // Exec with SQL Expression
  4. db.Exec("UPDATE users SET money = ? WHERE name = ?", gorm.Expr("money * ? + ?", 10000, 1), "jinzhu")

注意 GORM 允许缓存预编译 SQL 语句来提高性能,查看 性能 获取详情

命名参数

GORM 支持 sql.NamedArgmap[string]interface{}{} 或 struct 形式的命名参数,例如:

  1. db.Where("name1 = @name OR name2 = @name", sql.Named("name", "jinzhu")).Find(&user)
  2. // SELECT * FROM `users` WHERE name1 = "jinzhu" OR name2 = "jinzhu"
  3. db.Where("name1 = @name OR name2 = @name", map[string]interface{}{"name": "jinzhu2"}).First(&result3)
  4. // SELECT * FROM `users` WHERE name1 = "jinzhu2" OR name2 = "jinzhu2" ORDER BY `users`.`id` LIMIT 1
  5. // 原生 SQL 及命名参数
  6. db.Raw("SELECT * FROM users WHERE name1 = @name OR name2 = @name2 OR name3 = @name",
  7. sql.Named("name", "jinzhu1"), sql.Named("name2", "jinzhu2")).Find(&user)
  8. // SELECT * FROM users WHERE name1 = "jinzhu1" OR name2 = "jinzhu2" OR name3 = "jinzhu1"
  9. db.Exec("UPDATE users SET name1 = @name, name2 = @name2, name3 = @name",
  10. sql.Named("name", "jinzhunew"), sql.Named("name2", "jinzhunew2"))
  11. // UPDATE users SET name1 = "jinzhunew", name2 = "jinzhunew2", name3 = "jinzhunew"
  12. db.Raw("SELECT * FROM users WHERE (name1 = @name AND name3 = @name) AND name2 = @name2",
  13. map[string]interface{}{"name": "jinzhu", "name2": "jinzhu2"}).Find(&user)
  14. // SELECT * FROM users WHERE (name1 = "jinzhu" AND name3 = "jinzhu") AND name2 = "jinzhu2"
  15. type NamedArgument struct {
  16. Name string
  17. Name2 string
  18. }
  19. db.Raw("SELECT * FROM users WHERE (name1 = @Name AND name3 = @Name) AND name2 = @Name2",
  20. NamedArgument{Name: "jinzhu", Name2: "jinzhu2"}).Find(&user)
  21. // SELECT * FROM users WHERE (name1 = "jinzhu" AND name3 = "jinzhu") AND name2 = "jinzhu2"

DryRun 模式

在不执行的情况下生成 SQL 及其参数,可以用于准备或测试生成的 SQL,详情请参考 Session

  1. stmt := db.Session(&Session{DryRun: true}).First(&user, 1).Statement
  2. stmt.SQL.String() //=> SELECT * FROM `users` WHERE `id` = $1 ORDER BY `id`
  3. stmt.Vars //=> []interface{}{1}

ToSQL

返回生成的 SQL 但不执行。

GORM使用 database/sql 的参数占位符来构建 SQL 语句,它会自动转义参数以避免 SQL 注入,但我们不保证生成 SQL 的安全,请只用于调试。

  1. sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
  2. return tx.Model(&User{}).Where("id = ?", 100).Limit(10).Order("age desc").Find(&[]User{})
  3. })
  4. sql //=> SELECT * FROM "users" WHERE id = 100 AND "users"."deleted_at" IS NULL ORDER BY age desc LIMIT 10

Row & Rows

获取 *sql.Row 结果

  1. // 使用 GORM API 构建 SQL
  2. row := db.Table("users").Where("name = ?", "jinzhu").Select("name", "age").Row()
  3. row.Scan(&name, &age)
  4. // 使用原生 SQL
  5. row := db.Raw("select name, age, email from users where name = ?", "jinzhu").Row()
  6. row.Scan(&name, &age, &email)

获取 *sql.Rows 结果

  1. // 使用 GORM API 构建 SQL
  2. rows, err := db.Model(&User{}).Where("name = ?", "jinzhu").Select("name, age, email").Rows()
  3. defer rows.Close()
  4. for rows.Next() {
  5. rows.Scan(&name, &age, &email)
  6. // 业务逻辑...
  7. }
  8. // 原生 SQL
  9. rows, err := db.Raw("select name, age, email from users where name = ?", "jinzhu").Rows()
  10. defer rows.Close()
  11. for rows.Next() {
  12. rows.Scan(&name, &age, &email)
  13. // 业务逻辑...
  14. }

转到 FindInBatches 获取如何在批量中查询和处理记录的信息, 转到 Group 条件 获取如何构建复杂 SQL 查询的信息

sql.Rows 扫描至 model

使用 ScanRows 将一行记录扫描至 struct,例如:

  1. rows, err := db.Model(&User{}).Where("name = ?", "jinzhu").Select("name, age, email").Rows() // (*sql.Rows, error)
  2. defer rows.Close()
  3. var user User
  4. for rows.Next() {
  5. // ScanRows 将一行扫描至 user
  6. db.ScanRows(rows, &user)
  7. // 业务逻辑...
  8. }

Connection

Run mutliple SQL in same db tcp connection (not in a transaction)

  1. db.Connection(func(tx *gorm.DB) error {
  2. tx.Exec("SET my.role = ?", "admin")
  3. tx.First(&User{})
  4. })

Advanced

子句(Clause)

GORM uses SQL builder generates SQL internally, for each operation, GORM creates a *gorm.Statement object, all GORM APIs add/change Clause for the Statement, at last, GORM generated SQL based on those clauses

For example, when querying with First, it adds the following clauses to the Statement

  1. clause.Select{Columns: "*"}
  2. clause.From{Tables: clause.CurrentTable}
  3. clause.Limit{Limit: 1}
  4. clause.OrderByColumn{
  5. Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
  6. }

Then GORM build finally querying SQL in the Query callbacks like:

  1. Statement.Build("SELECT", "FROM", "WHERE", "GROUP BY", "ORDER BY", "LIMIT", "FOR")

Which generate SQL:

  1. SELECT * FROM `users` ORDER BY `users`.`id` LIMIT 1

You can define your own Clause and use it with GORM, it needs to implements Interface

Check out examples for reference

子句构造器

For different databases, Clauses may generate different SQL, for example:

  1. db.Offset(10).Limit(5).Find(&users)
  2. // Generated for SQL Server
  3. // SELECT * FROM "users" OFFSET 10 ROW FETCH NEXT 5 ROWS ONLY
  4. // Generated for MySQL
  5. // SELECT * FROM `users` LIMIT 5 OFFSET 10

Which is supported because GORM allows database driver register Clause Builder to replace the default one, take the Limit as example

子句选项

GORM defined Many Clauses, and some clauses provide advanced options can be used for your application

Although most of them are rarely used, if you find GORM public API can’t match your requirements, may be good to check them out, for example:

  1. db.Clauses(clause.Insert{Modifier: "IGNORE"}).Create(&user)
  2. // INSERT IGNORE INTO users (name,age...) VALUES ("jinzhu",18...);

StatementModifier

GORM provides interface StatementModifier allows you modify statement to match your requirements, take Hints as example

  1. import "gorm.io/hints"
  2. db.Clauses(hints.New("hint")).Find(&User{})
  3. // SELECT * /*+ hint */ FROM `users`