You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
5.2 KiB

package gormExample
import (
"fmt"
"gorm.io/gorm"
"log"
)
func SessionIssue() {
// 连续控制条件
//db := DB.Model(&Content{}).Where("views > ?", 100)
//db.Where("likes > ?", 9)
//var cs []Content
//db.Find(&cs)
// [4.319ms] [rows:0] SELECT * FROM `msb_content` WHERE views > 100 AND likes > 9 AND `msb_content`.`deleted_at` IS NULL
// 连续执行查询
// 1
// Where("views > ?", 100).Where("likes > ?", 9)
db := DB.Model(&Content{}).Where("views > ?", 100)
db.Where("likes > ?", 9)
var cs1 []Content
db.Find(&cs1)
// [10.566ms] [rows:0] SELECT * FROM `msb_content` WHERE views > 100 AND likes > 9 AND `msb_content`.`deleted_at` IS NULL
// 2,找到likes<5
// Where("views > ?", 100).Where("likes < ?", 5)
db.Where("likes < ?", 5)
var cs2 []Content
db.Find(&cs2)
// [2.815ms] [rows:0] SELECT * FROM `msb_content` WHERE views > 100 AND likes > 9 AND `msb_content`.`deleted_at` IS NULL AND likes < 5
}
func SessionDB() {
// 连续执行查询
// 1
// Where("views > ?", 100).Where("likes > ?", 9)
db1 := DB.Model(&Content{}).Where("views > ?", 100)
db1.Where("likes > ?", 9)
var cs1 []Content
db1.Find(&cs1)
// [10.683ms] [rows:0] SELECT * FROM `msb_content` WHERE views > 100 AND likes > 9 AND `msb_content`.`deleted_at` IS NULL
// 2,找到likes<5
// Where("views > ?", 100).Where("likes < ?", 5)
db2 := DB.Model(&Content{}).Where("views > ?", 100)
db2.Where("likes < ?", 5)
var cs2 []Content
db2.Find(&cs2)
// [4.139ms] [rows:0] SELECT * FROM `msb_content` WHERE views > 100 AND likes < 5 AND `msb_content`.`deleted_at` IS NULL
}
func SessionNew() {
// 需要重复使用的部分
// 将Session方法前的配置记录到了当前的会话中
// 后边再次调用db的方法直到终结方法会保持会话中的子句选项
// 执行完终结方法后再次调用db的方法到终结方法可以重用会话中的子句选项。
db := DB.Model(&Content{}).Where("views > ?", 100).Session(&gorm.Session{})
// 连续执行查询
// 1
// Where("views > ?", 100).Where("likes > ?", 9)
var cs1 []Content
db.Where("likes > ?", 9).Find(&cs1)
// [4.633ms] [rows:0] SELECT * FROM `msb_content` WHERE views > 100 AND likes > 9 AND `msb_content`.`deleted_at` IS NULL
// 2,找到likes<5
// Where("views > ?", 100).Where("likes < ?", 5)
var cs2 []Content
db.Where("likes < ?", 5).Find(&cs2)
// [3.846ms] [rows:0] SELECT * FROM `msb_content` WHERE views > 100 AND likes < 5 AND `msb_content`.`deleted_at` IS NULL
}
func SessionOptions() {
// Skip Hook
//db := DB.Session(&gorm.Session{
// SkipHooks: true,
//})
//db.Save(&Content{Subject: "no create hook"})
// DryRun
//db := DB.Session(&gorm.Session{
// DryRun: true,
//})
//stmt := db.Save(&Content{}).Statement
//fmt.Println(stmt.SQL.String())
//// INSERT INTO `msb_content` (`created_at`,`updated_at`,`deleted_at`,`subject`,`likes`,`views`,`publish_time`,`author_id`) VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE `updated_at`=?,`deleted_at`=VALUES(`deleted_at`),`subject`=VALUES(`subject`),`likes`=VALUES(`likes`),`views`=VALUES(`views`),`publish_time`=VALUES(`publish_time`),`author_id`=VALUES(`author_id`)
//fmt.Println(stmt.Vars)
//// [2023-04-24 17:25:47.827 +0800 CST 2023-04-24 17:25:47.827 +0800 CST {0001-01-01 00:00:00 +0000 UTC false} 0 0 2023-04-24 17:25:47.823908 +0800 CST 0 2023-04-24 17:25:47.827 +0800 CST]
// Debug
//DB.Debug().First()
// prepare 预编译,编译重用,编译独立
// SQL, 编译,绑定数据,执行
// 预编译,将编译的过程缓存起来,便于重用。
// 在执行结构相同的SQL时可以重用。
db := DB.Session(&gorm.Session{
PrepareStmt: true,
})
// 预编译管理对象
stmtManager, ok := db.ConnPool.(*gorm.PreparedStmtDB)
if !ok {
log.Fatalln("*gorm.PreparedStmtDB assert failed")
}
fmt.Println(stmtManager.PreparedSQL)
fmt.Println(stmtManager.Stmts)
var c1 Content
db.First(&c1, 13)
fmt.Println(stmtManager.PreparedSQL)
fmt.Println(stmtManager.Stmts)
// [SELECT * FROM `msb_content` WHERE `msb_content`.`id` = ? AND `msb_content`.`deleted_at` IS NULL ORDER BY `msb_content`.`id` LIMIT 1]
// map[SELECT * FROM `msb_content` WHERE `msb_content`.`id` = ? AND `msb_content`.`deleted_at` IS NULL ORDER BY `msb_content`.`id` LIMIT 1:0xc0001cd920]
var c2 Content
db.First(&c2, 14)
fmt.Println(stmtManager.PreparedSQL)
fmt.Println(stmtManager.Stmts)
// [SELECT * FROM `msb_content` WHERE `msb_content`.`id` = ? AND `msb_content`.`deleted_at` IS NULL ORDER BY `msb_content`.`id` LIMIT 1]
// map[SELECT * FROM `msb_content` WHERE `msb_content`.`id` = ? AND `msb_content`.`deleted_at` IS NULL ORDER BY `msb_content`.`id` LIMIT 1:0xc0001cd920]
var c3 []Content
db.Find(&c3, []uint{15, 16})
fmt.Println(stmtManager.PreparedSQL)
fmt.Println(stmtManager.Stmts)
// [SELECT * FROM `msb_content` WHERE `msb_content`.`id` = ? AND `msb_content`.`deleted_at` IS NULL ORDER BY `msb_content`.`id` LIMIT 1 SELECT * FROM `msb_content` WHERE `msb_content`.`id` IN (?,?) AND `msb_content`.`deleted_at` IS NULL]
// map[SELECT * FROM `msb_content` WHERE `msb_content`.`id` = ? AND `msb_content`.`deleted_at` IS NULL ORDER BY `msb_content`.`id` LIMIT 1:0xc0001cb920 SELECT * FROM `msb_content` WHERE `msb_content`.`id` IN (?,?) AND `msb_content`.`deleted_at` IS NULL:0xc0001cbe30]
}