Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add check option to create view #331

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1800,13 +1800,22 @@ func (node *DBDDL) Format(buf *TrackedBuffer) {
}
}

type ViewCheckOption string

const (
ViewCheckOptionUnspecified ViewCheckOption = ""
ViewCheckOptionCascaded ViewCheckOption = "cascaded"
ViewCheckOptionLocal ViewCheckOption = "local"
)

type ViewSpec struct {
ViewName TableName
Columns Columns
Algorithm string
Definer string
Security string
ViewExpr SelectStatement
ViewName TableName
Columns Columns
Algorithm string
Definer string
Security string
ViewExpr SelectStatement
CheckOption ViewCheckOption
}

type TriggerSpec struct {
Expand Down Expand Up @@ -2107,6 +2116,7 @@ func (node *DDL) Format(buf *TrackedBuffer) {
if node.ViewSpec != nil {
view := node.ViewSpec
afterCreate := ""
checkOpt := ""
if node.OrReplace {
afterCreate = "or replace "
}
Expand All @@ -2119,7 +2129,10 @@ func (node *DDL) Format(buf *TrackedBuffer) {
if view.Security != "" {
afterCreate = fmt.Sprintf("%ssql security %s ", afterCreate, strings.ToLower(view.Security))
}
buf.Myprintf("%s %sview %v%v as %v", node.Action, afterCreate, view.ViewName, view.Columns, view.ViewExpr)
if view.CheckOption != ViewCheckOptionUnspecified {
checkOpt = fmt.Sprintf(" with %s check option", view.CheckOption)
}
buf.Myprintf("%s %sview %v%v as %v%s", node.Action, afterCreate, view.ViewName, view.Columns, view.ViewExpr, checkOpt)
} else if node.TriggerSpec != nil {
trigger := node.TriggerSpec
triggerDef := ""
Expand Down
49 changes: 29 additions & 20 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ var (
output: "set global GTID_PURGED = '+beabe64c-9dc6-11ed-8021-a0f9021e8e70:1-126'",
},
{
input: "show replicas",
input: "show replicas",
},
{
input: "show binary logs",
input: "show binary logs",
},
{
input: "show binary log status",
input: "show binary log status",
},
{
input: "start replica",
Expand Down Expand Up @@ -3198,6 +3198,15 @@ var (
}, {
input: "create view v_today(today) as select CURRENT_DATE()",
output: "create view v_today(today) as select CURRENT_DATE()",
}, {
input: "create or replace view v_today(today) as select CURRENT_DATE()",
output: "create or replace view v_today(today) as select CURRENT_DATE()",
}, {
input: "create view v_today(today) as select CURRENT_DATE() WITH CHECK OPTION",
output: "create view v_today(today) as select CURRENT_DATE() with cascaded check option",
}, {
input: "create view v_today(today) as select CURRENT_DATE() WITH LOCAL CHECK OPTION",
output: "create view v_today(today) as select CURRENT_DATE() with local check option",
}, {
input: "select 1 into @aaa",
}, {
Expand Down Expand Up @@ -3258,47 +3267,47 @@ var (
output: "select id from mytable order by id desc limit 15 into dumpfile 'dump.txt'",
},
{
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a'",
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a'",
},
{
input: "select * from tbl into outfile 'outfile.txt' columns terminated by 'a'",
output: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a'",
},
{
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' enclosed by 'b'",
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' enclosed by 'b'",
},
{
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' optionally enclosed by 'b'",
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' optionally enclosed by 'b'",
},
{
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' escaped by 'c'",
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' escaped by 'c'",
},
{
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' enclosed by 'b' escaped by 'c'",
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' enclosed by 'b' escaped by 'c'",
},
{
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' optionally enclosed by 'b' escaped by 'c'",
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' optionally enclosed by 'b' escaped by 'c'",
},
{
input: "select * from tbl into outfile 'outfile.txt' lines terminated by 'd'",
input: "select * from tbl into outfile 'outfile.txt' lines terminated by 'd'",
},
{
input: "select * from tbl into outfile 'outfile.txt' lines starting by 'd' terminated by 'e'",
input: "select * from tbl into outfile 'outfile.txt' lines starting by 'd' terminated by 'e'",
},
{
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' lines terminated by 'd'",
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' lines terminated by 'd'",
},
{
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' enclosed by 'b' lines terminated by 'd'",
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' enclosed by 'b' lines terminated by 'd'",
},
{
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' escaped by 'c' lines terminated by 'd'",
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' escaped by 'c' lines terminated by 'd'",
},
{
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' optionally enclosed by 'b' escaped by 'c' lines terminated by 'd'",
input: "select * from tbl into outfile 'outfile.txt' fields terminated by 'a' optionally enclosed by 'b' escaped by 'c' lines terminated by 'd'",
},
{
input: "select * from tbl into outfile 'outfile.txt' character set binary fields terminated by 'a'",
input: "select * from tbl into outfile 'outfile.txt' character set binary fields terminated by 'a'",
},
{
input: "table tbl into outfile 'outfile.txt' fields terminated by 'a' optionally enclosed by 'b' escaped by 'c' lines terminated by 'd'",
Expand Down Expand Up @@ -4795,12 +4804,12 @@ func TestInvalid(t *testing.T) {
},
{
// TODO: should work
input: "select * from tbl into outfile 'outfile.txt' lines terminated by 'e' starting by 'd'",
input: "select * from tbl into outfile 'outfile.txt' lines terminated by 'e' starting by 'd'",
err: "syntax error",
},
{
// TODO: should work
input: "select * from tbl into outfile 'outfile.txt' lines starting by 'd' terminated by 'e' starting by 'd' terminated by 'e'",
input: "select * from tbl into outfile 'outfile.txt' lines starting by 'd' terminated by 'e' starting by 'd' terminated by 'e'",
err: "syntax error",
},
}
Expand Down Expand Up @@ -5648,7 +5657,7 @@ func TestConvert(t *testing.T) {
input: "select cast('abc' as year) from t",
},
{
input: "select cast('abc' as date) from t",
input: "select cast('abc' as date) from t",
useSelectExpressionLiteral: true,
},
{
Expand Down Expand Up @@ -5733,7 +5742,7 @@ func TestConvert(t *testing.T) {
{
input: "select convert('abc', year) from t",
},
}
}

for _, tcase := range validSQL {
runParseTestCase(t, tcase)
Expand Down
Loading
Loading