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 replace statement #448

Merged
merged 15 commits into from
Oct 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 43 additions & 13 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ import (
references "REFERENCES"
regexp "REGEXP"
repeat "REPEAT"
replace "REPLACE"
right "RIGHT"
rlike "RLIKE"
rollback "ROLLBACK"
Expand Down Expand Up @@ -432,7 +433,7 @@ import (
IndexName "index name"
IndexType "index type"
InsertIntoStmt "INSERT INTO statement"
InsertRest "Rest part of INSERT INTO statement"
InsertValues "Rest part of INSERT/REPLACE INTO statement"
IntoOpt "INTO or EmptyString"
JoinTable "join table"
JoinType "join type"
Expand Down Expand Up @@ -474,6 +475,8 @@ import (
PrivType "Privilege type"
ReferDef "Reference definition"
RegexpSym "REGEXP or RLIKE"
ReplaceIntoStmt "REPLACE INTO statement"
ReplacePriority "replace statement priority"
RollbackStmt "ROLLBACK statement"
SelectLockOpt "FOR UPDATE or LOCK IN SHARE MODE,"
SelectStmt "SELECT statement"
Expand Down Expand Up @@ -1714,9 +1717,9 @@ NotKeywordToken:
* TODO: support PARTITION
**********************************************************************************/
InsertIntoStmt:
"INSERT" Priority IgnoreOptional IntoOpt TableIdent InsertRest OnDuplicateKeyUpdate
"INSERT" Priority IgnoreOptional IntoOpt TableIdent InsertValues OnDuplicateKeyUpdate
{
x := $6.(*stmts.InsertIntoStmt)
x := &stmts.InsertIntoStmt{InsertValues: $6.(stmts.InsertValues)}
x.Priority = $2.(int)
x.TableIdent = $5.(table.Ident)
if $7 != nil {
Expand All @@ -1735,36 +1738,36 @@ IntoOpt:
{
}

InsertRest:
InsertValues:
'(' ColumnNameListOpt ')' ValueSym ExpressionListList
{
$$ = &stmts.InsertIntoStmt{
$$ = stmts.InsertValues{
ColNames: $2.([]string),
Lists: $5.([][]expression.Expression)}
}
| '(' ColumnNameListOpt ')' SelectStmt
{
$$ = &stmts.InsertIntoStmt{ColNames: $2.([]string), Sel: $4.(*stmts.SelectStmt)}
$$ = stmts.InsertValues{ColNames: $2.([]string), Sel: $4.(*stmts.SelectStmt)}
}
| '(' ColumnNameListOpt ')' UnionStmt
{
$$ = &stmts.InsertIntoStmt{ColNames: $2.([]string), Sel: $4.(*stmts.UnionStmt)}
$$ = stmts.InsertValues{ColNames: $2.([]string), Sel: $4.(*stmts.UnionStmt)}
}
| ValueSym ExpressionListList %prec insertValues
{
$$ = &stmts.InsertIntoStmt{Lists: $2.([][]expression.Expression)}
$$ = stmts.InsertValues{Lists: $2.([][]expression.Expression)}
}
| SelectStmt
{
$$ = &stmts.InsertIntoStmt{Sel: $1.(*stmts.SelectStmt)}
$$ = stmts.InsertValues{Sel: $1.(*stmts.SelectStmt)}
}
| UnionStmt
{
$$ = &stmts.InsertIntoStmt{Sel: $1.(*stmts.UnionStmt)}
$$ = stmts.InsertValues{Sel: $1.(*stmts.UnionStmt)}
}
| "SET" ColumnSetValueList
{
$$ = &stmts.InsertIntoStmt{Setlist: $2.([]*expression.Assignment)}
$$ = stmts.InsertValues{Setlist: $2.([]*expression.Assignment)}
}

ValueSym:
Expand Down Expand Up @@ -1823,7 +1826,33 @@ OnDuplicateKeyUpdate:
$$ = $5
}

/***********************************Insert Statments END************************************/
/************************************************************************************
* Replace Statments
* See: https://dev.mysql.com/doc/refman/5.7/en/replace.html
*
* TODO: support PARTITION
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a reference here. SQL statement syntax maybe different for different mysql version. So we need add reference.

We use 5.7 as standard. https://dev.mysql.com/doc/refman/5.7/en/replace.html

**********************************************************************************/
ReplaceIntoStmt:
"REPLACE" ReplacePriority IntoOpt TableIdent InsertValues
{
x := &stmts.ReplaceIntoStmt{InsertValues: $5.(stmts.InsertValues)}
x.Priority = $2.(int)
x.TableIdent = $4.(table.Ident)
$$ = x
}

ReplacePriority:
{
$$ = stmts.NoPriority
}
| "LOW_PRIORITY"
{
$$ = stmts.LowPriority
}
| "DELAYED"
{
$$ = stmts.DelayedPriority
}

Literal:
"false"
Expand Down Expand Up @@ -2776,7 +2805,6 @@ PrimaryFactor:
}
| PrimaryExpression


Priority:
{
$$ = stmts.NoPriority
Expand Down Expand Up @@ -3533,6 +3561,7 @@ Statement:
| InsertIntoStmt
| PreparedStmt
| RollbackStmt
| ReplaceIntoStmt
| SelectStmt
| SetStmt
| ShowStmt
Expand All @@ -3552,6 +3581,7 @@ ExplainableStmt:
| DeleteFromStmt
| UpdateStmt
| InsertIntoStmt
| ReplaceIntoStmt

StatementList:
Statement
Expand Down
11 changes: 11 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ func (s *testParserSuite) TestDMLStmt(c *C) {
{"INSERT INTO foo (a,b,) VALUES (42,314,)", false},
{"INSERT INTO foo () VALUES ()", true},
{"INSERT INTO foo VALUE ()", true},

{"REPLACE INTO foo VALUES (1 || 2)", true},
{"REPLACE INTO foo VALUES (1 | 2)", true},
{"REPLACE INTO foo VALUES (false || true)", true},
{"REPLACE INTO foo VALUES (bar(5678))", false},
{"REPLACE INTO foo VALUES ()", true},
{"REPLACE INTO foo (a,b) VALUES (42,314)", true},
{"REPLACE INTO foo (a,b,) VALUES (42,314)", false},
{"REPLACE INTO foo (a,b,) VALUES (42,314,)", false},
{"REPLACE INTO foo () VALUES ()", true},
{"REPLACE INTO foo VALUE ()", true},
// 40
{`SELECT stuff.id
FROM stuff
Expand Down
2 changes: 2 additions & 0 deletions parser/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ rand {r}{a}{n}{d}
repeat {r}{e}{p}{e}{a}{t}
references {r}{e}{f}{e}{r}{e}{n}{c}{e}{s}
regexp {r}{e}{g}{e}{x}{p}
replace {r}{e}{p}{l}{a}{c}{e}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alignment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alignment in view is ok.

right {r}{i}{g}{h}{t}
rlike {r}{l}{i}{k}{e}
rollback {r}{o}{l}{l}{b}{a}{c}{k}
Expand Down Expand Up @@ -802,6 +803,7 @@ year_month {y}{e}{a}{r}_{m}{o}{n}{t}{h}
return repeat
{regexp} return regexp
{references} return references
{replace} return replace
{rlike} return rlike

{sys_var} lval.item = string(l.val)
Expand Down
4 changes: 2 additions & 2 deletions stmt/stmts/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s *DeleteStmt) plan(ctx context.Context) (plan.Plan, error) {
return r, nil
}

func (s *DeleteStmt) removeRow(ctx context.Context, t table.Table, h int64, data []interface{}) error {
func removeRow(ctx context.Context, t table.Table, h int64, data []interface{}) error {
// remove row's all indexies
if err := t.RemoveRowAllIndex(ctx, h, data); err != nil {
return err
Expand Down Expand Up @@ -186,7 +186,7 @@ func (s *DeleteStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {
if err != nil {
return nil, errors.Trace(err)
}
err = s.removeRow(ctx, t, handle, data)
err = removeRow(ctx, t, handle, data)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
Loading