query_column_drop.go (2783B)
1 package bun 2 3 import ( 4 "context" 5 "database/sql" 6 "fmt" 7 8 "github.com/uptrace/bun/internal" 9 "github.com/uptrace/bun/schema" 10 ) 11 12 type DropColumnQuery struct { 13 baseQuery 14 } 15 16 var _ Query = (*DropColumnQuery)(nil) 17 18 func NewDropColumnQuery(db *DB) *DropColumnQuery { 19 q := &DropColumnQuery{ 20 baseQuery: baseQuery{ 21 db: db, 22 conn: db.DB, 23 }, 24 } 25 return q 26 } 27 28 func (q *DropColumnQuery) Conn(db IConn) *DropColumnQuery { 29 q.setConn(db) 30 return q 31 } 32 33 func (q *DropColumnQuery) Model(model interface{}) *DropColumnQuery { 34 q.setModel(model) 35 return q 36 } 37 38 func (q *DropColumnQuery) Err(err error) *DropColumnQuery { 39 q.setErr(err) 40 return q 41 } 42 43 func (q *DropColumnQuery) Apply(fn func(*DropColumnQuery) *DropColumnQuery) *DropColumnQuery { 44 if fn != nil { 45 return fn(q) 46 } 47 return q 48 } 49 50 //------------------------------------------------------------------------------ 51 52 func (q *DropColumnQuery) Table(tables ...string) *DropColumnQuery { 53 for _, table := range tables { 54 q.addTable(schema.UnsafeIdent(table)) 55 } 56 return q 57 } 58 59 func (q *DropColumnQuery) TableExpr(query string, args ...interface{}) *DropColumnQuery { 60 q.addTable(schema.SafeQuery(query, args)) 61 return q 62 } 63 64 func (q *DropColumnQuery) ModelTableExpr(query string, args ...interface{}) *DropColumnQuery { 65 q.modelTableName = schema.SafeQuery(query, args) 66 return q 67 } 68 69 //------------------------------------------------------------------------------ 70 71 func (q *DropColumnQuery) Column(columns ...string) *DropColumnQuery { 72 for _, column := range columns { 73 q.addColumn(schema.UnsafeIdent(column)) 74 } 75 return q 76 } 77 78 func (q *DropColumnQuery) ColumnExpr(query string, args ...interface{}) *DropColumnQuery { 79 q.addColumn(schema.SafeQuery(query, args)) 80 return q 81 } 82 83 //------------------------------------------------------------------------------ 84 85 func (q *DropColumnQuery) Operation() string { 86 return "DROP COLUMN" 87 } 88 89 func (q *DropColumnQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) { 90 if q.err != nil { 91 return nil, q.err 92 } 93 if len(q.columns) != 1 { 94 return nil, fmt.Errorf("bun: DropColumnQuery requires exactly one column") 95 } 96 97 b = append(b, "ALTER TABLE "...) 98 99 b, err = q.appendFirstTable(fmter, b) 100 if err != nil { 101 return nil, err 102 } 103 104 b = append(b, " DROP COLUMN "...) 105 106 b, err = q.columns[0].AppendQuery(fmter, b) 107 if err != nil { 108 return nil, err 109 } 110 111 return b, nil 112 } 113 114 //------------------------------------------------------------------------------ 115 116 func (q *DropColumnQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) { 117 queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes()) 118 if err != nil { 119 return nil, err 120 } 121 122 query := internal.String(queryBytes) 123 124 res, err := q.exec(ctx, q, query) 125 if err != nil { 126 return nil, err 127 } 128 129 return res, nil 130 }