Skip to content

Commit

Permalink
fix: UPSERT function in db template
Browse files Browse the repository at this point in the history
  • Loading branch information
si3nloong committed Jul 5, 2024
1 parent 619c2f5 commit 7eb9315
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 63 deletions.
46 changes: 18 additions & 28 deletions codegen/templates/db.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,10 @@ func UpsertOne[T sequel.KeyValuer, Ptr sequel.KeyValueScanner[T]](ctx context.Co
}
// Upsert is a helper function to upsert multiple records.
func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, sqlConn sequel.DB, data []T, opts ...UpsertOption) (sql.Result, error) {
func Upsert[T interface {
sequel.Keyer
sequel.Inserter
}, Ptr sequel.PtrScanner[T]](ctx context.Context, sqlConn sequel.DB, data []T, opts ...UpsertOption) (sql.Result, error) {
noOfData := len(data)
if noOfData == 0 {
return new(sequel.EmptyResult), nil
Expand Down Expand Up @@ -398,17 +401,10 @@ func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, s
stmt.WriteString("INSERT INTO " + DbTable(model) + " (" + strings.Join(columns, ",") + ") VALUES ")
for i := 0; i < noOfData; i++ {
if i > 0 {
stmt.WriteByte(',')
}
stmt.WriteByte('(')
for j := 1; j <= noOfCols; j++ {
if j > 1 {
stmt.WriteString("," + wrapVar((i*noOfCols)+j))
} else {
stmt.WriteString(wrapVar((i * noOfCols) + j))
}
stmt.WriteString("," + model.InsertPlaceholders(i))
} else {
stmt.WriteString(model.InsertPlaceholders(i))
}
stmt.WriteByte(')')
args = append(args, data[i].Values()...)
}
case sequel.CompositeKeyer:
Expand All @@ -417,17 +413,10 @@ func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, s
stmt.WriteString("INSERT INTO " + DbTable(model) + " (" + strings.Join(columns, ",") + ") VALUES ")
for i := 0; i < noOfData; i++ {
if i > 0 {
stmt.WriteByte(',')
}
stmt.WriteByte('(')
for j := 1; j <= noOfCols; j++ {
if j > 1 {
stmt.WriteString("," + wrapVar((i*noOfCols)+j))
} else {
stmt.WriteString(wrapVar((i * noOfCols) + j))
}
stmt.WriteString("," + model.InsertPlaceholders(i))
} else {
stmt.WriteString(model.InsertPlaceholders(i))
}
stmt.WriteByte(')')
args = append(args, data[i].Values()...)
}
default:
Expand Down Expand Up @@ -578,7 +567,10 @@ func UpsertOne[T sequel.KeyValuer, Ptr sequel.KeyValueScanner[T]](ctx context.Co
}

// Upsert is a helper function to upsert multiple records.
func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, sqlConn sequel.DB, data []T, override bool, omittedFields ...string) (sql.Result, error) {
func Upsert[T interface {
sequel.Keyer
sequel.Inserter
}, Ptr sequel.PtrScanner[T]](ctx context.Context, sqlConn sequel.DB, data []T, override bool, omittedFields ...string) (sql.Result, error) {
noOfData := len(data)
if noOfData == 0 {
return new(sequel.EmptyResult), nil
Expand Down Expand Up @@ -623,12 +615,11 @@ func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, s
} else {
stmt.WriteString("INSERT IGNORE INTO " + DbTable(model) + " (" + strings.Join(columns, ",") + ") VALUES ")
}
placeholder := ",(" + strings.Repeat(",?", noOfCols)[1:] + ")"
for i := 0; i < noOfData; i++ {
if i > 0 {
stmt.WriteString(placeholder)
stmt.WriteString("," + model.InsertPlaceholders(i))
} else {
stmt.WriteString(placeholder[1:])
stmt.WriteString(model.InsertPlaceholders(i))
}
args = append(args, data[i].Values()...)
}
Expand All @@ -638,12 +629,11 @@ func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, s
} else {
stmt.WriteString("INSERT IGNORE INTO " + DbTable(model) + " (" + strings.Join(columns, ",") + ") VALUES ")
}
placeholder := ",(" + strings.Repeat(",?", noOfCols)[1:] + ")"
for i := 0; i < noOfData; i++ {
if i > 0 {
stmt.WriteString(placeholder)
stmt.WriteString("," + model.InsertPlaceholders(i))
} else {
stmt.WriteString(placeholder[1:])
stmt.WriteString(model.InsertPlaceholders(i))
}
args = append(args, data[i].Values()...)
}
Expand Down
15 changes: 8 additions & 7 deletions examples/db/mysql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ func UpsertOne[T sequel.KeyValuer, Ptr sequel.KeyValueScanner[T]](ctx context.Co
}

// Upsert is a helper function to upsert multiple records.
func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, sqlConn sequel.DB, data []T, override bool, omittedFields ...string) (sql.Result, error) {
func Upsert[T interface {
sequel.Keyer
sequel.Inserter
}, Ptr sequel.PtrScanner[T]](ctx context.Context, sqlConn sequel.DB, data []T, override bool, omittedFields ...string) (sql.Result, error) {
noOfData := len(data)
if noOfData == 0 {
return new(sequel.EmptyResult), nil
Expand Down Expand Up @@ -240,12 +243,11 @@ func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, s
} else {
stmt.WriteString("INSERT IGNORE INTO " + DbTable(model) + " (" + strings.Join(columns, ",") + ") VALUES ")
}
placeholder := ",(" + strings.Repeat(",?", noOfCols)[1:] + ")"
for i := 0; i < noOfData; i++ {
if i > 0 {
stmt.WriteString(placeholder)
stmt.WriteString("," + model.InsertPlaceholders(i))
} else {
stmt.WriteString(placeholder[1:])
stmt.WriteString(model.InsertPlaceholders(i))
}
args = append(args, data[i].Values()...)
}
Expand All @@ -255,12 +257,11 @@ func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, s
} else {
stmt.WriteString("INSERT IGNORE INTO " + DbTable(model) + " (" + strings.Join(columns, ",") + ") VALUES ")
}
placeholder := ",(" + strings.Repeat(",?", noOfCols)[1:] + ")"
for i := 0; i < noOfData; i++ {
if i > 0 {
stmt.WriteString(placeholder)
stmt.WriteString("," + model.InsertPlaceholders(i))
} else {
stmt.WriteString(placeholder[1:])
stmt.WriteString(model.InsertPlaceholders(i))
}
args = append(args, data[i].Values()...)
}
Expand Down
31 changes: 10 additions & 21 deletions examples/db/postgres/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ func UpsertOne[T sequel.KeyValuer, Ptr sequel.KeyValueScanner[T]](ctx context.Co
}

// Upsert is a helper function to upsert multiple records.
func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, sqlConn sequel.DB, data []T, opts ...UpsertOption) (sql.Result, error) {
func Upsert[T interface {
sequel.Keyer
sequel.Inserter
}, Ptr sequel.PtrScanner[T]](ctx context.Context, sqlConn sequel.DB, data []T, opts ...UpsertOption) (sql.Result, error) {
noOfData := len(data)
if noOfData == 0 {
return new(sequel.EmptyResult), nil
Expand Down Expand Up @@ -306,17 +309,10 @@ func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, s
stmt.WriteString("INSERT INTO " + DbTable(model) + " (" + strings.Join(columns, ",") + ") VALUES ")
for i := 0; i < noOfData; i++ {
if i > 0 {
stmt.WriteByte(',')
}
stmt.WriteByte('(')
for j := 1; j <= noOfCols; j++ {
if j > 1 {
stmt.WriteString("," + wrapVar((i*noOfCols)+j))
} else {
stmt.WriteString(wrapVar((i * noOfCols) + j))
}
stmt.WriteString("," + model.InsertPlaceholders(i))
} else {
stmt.WriteString(model.InsertPlaceholders(i))
}
stmt.WriteByte(')')
args = append(args, data[i].Values()...)
}
case sequel.CompositeKeyer:
Expand All @@ -325,17 +321,10 @@ func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, s
stmt.WriteString("INSERT INTO " + DbTable(model) + " (" + strings.Join(columns, ",") + ") VALUES ")
for i := 0; i < noOfData; i++ {
if i > 0 {
stmt.WriteByte(',')
}
stmt.WriteByte('(')
for j := 1; j <= noOfCols; j++ {
if j > 1 {
stmt.WriteString("," + wrapVar((i*noOfCols)+j))
} else {
stmt.WriteString(wrapVar((i * noOfCols) + j))
}
stmt.WriteString("," + model.InsertPlaceholders(i))
} else {
stmt.WriteString(model.InsertPlaceholders(i))
}
stmt.WriteByte(')')
args = append(args, data[i].Values()...)
}
default:
Expand Down
15 changes: 8 additions & 7 deletions examples/db/sqlite/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ func UpsertOne[T sequel.KeyValuer, Ptr sequel.KeyValueScanner[T]](ctx context.Co
}

// Upsert is a helper function to upsert multiple records.
func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, sqlConn sequel.DB, data []T, override bool, omittedFields ...string) (sql.Result, error) {
func Upsert[T interface {
sequel.Keyer
sequel.Inserter
}, Ptr sequel.PtrScanner[T]](ctx context.Context, sqlConn sequel.DB, data []T, override bool, omittedFields ...string) (sql.Result, error) {
noOfData := len(data)
if noOfData == 0 {
return new(sequel.EmptyResult), nil
Expand Down Expand Up @@ -240,12 +243,11 @@ func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, s
} else {
stmt.WriteString("INSERT IGNORE INTO " + DbTable(model) + " (" + strings.Join(columns, ",") + ") VALUES ")
}
placeholder := ",(" + strings.Repeat(",?", noOfCols)[1:] + ")"
for i := 0; i < noOfData; i++ {
if i > 0 {
stmt.WriteString(placeholder)
stmt.WriteString("," + model.InsertPlaceholders(i))
} else {
stmt.WriteString(placeholder[1:])
stmt.WriteString(model.InsertPlaceholders(i))
}
args = append(args, data[i].Values()...)
}
Expand All @@ -255,12 +257,11 @@ func Upsert[T sequel.KeyValuer, Ptr sequel.PtrScanner[T]](ctx context.Context, s
} else {
stmt.WriteString("INSERT IGNORE INTO " + DbTable(model) + " (" + strings.Join(columns, ",") + ") VALUES ")
}
placeholder := ",(" + strings.Repeat(",?", noOfCols)[1:] + ")"
for i := 0; i < noOfData; i++ {
if i > 0 {
stmt.WriteString(placeholder)
stmt.WriteString("," + model.InsertPlaceholders(i))
} else {
stmt.WriteString(placeholder[1:])
stmt.WriteString(model.InsertPlaceholders(i))
}
args = append(args, data[i].Values()...)
}
Expand Down

0 comments on commit 7eb9315

Please sign in to comment.