common/dal/sqlcgen/queries.sql.go
2024-03-17 20:35:47 +03:00

1850 lines
55 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
// source: queries.sql
package sqlcgen
import (
"context"
"database/sql"
"time"
"github.com/google/uuid"
"github.com/lib/pq"
)
const accountPagination = `-- name: AccountPagination :many
SELECT a.id, a.user_id, a.created_at, a.deleted
FROM account a ORDER BY a.created_at DESC LIMIT $1 OFFSET $2
`
type AccountPaginationParams struct {
Limit int32 `db:"limit" json:"limit"`
Offset int32 `db:"offset" json:"offset"`
}
type AccountPaginationRow struct {
ID uuid.UUID `db:"id" json:"id"`
UserID sql.NullString `db:"user_id" json:"user_id"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
Deleted sql.NullBool `db:"deleted" json:"deleted"`
}
func (q *Queries) AccountPagination(ctx context.Context, arg AccountPaginationParams) ([]AccountPaginationRow, error) {
rows, err := q.db.QueryContext(ctx, accountPagination, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
var items []AccountPaginationRow
for rows.Next() {
var i AccountPaginationRow
if err := rows.Scan(
&i.ID,
&i.UserID,
&i.CreatedAt,
&i.Deleted,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const archiveQuiz = `-- name: ArchiveQuiz :exec
UPDATE quiz SET archived = true WHERE id=$1 AND accountId=$2
`
type ArchiveQuizParams struct {
ID int64 `db:"id" json:"id"`
Accountid string `db:"accountid" json:"accountid"`
}
func (q *Queries) ArchiveQuiz(ctx context.Context, arg ArchiveQuizParams) error {
_, err := q.db.ExecContext(ctx, archiveQuiz, arg.ID, arg.Accountid)
return err
}
const checkAndAddDefault = `-- name: CheckAndAddDefault :exec
UPDATE privileges
SET amount = $1, created_at = NOW()
WHERE privilege_name = $2
AND (amount < $3 OR created_at <= NOW() - INTERVAL '1 month')
`
type CheckAndAddDefaultParams struct {
Amount sql.NullInt32 `db:"amount" json:"amount"`
PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"`
Amount_2 sql.NullInt32 `db:"amount_2" json:"amount_2"`
}
func (q *Queries) CheckAndAddDefault(ctx context.Context, arg CheckAndAddDefaultParams) error {
_, err := q.db.ExecContext(ctx, checkAndAddDefault, arg.Amount, arg.PrivilegeName, arg.Amount_2)
return err
}
const checkResultOwner = `-- name: CheckResultOwner :one
SELECT q.accountid FROM answer a JOIN quiz q ON a.quiz_id = q.id WHERE a.id = $1 AND a.deleted = FALSE AND a.start = false
`
func (q *Queries) CheckResultOwner(ctx context.Context, id int64) (string, error) {
row := q.db.QueryRowContext(ctx, checkResultOwner, id)
var accountid string
err := row.Scan(&accountid)
return accountid, err
}
const checkResultsOwner = `-- name: CheckResultsOwner :many
SELECT a.id
FROM answer a
JOIN quiz q ON a.quiz_id = q.id
WHERE a.id = ANY($1::bigint[]) AND a.deleted = FALSE AND q.accountid = $2 AND a.start = false
`
type CheckResultsOwnerParams struct {
Column1 []int64 `db:"column_1" json:"column_1"`
Accountid string `db:"accountid" json:"accountid"`
}
func (q *Queries) CheckResultsOwner(ctx context.Context, arg CheckResultsOwnerParams) ([]int64, error) {
rows, err := q.db.QueryContext(ctx, checkResultsOwner, pq.Array(arg.Column1), arg.Accountid)
if err != nil {
return nil, err
}
defer rows.Close()
var items []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
return nil, err
}
items = append(items, id)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const copyQuestion = `-- name: CopyQuestion :one
INSERT INTO question(
quiz_id, title, description, questiontype, required,
page, content, version, parent_ids
)
SELECT $1, title, description, questiontype, required,
page, content, version, parent_ids
FROM question WHERE question.id=$2
RETURNING question.id, quiz_id, created_at, updated_at
`
type CopyQuestionParams struct {
QuizID int64 `db:"quiz_id" json:"quiz_id"`
ID int64 `db:"id" json:"id"`
}
type CopyQuestionRow struct {
ID int64 `db:"id" json:"id"`
QuizID int64 `db:"quiz_id" json:"quiz_id"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
}
func (q *Queries) CopyQuestion(ctx context.Context, arg CopyQuestionParams) (CopyQuestionRow, error) {
row := q.db.QueryRowContext(ctx, copyQuestion, arg.QuizID, arg.ID)
var i CopyQuestionRow
err := row.Scan(
&i.ID,
&i.QuizID,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const copyQuiz = `-- name: CopyQuiz :one
INSERT INTO quiz(
accountid, archived,fingerprinting,repeatable,note_prevented,mail_notifications,unique_answers,name,description,config,
status,limit_answers,due_to,time_of_passing,pausable,version,version_comment,parent_ids,questions_count,answers_count,average_time_passing, super, group_id
)
SELECT accountid, archived,fingerprinting,repeatable,note_prevented,mail_notifications,unique_answers,name,description,config,
status,limit_answers,due_to,time_of_passing,pausable,version,version_comment,parent_ids,questions_count,answers_count,average_time_passing, super, group_id
FROM quiz WHERE quiz.id=$1 AND quiz.accountId=$2
RETURNING id, qid,created_at, updated_at
`
type CopyQuizParams struct {
ID int64 `db:"id" json:"id"`
Accountid string `db:"accountid" json:"accountid"`
}
type CopyQuizRow struct {
ID int64 `db:"id" json:"id"`
Qid uuid.NullUUID `db:"qid" json:"qid"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
}
func (q *Queries) CopyQuiz(ctx context.Context, arg CopyQuizParams) (CopyQuizRow, error) {
row := q.db.QueryRowContext(ctx, copyQuiz, arg.ID, arg.Accountid)
var i CopyQuizRow
err := row.Scan(
&i.ID,
&i.Qid,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const copyQuizQuestions = `-- name: CopyQuizQuestions :exec
INSERT INTO question(
quiz_id, title, description, questiontype, required, page, content, version, parent_ids
)
SELECT $2, title, description, questiontype, required, page, content, version, parent_ids
FROM question WHERE question.quiz_id=$1 AND deleted=false
`
type CopyQuizQuestionsParams struct {
QuizID int64 `db:"quiz_id" json:"quiz_id"`
QuizID_2 int64 `db:"quiz_id_2" json:"quiz_id_2"`
}
func (q *Queries) CopyQuizQuestions(ctx context.Context, arg CopyQuizQuestionsParams) error {
_, err := q.db.ExecContext(ctx, copyQuizQuestions, arg.QuizID, arg.QuizID_2)
return err
}
const createAccount = `-- name: CreateAccount :exec
INSERT INTO account (id, user_id, email, created_at, deleted) VALUES ($1, $2, $3, $4, $5)
`
type CreateAccountParams struct {
ID uuid.UUID `db:"id" json:"id"`
UserID sql.NullString `db:"user_id" json:"user_id"`
Email sql.NullString `db:"email" json:"email"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
Deleted sql.NullBool `db:"deleted" json:"deleted"`
}
func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) error {
_, err := q.db.ExecContext(ctx, createAccount,
arg.ID,
arg.UserID,
arg.Email,
arg.CreatedAt,
arg.Deleted,
)
return err
}
const deleteAccountById = `-- name: DeleteAccountById :exec
DELETE FROM account WHERE id = $1
`
func (q *Queries) DeleteAccountById(ctx context.Context, id uuid.UUID) error {
_, err := q.db.ExecContext(ctx, deleteAccountById, id)
return err
}
const deletePrivilegeByAccID = `-- name: DeletePrivilegeByAccID :exec
DELETE FROM privileges WHERE account_id = $1
`
func (q *Queries) DeletePrivilegeByAccID(ctx context.Context, accountID uuid.NullUUID) error {
_, err := q.db.ExecContext(ctx, deletePrivilegeByAccID, accountID)
return err
}
const deletePrivilegeByID = `-- name: DeletePrivilegeByID :exec
DELETE FROM privileges WHERE id = $1
`
func (q *Queries) DeletePrivilegeByID(ctx context.Context, id int32) error {
_, err := q.db.ExecContext(ctx, deletePrivilegeByID, id)
return err
}
const deleteQuestion = `-- name: DeleteQuestion :one
UPDATE question SET deleted=true WHERE id=$1 RETURNING question.id, question.quiz_id, question.title, question.description, question.questiontype, question.required, question.deleted, question.page, question.content, question.version, question.parent_ids, question.created_at, question.updated_at
`
func (q *Queries) DeleteQuestion(ctx context.Context, id int64) (Question, error) {
row := q.db.QueryRowContext(ctx, deleteQuestion, id)
var i Question
err := row.Scan(
&i.ID,
&i.QuizID,
&i.Title,
&i.Description,
&i.Questiontype,
&i.Required,
&i.Deleted,
&i.Page,
&i.Content,
&i.Version,
pq.Array(&i.ParentIds),
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const deleteQuizByID = `-- name: DeleteQuizByID :one
UPDATE quiz SET deleted=true WHERE quiz.id=$1 AND accountid=$2 RETURNING quiz.id, quiz.qid, quiz.accountid, quiz.deleted, quiz.archived, quiz.fingerprinting, quiz.repeatable, quiz.note_prevented, quiz.mail_notifications, quiz.unique_answers, quiz.super, quiz.group_id, quiz.name, quiz.description, quiz.config, quiz.status, quiz.limit_answers, quiz.due_to, quiz.time_of_passing, quiz.pausable, quiz.version, quiz.version_comment, quiz.parent_ids, quiz.created_at, quiz.updated_at, quiz.questions_count, quiz.answers_count, quiz.average_time_passing, quiz.sessions_count
`
type DeleteQuizByIDParams struct {
ID int64 `db:"id" json:"id"`
Accountid string `db:"accountid" json:"accountid"`
}
func (q *Queries) DeleteQuizByID(ctx context.Context, arg DeleteQuizByIDParams) (Quiz, error) {
row := q.db.QueryRowContext(ctx, deleteQuizByID, arg.ID, arg.Accountid)
var i Quiz
err := row.Scan(
&i.ID,
&i.Qid,
&i.Accountid,
&i.Deleted,
&i.Archived,
&i.Fingerprinting,
&i.Repeatable,
&i.NotePrevented,
&i.MailNotifications,
&i.UniqueAnswers,
&i.Super,
&i.GroupID,
&i.Name,
&i.Description,
&i.Config,
&i.Status,
&i.LimitAnswers,
&i.DueTo,
&i.TimeOfPassing,
&i.Pausable,
&i.Version,
&i.VersionComment,
pq.Array(&i.ParentIds),
&i.CreatedAt,
&i.UpdatedAt,
&i.QuestionsCount,
&i.AnswersCount,
&i.AverageTimePassing,
&i.SessionsCount,
)
return i, err
}
const deviceStatistics = `-- name: DeviceStatistics :many
WITH DeviceStats AS (
SELECT
device_type,
COUNT(*) AS device_count
FROM
answer
WHERE
answer.quiz_id = $1
AND created_at >= to_timestamp($2)
AND created_at <= to_timestamp($3)
AND result = TRUE
GROUP BY
device_type
),
OSStats AS (
SELECT
os,
COUNT(*) AS os_count
FROM
answer
WHERE
answer.quiz_id = $1
AND created_at >= to_timestamp($2)
AND created_at <= to_timestamp($3)
AND result = TRUE
GROUP BY
os
),
BrowserStats AS (
SELECT
browser,
COUNT(*) AS browser_count
FROM
answer
WHERE
answer.quiz_id = $1
AND created_at >= to_timestamp($2)
AND created_at <= to_timestamp($3)
AND result = TRUE
GROUP BY
browser
),
TotalStats AS (
SELECT
COUNT(*) AS total_count
FROM
answer
WHERE
answer.quiz_id = $1
AND created_at >= to_timestamp($2)
AND created_at <= to_timestamp($3)
AND result = TRUE
)
SELECT
DeviceStats.device_type,
CAST((DeviceStats.device_count::FLOAT / TotalStats.total_count) * 100.0 AS INT) AS device_percentage,
OSStats.os,
CAST((OSStats.os_count::FLOAT / TotalStats.total_count) * 100.0 AS INT) AS os_percentage,
BrowserStats.browser,
CAST((BrowserStats.browser_count::FLOAT / TotalStats.total_count) * 100.0 AS INT) AS browser_percentage
FROM
DeviceStats,
OSStats,
BrowserStats,
TotalStats
`
type DeviceStatisticsParams struct {
QuizID int64 `db:"quiz_id" json:"quiz_id"`
ToTimestamp float64 `db:"to_timestamp" json:"to_timestamp"`
ToTimestamp_2 float64 `db:"to_timestamp_2" json:"to_timestamp_2"`
}
type DeviceStatisticsRow struct {
DeviceType string `db:"device_type" json:"device_type"`
DevicePercentage int32 `db:"device_percentage" json:"device_percentage"`
Os string `db:"os" json:"os"`
OsPercentage int32 `db:"os_percentage" json:"os_percentage"`
Browser string `db:"browser" json:"browser"`
BrowserPercentage int32 `db:"browser_percentage" json:"browser_percentage"`
}
func (q *Queries) DeviceStatistics(ctx context.Context, arg DeviceStatisticsParams) ([]DeviceStatisticsRow, error) {
rows, err := q.db.QueryContext(ctx, deviceStatistics, arg.QuizID, arg.ToTimestamp, arg.ToTimestamp_2)
if err != nil {
return nil, err
}
defer rows.Close()
var items []DeviceStatisticsRow
for rows.Next() {
var i DeviceStatisticsRow
if err := rows.Scan(
&i.DeviceType,
&i.DevicePercentage,
&i.Os,
&i.OsPercentage,
&i.Browser,
&i.BrowserPercentage,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const duplicateQuestion = `-- name: DuplicateQuestion :one
INSERT INTO question(
quiz_id, title, description, questiontype, required,
page, content, version, parent_ids
)
SELECT quiz_id, title, description, questiontype, required,
page, content, version, parent_ids
FROM question WHERE question.id=$1
RETURNING question.id, quiz_id, created_at, updated_at
`
type DuplicateQuestionRow struct {
ID int64 `db:"id" json:"id"`
QuizID int64 `db:"quiz_id" json:"quiz_id"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
}
func (q *Queries) DuplicateQuestion(ctx context.Context, id int64) (DuplicateQuestionRow, error) {
row := q.db.QueryRowContext(ctx, duplicateQuestion, id)
var i DuplicateQuestionRow
err := row.Scan(
&i.ID,
&i.QuizID,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const generalStatistics = `-- name: GeneralStatistics :many
WITH TimeBucket AS (
SELECT
CASE
WHEN EXTRACT(epoch FROM $2::timestamp) - EXTRACT(epoch FROM $1::timestamp) > 172800 THEN date_trunc('day', timestamp_bucket)
ELSE date_trunc('hour', timestamp_bucket)
END::TIMESTAMP AS time_interval
FROM
generate_series($1::timestamp, $2::timestamp, '1 hour'::interval) AS timestamp_bucket
),
OpenStats AS (
SELECT
tb.time_interval,
COUNT(DISTINCT session) AS open_count
FROM
(
SELECT
session,
MIN(created_at) AS first_start_time
FROM
answer
WHERE
answer.quiz_id = $3
AND start = TRUE
AND created_at >= $1::timestamp
AND created_at <= $2::timestamp
GROUP BY
session
) AS first_starts
JOIN TimeBucket tb ON date_trunc('hour', first_starts.first_start_time) = tb.time_interval
GROUP BY
tb.time_interval
),
ResultStats AS (
SELECT
tb.time_interval,
COUNT(DISTINCT session) AS result_count
FROM
(
SELECT
session,
MIN(created_at) AS first_result_time
FROM
answer
WHERE
answer.quiz_id = $3
AND result = TRUE
AND created_at >= $1::timestamp
AND created_at <= $2::timestamp
GROUP BY
session
) AS first_results
JOIN TimeBucket tb ON date_trunc('hour', first_results.first_result_time) = tb.time_interval
GROUP BY
tb.time_interval
),
AvTimeStats AS (
SELECT
tb.time_interval,
AVG(EXTRACT(epoch FROM (a.created_at - b.created_at))) AS avg_time
FROM
answer a
JOIN
answer b ON a.session = b.session
JOIN
TimeBucket tb ON date_trunc('hour', a.created_at) = tb.time_interval
WHERE
a.quiz_id = $3
AND a.result = TRUE
AND b.start = TRUE
AND a.created_at >= $1::timestamp
AND a.created_at <= $2::timestamp
GROUP BY
tb.time_interval
)
SELECT
tb.time_interval AS time_bucket,
COALESCE(os.open_count, 0) AS open_count,
COALESCE(rs.result_count, 0) AS result_count,
COALESCE(at.avg_time, 0) AS avg_time,
CASE
WHEN COALESCE(os.open_count, 0) > 0 THEN COALESCE(rs.result_count, 0) / COALESCE(os.open_count, 0)
ELSE 0
END AS conversion
FROM
TimeBucket tb
LEFT JOIN
OpenStats os ON tb.time_interval = os.time_interval
LEFT JOIN
ResultStats rs ON tb.time_interval = rs.time_interval
LEFT JOIN
AvTimeStats at ON tb.time_interval = at.time_interval
`
type GeneralStatisticsParams struct {
Column1 time.Time `db:"column_1" json:"column_1"`
Column2 time.Time `db:"column_2" json:"column_2"`
QuizID int64 `db:"quiz_id" json:"quiz_id"`
}
type GeneralStatisticsRow struct {
TimeBucket time.Time `db:"time_bucket" json:"time_bucket"`
OpenCount int64 `db:"open_count" json:"open_count"`
ResultCount int64 `db:"result_count" json:"result_count"`
AvgTime float64 `db:"avg_time" json:"avg_time"`
Conversion int32 `db:"conversion" json:"conversion"`
}
func (q *Queries) GeneralStatistics(ctx context.Context, arg GeneralStatisticsParams) ([]GeneralStatisticsRow, error) {
rows, err := q.db.QueryContext(ctx, generalStatistics, arg.Column1, arg.Column2, arg.QuizID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GeneralStatisticsRow
for rows.Next() {
var i GeneralStatisticsRow
if err := rows.Scan(
&i.TimeBucket,
&i.OpenCount,
&i.ResultCount,
&i.AvgTime,
&i.Conversion,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getAccAndPrivilegeByEmail = `-- name: GetAccAndPrivilegeByEmail :one
SELECT
a.id,
a.user_id,
a.email,
a.created_at,
COALESCE(p.ID,0),
coalesce(p.privilegeid,''),
coalesce(p.amount,0),
coalesce(p.created_at,Now())
FROM
account AS a
LEFT JOIN privileges AS p ON a.id = p.account_id
WHERE
a.user_id = $1
`
type GetAccAndPrivilegeByEmailRow struct {
ID uuid.UUID `db:"id" json:"id"`
UserID sql.NullString `db:"user_id" json:"user_id"`
Email sql.NullString `db:"email" json:"email"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
ID_2 int32 `db:"id_2" json:"id_2"`
Privilegeid string `db:"privilegeid" json:"privilegeid"`
Amount int32 `db:"amount" json:"amount"`
CreatedAt_2 sql.NullTime `db:"created_at_2" json:"created_at_2"`
}
func (q *Queries) GetAccAndPrivilegeByEmail(ctx context.Context, userID sql.NullString) (GetAccAndPrivilegeByEmailRow, error) {
row := q.db.QueryRowContext(ctx, getAccAndPrivilegeByEmail, userID)
var i GetAccAndPrivilegeByEmailRow
err := row.Scan(
&i.ID,
&i.UserID,
&i.Email,
&i.CreatedAt,
&i.ID_2,
&i.Privilegeid,
&i.Amount,
&i.CreatedAt_2,
)
return i, err
}
const getAccountWithPrivileges = `-- name: GetAccountWithPrivileges :many
SELECT a.id, a.user_id, a.created_at, a.deleted,
coalesce(p.id,0) AS privilege_id,
coalesce(p.privilegeID,''),
coalesce(p.privilege_name,''),
coalesce(p.amount,0), coalesce(p.created_at,Now()) AS privilege_created_at
FROM account a
LEFT JOIN privileges AS p ON a.id = p.account_id
WHERE a.user_id = $1
`
type GetAccountWithPrivilegesRow struct {
ID uuid.UUID `db:"id" json:"id"`
UserID sql.NullString `db:"user_id" json:"user_id"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
Deleted sql.NullBool `db:"deleted" json:"deleted"`
PrivilegeID int32 `db:"privilege_id" json:"privilege_id"`
Privilegeid string `db:"privilegeid" json:"privilegeid"`
PrivilegeName string `db:"privilege_name" json:"privilege_name"`
Amount int32 `db:"amount" json:"amount"`
PrivilegeCreatedAt sql.NullTime `db:"privilege_created_at" json:"privilege_created_at"`
}
func (q *Queries) GetAccountWithPrivileges(ctx context.Context, userID sql.NullString) ([]GetAccountWithPrivilegesRow, error) {
rows, err := q.db.QueryContext(ctx, getAccountWithPrivileges, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAccountWithPrivilegesRow
for rows.Next() {
var i GetAccountWithPrivilegesRow
if err := rows.Scan(
&i.ID,
&i.UserID,
&i.CreatedAt,
&i.Deleted,
&i.PrivilegeID,
&i.Privilegeid,
&i.PrivilegeName,
&i.Amount,
&i.PrivilegeCreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getAllAnswersByQuizID = `-- name: GetAllAnswersByQuizID :many
SELECT DISTINCT ON(question_id) content, created_at, question_id, id FROM answer WHERE session = $1 AND start = false ORDER BY question_id ASC, created_at DESC
`
type GetAllAnswersByQuizIDRow struct {
Content sql.NullString `db:"content" json:"content"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
QuestionID int64 `db:"question_id" json:"question_id"`
ID int64 `db:"id" json:"id"`
}
func (q *Queries) GetAllAnswersByQuizID(ctx context.Context, session sql.NullString) ([]GetAllAnswersByQuizIDRow, error) {
rows, err := q.db.QueryContext(ctx, getAllAnswersByQuizID, session)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAllAnswersByQuizIDRow
for rows.Next() {
var i GetAllAnswersByQuizIDRow
if err := rows.Scan(
&i.Content,
&i.CreatedAt,
&i.QuestionID,
&i.ID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getExpiredPrivilege = `-- name: GetExpiredPrivilege :many
SELECT id, privilegeID, privilege_name, amount, created_at
FROM privileges
WHERE created_at + amount * interval '1 day' < NOW()
AND privilegeid = $1
`
type GetExpiredPrivilegeRow struct {
ID int32 `db:"id" json:"id"`
Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"`
PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"`
Amount sql.NullInt32 `db:"amount" json:"amount"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
}
func (q *Queries) GetExpiredPrivilege(ctx context.Context, privilegeid sql.NullString) ([]GetExpiredPrivilegeRow, error) {
rows, err := q.db.QueryContext(ctx, getExpiredPrivilege, privilegeid)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetExpiredPrivilegeRow
for rows.Next() {
var i GetExpiredPrivilegeRow
if err := rows.Scan(
&i.ID,
&i.Privilegeid,
&i.PrivilegeName,
&i.Amount,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getPrivilegesByAccountID = `-- name: GetPrivilegesByAccountID :many
SELECT id,privilegeID,privilege_name,amount, created_at FROM privileges WHERE account_id = $1
`
type GetPrivilegesByAccountIDRow struct {
ID int32 `db:"id" json:"id"`
Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"`
PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"`
Amount sql.NullInt32 `db:"amount" json:"amount"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
}
func (q *Queries) GetPrivilegesByAccountID(ctx context.Context, accountID uuid.NullUUID) ([]GetPrivilegesByAccountIDRow, error) {
rows, err := q.db.QueryContext(ctx, getPrivilegesByAccountID, accountID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetPrivilegesByAccountIDRow
for rows.Next() {
var i GetPrivilegesByAccountIDRow
if err := rows.Scan(
&i.ID,
&i.Privilegeid,
&i.PrivilegeName,
&i.Amount,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getPrivilegesByAccountIDWC = `-- name: GetPrivilegesByAccountIDWC :many
SELECT p.id,p.privilegeID,p.privilege_name,p.amount, p.created_at FROM privileges as p JOIN account as a on p.account_id = a.id WHERE a.user_id = $1
`
type GetPrivilegesByAccountIDWCRow struct {
ID int32 `db:"id" json:"id"`
Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"`
PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"`
Amount sql.NullInt32 `db:"amount" json:"amount"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
}
func (q *Queries) GetPrivilegesByAccountIDWC(ctx context.Context, userID sql.NullString) ([]GetPrivilegesByAccountIDWCRow, error) {
rows, err := q.db.QueryContext(ctx, getPrivilegesByAccountIDWC, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetPrivilegesByAccountIDWCRow
for rows.Next() {
var i GetPrivilegesByAccountIDWCRow
if err := rows.Scan(
&i.ID,
&i.Privilegeid,
&i.PrivilegeName,
&i.Amount,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getPrivilegesQuizAccount = `-- name: GetPrivilegesQuizAccount :many
SELECT
p.privilegeID,
p.privilege_name,
p.amount,
p.created_at,
a.id,
a.email,
qz.config
FROM
privileges AS p
INNER JOIN account AS a ON p.account_id = a.id
INNER JOIN quiz AS qz ON qz.accountid = a.user_id
WHERE
qz.id = $1
`
type GetPrivilegesQuizAccountRow struct {
Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"`
PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"`
Amount sql.NullInt32 `db:"amount" json:"amount"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
ID uuid.UUID `db:"id" json:"id"`
Email sql.NullString `db:"email" json:"email"`
Config sql.NullString `db:"config" json:"config"`
}
func (q *Queries) GetPrivilegesQuizAccount(ctx context.Context, id int64) ([]GetPrivilegesQuizAccountRow, error) {
rows, err := q.db.QueryContext(ctx, getPrivilegesQuizAccount, id)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetPrivilegesQuizAccountRow
for rows.Next() {
var i GetPrivilegesQuizAccountRow
if err := rows.Scan(
&i.Privilegeid,
&i.PrivilegeName,
&i.Amount,
&i.CreatedAt,
&i.ID,
&i.Email,
&i.Config,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getQuestionHistory = `-- name: GetQuestionHistory :many
SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at FROM question WHERE question.id = $1 OR question.id = ANY(
SELECT unnest(parent_ids) FROM question WHERE id = $1
) ORDER BY question.id DESC LIMIT $2 OFFSET $3
`
type GetQuestionHistoryParams struct {
ID int64 `db:"id" json:"id"`
Limit int32 `db:"limit" json:"limit"`
Offset int32 `db:"offset" json:"offset"`
}
func (q *Queries) GetQuestionHistory(ctx context.Context, arg GetQuestionHistoryParams) ([]Question, error) {
rows, err := q.db.QueryContext(ctx, getQuestionHistory, arg.ID, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Question
for rows.Next() {
var i Question
if err := rows.Scan(
&i.ID,
&i.QuizID,
&i.Title,
&i.Description,
&i.Questiontype,
&i.Required,
&i.Deleted,
&i.Page,
&i.Content,
&i.Version,
pq.Array(&i.ParentIds),
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getQuestionTitle = `-- name: GetQuestionTitle :one
SELECT title, questiontype FROM question WHERE id = $1
`
type GetQuestionTitleRow struct {
Title string `db:"title" json:"title"`
Questiontype interface{} `db:"questiontype" json:"questiontype"`
}
func (q *Queries) GetQuestionTitle(ctx context.Context, id int64) (GetQuestionTitleRow, error) {
row := q.db.QueryRowContext(ctx, getQuestionTitle, id)
var i GetQuestionTitleRow
err := row.Scan(&i.Title, &i.Questiontype)
return i, err
}
const getQuestions = `-- name: GetQuestions :many
SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at FROM question WHERE quiz_id = $1 AND deleted = FALSE
`
func (q *Queries) GetQuestions(ctx context.Context, quizID int64) ([]Question, error) {
rows, err := q.db.QueryContext(ctx, getQuestions, quizID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Question
for rows.Next() {
var i Question
if err := rows.Scan(
&i.ID,
&i.QuizID,
&i.Title,
&i.Description,
&i.Questiontype,
&i.Required,
&i.Deleted,
&i.Page,
&i.Content,
&i.Version,
pq.Array(&i.ParentIds),
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getQuizById = `-- name: GetQuizById :one
SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz WHERE id=$1 AND accountId=$2
`
type GetQuizByIdParams struct {
ID int64 `db:"id" json:"id"`
Accountid string `db:"accountid" json:"accountid"`
}
func (q *Queries) GetQuizById(ctx context.Context, arg GetQuizByIdParams) (Quiz, error) {
row := q.db.QueryRowContext(ctx, getQuizById, arg.ID, arg.Accountid)
var i Quiz
err := row.Scan(
&i.ID,
&i.Qid,
&i.Accountid,
&i.Deleted,
&i.Archived,
&i.Fingerprinting,
&i.Repeatable,
&i.NotePrevented,
&i.MailNotifications,
&i.UniqueAnswers,
&i.Super,
&i.GroupID,
&i.Name,
&i.Description,
&i.Config,
&i.Status,
&i.LimitAnswers,
&i.DueTo,
&i.TimeOfPassing,
&i.Pausable,
&i.Version,
&i.VersionComment,
pq.Array(&i.ParentIds),
&i.CreatedAt,
&i.UpdatedAt,
&i.QuestionsCount,
&i.AnswersCount,
&i.AverageTimePassing,
&i.SessionsCount,
)
return i, err
}
const getQuizByQid = `-- name: GetQuizByQid :one
SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz
WHERE
deleted = false AND
archived = false AND
status = 'start' AND
qid = $1
`
func (q *Queries) GetQuizByQid(ctx context.Context, qid uuid.NullUUID) (Quiz, error) {
row := q.db.QueryRowContext(ctx, getQuizByQid, qid)
var i Quiz
err := row.Scan(
&i.ID,
&i.Qid,
&i.Accountid,
&i.Deleted,
&i.Archived,
&i.Fingerprinting,
&i.Repeatable,
&i.NotePrevented,
&i.MailNotifications,
&i.UniqueAnswers,
&i.Super,
&i.GroupID,
&i.Name,
&i.Description,
&i.Config,
&i.Status,
&i.LimitAnswers,
&i.DueTo,
&i.TimeOfPassing,
&i.Pausable,
&i.Version,
&i.VersionComment,
pq.Array(&i.ParentIds),
&i.CreatedAt,
&i.UpdatedAt,
&i.QuestionsCount,
&i.AnswersCount,
&i.AverageTimePassing,
&i.SessionsCount,
)
return i, err
}
const getQuizConfig = `-- name: GetQuizConfig :one
SELECT config, accountid FROM quiz WHERE id = $1 AND deleted = false
`
type GetQuizConfigRow struct {
Config sql.NullString `db:"config" json:"config"`
Accountid string `db:"accountid" json:"accountid"`
}
func (q *Queries) GetQuizConfig(ctx context.Context, id int64) (GetQuizConfigRow, error) {
row := q.db.QueryRowContext(ctx, getQuizConfig, id)
var i GetQuizConfigRow
err := row.Scan(&i.Config, &i.Accountid)
return i, err
}
const getQuizHistory = `-- name: GetQuizHistory :many
SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz WHERE quiz.id = $1 AND quiz.accountId = $4 OR quiz.id = ANY(
SELECT unnest(parent_ids) FROM quiz WHERE id = $1
) ORDER BY quiz.id DESC LIMIT $2 OFFSET $3
`
type GetQuizHistoryParams struct {
ID int64 `db:"id" json:"id"`
Limit int32 `db:"limit" json:"limit"`
Offset int32 `db:"offset" json:"offset"`
Accountid string `db:"accountid" json:"accountid"`
}
func (q *Queries) GetQuizHistory(ctx context.Context, arg GetQuizHistoryParams) ([]Quiz, error) {
rows, err := q.db.QueryContext(ctx, getQuizHistory,
arg.ID,
arg.Limit,
arg.Offset,
arg.Accountid,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Quiz
for rows.Next() {
var i Quiz
if err := rows.Scan(
&i.ID,
&i.Qid,
&i.Accountid,
&i.Deleted,
&i.Archived,
&i.Fingerprinting,
&i.Repeatable,
&i.NotePrevented,
&i.MailNotifications,
&i.UniqueAnswers,
&i.Super,
&i.GroupID,
&i.Name,
&i.Description,
&i.Config,
&i.Status,
&i.LimitAnswers,
&i.DueTo,
&i.TimeOfPassing,
&i.Pausable,
&i.Version,
&i.VersionComment,
pq.Array(&i.ParentIds),
&i.CreatedAt,
&i.UpdatedAt,
&i.QuestionsCount,
&i.AnswersCount,
&i.AverageTimePassing,
&i.SessionsCount,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getResultAnswers = `-- name: GetResultAnswers :many
SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted FROM answer WHERE session = (
SELECT session FROM answer WHERE answer.id = $1) AND start = false ORDER BY question_id, created_at DESC
`
type GetResultAnswersRow struct {
ID int64 `db:"id" json:"id"`
Content sql.NullString `db:"content" json:"content"`
QuizID int64 `db:"quiz_id" json:"quiz_id"`
QuestionID int64 `db:"question_id" json:"question_id"`
Fingerprint sql.NullString `db:"fingerprint" json:"fingerprint"`
Session sql.NullString `db:"session" json:"session"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
Result sql.NullBool `db:"result" json:"result"`
New sql.NullBool `db:"new" json:"new"`
Deleted sql.NullBool `db:"deleted" json:"deleted"`
}
func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAnswersRow, error) {
rows, err := q.db.QueryContext(ctx, getResultAnswers, id)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetResultAnswersRow
for rows.Next() {
var i GetResultAnswersRow
if err := rows.Scan(
&i.ID,
&i.Content,
&i.QuizID,
&i.QuestionID,
&i.Fingerprint,
&i.Session,
&i.CreatedAt,
&i.Result,
&i.New,
&i.Deleted,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const insertAnswers = `-- name: InsertAnswers :exec
INSERT INTO answer(
content,
quiz_id,
question_id,
fingerprint,
session,
result,
email,
device_type,
device,
os,
browser,
ip,
start
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)
`
type InsertAnswersParams struct {
Content sql.NullString `db:"content" json:"content"`
QuizID int64 `db:"quiz_id" json:"quiz_id"`
QuestionID int64 `db:"question_id" json:"question_id"`
Fingerprint sql.NullString `db:"fingerprint" json:"fingerprint"`
Session sql.NullString `db:"session" json:"session"`
Result sql.NullBool `db:"result" json:"result"`
Email string `db:"email" json:"email"`
DeviceType string `db:"device_type" json:"device_type"`
Device string `db:"device" json:"device"`
Os string `db:"os" json:"os"`
Browser string `db:"browser" json:"browser"`
Ip string `db:"ip" json:"ip"`
Start bool `db:"start" json:"start"`
}
func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) error {
_, err := q.db.ExecContext(ctx, insertAnswers,
arg.Content,
arg.QuizID,
arg.QuestionID,
arg.Fingerprint,
arg.Session,
arg.Result,
arg.Email,
arg.DeviceType,
arg.Device,
arg.Os,
arg.Browser,
arg.Ip,
arg.Start,
)
return err
}
const insertPrivilege = `-- name: InsertPrivilege :exec
INSERT INTO privileges (privilegeID, account_id, privilege_name, amount, created_at) VALUES ($1, $2, $3, $4, $5)
`
type InsertPrivilegeParams struct {
Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"`
AccountID uuid.NullUUID `db:"account_id" json:"account_id"`
PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"`
Amount sql.NullInt32 `db:"amount" json:"amount"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
}
func (q *Queries) InsertPrivilege(ctx context.Context, arg InsertPrivilegeParams) error {
_, err := q.db.ExecContext(ctx, insertPrivilege,
arg.Privilegeid,
arg.AccountID,
arg.PrivilegeName,
arg.Amount,
arg.CreatedAt,
)
return err
}
const insertPrivilegeWC = `-- name: InsertPrivilegeWC :exec
UPDATE privileges SET amount = $1, created_at = $2 WHERE account_id = $3 AND privilegeID = $4
`
type InsertPrivilegeWCParams struct {
Amount sql.NullInt32 `db:"amount" json:"amount"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
AccountID uuid.NullUUID `db:"account_id" json:"account_id"`
Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"`
}
func (q *Queries) InsertPrivilegeWC(ctx context.Context, arg InsertPrivilegeWCParams) error {
_, err := q.db.ExecContext(ctx, insertPrivilegeWC,
arg.Amount,
arg.CreatedAt,
arg.AccountID,
arg.Privilegeid,
)
return err
}
const insertQuestion = `-- name: InsertQuestion :one
INSERT INTO question (
quiz_id,
title,
description,
questiontype,
required,
page,
content,
parent_ids,
updated_at
)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)
RETURNING id, created_at, updated_at
`
type InsertQuestionParams struct {
QuizID int64 `db:"quiz_id" json:"quiz_id"`
Title string `db:"title" json:"title"`
Description sql.NullString `db:"description" json:"description"`
Questiontype interface{} `db:"questiontype" json:"questiontype"`
Required sql.NullBool `db:"required" json:"required"`
Page sql.NullInt16 `db:"page" json:"page"`
Content sql.NullString `db:"content" json:"content"`
ParentIds []int32 `db:"parent_ids" json:"parent_ids"`
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
}
type InsertQuestionRow struct {
ID int64 `db:"id" json:"id"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
}
func (q *Queries) InsertQuestion(ctx context.Context, arg InsertQuestionParams) (InsertQuestionRow, error) {
row := q.db.QueryRowContext(ctx, insertQuestion,
arg.QuizID,
arg.Title,
arg.Description,
arg.Questiontype,
arg.Required,
arg.Page,
arg.Content,
pq.Array(arg.ParentIds),
arg.UpdatedAt,
)
var i InsertQuestionRow
err := row.Scan(&i.ID, &i.CreatedAt, &i.UpdatedAt)
return i, err
}
const insertQuiz = `-- name: InsertQuiz :one
INSERT INTO quiz (accountid,
fingerprinting,
repeatable,
note_prevented,
mail_notifications,
unique_answers,
super,
group_id,
name,
description,
config,
status,
limit_answers,
due_to,
time_of_passing,
pausable,
parent_ids,
questions_count,
qid
)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19)
RETURNING id, created_at, updated_at, qid
`
type InsertQuizParams struct {
Accountid string `db:"accountid" json:"accountid"`
Fingerprinting sql.NullBool `db:"fingerprinting" json:"fingerprinting"`
Repeatable sql.NullBool `db:"repeatable" json:"repeatable"`
NotePrevented sql.NullBool `db:"note_prevented" json:"note_prevented"`
MailNotifications sql.NullBool `db:"mail_notifications" json:"mail_notifications"`
UniqueAnswers sql.NullBool `db:"unique_answers" json:"unique_answers"`
Super sql.NullBool `db:"super" json:"super"`
GroupID sql.NullInt64 `db:"group_id" json:"group_id"`
Name sql.NullString `db:"name" json:"name"`
Description sql.NullString `db:"description" json:"description"`
Config sql.NullString `db:"config" json:"config"`
Status interface{} `db:"status" json:"status"`
LimitAnswers sql.NullInt32 `db:"limit_answers" json:"limit_answers"`
DueTo sql.NullInt32 `db:"due_to" json:"due_to"`
TimeOfPassing sql.NullInt32 `db:"time_of_passing" json:"time_of_passing"`
Pausable sql.NullBool `db:"pausable" json:"pausable"`
ParentIds []int32 `db:"parent_ids" json:"parent_ids"`
QuestionsCount sql.NullInt32 `db:"questions_count" json:"questions_count"`
Qid uuid.NullUUID `db:"qid" json:"qid"`
}
type InsertQuizRow struct {
ID int64 `db:"id" json:"id"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
Qid uuid.NullUUID `db:"qid" json:"qid"`
}
func (q *Queries) InsertQuiz(ctx context.Context, arg InsertQuizParams) (InsertQuizRow, error) {
row := q.db.QueryRowContext(ctx, insertQuiz,
arg.Accountid,
arg.Fingerprinting,
arg.Repeatable,
arg.NotePrevented,
arg.MailNotifications,
arg.UniqueAnswers,
arg.Super,
arg.GroupID,
arg.Name,
arg.Description,
arg.Config,
arg.Status,
arg.LimitAnswers,
arg.DueTo,
arg.TimeOfPassing,
arg.Pausable,
pq.Array(arg.ParentIds),
arg.QuestionsCount,
arg.Qid,
)
var i InsertQuizRow
err := row.Scan(
&i.ID,
&i.CreatedAt,
&i.UpdatedAt,
&i.Qid,
)
return i, err
}
const moveToHistory = `-- name: MoveToHistory :one
INSERT INTO question(
quiz_id, title, description, questiontype, required,
page, content, version, parent_ids, deleted
)
SELECT quiz_id, title, description, questiontype, required,
page, content, version, parent_ids, true as deleted
FROM question WHERE question.id=$1
RETURNING question.id, quiz_id, parent_ids
`
type MoveToHistoryRow struct {
ID int64 `db:"id" json:"id"`
QuizID int64 `db:"quiz_id" json:"quiz_id"`
ParentIds []int32 `db:"parent_ids" json:"parent_ids"`
}
func (q *Queries) MoveToHistory(ctx context.Context, id int64) (MoveToHistoryRow, error) {
row := q.db.QueryRowContext(ctx, moveToHistory, id)
var i MoveToHistoryRow
err := row.Scan(&i.ID, &i.QuizID, pq.Array(&i.ParentIds))
return i, err
}
const moveToHistoryQuiz = `-- name: MoveToHistoryQuiz :one
INSERT INTO quiz(deleted,
accountid, archived,fingerprinting,repeatable,note_prevented,mail_notifications,unique_answers,name,description,config,
status,limit_answers,due_to,time_of_passing,pausable,version,version_comment,parent_ids,questions_count,answers_count,average_time_passing, super, group_id
)
SELECT true as deleted, accountid, archived,fingerprinting,repeatable,note_prevented,mail_notifications,unique_answers,name,description,config,
status,limit_answers,due_to,time_of_passing,pausable,version,version_comment,parent_ids,questions_count,answers_count,average_time_passing, super, group_id
FROM quiz WHERE quiz.id=$1 AND quiz.accountid=$2
RETURNING quiz.id, qid, parent_ids
`
type MoveToHistoryQuizParams struct {
ID int64 `db:"id" json:"id"`
Accountid string `db:"accountid" json:"accountid"`
}
type MoveToHistoryQuizRow struct {
ID int64 `db:"id" json:"id"`
Qid uuid.NullUUID `db:"qid" json:"qid"`
ParentIds []int32 `db:"parent_ids" json:"parent_ids"`
}
func (q *Queries) MoveToHistoryQuiz(ctx context.Context, arg MoveToHistoryQuizParams) (MoveToHistoryQuizRow, error) {
row := q.db.QueryRowContext(ctx, moveToHistoryQuiz, arg.ID, arg.Accountid)
var i MoveToHistoryQuizRow
err := row.Scan(&i.ID, &i.Qid, pq.Array(&i.ParentIds))
return i, err
}
const questionsStatistics = `-- name: QuestionsStatistics :many
WITH Funnel AS (
SELECT
COUNT(DISTINCT a.session) FILTER (WHERE a.start = FALSE) AS count_start_false,
COUNT(DISTINCT a.session) FILTER (WHERE a.start = TRUE) AS count_start_true,
COUNT(DISTINCT CASE WHEN a.result = FALSE AND qid_true_result IS NOT NULL THEN a.session END) AS count_f_result_with_t_question,
COUNT(DISTINCT a.session) FILTER (WHERE a.result = TRUE) AS count_t_result
FROM
answer a
LEFT JOIN (
SELECT DISTINCT a.session, q.id AS qid_true_result
FROM answer a
JOIN question q ON a.question_id = q.id
WHERE a.result = TRUE
) AS q ON a.session = q.session
WHERE
a.quiz_id = $1
AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3)
),
TotalAnswers AS (
SELECT
q.id AS question_id,
COUNT(*) AS total_answers
FROM
question q
JOIN answer a ON q.id = a.question_id
WHERE
a.quiz_id = $1
AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3)
GROUP BY
q.id
),
Results AS (
SELECT
q.title AS question_title,
ta.total_answers,
CAST(COUNT(*) * 100.0 / NULLIF(ta.total_answers, 0) AS INTEGER) AS percentage
FROM
answer a
JOIN
question q ON a.question_id = q.id
JOIN
TotalAnswers ta ON q.id = ta.question_id
WHERE
a.quiz_id = $1
AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3)
AND a.result = TRUE
GROUP BY
q.id, q.title, ta.total_answers
HAVING
COUNT(*) >= 1
),
Questions AS (
SELECT
q.title AS question_title,
a.content AS answer_content,
CAST(COUNT(*) * 100.0 / NULLIF(q.total_answers, 0) AS INTEGER) AS percentage
FROM
answer a
JOIN (
SELECT
q.id,
q.title,
a.content,
COUNT(*) AS total_answers
FROM
question q
JOIN
answer a ON q.id = a.question_id
WHERE
a.quiz_id = $1
AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3)
GROUP BY
q.id, q.title, a.content
) q ON a.question_id = q.id
WHERE
a.quiz_id = $1
AND a.created_at >= TO_TIMESTAMP($2)
AND a.created_at <= TO_TIMESTAMP($3)
GROUP BY
q.title, a.content, q.total_answers
HAVING
COUNT(*) >= 1
)
SELECT
Funnel.count_start_false,
Funnel.count_start_true,
Funnel.count_f_result_with_t_question,
Funnel.count_t_result,
Results.question_title AS results_title,
Results.percentage AS results_percentage,
Questions.question_title AS questions_title,
Questions.answer_content AS answer_content,
Questions.percentage AS questions_percentage
FROM
Funnel,
Results,
Questions
WHERE
Questions.percentage >= 1
`
type QuestionsStatisticsParams struct {
QuizID int64 `db:"quiz_id" json:"quiz_id"`
ToTimestamp float64 `db:"to_timestamp" json:"to_timestamp"`
ToTimestamp_2 float64 `db:"to_timestamp_2" json:"to_timestamp_2"`
}
type QuestionsStatisticsRow struct {
CountStartFalse int64 `db:"count_start_false" json:"count_start_false"`
CountStartTrue int64 `db:"count_start_true" json:"count_start_true"`
CountFResultWithTQuestion int64 `db:"count_f_result_with_t_question" json:"count_f_result_with_t_question"`
CountTResult int64 `db:"count_t_result" json:"count_t_result"`
ResultsTitle string `db:"results_title" json:"results_title"`
ResultsPercentage int32 `db:"results_percentage" json:"results_percentage"`
QuestionsTitle string `db:"questions_title" json:"questions_title"`
AnswerContent sql.NullString `db:"answer_content" json:"answer_content"`
QuestionsPercentage int32 `db:"questions_percentage" json:"questions_percentage"`
}
func (q *Queries) QuestionsStatistics(ctx context.Context, arg QuestionsStatisticsParams) ([]QuestionsStatisticsRow, error) {
rows, err := q.db.QueryContext(ctx, questionsStatistics, arg.QuizID, arg.ToTimestamp, arg.ToTimestamp_2)
if err != nil {
return nil, err
}
defer rows.Close()
var items []QuestionsStatisticsRow
for rows.Next() {
var i QuestionsStatisticsRow
if err := rows.Scan(
&i.CountStartFalse,
&i.CountStartTrue,
&i.CountFResultWithTQuestion,
&i.CountTResult,
&i.ResultsTitle,
&i.ResultsPercentage,
&i.QuestionsTitle,
&i.AnswerContent,
&i.QuestionsPercentage,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const softDeleteResultByID = `-- name: SoftDeleteResultByID :exec
UPDATE answer SET deleted = TRUE WHERE id = $1 AND deleted = FALSE
`
func (q *Queries) SoftDeleteResultByID(ctx context.Context, id int64) error {
_, err := q.db.ExecContext(ctx, softDeleteResultByID, id)
return err
}
const updatePrivilege = `-- name: UpdatePrivilege :exec
UPDATE privileges SET amount = $1, created_at = $2 WHERE account_id = $3 AND privilegeID = $4
`
type UpdatePrivilegeParams struct {
Amount sql.NullInt32 `db:"amount" json:"amount"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
AccountID uuid.NullUUID `db:"account_id" json:"account_id"`
Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"`
}
func (q *Queries) UpdatePrivilege(ctx context.Context, arg UpdatePrivilegeParams) error {
_, err := q.db.ExecContext(ctx, updatePrivilege,
arg.Amount,
arg.CreatedAt,
arg.AccountID,
arg.Privilegeid,
)
return err
}
const updatePrivilegeAmount = `-- name: UpdatePrivilegeAmount :exec
UPDATE privileges SET amount = $1 WHERE id = $2
`
type UpdatePrivilegeAmountParams struct {
Amount sql.NullInt32 `db:"amount" json:"amount"`
ID int32 `db:"id" json:"id"`
}
func (q *Queries) UpdatePrivilegeAmount(ctx context.Context, arg UpdatePrivilegeAmountParams) error {
_, err := q.db.ExecContext(ctx, updatePrivilegeAmount, arg.Amount, arg.ID)
return err
}
const workerStatProcess = `-- name: WorkerStatProcess :exec
WITH answer_aggregates AS (
SELECT
quiz_id,
COUNT(DISTINCT session) AS unique_true_answers_count
FROM
answer
WHERE
result = TRUE
GROUP BY
quiz_id
),
question_aggregates AS (
SELECT
q.id AS quiz_id,
COUNT(qs.id) AS total_questions
FROM
quiz q
INNER JOIN
question qs ON q.id = qs.quiz_id
WHERE
q.deleted = false
AND q.archived = false
AND qs.deleted = false
GROUP BY
q.id
),
session_times_aggregates AS (
SELECT
quiz_id, COUNT(session) as sess,
AVG(extract(epoch FROM session_time)) AS average_session_time
FROM (
SELECT
quiz_id,
session,
(MAX(created_at) - MIN(created_at)) AS session_time
FROM
answer
GROUP BY
quiz_id,
session
) AS all_sessions
GROUP BY
quiz_id
)
UPDATE quiz q
SET
questions_count = COALESCE(qa.total_questions, 0),
answers_count = COALESCE(aa.unique_true_answers_count, 0),
average_time_passing = COALESCE(sta.average_session_time, 0),
sessions_count = COALESCE(sta.sess,0)
FROM
(SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz WHERE deleted = FALSE AND archived = FALSE) q_sub
LEFT JOIN answer_aggregates aa ON q_sub.id = aa.quiz_id
LEFT JOIN question_aggregates qa ON q_sub.id = qa.quiz_id
LEFT JOIN session_times_aggregates sta ON q_sub.id = sta.quiz_id
WHERE
q.id = q_sub.id
`
func (q *Queries) WorkerStatProcess(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, workerStatProcess)
return err
}
const workerTimeoutProcess = `-- name: WorkerTimeoutProcess :exec
UPDATE quiz SET status = 'timeout' WHERE deleted = false AND due_to <> 0 AND due_to < EXTRACT(epoch FROM CURRENT_TIMESTAMP)
`
func (q *Queries) WorkerTimeoutProcess(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, workerTimeoutProcess)
return err
}