gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

query_column_add.go (2694B)


      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 AddColumnQuery struct {
     13 	baseQuery
     14 
     15 	ifNotExists bool
     16 }
     17 
     18 var _ Query = (*AddColumnQuery)(nil)
     19 
     20 func NewAddColumnQuery(db *DB) *AddColumnQuery {
     21 	q := &AddColumnQuery{
     22 		baseQuery: baseQuery{
     23 			db:   db,
     24 			conn: db.DB,
     25 		},
     26 	}
     27 	return q
     28 }
     29 
     30 func (q *AddColumnQuery) Conn(db IConn) *AddColumnQuery {
     31 	q.setConn(db)
     32 	return q
     33 }
     34 
     35 func (q *AddColumnQuery) Model(model interface{}) *AddColumnQuery {
     36 	q.setModel(model)
     37 	return q
     38 }
     39 
     40 func (q *AddColumnQuery) Err(err error) *AddColumnQuery {
     41 	q.setErr(err)
     42 	return q
     43 }
     44 
     45 func (q *AddColumnQuery) Apply(fn func(*AddColumnQuery) *AddColumnQuery) *AddColumnQuery {
     46 	if fn != nil {
     47 		return fn(q)
     48 	}
     49 	return q
     50 }
     51 
     52 //------------------------------------------------------------------------------
     53 
     54 func (q *AddColumnQuery) Table(tables ...string) *AddColumnQuery {
     55 	for _, table := range tables {
     56 		q.addTable(schema.UnsafeIdent(table))
     57 	}
     58 	return q
     59 }
     60 
     61 func (q *AddColumnQuery) TableExpr(query string, args ...interface{}) *AddColumnQuery {
     62 	q.addTable(schema.SafeQuery(query, args))
     63 	return q
     64 }
     65 
     66 func (q *AddColumnQuery) ModelTableExpr(query string, args ...interface{}) *AddColumnQuery {
     67 	q.modelTableName = schema.SafeQuery(query, args)
     68 	return q
     69 }
     70 
     71 //------------------------------------------------------------------------------
     72 
     73 func (q *AddColumnQuery) ColumnExpr(query string, args ...interface{}) *AddColumnQuery {
     74 	q.addColumn(schema.SafeQuery(query, args))
     75 	return q
     76 }
     77 
     78 func (q *AddColumnQuery) IfNotExists() *AddColumnQuery {
     79 	q.ifNotExists = true
     80 	return q
     81 }
     82 
     83 //------------------------------------------------------------------------------
     84 
     85 func (q *AddColumnQuery) Operation() string {
     86 	return "ADD COLUMN"
     87 }
     88 
     89 func (q *AddColumnQuery) 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: AddColumnQuery 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, " ADD "...)
    105 
    106 	if q.ifNotExists {
    107 		b = append(b, "IF NOT EXISTS "...)
    108 	}
    109 
    110 	b, err = q.columns[0].AppendQuery(fmter, b)
    111 	if err != nil {
    112 		return nil, err
    113 	}
    114 
    115 	return b, nil
    116 }
    117 
    118 //------------------------------------------------------------------------------
    119 
    120 func (q *AddColumnQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) {
    121 	queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
    122 	if err != nil {
    123 		return nil, err
    124 	}
    125 
    126 	query := internal.String(queryBytes)
    127 	return q.exec(ctx, q, query)
    128 }