Skip to content

Commit

Permalink
fix: correct binary serialization for mysql (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
maebeam authored and vmihailenco committed Oct 22, 2021
1 parent 58f9cbb commit e899f50
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dialect/append.go
Expand Up @@ -82,7 +82,7 @@ func AppendBytes(b []byte, bs []byte) []byte {
return AppendNull(b)
}

b = append(b, `'\x`...)
b = append(b, `X'`...)

s := len(b)
b = append(b, make([]byte, hex.EncodedLen(len(bs)))...)
Expand Down
29 changes: 29 additions & 0 deletions query_insert.go
Expand Up @@ -442,6 +442,17 @@ func (q *InsertQuery) appendOn(fmter schema.Formatter, b []byte) (_ []byte, err
}

b = q.appendSetExcluded(b, fields)
} else if q.onDuplicateKeyUpdate() {
fields, err := q.getDataFields()
if err != nil {
return nil, err
}

if len(fields) == 0 {
fields = q.tableModel.Table().DataFields
}

b = q.appendSetValues(b, fields)
}

if len(q.where) > 0 {
Expand All @@ -460,6 +471,10 @@ func (q *InsertQuery) onConflictDoUpdate() bool {
return strings.HasSuffix(strings.ToUpper(q.on.Query), " DO UPDATE")
}

func (q *InsertQuery) onDuplicateKeyUpdate() bool {
return strings.ToUpper(q.on.Query) == "DUPLICATE KEY UPDATE"
}

func (q *InsertQuery) appendSetExcluded(b []byte, fields []*schema.Field) []byte {
b = append(b, " SET "...)
for i, f := range fields {
Expand All @@ -473,6 +488,20 @@ func (q *InsertQuery) appendSetExcluded(b []byte, fields []*schema.Field) []byte
return b
}

func (q *InsertQuery) appendSetValues(b []byte, fields []*schema.Field) []byte {
b = append(b, " "...)
for i, f := range fields {
if i > 0 {
b = append(b, ", "...)
}
b = append(b, f.SQLName...)
b = append(b, " = VALUES("...)
b = append(b, f.SQLName...)
b = append(b, ")"...)
}
return b
}

//------------------------------------------------------------------------------

func (q *InsertQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) {
Expand Down

0 comments on commit e899f50

Please sign in to comment.