This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Add support for DROP VIEW #860
Open
agarciamontoro
wants to merge
7
commits into
src-d:feature/views
Choose a base branch
from
agarciamontoro:feature.views.drop
base: feature/views
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
02c010c
Make ViewKey struct public
agarciamontoro fc97414
Add Exists and DeleteList to ViewRegistry
agarciamontoro f6ff425
Parse and analyze DROP VIEW statements
agarciamontoro 889bd1e
Test parsing utility readQualifiedIdentifierList
agarciamontoro 38bf07f
Test DropView node
agarciamontoro cb3b61c
Make sure that DropView is not executed without write permissions
agarciamontoro 9b8ecfa
Make qualifiedName type private to parse package
agarciamontoro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Parse and analyze DROP VIEW statements
Signed-off-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
- Loading branch information
commit f6ff42503eb08bf4511e623f58e679a998361367
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package plan | ||
|
||
import ( | ||
"github.com/src-d/go-mysql-server/sql" | ||
errors "gopkg.in/src-d/go-errors.v1" | ||
) | ||
|
||
var errDropViewChild = errors.NewKind("any child of DropView must be of type SingleDropView") | ||
|
||
type SingleDropView struct { | ||
database sql.Database | ||
viewName string | ||
} | ||
|
||
// NewSingleDropView creates a SingleDropView. | ||
func NewSingleDropView( | ||
database sql.Database, | ||
viewName string, | ||
) *SingleDropView { | ||
return &SingleDropView{database, viewName} | ||
} | ||
|
||
// Children implements the Node interface. It always returns nil. | ||
func (dv *SingleDropView) Children() []sql.Node { | ||
return nil | ||
} | ||
|
||
// Resolved implements the Node interface. This node is resolved if and only if | ||
// its database is resolved. | ||
func (dv *SingleDropView) Resolved() bool { | ||
_, ok := dv.database.(sql.UnresolvedDatabase) | ||
return !ok | ||
} | ||
|
||
// RowIter implements the Node interface. It always returns an empty iterator. | ||
func (dv *SingleDropView) RowIter(ctx *sql.Context) (sql.RowIter, error) { | ||
return sql.RowsToRowIter(), nil | ||
} | ||
|
||
// Schema implements the Node interface. It always returns nil. | ||
func (dv *SingleDropView) Schema() sql.Schema { return nil } | ||
|
||
// String implements the fmt.Stringer interface, using sql.TreePrinter to | ||
// generate the string. | ||
func (dv *SingleDropView) String() string { | ||
pr := sql.NewTreePrinter() | ||
_ = pr.WriteNode("SingleDropView(%s.%s)", dv.database.Name(), dv.viewName) | ||
|
||
return pr.String() | ||
} | ||
|
||
// WithChildren implements the Node interface. It only succeeds if the length | ||
// of the specified children equals 0. | ||
func (dv *SingleDropView) WithChildren(children ...sql.Node) (sql.Node, error) { | ||
if len(children) != 0 { | ||
return nil, sql.ErrInvalidChildrenNumber.New(dv, len(children), 0) | ||
} | ||
|
||
return dv, nil | ||
} | ||
|
||
// Database implements the Databaser interfacee. It returns the node's database. | ||
func (dv *SingleDropView) Database() sql.Database { | ||
return dv.database | ||
} | ||
|
||
// Database implements the Databaser interface, and it returns a copy of this | ||
// node with the specified database. | ||
func (dv *SingleDropView) WithDatabase(database sql.Database) (sql.Node, error) { | ||
newDrop := *dv | ||
newDrop.database = database | ||
return &newDrop, nil | ||
} | ||
|
||
// DropView is a node representing the removal of a list of views, defined by | ||
// the children member. The flag ifExists represents whether the user wants the | ||
// node to fail if any of the views in children does not exist. | ||
type DropView struct { | ||
children []sql.Node | ||
Catalog *sql.Catalog | ||
ifExists bool | ||
} | ||
|
||
// NewDropView creates a DropView node with the specified parameters, | ||
// setting its catalog to nil. | ||
func NewDropView(children []sql.Node, ifExists bool) *DropView { | ||
erizocosmico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &DropView{children, nil, ifExists} | ||
} | ||
|
||
// Children implements the Node interface. It returns the children of the | ||
// CreateView node; i.e., all the views that will be dropped. | ||
func (dvs *DropView) Children() []sql.Node { | ||
return dvs.children | ||
} | ||
|
||
// Resolved implements the Node interface. This node is resolved if and only if | ||
// all of its children are resolved. | ||
func (dvs *DropView) Resolved() bool { | ||
for _, child := range dvs.children { | ||
if !child.Resolved() { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// RowIter implements the Node interface. When executed, this function drops | ||
// all the views defined by the node's children. It errors if the flag ifExists | ||
// is set to false and there is some view that does not exist. | ||
func (dvs *DropView) RowIter(ctx *sql.Context) (sql.RowIter, error) { | ||
viewList := make([]sql.ViewKey, len(dvs.children)) | ||
for i, child := range dvs.children { | ||
drop, ok := child.(*SingleDropView) | ||
if !ok { | ||
return sql.RowsToRowIter(), errDropViewChild.New() | ||
} | ||
|
||
viewList[i] = sql.NewViewKey(drop.database.Name(), drop.viewName) | ||
} | ||
|
||
return sql.RowsToRowIter(), dvs.Catalog.ViewRegistry.DeleteList(viewList, !dvs.ifExists) | ||
} | ||
|
||
// Schema implements the Node interface. It always returns nil. | ||
func (dvs *DropView) Schema() sql.Schema { return nil } | ||
|
||
// String implements the fmt.Stringer interface, using sql.TreePrinter to | ||
// generate the string. | ||
func (dvs *DropView) String() string { | ||
childrenStrings := make([]string, len(dvs.children)) | ||
for i, child := range dvs.children { | ||
childrenStrings[i] = child.String() | ||
} | ||
|
||
pr := sql.NewTreePrinter() | ||
_ = pr.WriteNode("DropView") | ||
_ = pr.WriteChildren(childrenStrings...) | ||
|
||
return pr.String() | ||
} | ||
|
||
// WithChildren implements the Node interface. It always suceeds, returning a | ||
// copy of this node with the new array of nodes as children. | ||
func (dvs *DropView) WithChildren(children ...sql.Node) (sql.Node, error) { | ||
newDrop := dvs | ||
newDrop.children = children | ||
return newDrop, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.