Skip to content

Commit

Permalink
Merge pull request #121 from doug-martin/issue115-fix
Browse files Browse the repository at this point in the history
Fix for #115
  • Loading branch information
doug-martin authored Aug 2, 2019
2 parents 964b99c + de52f21 commit 0ba9f96
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v8.2.1

* [FIX] Return an error when an empty identifier is encountered [#115](https://github.com/doug-martin/goqu/issues/118)

## v8.2.0

* [FIX] Fix reflection errors related to nil pointers and unexported fields [#118](https://github.com/doug-martin/goqu/issues/118)
Expand Down
3 changes: 3 additions & 0 deletions exp/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ type (
// Returns a new IdentifierExpression with the column set to *
// I("my_table").All() //"my_table".*
All() IdentifierExpression

// Returns true if schema table and identifier are all zero values.
IsEmpty() bool
}
InsertExpression interface {
Expression
Expand Down
17 changes: 16 additions & 1 deletion exp/ident.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func ParseIdentifier(ident string) IdentifierExpression {
return NewIdentifierExpression("", "", ident)
}

func NewIdentifierExpression(schema, table, col string) IdentifierExpression {
func NewIdentifierExpression(schema, table string, col interface{}) IdentifierExpression {
return identifier{}.Schema(schema).Table(table).Col(col)
}

Expand Down Expand Up @@ -77,6 +77,21 @@ func (i identifier) Expression() Expression { return i }
// Qualifies the epression with a * literal (e.g. "table".*)
func (i identifier) All() IdentifierExpression { return i.Col("*") }

func (i identifier) IsEmpty() bool {
isEmpty := i.schema == "" && i.table == ""
if isEmpty {
switch t := i.col.(type) {
case nil:
return true
case string:
return t == ""
default:
return false
}
}
return isEmpty
}

// Gets the column identifier
func (i identifier) GetCol() interface{} { return i.col }

Expand Down
19 changes: 19 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goqu_test

import (
"strings"
"sync"
"testing"
"time"
Expand All @@ -14,6 +15,10 @@ type githubIssuesSuite struct {
suite.Suite
}

func (gis *githubIssuesSuite) AfterTest(suiteName, testName string) {
goqu.SetColumnRenameFunction(strings.ToLower)
}

// Test for https://github.com/doug-martin/goqu/issues/49
func (gis *githubIssuesSuite) TestIssue49() {
t := gis.T()
Expand All @@ -36,6 +41,20 @@ func (gis *githubIssuesSuite) TestIssue49() {
assert.Equal(t, `SELECT * FROM "table"`, sql)
}

// Test for https://github.com/doug-martin/goqu/issues/115
func (gis *githubIssuesSuite) TestIssue115() {

type TestStruct struct {
Field string
}
goqu.SetColumnRenameFunction(func(col string) string {
return ""
})

_, _, err := goqu.Insert("test").Rows(TestStruct{Field: "hello"}).ToSQL()
gis.EqualError(err, `goqu: a empty identifier was encountered, please specify a "schema", "table" or "column"`)
}

// Test for https://github.com/doug-martin/goqu/issues/118
func (gis *githubIssuesSuite) TestIssue118_withEmbeddedStructWithoutExportedFields() {
// struct is in a custom package
Expand Down
5 changes: 5 additions & 0 deletions sql_dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var (
errNoSourceForTruncate = errors.New("no source found when generating truncate sql")
errReturnNotSupported = errors.New("adapter does not support RETURNING clause")
errNoSetValuesForUpdate = errors.New("no set values found when generating UPDATE sql")
errEmptyIdentifier = errors.New(`a empty identifier was encountered, please specify a "schema", "table" or "column"`)
)

func notSupportedFragmentErr(sqlType string, f SQLFragmentType) error {
Expand Down Expand Up @@ -898,6 +899,10 @@ func (d *sqlDialect) appendableExpressionSQL(b sb.SQLBuilder, a exp.AppendableEx

// Quotes an identifier (e.g. "col", "table"."col"
func (d *sqlDialect) quoteIdentifier(b sb.SQLBuilder, ident exp.IdentifierExpression) {
if ident.IsEmpty() {
b.SetError(errEmptyIdentifier)
return
}
schema, table, col := ident.GetSchema(), ident.GetTable(), ident.GetCol()
if schema != d.dialectOptions.EmptyString {
b.WriteRunes(d.dialectOptions.QuoteRune).
Expand Down
6 changes: 6 additions & 0 deletions sql_dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,12 @@ func (dts *dialectTestSuite) TestLiteral_IdentifierExpression() {
d := sqlDialect{dialect: "test", dialectOptions: DefaultDialectOptions()}

b := sb.NewSQLBuilder(false)
d.Literal(b.Clear(), exp.NewIdentifierExpression("", "", ""))
dts.assertErrorSQL(b, `goqu: a empty identifier was encountered, please specify a "schema", "table" or "column"`)

d.Literal(b.Clear(), exp.NewIdentifierExpression("", "", nil))
dts.assertErrorSQL(b, `goqu: a empty identifier was encountered, please specify a "schema", "table" or "column"`)

d.Literal(b.Clear(), exp.NewIdentifierExpression("", "", "col"))
dts.assertNotPreparedSQL(b, `"col"`)

Expand Down

0 comments on commit 0ba9f96

Please sign in to comment.