query_index_drop.go (2255B)
1 package bun 2 3 import ( 4 "context" 5 "database/sql" 6 7 "github.com/uptrace/bun/internal" 8 "github.com/uptrace/bun/schema" 9 ) 10 11 type DropIndexQuery struct { 12 baseQuery 13 cascadeQuery 14 15 concurrently bool 16 ifExists bool 17 18 index schema.QueryWithArgs 19 } 20 21 var _ Query = (*DropIndexQuery)(nil) 22 23 func NewDropIndexQuery(db *DB) *DropIndexQuery { 24 q := &DropIndexQuery{ 25 baseQuery: baseQuery{ 26 db: db, 27 conn: db.DB, 28 }, 29 } 30 return q 31 } 32 33 func (q *DropIndexQuery) Conn(db IConn) *DropIndexQuery { 34 q.setConn(db) 35 return q 36 } 37 38 func (q *DropIndexQuery) Model(model interface{}) *DropIndexQuery { 39 q.setModel(model) 40 return q 41 } 42 43 func (q *DropIndexQuery) Err(err error) *DropIndexQuery { 44 q.setErr(err) 45 return q 46 } 47 48 //------------------------------------------------------------------------------ 49 50 func (q *DropIndexQuery) Concurrently() *DropIndexQuery { 51 q.concurrently = true 52 return q 53 } 54 55 func (q *DropIndexQuery) IfExists() *DropIndexQuery { 56 q.ifExists = true 57 return q 58 } 59 60 func (q *DropIndexQuery) Cascade() *DropIndexQuery { 61 q.cascade = true 62 return q 63 } 64 65 func (q *DropIndexQuery) Restrict() *DropIndexQuery { 66 q.restrict = true 67 return q 68 } 69 70 func (q *DropIndexQuery) Index(query string, args ...interface{}) *DropIndexQuery { 71 q.index = schema.SafeQuery(query, args) 72 return q 73 } 74 75 //------------------------------------------------------------------------------ 76 77 func (q *DropIndexQuery) Operation() string { 78 return "DROP INDEX" 79 } 80 81 func (q *DropIndexQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) { 82 if q.err != nil { 83 return nil, q.err 84 } 85 86 b = append(b, "DROP INDEX "...) 87 88 if q.concurrently { 89 b = append(b, "CONCURRENTLY "...) 90 } 91 if q.ifExists { 92 b = append(b, "IF EXISTS "...) 93 } 94 95 b, err = q.index.AppendQuery(fmter, b) 96 if err != nil { 97 return nil, err 98 } 99 100 b = q.appendCascade(fmter, b) 101 102 return b, nil 103 } 104 105 //------------------------------------------------------------------------------ 106 107 func (q *DropIndexQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) { 108 queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes()) 109 if err != nil { 110 return nil, err 111 } 112 113 query := internal.String(queryBytes) 114 115 res, err := q.exec(ctx, q, query) 116 if err != nil { 117 return nil, err 118 } 119 120 return res, nil 121 }