query_table_drop.go (3094B)
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 DropTableQuery struct { 12 baseQuery 13 cascadeQuery 14 15 ifExists bool 16 } 17 18 var _ Query = (*DropTableQuery)(nil) 19 20 func NewDropTableQuery(db *DB) *DropTableQuery { 21 q := &DropTableQuery{ 22 baseQuery: baseQuery{ 23 db: db, 24 conn: db.DB, 25 }, 26 } 27 return q 28 } 29 30 func (q *DropTableQuery) Conn(db IConn) *DropTableQuery { 31 q.setConn(db) 32 return q 33 } 34 35 func (q *DropTableQuery) Model(model interface{}) *DropTableQuery { 36 q.setModel(model) 37 return q 38 } 39 40 func (q *DropTableQuery) Err(err error) *DropTableQuery { 41 q.setErr(err) 42 return q 43 } 44 45 //------------------------------------------------------------------------------ 46 47 func (q *DropTableQuery) Table(tables ...string) *DropTableQuery { 48 for _, table := range tables { 49 q.addTable(schema.UnsafeIdent(table)) 50 } 51 return q 52 } 53 54 func (q *DropTableQuery) TableExpr(query string, args ...interface{}) *DropTableQuery { 55 q.addTable(schema.SafeQuery(query, args)) 56 return q 57 } 58 59 func (q *DropTableQuery) ModelTableExpr(query string, args ...interface{}) *DropTableQuery { 60 q.modelTableName = schema.SafeQuery(query, args) 61 return q 62 } 63 64 //------------------------------------------------------------------------------ 65 66 func (q *DropTableQuery) IfExists() *DropTableQuery { 67 q.ifExists = true 68 return q 69 } 70 71 func (q *DropTableQuery) Cascade() *DropTableQuery { 72 q.cascade = true 73 return q 74 } 75 76 func (q *DropTableQuery) Restrict() *DropTableQuery { 77 q.restrict = true 78 return q 79 } 80 81 //------------------------------------------------------------------------------ 82 83 func (q *DropTableQuery) Operation() string { 84 return "DROP TABLE" 85 } 86 87 func (q *DropTableQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) { 88 if q.err != nil { 89 return nil, q.err 90 } 91 92 b = append(b, "DROP TABLE "...) 93 if q.ifExists { 94 b = append(b, "IF EXISTS "...) 95 } 96 97 b, err = q.appendTables(fmter, b) 98 if err != nil { 99 return nil, err 100 } 101 102 b = q.appendCascade(fmter, b) 103 104 return b, nil 105 } 106 107 //------------------------------------------------------------------------------ 108 109 func (q *DropTableQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) { 110 if q.table != nil { 111 if err := q.beforeDropTableHook(ctx); err != nil { 112 return nil, err 113 } 114 } 115 116 queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes()) 117 if err != nil { 118 return nil, err 119 } 120 121 query := internal.String(queryBytes) 122 123 res, err := q.exec(ctx, q, query) 124 if err != nil { 125 return nil, err 126 } 127 128 if q.table != nil { 129 if err := q.afterDropTableHook(ctx); err != nil { 130 return nil, err 131 } 132 } 133 134 return res, nil 135 } 136 137 func (q *DropTableQuery) beforeDropTableHook(ctx context.Context) error { 138 if hook, ok := q.table.ZeroIface.(BeforeDropTableHook); ok { 139 if err := hook.BeforeDropTable(ctx, q); err != nil { 140 return err 141 } 142 } 143 return nil 144 } 145 146 func (q *DropTableQuery) afterDropTableHook(ctx context.Context) error { 147 if hook, ok := q.table.ZeroIface.(AfterDropTableHook); ok { 148 if err := hook.AfterDropTable(ctx, q); err != nil { 149 return err 150 } 151 } 152 return nil 153 }