前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang 对象关系映射框架 GORM 实现自定义 SQL 提示

Golang 对象关系映射框架 GORM 实现自定义 SQL 提示

作者头像
子兮子兮
发布2022-08-30 15:14:57
1.1K0
发布2022-08-30 15:14:57
举报
文章被收录于专栏:JavaGoRustJavaGoRust

NO-ORMer 请绕道 👉 Golang, ORMs, and why I am still not using one. (hydrogen18.com)

GORM 原生支持 3 种提示,分别是:

  • Index Hints:MySQL 索引提示;
  • Optimizer Hints:MySQL 优化器提示;
  • Comment Hints:注释提示,在任意 SQL 关键字之前或之后添加 /* */ 块注释。

Hints | GORM - The fantastic ORM library for Golang, aims to be developer friendly.

所以 GORM 对 MySQL 的支持很好,然而本人工作中主要使用 SQL Server,其表提示 WITH(...) 会经常使用,所以查看了 GORM Hints 相关代码后,实现了 SQL Server 的 WITH 表提示,代码如下所示:

TableHints.go

代码语言:javascript
复制
 1package tool
 2
 3import (
 4	"gorm.io/gorm"
 5	"gorm.io/gorm/clause"
 6	"gorm.io/hints"
 7)
 8
 9// TableHints 自定义 GORM 表提示
10//
11// @Author zixizixi.cn
12type TableHints struct {
13	Type string
14	Keys []string
15}
16
17// ModifyStatement 实现 gorm.StatementModifier 接口方法
18func (tableHint TableHints) ModifyStatement(stmt *gorm.Statement) {
19	for _, name := range []string{"FROM", "INTO"} {
20		stmtClause := stmt.Clauses[name]
21
22		if stmtClause.AfterExpression == nil {
23			stmtClause.AfterExpression = tableHint
24		} else {
25			stmtClause.AfterExpression = hints.Exprs{stmtClause.AfterExpression, tableHint}
26		}
27
28		stmt.Clauses[name] = stmtClause
29	}
30}
31
32// Build 实现 clause.Expression 接口方法
33func (tableHint TableHints) Build(builder clause.Builder) {
34	if len(tableHint.Keys) > 0 {
35		_, _ = builder.WriteString(tableHint.Type)
36		_ = builder.WriteByte('(')
37		for idx, key := range tableHint.Keys {
38			if idx > 0 {
39				_ = builder.WriteByte(',')
40			}
41			_, _ = builder.WriteString(key)
42		}
43		_ = builder.WriteByte(')')
44	}
45}
46
47// With Sql Server 使用 WITH(?) 表提示
48func (tableHint TableHints) With(names ...string) TableHints {
49	return TableHints{Type: "WITH", Keys: names}
50}
51
52// UseNolock Sql Server 使用 WITH(NOLOCK) 表提示
53func (tableHint TableHints) UseNolock() TableHints {
54	return tableHint.With("NOLOCK")
55}

Usage

  • WITH(NOLOCK) 脏读表提示
代码语言:javascript
复制
1import (
2    "gorm.io/hints"
3    "tool"
4)
5
6db.Clauses(tool.TableHints{}.UseNolock()).Find(&User{})
7// SELECT * FROM users WITH(NOLOCK)
  • 其他表提示
代码语言:javascript
复制
 1import (
 2    "gorm.io/hints"
 3    "tool"
 4)
 5
 6db.Clauses(tool.TableHints{}.With("NOWAIT")).Find(&User{})
 7// SELECT * FROM users WITH(NOWAIT)
 8
 9db.Clauses(tool.TableHints{}.With("ROWLOCK")).Find(&User{})
10// SELECT * FROM users WITH(ROWLOCK)

2021 年 12 月 25 日发布于 https://zixizixi.cn/golang-gorm-custom-table-hints


内容声明

标题: Golang 对象关系映射框架 GORM 实现自定义 SQL 提示

链接: https://zixizixi.cn/golang-gorm-custom-table-hints

来源: iTanken

本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可,转载请保留此声明。


我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=wh4u6zpyhe1d

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-04-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • TableHints.go
  • Usage
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档