added version to sqlc queryes

This commit is contained in:
Pavel 2024-09-18 19:22:34 +03:00
parent f1687f2e61
commit cd18781525
7 changed files with 31 additions and 17 deletions

@ -286,7 +286,7 @@ WHERE privilege_name = $2
-- name: GetAllAnswersByQuizID :many -- name: GetAllAnswersByQuizID :many
SELECT DISTINCT ON (a.question_id) SELECT DISTINCT ON (a.question_id)
a.content, a.created_at, a.question_id, a.id, q.questiontype::Text as questiontype, quiz.qid a.content, a.created_at, a.question_id, a.id,a.version, q.questiontype::Text as questiontype, quiz.qid
FROM FROM
answer a answer a
JOIN JOIN
@ -312,12 +312,13 @@ INSERT INTO answer(
browser, browser,
ip, ip,
start, start,
utm utm,
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) version
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15)
RETURNING *; RETURNING *;
-- name: GetResultAnswers :many -- name: GetResultAnswers :many
SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted, device_type,device,os,browser,ip FROM answer WHERE session = ( SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted, device_type,device,os,browser,ip,version FROM answer WHERE session = (
SELECT session FROM answer WHERE answer.id = $1) ORDER BY question_id, created_at DESC; SELECT session FROM answer WHERE answer.id = $1) ORDER BY question_id, created_at DESC;
-- name: GetQuestions :many -- name: GetQuestions :many

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT. // Code generated by sqlc. DO NOT EDIT.
// versions: // versions:
// sqlc v1.26.0 // sqlc v1.27.0
package sqlcgen package sqlcgen

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT. // Code generated by sqlc. DO NOT EDIT.
// versions: // versions:
// sqlc v1.26.0 // sqlc v1.27.0
package sqlcgen package sqlcgen
@ -66,6 +66,7 @@ type Answer struct {
Ip string `db:"ip" json:"ip"` Ip string `db:"ip" json:"ip"`
Start bool `db:"start" json:"start"` Start bool `db:"start" json:"start"`
Utm json.RawMessage `db:"utm" json:"utm"` Utm json.RawMessage `db:"utm" json:"utm"`
Version int32 `db:"version" json:"version"`
} }
type Field struct { type Field struct {

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT. // Code generated by sqlc. DO NOT EDIT.
// versions: // versions:
// sqlc v1.26.0 // sqlc v1.27.0
// source: queries.sql // source: queries.sql
package sqlcgen package sqlcgen
@ -1361,7 +1361,7 @@ func (q *Queries) GetAccountWithPrivileges(ctx context.Context, userID string) (
const getAllAnswersByQuizID = `-- name: GetAllAnswersByQuizID :many const getAllAnswersByQuizID = `-- name: GetAllAnswersByQuizID :many
SELECT DISTINCT ON (a.question_id) SELECT DISTINCT ON (a.question_id)
a.content, a.created_at, a.question_id, a.id, q.questiontype::Text as questiontype, quiz.qid a.content, a.created_at, a.question_id, a.id,a.version, q.questiontype::Text as questiontype, quiz.qid
FROM FROM
answer a answer a
JOIN JOIN
@ -1379,6 +1379,7 @@ type GetAllAnswersByQuizIDRow struct {
CreatedAt sql.NullTime `db:"created_at" json:"created_at"` CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
QuestionID int64 `db:"question_id" json:"question_id"` QuestionID int64 `db:"question_id" json:"question_id"`
ID int64 `db:"id" json:"id"` ID int64 `db:"id" json:"id"`
Version int32 `db:"version" json:"version"`
Questiontype string `db:"questiontype" json:"questiontype"` Questiontype string `db:"questiontype" json:"questiontype"`
Qid uuid.NullUUID `db:"qid" json:"qid"` Qid uuid.NullUUID `db:"qid" json:"qid"`
} }
@ -1397,6 +1398,7 @@ func (q *Queries) GetAllAnswersByQuizID(ctx context.Context, session sql.NullStr
&i.CreatedAt, &i.CreatedAt,
&i.QuestionID, &i.QuestionID,
&i.ID, &i.ID,
&i.Version,
&i.Questiontype, &i.Questiontype,
&i.Qid, &i.Qid,
); err != nil { ); err != nil {
@ -2413,7 +2415,7 @@ func (q *Queries) GetQuizRule(ctx context.Context, quizid int32) (Rule, error) {
} }
const getResultAnswers = `-- name: GetResultAnswers :many const getResultAnswers = `-- name: GetResultAnswers :many
SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted, device_type,device,os,browser,ip FROM answer WHERE session = ( SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted, device_type,device,os,browser,ip,version FROM answer WHERE session = (
SELECT session FROM answer WHERE answer.id = $1) ORDER BY question_id, created_at DESC SELECT session FROM answer WHERE answer.id = $1) ORDER BY question_id, created_at DESC
` `
@ -2433,6 +2435,7 @@ type GetResultAnswersRow struct {
Os string `db:"os" json:"os"` Os string `db:"os" json:"os"`
Browser string `db:"browser" json:"browser"` Browser string `db:"browser" json:"browser"`
Ip string `db:"ip" json:"ip"` Ip string `db:"ip" json:"ip"`
Version int32 `db:"version" json:"version"`
} }
func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAnswersRow, error) { func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAnswersRow, error) {
@ -2460,6 +2463,7 @@ func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAn
&i.Os, &i.Os,
&i.Browser, &i.Browser,
&i.Ip, &i.Ip,
&i.Version,
); err != nil { ); err != nil {
return nil, err return nil, err
} }
@ -3000,9 +3004,10 @@ INSERT INTO answer(
browser, browser,
ip, ip,
start, start,
utm utm,
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) version
RETURNING id, content, quiz_id, question_id, fingerprint, session, created_at, result, new, deleted, email, device_type, device, os, browser, ip, start, utm ) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15)
RETURNING id, content, quiz_id, question_id, fingerprint, session, created_at, result, new, deleted, email, device_type, device, os, browser, ip, start, utm, version
` `
type InsertAnswersParams struct { type InsertAnswersParams struct {
@ -3020,6 +3025,7 @@ type InsertAnswersParams struct {
Ip string `db:"ip" json:"ip"` Ip string `db:"ip" json:"ip"`
Start bool `db:"start" json:"start"` Start bool `db:"start" json:"start"`
Utm json.RawMessage `db:"utm" json:"utm"` Utm json.RawMessage `db:"utm" json:"utm"`
Version int32 `db:"version" json:"version"`
} }
func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) (Answer, error) { func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) (Answer, error) {
@ -3038,6 +3044,7 @@ func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) (A
arg.Ip, arg.Ip,
arg.Start, arg.Start,
arg.Utm, arg.Utm,
arg.Version,
) )
var i Answer var i Answer
err := row.Scan( err := row.Scan(
@ -3059,6 +3066,7 @@ func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) (A
&i.Ip, &i.Ip,
&i.Start, &i.Start,
&i.Utm, &i.Utm,
&i.Version,
) )
return i, err return i, err
} }

@ -131,7 +131,7 @@ type Answer struct {
OS string OS string
Start bool Start bool
Utm UTMSavingMap Utm UTMSavingMap
Version int Version int32
} }
type ResultContent struct { type ResultContent struct {
@ -162,6 +162,7 @@ type ResultAnswer struct {
CreatedAt time.Time CreatedAt time.Time
QuestionID uint64 QuestionID uint64
AnswerID uint64 AnswerID uint64
Version int32
} }
const skey = "squiz" const skey = "squiz"
@ -305,6 +306,7 @@ type AnswerExport struct {
Id uint64 `json:"id"` Id uint64 `json:"id"`
New bool `json:"new"` New bool `json:"new"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
Version int32
} }
type UTMSavingMap map[string]string type UTMSavingMap map[string]string

@ -114,6 +114,7 @@ func (r *AnswerRepository) GetAllAnswersByQuizID(ctx context.Context, session st
CreatedAt: row.CreatedAt.Time, CreatedAt: row.CreatedAt.Time,
QuestionID: uint64(row.QuestionID), QuestionID: uint64(row.QuestionID),
AnswerID: uint64(row.ID), AnswerID: uint64(row.ID),
Version: row.Version,
} }
results = append(results, resultAnswer) results = append(results, resultAnswer)

@ -57,7 +57,7 @@ func (r *ResultRepository) GetQuizResults(ctx context.Context, quizID uint64, re
} }
offset := reqExport.Page * reqExport.Limit offset := reqExport.Page * reqExport.Limit
mainQuery := "SELECT content, id, new, created_at " + queryBase + " ORDER BY created_at DESC LIMIT $" + strconv.Itoa(len(queryParams)+1) + " OFFSET $" + strconv.Itoa(len(queryParams)+2) mainQuery := "SELECT content, id, new, created_at,version " + queryBase + " ORDER BY created_at DESC LIMIT $" + strconv.Itoa(len(queryParams)+1) + " OFFSET $" + strconv.Itoa(len(queryParams)+2)
queryParams = append(queryParams, reqExport.Limit, offset) queryParams = append(queryParams, reqExport.Limit, offset)
rows, err := r.pool.QueryContext(ctx, mainQuery, queryParams...) rows, err := r.pool.QueryContext(ctx, mainQuery, queryParams...)
@ -68,7 +68,7 @@ func (r *ResultRepository) GetQuizResults(ctx context.Context, quizID uint64, re
for rows.Next() { for rows.Next() {
var answer model.AnswerExport var answer model.AnswerExport
if err := rows.Scan(&answer.Content, &answer.Id, &answer.New, &answer.CreatedAt); err != nil { if err := rows.Scan(&answer.Content, &answer.Id, &answer.New, &answer.CreatedAt, &answer.Version); err != nil {
return nil, 0, err return nil, 0, err
} }
@ -118,7 +118,7 @@ func (r *ResultRepository) GetQuizResultsCSV(ctx context.Context, quizID uint64,
var results []model.Answer var results []model.Answer
mainQuery := `SELECT DISTINCT ON (a.question_id, a.session) mainQuery := `SELECT DISTINCT ON (a.question_id, a.session)
a.id, a.content, a.question_id, a.quiz_id, a.fingerprint, a.session, a.result, a.created_at, a.new, a.deleted a.id, a.content, a.question_id, a.quiz_id, a.fingerprint, a.session, a.result, a.created_at, a.new, a.deleted,a.version
FROM FROM
answer a answer a
WHERE WHERE
@ -151,7 +151,7 @@ func (r *ResultRepository) GetQuizResultsCSV(ctx context.Context, quizID uint64,
for rows.Next() { for rows.Next() {
var answer model.Answer var answer model.Answer
if err := rows.Scan(&answer.Id, &answer.Content, &answer.QuestionId, &answer.QuizId, &answer.Fingerprint, &answer.Session, &answer.Result, &answer.CreatedAt, &answer.New, &answer.Deleted); err != nil { if err := rows.Scan(&answer.Id, &answer.Content, &answer.QuestionId, &answer.QuizId, &answer.Fingerprint, &answer.Session, &answer.Result, &answer.CreatedAt, &answer.New, &answer.Deleted, &answer.Version); err != nil {
return nil, err return nil, err
} }
results = append(results, answer) results = append(results, answer)
@ -245,6 +245,7 @@ func (r *ResultRepository) GetResultAnswers(ctx context.Context, answerID uint64
OS: row.Os, OS: row.Os,
IP: row.Ip, IP: row.Ip,
Browser: row.Browser, Browser: row.Browser,
Version: row.Version,
} }
answers = append(answers, answer) answers = append(answers, answer)