Files
chaisql/query/drop.go
2019-12-15 14:31:04 +01:00

65 lines
1.5 KiB
Go

package query
import (
"database/sql/driver"
"errors"
"github.com/asdine/genji/database"
)
// DropTableStmt is a DSL that allows creating a DROP TABLE query.
type DropTableStmt struct {
TableName string
IfExists bool
}
// IsReadOnly always returns false. It implements the Statement interface.
func (stmt DropTableStmt) IsReadOnly() bool {
return false
}
// Run runs the DropTable statement in the given transaction.
// It implements the Statement interface.
func (stmt DropTableStmt) Run(tx *database.Transaction, args []driver.NamedValue) (Result, error) {
var res Result
if stmt.TableName == "" {
return res, errors.New("missing table name")
}
err := tx.DropTable(stmt.TableName)
if err == database.ErrTableNotFound && stmt.IfExists {
err = nil
}
return res, err
}
// DropIndexStmt is a DSL that allows creating a DROP INDEX query.
type DropIndexStmt struct {
IndexName string
IfExists bool
}
// IsReadOnly always returns false. It implements the Statement interface.
func (stmt DropIndexStmt) IsReadOnly() bool {
return false
}
// Run runs the DropIndex statement in the given transaction.
// It implements the Statement interface.
func (stmt DropIndexStmt) Run(tx *database.Transaction, args []driver.NamedValue) (Result, error) {
var res Result
if stmt.IndexName == "" {
return res, errors.New("missing index name")
}
err := tx.DropIndex(stmt.IndexName)
if err == database.ErrIndexNotFound && stmt.IfExists {
err = nil
}
return res, err
}