hints

package module
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2023 License: MIT Imports: 3 Imported by: 46

README

Hints

CI

Optimizer/Index/Comment Hints support for GORM

Optimizer Hints

import "gorm.io/hints"

DB.Clauses(hints.New("hint")).Find(&User{})
// SELECT * /*+ hint */ FROM `users`

Index Hints

import "gorm.io/hints"

DB.Clauses(hints.UseIndex("idx_user_name")).Find(&User{})
// SELECT * FROM `users` USE INDEX (`idx_user_name`)

DB.Clauses(hints.ForceIndex("idx_user_name", "idx_user_id").ForJoin()).Find(&User{})
// SELECT * FROM `users` FORCE INDEX FOR JOIN (`idx_user_name`,`idx_user_id`)"

DB.Clauses(
	hints.ForceIndex("idx_user_name", "idx_user_id").ForOrderBy(),
	hints.IgnoreIndex("idx_user_name").ForGroupBy(),
).Find(&User{})
// SELECT * FROM `users` FORCE INDEX FOR ORDER BY (`idx_user_name`,`idx_user_id`) IGNORE INDEX FOR GROUP BY (`idx_user_name`)"

Comment Hints

import "gorm.io/hints"

DB.Clauses(hints.Comment("select", "master")).Find(&User{})
// SELECT /*master*/ * FROM `users`;

DB.Clauses(hints.CommentBefore("insert", "node2")).Create(&user)
// /*node2*/ INSERT INTO `users` ...;

DB.Clauses(hints.CommentAfter("select", "node2")).Create(&user)
// /*node2*/ INSERT INTO `users` ...;

DB.Clauses(hints.CommentAfter("where", "hint")).Find(&User{}, "id = ?", 1)
// SELECT * FROM `users` WHERE id = ? /* hint */

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IndexHintFromClauseBuilder added in v1.1.2

func IndexHintFromClauseBuilder(c clause.Clause, builder clause.Builder)

Types

type Exprs

type Exprs []clause.Expression

func (Exprs) Build

func (exprs Exprs) Build(builder clause.Builder)

type Hints

type Hints struct {
	Prefix  string
	Suffix  string
	Content string
	// contains filtered or unexported fields
}

func Comment

func Comment(clause string, comment string) Hints

func CommentAfter

func CommentAfter(clause string, comment string) Hints

func CommentBefore

func CommentBefore(clause string, comment string) Hints

func New

func New(content string) Hints

func (Hints) Build

func (hints Hints) Build(builder clause.Builder)

func (*Hints) Merge

func (hints *Hints) Merge(h Hints)

func (Hints) ModifyStatement

func (hints Hints) ModifyStatement(stmt *gorm.Statement)

type IndexHint

type IndexHint struct {
	Type string
	Keys []string
}

func ForceIndex

func ForceIndex(names ...string) IndexHint

func IgnoreIndex

func IgnoreIndex(names ...string) IndexHint

func UseIndex

func UseIndex(names ...string) IndexHint

func (IndexHint) Build

func (indexHint IndexHint) Build(builder clause.Builder)

func (IndexHint) ForGroupBy

func (indexHint IndexHint) ForGroupBy() IndexHint

func (IndexHint) ForJoin

func (indexHint IndexHint) ForJoin() IndexHint

func (IndexHint) ForOrderBy

func (indexHint IndexHint) ForOrderBy() IndexHint

func (IndexHint) ModifyStatement

func (indexHint IndexHint) ModifyStatement(stmt *gorm.Statement)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL