From eb033c49226b6e20e147edca6736ba907cb2b2d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matczuk?= Date: Fri, 28 Jul 2017 09:43:58 +0200 Subject: [PATCH] qb: removed error from builder ToCql function Builder is not in a position to validate the statement and the effort is duplicated when the query is build. The checked errors are simple and do not occur most of the time. --- qb/delete.go | 13 +------------ qb/delete_test.go | 5 +---- qb/insert.go | 13 +------------ qb/insert_test.go | 5 +---- qb/select.go | 12 +----------- qb/select_test.go | 5 +---- qb/update.go | 17 +---------------- qb/update_test.go | 5 +---- 8 files changed, 8 insertions(+), 67 deletions(-) diff --git a/qb/delete.go b/qb/delete.go index aca64b3..45ab123 100644 --- a/qb/delete.go +++ b/qb/delete.go @@ -5,8 +5,6 @@ package qb import ( "bytes" - "errors" - "fmt" "time" ) @@ -26,16 +24,7 @@ func Delete(table string) *DeleteBuilder { } } -func (b *DeleteBuilder) ToCql() (stmt string, names []string, err error) { - if b.table == "" { - err = errors.New("delete statements must specify a table") - return - } - if len(b.where) == 0 { - err = fmt.Errorf("delete statements must have at least one WHERE clause") - return - } - +func (b *DeleteBuilder) ToCql() (stmt string, names []string) { cql := bytes.Buffer{} cql.WriteString("DELETE ") diff --git a/qb/delete_test.go b/qb/delete_test.go index 48037db..db01ca1 100644 --- a/qb/delete_test.go +++ b/qb/delete_test.go @@ -60,10 +60,7 @@ func TestDeleteBuilder(t *testing.T) { } for _, test := range table { - stmt, names, err := test.B.ToCql() - if err != nil { - t.Error("unexpected error", err) - } + stmt, names := test.B.ToCql() if diff := cmp.Diff(test.S, stmt); diff != "" { t.Error(diff) } diff --git a/qb/insert.go b/qb/insert.go index 8a2cb89..2b31bb6 100644 --- a/qb/insert.go +++ b/qb/insert.go @@ -5,7 +5,6 @@ package qb import ( "bytes" - "errors" "time" ) @@ -23,17 +22,7 @@ func Insert(table string) *InsertBuilder { } } -func (b *InsertBuilder) ToCql() (stmt string, names []string, err error) { - if b.table == "" { - err = errors.New("insert statements must specify a table") - return - } - - if len(b.columns) == 0 { - err = errors.New("insert statements must specify columns") - return - } - +func (b *InsertBuilder) ToCql() (stmt string, names []string) { cql := bytes.Buffer{} cql.WriteString("INSERT ") diff --git a/qb/insert_test.go b/qb/insert_test.go index b00eeff..02299eb 100644 --- a/qb/insert_test.go +++ b/qb/insert_test.go @@ -53,10 +53,7 @@ func TestInsertBuilder(t *testing.T) { } for _, test := range table { - stmt, names, err := test.B.ToCql() - if err != nil { - t.Error("unexpected error", err) - } + stmt, names := test.B.ToCql() if diff := cmp.Diff(test.S, stmt); diff != "" { t.Error(diff) } diff --git a/qb/select.go b/qb/select.go index 4e69786..7e3c03f 100644 --- a/qb/select.go +++ b/qb/select.go @@ -5,7 +5,6 @@ package qb import ( "bytes" - "errors" "fmt" ) @@ -36,16 +35,7 @@ func Select(table string) *SelectBuilder { } } -func (b *SelectBuilder) ToCql() (stmt string, names []string, err error) { - if b.table == "" { - err = errors.New("select statements must specify a table") - return - } - if b.distinct != "" && len(b.columns) > 0 { - err = fmt.Errorf("select statements must specify either a column list or DISTINCT partition_key") - return - } - +func (b *SelectBuilder) ToCql() (stmt string, names []string) { cql := bytes.Buffer{} cql.WriteString("SELECT ") diff --git a/qb/select_test.go b/qb/select_test.go index a5fc761..ee0371c 100644 --- a/qb/select_test.go +++ b/qb/select_test.go @@ -72,10 +72,7 @@ func TestSelectBuilder(t *testing.T) { } for _, test := range table { - stmt, names, err := test.B.ToCql() - if err != nil { - t.Error("unexpected error", err) - } + stmt, names := test.B.ToCql() if diff := cmp.Diff(test.S, stmt); diff != "" { t.Error(diff) } diff --git a/qb/update.go b/qb/update.go index ebd99b9..14c3294 100644 --- a/qb/update.go +++ b/qb/update.go @@ -2,8 +2,6 @@ package qb import ( "bytes" - "errors" - "fmt" ) // UPDATE reference: @@ -29,20 +27,7 @@ func Update(table string) *UpdateBuilder { } } -func (b *UpdateBuilder) ToCql() (stmt string, names []string, err error) { - if b.table == "" { - err = errors.New("update statements must specify a table") - return - } - if len(b.columns) == 0 { - err = fmt.Errorf("update statements must have at least one SET clause") - return - } - if len(b.where) == 0 { - err = fmt.Errorf("update statements must have at least one WHERE clause") - return - } - +func (b *UpdateBuilder) ToCql() (stmt string, names []string) { cql := bytes.Buffer{} cql.WriteString("UPDATE ") diff --git a/qb/update_test.go b/qb/update_test.go index 81ae220..ba77154 100644 --- a/qb/update_test.go +++ b/qb/update_test.go @@ -66,10 +66,7 @@ func TestUpdateBuilder(t *testing.T) { } for _, test := range table { - stmt, names, err := test.B.ToCql() - if err != nil { - t.Error("unexpected error", err) - } + stmt, names := test.B.ToCql() if diff := cmp.Diff(test.S, stmt); diff != "" { t.Error(diff) }