query_table_truncate.go (2763B)
1 package bun 2 3 import ( 4 "context" 5 "database/sql" 6 7 "github.com/uptrace/bun/dialect/feature" 8 "github.com/uptrace/bun/internal" 9 "github.com/uptrace/bun/schema" 10 ) 11 12 type TruncateTableQuery struct { 13 baseQuery 14 cascadeQuery 15 16 continueIdentity bool 17 } 18 19 var _ Query = (*TruncateTableQuery)(nil) 20 21 func NewTruncateTableQuery(db *DB) *TruncateTableQuery { 22 q := &TruncateTableQuery{ 23 baseQuery: baseQuery{ 24 db: db, 25 conn: db.DB, 26 }, 27 } 28 return q 29 } 30 31 func (q *TruncateTableQuery) Conn(db IConn) *TruncateTableQuery { 32 q.setConn(db) 33 return q 34 } 35 36 func (q *TruncateTableQuery) Model(model interface{}) *TruncateTableQuery { 37 q.setModel(model) 38 return q 39 } 40 41 func (q *TruncateTableQuery) Err(err error) *TruncateTableQuery { 42 q.setErr(err) 43 return q 44 } 45 46 //------------------------------------------------------------------------------ 47 48 func (q *TruncateTableQuery) Table(tables ...string) *TruncateTableQuery { 49 for _, table := range tables { 50 q.addTable(schema.UnsafeIdent(table)) 51 } 52 return q 53 } 54 55 func (q *TruncateTableQuery) TableExpr(query string, args ...interface{}) *TruncateTableQuery { 56 q.addTable(schema.SafeQuery(query, args)) 57 return q 58 } 59 60 //------------------------------------------------------------------------------ 61 62 func (q *TruncateTableQuery) ContinueIdentity() *TruncateTableQuery { 63 q.continueIdentity = true 64 return q 65 } 66 67 func (q *TruncateTableQuery) Cascade() *TruncateTableQuery { 68 q.cascade = true 69 return q 70 } 71 72 func (q *TruncateTableQuery) Restrict() *TruncateTableQuery { 73 q.restrict = true 74 return q 75 } 76 77 //------------------------------------------------------------------------------ 78 79 func (q *TruncateTableQuery) Operation() string { 80 return "TRUNCATE TABLE" 81 } 82 83 func (q *TruncateTableQuery) AppendQuery( 84 fmter schema.Formatter, b []byte, 85 ) (_ []byte, err error) { 86 if q.err != nil { 87 return nil, q.err 88 } 89 90 if !fmter.HasFeature(feature.TableTruncate) { 91 b = append(b, "DELETE FROM "...) 92 93 b, err = q.appendTables(fmter, b) 94 if err != nil { 95 return nil, err 96 } 97 98 return b, nil 99 } 100 101 b = append(b, "TRUNCATE TABLE "...) 102 103 b, err = q.appendTables(fmter, b) 104 if err != nil { 105 return nil, err 106 } 107 108 if q.db.features.Has(feature.TableIdentity) { 109 if q.continueIdentity { 110 b = append(b, " CONTINUE IDENTITY"...) 111 } else { 112 b = append(b, " RESTART IDENTITY"...) 113 } 114 } 115 116 b = q.appendCascade(fmter, b) 117 118 return b, nil 119 } 120 121 //------------------------------------------------------------------------------ 122 123 func (q *TruncateTableQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) { 124 queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes()) 125 if err != nil { 126 return nil, err 127 } 128 129 query := internal.String(queryBytes) 130 131 res, err := q.exec(ctx, q, query) 132 if err != nil { 133 return nil, err 134 } 135 136 return res, nil 137 }