From 7473413419a70aaac6dbc85211e0aa1592d369fe Mon Sep 17 00:00:00 2001 From: Asdine El Hrychy Date: Tue, 13 Apr 2021 09:44:38 +0400 Subject: [PATCH] Remove duplicate error check --- query/create.go | 18 ------------------ sql/parser/create.go | 5 +++++ sql/parser/create_test.go | 3 ++- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/query/create.go b/query/create.go index 2e3b6860e..445e5ed27 100644 --- a/query/create.go +++ b/query/create.go @@ -1,8 +1,6 @@ package query import ( - "errors" - "github.com/genjidb/genji/database" "github.com/genjidb/genji/document" "github.com/genjidb/genji/expr" @@ -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 @@ -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, diff --git a/sql/parser/create.go b/sql/parser/create.go index 711b81f26..1d89f7e11 100644 --- a/sql/parser/create.go +++ b/sql/parser/create.go @@ -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() } diff --git a/sql/parser/create_test.go b/sql/parser/create_test.go index 643f73e23..bfc432851 100644 --- a/sql/parser/create_test.go +++ b/sql/parser/create_test.go @@ -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}, }