Skip to content

Commit

Permalink
Fixed a bug that was causing slices of bytes to get returned that hav…
Browse files Browse the repository at this point in the history
…e underlying arrays in memory owned by the database driver. Also fixed the SQL tests that were failing. (#99)
  • Loading branch information
spencerlbeard authored and namtzigla committed May 30, 2019
1 parent 495afb9 commit 9db8ac3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 70 deletions.
124 changes: 62 additions & 62 deletions examples/user_sql/pb/user.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions examples/user_sql/pb/user.persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ func (this *Query_UServ_CreateUsersTable) Execute(x In_UServ_CreateUsersTable) *
result.err = setupErr
return result
}
result.result, result.err = this.db.ExecContext(this.ctx, "CREATE TABLE users(id integer PRIMARY KEY, name VARCHAR(50), friends BYTEA, created_on VARCHAR(50), id2 SMALLINT, counts []BIGINT)", params...)
result.result, result.err = this.db.ExecContext(this.ctx, "CREATE TABLE users(id integer PRIMARY KEY, name VARCHAR(50), friends BYTEA, created_on VARCHAR(50), id2 SMALLINT, counts BIGINT[])", params...)
return result
}

// InsertUsers returns a struct that will perform the 'insert_users' query.
// When Execute is called, it will use the following fields:
// [id name friends created_on counts]
// [id name friends created_on id2 counts]
func (this *Queries_UServ) InsertUsers(ctx context.Context, db persist.Runnable) *Query_UServ_InsertUsers {
return &Query_UServ_InsertUsers{
opts: this.opts,
Expand All @@ -97,7 +97,7 @@ func (this *Query_UServ_InsertUsers) QueryInType_User() {}
func (this *Query_UServ_InsertUsers) QueryOutType_Empty() {}

// Executes the query 'insert_users' with parameters retrieved from x.
// Fields used: [id name friends created_on counts]
// Fields used: [id name friends created_on id2 counts]
func (this *Query_UServ_InsertUsers) Execute(x In_UServ_InsertUsers) *Iter_UServ_InsertUsers {
var setupErr error
params := []interface{}{
Expand All @@ -122,6 +122,10 @@ func (this *Query_UServ_InsertUsers) Execute(x In_UServ_InsertUsers) *Iter_UServ
out = mapper.ToSql(x.GetCreatedOn())
return
}(),
func() (out interface{}) {
out = x.GetId2()
return
}(),
func() (out interface{}) {
mapper := this.opts.MAPPINGS.Int64Slice()
out = mapper.ToSql(x.GetCounts())
Expand All @@ -136,7 +140,7 @@ func (this *Query_UServ_InsertUsers) Execute(x In_UServ_InsertUsers) *Iter_UServ
result.err = setupErr
return result
}
result.result, result.err = this.db.ExecContext(this.ctx, "INSERT INTO users (id, name, friends, created_on, id2, counts) VALUES ($1, $2, $3, $4, $5)", params...)
result.result, result.err = this.db.ExecContext(this.ctx, "INSERT INTO users (id, name, friends, created_on, id2, counts) VALUES ($1, $2, $3, $4, $5, $6)", params...)
return result
}

Expand Down Expand Up @@ -2694,7 +2698,18 @@ type alwaysScanner struct {
}

func (s *alwaysScanner) Scan(src interface{}) error {
s.i = &src
// From database.sql.Scanner:
// Reference types such as []byte are only valid until the next call to Scan
// and should not be retained. Their underlying memory is owned by the driver.
// If retention is necessary, copy their values before the next call to Scan.
switch t := src.(type) {
case []byte:
var tmp interface{} = make([]byte, len(t))
copy(tmp.([]byte), t)
s.i = &tmp
default:
s.i = &t
}
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions examples/user_sql/pb/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ service UServ {
queries: [{
name: "create_users_table",
query: ["CREATE TABLE users(id integer PRIMARY KEY, name VARCHAR(50), friends BYTEA,",
"created_on VARCHAR(50), id2 SMALLINT, counts []BIGINT)"],
"created_on VARCHAR(50), id2 SMALLINT, counts BIGINT[])"],
pm_strategy: "$",
in: ".pb.Empty",
out: ".pb.Empty",
}, {
name: "insert_users",
query: ["INSERT INTO users (id, name, friends, created_on, id2, counts) VALUES (@id, @name, @friends, @created_on, @counts)"],
query: ["INSERT INTO users (id, name, friends, created_on, id2, counts) VALUES (@id, @name, @friends, @created_on, @id2, @counts)"],
pm_strategy: "$",
in: ".pb.User",
out: ".pb.Empty",
Expand Down
13 changes: 12 additions & 1 deletion generator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,18 @@ func WritePackageLevelDeclarations(p *Printer, files *FileList) error {
}
func (s *alwaysScanner) Scan(src interface{}) error {
s.i = &src
// From database.sql.Scanner:
// Reference types such as []byte are only valid until the next call to Scan
// and should not be retained. Their underlying memory is owned by the driver.
// If retention is necessary, copy their values before the next call to Scan.
switch t := src.(type) {
case []byte:
var tmp interface{} = make([]byte, len(t))
copy(tmp.([]byte), t)
s.i = &tmp
default:
s.i = &t
}
return nil
}
Expand Down

0 comments on commit 9db8ac3

Please sign in to comment.