mirror of
https://github.com/chaisql/chai.git
synced 2025-10-23 23:53:09 +08:00
Move tests to query package
This commit is contained in:
64
query/drop.go
Normal file
64
query/drop.go
Normal file
@@ -0,0 +1,64 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user