Add missing index hint parameter (#2378)

Reviewed-on: https://gitea.com/xorm/xorm/pulls/2378
This commit is contained in:
Lunny Xiao 2023-12-19 04:54:04 +00:00
parent b571d91858
commit 7ae7474bcb
5 changed files with 24 additions and 7 deletions

View File

@ -1434,9 +1434,9 @@ func (engine *Engine) Transaction(f func(*Session) (interface{}, error)) (interf
return result, nil return result, nil
} }
func (engine *Engine) IndexHint(op, indexerOrColName string) *Session { func (engine *Engine) IndexHint(op, forType, indexerOrColName string) *Session {
session := engine.NewSession() session := engine.NewSession()
session.isAutoClose = true session.isAutoClose = true
session.statement.LastError = session.statement.IndexHint(op, indexerOrColName) session.statement.LastError = session.statement.IndexHint(op, forType, indexerOrColName)
return session return session
} }

View File

@ -19,10 +19,11 @@ func (e ErrInvalidIndexHintOperator) Error() string {
return "invalid index hint operator: " + e.Op return "invalid index hint operator: " + e.Op
} }
func (statement *Statement) IndexHint(op, indexName string) error { func (statement *Statement) IndexHint(op, forType, indexName string) error {
op = strings.ToUpper(op) op = strings.ToUpper(op)
statement.indexHints = append(statement.indexHints, indexHint{ statement.indexHints = append(statement.indexHints, indexHint{
op: op, op: op,
forType: forType,
indexName: indexName, indexName: indexName,
}) })
return nil return nil
@ -46,7 +47,16 @@ func (statement *Statement) writeIndexHintsMySQL(w *builder.BytesWriter) error {
if hint.op != "USE" && hint.op != "FORCE" && hint.op != "IGNORE" { if hint.op != "USE" && hint.op != "FORCE" && hint.op != "IGNORE" {
return ErrInvalidIndexHintOperator{Op: hint.op} return ErrInvalidIndexHintOperator{Op: hint.op}
} }
if err := statement.writeStrings(" ", hint.op, " INDEX(", hint.indexName, ")")(w); err != nil { if err := statement.writeStrings(" ", hint.op, " INDEX")(w); err != nil {
return err
}
if hint.forType != "" {
if err := statement.writeStrings(" FOR ", hint.forType)(w); err != nil {
return err
}
}
if err := statement.writeStrings("(", hint.indexName, ")")(w); err != nil {
return err return err
} }
} }

View File

@ -43,6 +43,7 @@ type join struct {
type indexHint struct { type indexHint struct {
op string op string
forType string
indexName string indexName string
} }

View File

@ -312,7 +312,7 @@ func (session *Session) Import(r io.Reader) ([]sql.Result, error) {
return results, lastError return results, lastError
} }
func (session *Session) IndexHint(op, indexerOrColName string) *Session { func (session *Session) IndexHint(op, forType, indexerOrColName string) *Session {
session.statement.IndexHint(op, indexerOrColName) session.statement.IndexHint(op, forType, indexerOrColName)
return session return session
} }

View File

@ -70,6 +70,12 @@ func TestIndexHint(t *testing.T) {
return return
} }
_, err := testEngine.Table("userinfo").IndexHint("USE", "UQE_userinfo_username").Get(new(Userinfo)) _, err := testEngine.Table("userinfo").IndexHint("USE", "", "UQE_userinfo_username").Get(new(Userinfo))
assert.NoError(t, err)
_, err = testEngine.Table("userinfo").IndexHint("USE", "ORDER BY", "UQE_userinfo_username").Get(new(Userinfo))
assert.NoError(t, err)
_, err = testEngine.Table("userinfo").IndexHint("USE", "GROUP BY", "UQE_userinfo_username").Get(new(Userinfo))
assert.NoError(t, err) assert.NoError(t, err)
} }