From 1ae76d5efd10b1e137a1b2b434092119382895b5 Mon Sep 17 00:00:00 2001 From: go-jet Date: Sat, 20 Jul 2019 17:44:43 +0200 Subject: [PATCH] Function relocation. --- clause.go | 2 +- execution/execution.go | 3 ++- insert_statement.go | 3 ++- internal/utils/utils.go | 5 +++++ table.go | 5 +++-- update_statement.go | 3 ++- utils.go | 4 ---- 7 files changed, 15 insertions(+), 10 deletions(-) diff --git a/clause.go b/clause.go index 8714992..e007470 100644 --- a/clause.go +++ b/clause.go @@ -200,7 +200,7 @@ func (q *sqlBuilder) insertParametrizedArgument(arg interface{}) { } func argToString(value interface{}) string { - if isNil(value) { + if utils.IsNil(value) { return "NULL" } diff --git a/execution/execution.go b/execution/execution.go index 86075b2..b589d68 100644 --- a/execution/execution.go +++ b/execution/execution.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "github.com/go-jet/jet/execution/internal" + "github.com/go-jet/jet/internal/utils" "reflect" "strconv" "strings" @@ -17,7 +18,7 @@ import ( // Destination can be either pointer to struct or pointer to slice of structs. func Query(context context.Context, db DB, query string, args []interface{}, destinationPtr interface{}) error { - if destinationPtr == nil { + if utils.IsNil(destinationPtr) { return errors.New("jet: Destination is nil") } diff --git a/insert_statement.go b/insert_statement.go index 5d765ee..de48064 100644 --- a/insert_statement.go +++ b/insert_statement.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" "github.com/go-jet/jet/execution" + "github.com/go-jet/jet/internal/utils" ) // InsertStatement is interface for SQL INSERT statements @@ -82,7 +83,7 @@ func (i *insertStatementImpl) Sql() (sql string, args []interface{}, err error) queryData.newLine() queryData.writeString("INSERT INTO") - if isNil(i.table) { + if utils.IsNil(i.table) { return "", nil, errors.New("jet: table is nil") } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 85aeec2..fab5baa 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -6,6 +6,7 @@ import ( "go/format" "os" "path/filepath" + "reflect" "strconv" "strings" "text/template" @@ -154,3 +155,7 @@ func FormatTimestamp(t time.Time) []byte { } return b } + +func IsNil(v interface{}) bool { + return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil()) +} diff --git a/table.go b/table.go index e4f5657..1e12370 100644 --- a/table.go +++ b/table.go @@ -2,6 +2,7 @@ package jet import ( "errors" + "github.com/go-jet/jet/internal/utils" ) type table interface { @@ -239,7 +240,7 @@ func (t *joinTable) serialize(statement statementType, out *sqlBuilder, options return errors.New("jet: Join table is nil. ") } - if isNil(t.lhs) { + if utils.IsNil(t.lhs) { return errors.New("jet: left hand side of join operation is nil table") } @@ -262,7 +263,7 @@ func (t *joinTable) serialize(statement statementType, out *sqlBuilder, options out.writeString("CROSS JOIN") } - if isNil(t.rhs) { + if utils.IsNil(t.rhs) { return errors.New("jet: right hand side of join operation is nil table") } diff --git a/update_statement.go b/update_statement.go index d261410..b124a42 100644 --- a/update_statement.go +++ b/update_statement.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" "github.com/go-jet/jet/execution" + "github.com/go-jet/jet/internal/utils" ) // UpdateStatement is interface of SQL UPDATE statement @@ -62,7 +63,7 @@ func (u *updateStatementImpl) Sql() (sql string, args []interface{}, err error) out.newLine() out.writeString("UPDATE") - if isNil(u.table) { + if utils.IsNil(u.table) { return "", nil, errors.New("jet: table to update is nil") } diff --git a/utils.go b/utils.go index 3dbc467..1c26b6f 100644 --- a/utils.go +++ b/utils.go @@ -124,10 +124,6 @@ func columnListToProjectionList(columns []Column) []projection { return ret } -func isNil(v interface{}) bool { - return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil()) -} - func valueToClause(value interface{}) clause { if clause, ok := value.(clause); ok { return clause