github.com/goravel/framework@v1.13.9/database/gorm/hints/with_hint.go (about) 1 package hints 2 3 import ( 4 "gorm.io/driver/sqlserver" 5 "gorm.io/gorm" 6 "gorm.io/gorm/clause" 7 ) 8 9 type WithHint struct { 10 Type string 11 Keys []string 12 } 13 14 func (indexHint WithHint) ModifyStatement(stmt *gorm.Statement) { 15 dialector := sqlserver.Dialector{} 16 if stmt.Name() == dialector.Name() { 17 for _, name := range []string{"FROM"} { 18 clause := stmt.Clauses[name] 19 20 if clause.AfterExpression == nil { 21 clause.AfterExpression = indexHint 22 } else { 23 clause.AfterExpression = Exprs{clause.AfterExpression, indexHint} 24 } 25 26 stmt.Clauses[name] = clause 27 } 28 } 29 } 30 31 func (indexHint WithHint) Build(builder clause.Builder) { 32 if len(indexHint.Keys) > 0 { 33 _, _ = builder.WriteString(indexHint.Type) 34 _ = builder.WriteByte('(') 35 for idx, key := range indexHint.Keys { 36 if idx > 0 { 37 _ = builder.WriteByte(',') 38 } 39 _, _ = builder.WriteString(key) 40 } 41 _ = builder.WriteByte(')') 42 } 43 } 44 45 func With(names ...string) WithHint { 46 return WithHint{Type: "with ", Keys: names} 47 } 48 49 type Exprs []clause.Expression 50 51 func (exprs Exprs) Build(builder clause.Builder) { 52 for idx, expr := range exprs { 53 if idx > 0 { 54 _ = builder.WriteByte(' ') 55 } 56 expr.Build(builder) 57 } 58 }