it's easy to insert one record and get result like this:
s := "INSERT INTO quiz_answer_details (quiz_answer_id, question_id, type, choices, content) VALUES ($1, $2, $3, $4, $5) RETURNING *"
d, err := fromQuizAnswerDetail(in)
if err != nil {
return nil, err
}
var out quizAnswerDetail
if err := m.core.GetContextOnMaster(ctx, &out, s, d.QuizAnswerID, d.QuestionID, d.Type, d.Choices, d.Content); err != nil {
return nil, err
}
but how to do batch insert and get all results? i tried several methods but got nothing.
this is one i think should work but it doesn't
s := "INSERT INTO quiz_answer_details (quiz_answer_id, question_id, type, choices, content) VALUES ($1, $2, $3, $4, $5) RETURNING *"
data, err := fromQuizAnswerDetails(ins)
if err != nil {
return nil, err
}
dbs, _ := m.core.GetAllMasters()
stmt, err := dbs[0].PreparexContext(ctx, s)
if err != nil {
return nil, err
}
var out quizAnswerDetails
for _, d := range data {
var detail quizAnswerDetail
if err := stmt.GetContext(ctx, &detail, d.QuizAnswerID, d.QuestionID, d.Type, pq.Array(d.Choices), d.Content); err != nil {
return nil, err
}
out = append(out, detail)
}
return out.to()
The error message is something like this:
quiz-answer_test.go:35: driver: skip fast-path; continue as if unimplemented
thanks in advance
I can't help you specifically with
sqlx
as I am not familiar with that package, but when using the standard library'sdatabase/sql
package one can do a batch insert as demonstrated below.It's quite verbose and can become hard to maintain if the number of different batch queries that need to be written is high, or if a project is in the design stage and the db schema is being changed more frequently than one would like. In those cases the example below could be generalized using reflection, or, as I like to do, a generator could be written to write the code for you.