Skip to content

Commit

Permalink
change db.InsertResult into an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
xiam committed Jan 9, 2021
1 parent dc09fbd commit 10f7089
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion adapter/mongo/collection.go
Expand Up @@ -267,7 +267,7 @@ func (col *Collection) UpdateReturning(item interface{}) error {
}

// Insert inserts a record (map or struct) into the collection.
func (col *Collection) Insert(item interface{}) (*db.InsertResult, error) {
func (col *Collection) Insert(item interface{}) (db.InsertResult, error) {
var err error

id := getID(item)
Expand Down
2 changes: 2 additions & 0 deletions adapter/postgresql/helper_test.go
Expand Up @@ -236,6 +236,8 @@ func (h *Helper) TearUp() error {
, varchar_value_ptr varchar(64)
, decimal_value_ptr decimal
, uuid_value_string UUID
)`,

`DROP TABLE IF EXISTS issue_370`,
Expand Down
9 changes: 8 additions & 1 deletion adapter/postgresql/postgresql_test.go
Expand Up @@ -258,19 +258,26 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) {
IntegerValuePtr *int64 `db:"integer_value_ptr,omitempty"`
VarcharValuePtr *string `db:"varchar_value_ptr,omitempty"`
DecimalValuePtr *float64 `db:"decimal_value_ptr,omitempty"`

UUIDValueString *string `db:"uuid_value_string,omitempty"`
}

integerValue := int64(10)
stringValue := string("ten")
decimalValue := float64(10.0)

uuidStringValue := "52356d08-6a16-4839-9224-75f0a547e13c"

integerArrayValue := Int64Array{1, 2, 3, 4}
stringArrayValue := StringArray{"a", "b", "c"}
jsonbMapValue := JSONBMap{"Hello": "World"}

testValue := "Hello world!"

origPgTypeTests := []PGType{
PGType{
UUIDValueString: &uuidStringValue,
},
PGType{
UInt8Value: 7,
UInt8ValueArray: uint8CompatArray{1, 2, 3, 4, 5, 6},
Expand Down Expand Up @@ -922,7 +929,7 @@ var _ interface {
db.BeforeUpdateHook
} = &issue602Organization{}

func (s *AdapterTests) TestIncorrectBinaryFormat() {
func (s *AdapterTests) Test_Issue602_IncorrectBinaryFormat() {
settingsWithBinaryMode := ConnectionURL{
Database: settings.Database,
User: settings.User,
Expand Down
2 changes: 1 addition & 1 deletion collection.go
Expand Up @@ -43,7 +43,7 @@ type Collection interface {
// on both the database adapter and the column storing the ID. The ID
// returned by Insert() could be passed directly to Find() to retrieve the
// newly added element.
Insert(interface{}) (*InsertResult, error)
Insert(interface{}) (InsertResult, error)

// InsertReturning is like Insert() but it takes a pointer to map or struct
// and, if the operation succeeds, updates it with data from the newly
Expand Down
8 changes: 8 additions & 0 deletions internal/adapter/constraint.go
Expand Up @@ -21,6 +21,11 @@

package adapter

// ConstraintValuer allows constraints to use specific values of their own.
type ConstraintValuer interface {
ConstraintValue() interface{}
}

// Constraint interface represents a single condition, like "a = 1". where `a`
// is the key and `1` is the value. This is an exported interface but it's
// rarely used directly, you may want to use the `db.Cond{}` map instead.
Expand Down Expand Up @@ -51,6 +56,9 @@ func (c constraint) Key() interface{} {
}

func (c constraint) Value() interface{} {
if constraintValuer, ok := c.v.(ConstraintValuer); ok {
return constraintValuer.ConstraintValue()
}
return c.v
}

Expand Down
6 changes: 3 additions & 3 deletions internal/sqladapter/collection.go
Expand Up @@ -20,7 +20,7 @@ type CollectionAdapter interface {
// Collection satisfies db.Collection.
type Collection interface {
// Insert inserts a new item into the collection.
Insert(interface{}) (*db.InsertResult, error)
Insert(interface{}) (db.InsertResult, error)

// Name returns the name of the collection.
Name() string
Expand Down Expand Up @@ -102,7 +102,7 @@ func (c *collection) Count() (uint64, error) {
return c.Find().Count()
}

func (c *collection) Insert(item interface{}) (*db.InsertResult, error) {
func (c *collection) Insert(item interface{}) (db.InsertResult, error) {
id, err := c.adapter.Insert(c, item)
if err != nil {
return nil, err
Expand Down Expand Up @@ -211,7 +211,7 @@ func (c *collection) InsertReturning(item interface{}) error {
} else {
// We have one primary key, build a explicit db.Cond with it to prevent
// string keys to be considered as raw conditions.
newItemRes = col.Find(db.Cond{pks[0]: id.ID()}) // We already checked that pks is not empty, so pks[0] is defined.
newItemRes = col.Find(db.Cond{pks[0]: id}) // We already checked that pks is not empty, so pks[0] is defined.
}

// Fetch the row that was just interted into newItem
Expand Down
2 changes: 1 addition & 1 deletion internal/sqlbuilder/template.go
Expand Up @@ -128,7 +128,7 @@ func (tu *templateWithUtils) toWhereWithArguments(term interface{}) (where exql.
where.Conditions = append(where.Conditions, frag)
return

case *db.InsertResult:
case db.InsertResult:
return tu.toWhereWithArguments(t.ID())

case adapter.Constraint:
Expand Down
23 changes: 17 additions & 6 deletions result.go
Expand Up @@ -180,24 +180,35 @@ type Result interface {
}

// InsertResult provides infomation about an insert operation.
type InsertResult struct {
type InsertResult interface {
// ID returns the ID of the newly inserted record.
ID() ID
}

type insertResult struct {
id interface{}
}

// ID returns the ID of the newly inserted record.
func (r *InsertResult) ID() ID {
func (r *insertResult) ID() ID {
return r.id
}

// ConstraintValue satisfies adapter.ConstraintValuer
func (r *insertResult) ConstraintValue() interface{} {
return r.id
}

// Value satisfies driver.Valuer
func (r InsertResult) Value() (driver.Value, error) {
func (r *insertResult) Value() (driver.Value, error) {
return r.id, nil
}

// NewInsertResult creates an InsertResult
func NewInsertResult(id interface{}) *InsertResult {
return &InsertResult{id: id}
func NewInsertResult(id interface{}) InsertResult {
return &insertResult{id: id}
}

// ID represents a record ID
type ID interface{}

var _ = driver.Valuer(&insertResult{})

0 comments on commit 10f7089

Please sign in to comment.