1
0
mirror of https://github.com/uptrace/go-clickhouse.git synced 2025-06-08 23:26:11 +02:00
go-clickhouse/ch/query_view_drop.go
2022-11-03 11:58:19 +02:00

109 lines
2.2 KiB
Go

package ch
import (
"context"
"database/sql"
"github.com/uptrace/go-clickhouse/ch/chschema"
"github.com/uptrace/go-clickhouse/ch/internal"
)
type DropViewQuery struct {
baseQuery
ifExists bool
view chschema.QueryWithArgs
cluster chschema.QueryWithArgs
}
var _ Query = (*DropViewQuery)(nil)
func NewDropViewQuery(db *DB) *DropViewQuery {
q := &DropViewQuery{
baseQuery: baseQuery{
db: db,
},
}
return q
}
func (q *DropViewQuery) Model(model any) *DropViewQuery {
q.setTableModel(model)
return q
}
func (q *DropViewQuery) WithQuery(fn func(*DropViewQuery) *DropViewQuery) *DropViewQuery {
return fn(q)
}
//------------------------------------------------------------------------------
func (q *DropViewQuery) IfExists() *DropViewQuery {
q.ifExists = true
return q
}
func (q *DropViewQuery) View(view string) *DropViewQuery {
q.view = chschema.UnsafeIdent(view)
return q
}
func (q *DropViewQuery) ViewExpr(query string, args ...any) *DropViewQuery {
q.view = chschema.SafeQuery(query, args)
return q
}
func (q *DropViewQuery) OnCluster(cluster string) *DropViewQuery {
q.cluster = chschema.UnsafeIdent(cluster)
return q
}
func (q *DropViewQuery) OnClusterExpr(query string, args ...any) *DropViewQuery {
q.cluster = chschema.SafeQuery(query, args)
return q
}
//------------------------------------------------------------------------------
func (q *DropViewQuery) Operation() string {
return "DROP TABLE"
}
func (q *DropViewQuery) AppendQuery(fmter chschema.Formatter, b []byte) (_ []byte, err error) {
if q.err != nil {
return nil, q.err
}
b = append(b, "DROP VIEW "...)
if q.ifExists {
b = append(b, "IF EXISTS "...)
}
b, err = q.view.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
if !q.cluster.IsEmpty() {
b = append(b, " ON CLUSTER "...)
b, err = q.cluster.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
}
return b, nil
}
//------------------------------------------------------------------------------
func (q *DropViewQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error) {
queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
}
query := internal.String(queryBytes)
return q.exec(ctx, q, query)
}