Skip to content

Commit

Permalink
Remove duplicate error check
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Apr 13, 2021
1 parent baf316e commit 7473413
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 19 deletions.
18 changes: 0 additions & 18 deletions query/create.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package query

import (
"errors"

"github.com/genjidb/genji/database"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/expr"
Expand All @@ -25,10 +23,6 @@ func (stmt CreateTableStmt) IsReadOnly() bool {
func (stmt CreateTableStmt) Run(tx *database.Transaction, args []expr.Param) (Result, error) {
var res Result

if stmt.TableName == "" {
return res, errors.New("missing table name")
}

err := tx.CreateTable(stmt.TableName, &stmt.Info)
if stmt.IfNotExists && err == database.ErrTableAlreadyExists {
err = nil
Expand Down Expand Up @@ -57,18 +51,6 @@ func (stmt CreateIndexStmt) IsReadOnly() bool {
func (stmt CreateIndexStmt) Run(tx *database.Transaction, args []expr.Param) (Result, error) {
var res Result

if stmt.TableName == "" {
return res, errors.New("missing table name")
}

if stmt.IfNotExists && len(stmt.IndexName) == 0 {
return res, errors.New("missing index name with IF NOT EXISTS clause")
}

if len(stmt.Path) == 0 {
return res, errors.New("missing path")
}

err := tx.CreateIndex(&database.IndexInfo{
Unique: stmt.Unique,
IndexName: stmt.IndexName,
Expand Down
5 changes: 5 additions & 0 deletions sql/parser/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ func (p *Parser) parseCreateIndexStatement(unique bool) (query.CreateIndexStmt,
// Parse optional index name
stmt.IndexName, err = p.parseIdent()
if err != nil {
// if IF NOT EXISTS is set, index name is mandatory
if stmt.IfNotExists {
return stmt, err
}

p.Unscan()
}

Expand Down
3 changes: 2 additions & 1 deletion sql/parser/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ func TestParserCreateIndex(t *testing.T) {
{"Basic", "CREATE INDEX idx ON test (foo)", query.CreateIndexStmt{IndexName: "idx", TableName: "test", Path: document.Path(parsePath(t, "foo"))}, false},
{"If not exists", "CREATE INDEX IF NOT EXISTS idx ON test (foo.bar[1])", query.CreateIndexStmt{IndexName: "idx", TableName: "test", Path: document.Path(parsePath(t, "foo.bar[1]")), IfNotExists: true}, false},
{"Unique", "CREATE UNIQUE INDEX IF NOT EXISTS idx ON test (foo[3].baz)", query.CreateIndexStmt{IndexName: "idx", TableName: "test", Path: document.Path(parsePath(t, "foo[3].baz")), IfNotExists: true, Unique: true}, false},
{"No name", "CREATE UNIQUE INDEX IF NOT EXISTS ON test (foo[3].baz)", query.CreateIndexStmt{TableName: "test", Path: document.Path(parsePath(t, "foo[3].baz")), IfNotExists: true, Unique: true}, false},
{"No name", "CREATE UNIQUE INDEX ON test (foo[3].baz)", query.CreateIndexStmt{TableName: "test", Path: document.Path(parsePath(t, "foo[3].baz")), Unique: true}, false},
{"No name with IF NOT EXISTS", "CREATE UNIQUE INDEX IF NOT EXISTS ON test (foo[3].baz)", nil, true},
{"No fields", "CREATE INDEX idx ON test", nil, true},
{"More than 1 path", "CREATE INDEX idx ON test (foo, bar)", nil, true},
}
Expand Down

0 comments on commit 7473413

Please sign in to comment.