Files
gochat/channels/shangwutong/db/generated/inbound.sql.go
T

442 lines
14 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: inbound.sql
package dbgen
import (
"context"
"time"
)
const claimInboundEvent = `-- name: ClaimInboundEvent :one
UPDATE inbound_events SET
delivery_status = 'delivering',
attempts = attempts + 1,
updated_at = CURRENT_TIMESTAMP
WHERE id = (
SELECT candidate.id
FROM inbound_events AS candidate
WHERE candidate.delivery_status = 'pending'
AND (candidate.next_attempt_at IS NULL OR julianday(candidate.next_attempt_at) IS NULL OR julianday(candidate.next_attempt_at) <= julianday('now'))
AND NOT EXISTS (
SELECT 1 FROM inbound_events AS earlier
WHERE earlier.account_id = candidate.account_id
AND earlier.swt_sid = candidate.swt_sid
AND earlier.id < candidate.id
AND earlier.delivery_status IN ('pending', 'delivering')
)
ORDER BY candidate.id
LIMIT 1
)
AND delivery_status = 'pending'
RETURNING id, account_id, swt_sid, seq_id, kind, swt_event_key, event_subtype, op_name, text, swt_timestamp, raw_line, normalized_payload, mapping_strategy, delivery_status, attempts, next_attempt_at, gochat_message_id, last_error, created_at, updated_at
`
func (q *Queries) ClaimInboundEvent(ctx context.Context) (*InboundEvent, error) {
row := q.db.QueryRowContext(ctx, claimInboundEvent)
var i InboundEvent
err := row.Scan(
&i.ID,
&i.AccountID,
&i.SwtSid,
&i.SeqID,
&i.Kind,
&i.SwtEventKey,
&i.EventSubtype,
&i.OpName,
&i.Text,
&i.SwtTimestamp,
&i.RawLine,
&i.NormalizedPayload,
&i.MappingStrategy,
&i.DeliveryStatus,
&i.Attempts,
&i.NextAttemptAt,
&i.GochatMessageID,
&i.LastError,
&i.CreatedAt,
&i.UpdatedAt,
)
return &i, err
}
const completeInboundEvent = `-- name: CompleteInboundEvent :exec
UPDATE inbound_events SET
delivery_status = 'delivered',
gochat_message_id = ?,
last_error = NULL,
updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND delivery_status = 'delivering'
`
type CompleteInboundEventParams struct {
GochatMessageID *int64 `json:"gochat_message_id"`
ID int64 `json:"id"`
}
func (q *Queries) CompleteInboundEvent(ctx context.Context, arg CompleteInboundEventParams) error {
_, err := q.db.ExecContext(ctx, completeInboundEvent, arg.GochatMessageID, arg.ID)
return err
}
const completeInboundEventWithMapping = `-- name: CompleteInboundEventWithMapping :exec
UPDATE inbound_events SET
delivery_status = 'delivered',
gochat_message_id = ?1,
event_subtype = ?2,
normalized_payload = ?3,
mapping_strategy = ?4,
last_error = NULL,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?5 AND delivery_status = 'delivering'
`
type CompleteInboundEventWithMappingParams struct {
GochatMessageID *int64 `json:"gochat_message_id"`
EventSubtype *string `json:"event_subtype"`
NormalizedPayload *string `json:"normalized_payload"`
MappingStrategy *string `json:"mapping_strategy"`
ID int64 `json:"id"`
}
func (q *Queries) CompleteInboundEventWithMapping(ctx context.Context, arg CompleteInboundEventWithMappingParams) error {
_, err := q.db.ExecContext(ctx, completeInboundEventWithMapping,
arg.GochatMessageID,
arg.EventSubtype,
arg.NormalizedPayload,
arg.MappingStrategy,
arg.ID,
)
return err
}
const failInboundEvent = `-- name: FailInboundEvent :exec
UPDATE inbound_events SET
delivery_status = 'failed',
mapping_strategy = ?1,
last_error = ?2,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?3 AND delivery_status = 'delivering'
`
type FailInboundEventParams struct {
MappingStrategy *string `json:"mapping_strategy"`
LastError *string `json:"last_error"`
ID int64 `json:"id"`
}
func (q *Queries) FailInboundEvent(ctx context.Context, arg FailInboundEventParams) error {
_, err := q.db.ExecContext(ctx, failInboundEvent, arg.MappingStrategy, arg.LastError, arg.ID)
return err
}
const getConversationMap = `-- name: GetConversationMap :one
SELECT account_id, swt_sid, gochat_contact_source_id, gochat_contact_id, gochat_conversation_id, gochat_display_id, swt_assignee_name, created_at, updated_at FROM conversation_maps
WHERE account_id = ? AND swt_sid = ?
LIMIT 1
`
type GetConversationMapParams struct {
AccountID int64 `json:"account_id"`
SwtSid string `json:"swt_sid"`
}
func (q *Queries) GetConversationMap(ctx context.Context, arg GetConversationMapParams) (*ConversationMap, error) {
row := q.db.QueryRowContext(ctx, getConversationMap, arg.AccountID, arg.SwtSid)
var i ConversationMap
err := row.Scan(
&i.AccountID,
&i.SwtSid,
&i.GochatContactSourceID,
&i.GochatContactID,
&i.GochatConversationID,
&i.GochatDisplayID,
&i.SwtAssigneeName,
&i.CreatedAt,
&i.UpdatedAt,
)
return &i, err
}
const getInboundEventByKey = `-- name: GetInboundEventByKey :one
SELECT id, account_id, swt_sid, seq_id, kind, swt_event_key, event_subtype, op_name, text, swt_timestamp, raw_line, normalized_payload, mapping_strategy, delivery_status, attempts, next_attempt_at, gochat_message_id, last_error, created_at, updated_at FROM inbound_events
WHERE swt_event_key = ?
LIMIT 1
`
func (q *Queries) GetInboundEventByKey(ctx context.Context, swtEventKey string) (*InboundEvent, error) {
row := q.db.QueryRowContext(ctx, getInboundEventByKey, swtEventKey)
var i InboundEvent
err := row.Scan(
&i.ID,
&i.AccountID,
&i.SwtSid,
&i.SeqID,
&i.Kind,
&i.SwtEventKey,
&i.EventSubtype,
&i.OpName,
&i.Text,
&i.SwtTimestamp,
&i.RawLine,
&i.NormalizedPayload,
&i.MappingStrategy,
&i.DeliveryStatus,
&i.Attempts,
&i.NextAttemptAt,
&i.GochatMessageID,
&i.LastError,
&i.CreatedAt,
&i.UpdatedAt,
)
return &i, err
}
const getMessageMapBySWTMessageID = `-- name: GetMessageMapBySWTMessageID :one
SELECT account_id, swt_sid, swt_message_id, swt_seq_id, kind, child_index, direction, gochat_message_id, gochat_source_id, content_fingerprint, retracted_at, created_at, updated_at FROM message_maps
WHERE account_id = ? AND swt_sid = ? AND swt_message_id = ?
LIMIT 1
`
type GetMessageMapBySWTMessageIDParams struct {
AccountID int64 `json:"account_id"`
SwtSid string `json:"swt_sid"`
SwtMessageID *string `json:"swt_message_id"`
}
func (q *Queries) GetMessageMapBySWTMessageID(ctx context.Context, arg GetMessageMapBySWTMessageIDParams) (*MessageMap, error) {
row := q.db.QueryRowContext(ctx, getMessageMapBySWTMessageID, arg.AccountID, arg.SwtSid, arg.SwtMessageID)
var i MessageMap
err := row.Scan(
&i.AccountID,
&i.SwtSid,
&i.SwtMessageID,
&i.SwtSeqID,
&i.Kind,
&i.ChildIndex,
&i.Direction,
&i.GochatMessageID,
&i.GochatSourceID,
&i.ContentFingerprint,
&i.RetractedAt,
&i.CreatedAt,
&i.UpdatedAt,
)
return &i, err
}
const insertInboundEvent = `-- name: InsertInboundEvent :one
INSERT INTO inbound_events (
account_id, swt_sid, seq_id, kind, swt_event_key,
event_subtype, op_name, text, swt_timestamp, raw_line,
normalized_payload, mapping_strategy, delivery_status
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(swt_event_key) DO NOTHING
RETURNING id, account_id, swt_sid, seq_id, kind, swt_event_key, event_subtype, op_name, text, swt_timestamp, raw_line, normalized_payload, mapping_strategy, delivery_status, attempts, next_attempt_at, gochat_message_id, last_error, created_at, updated_at
`
type InsertInboundEventParams struct {
AccountID int64 `json:"account_id"`
SwtSid string `json:"swt_sid"`
SeqID int64 `json:"seq_id"`
Kind int64 `json:"kind"`
SwtEventKey string `json:"swt_event_key"`
EventSubtype *string `json:"event_subtype"`
OpName *string `json:"op_name"`
Text *string `json:"text"`
SwtTimestamp *string `json:"swt_timestamp"`
RawLine string `json:"raw_line"`
NormalizedPayload *string `json:"normalized_payload"`
MappingStrategy *string `json:"mapping_strategy"`
DeliveryStatus string `json:"delivery_status"`
}
func (q *Queries) InsertInboundEvent(ctx context.Context, arg InsertInboundEventParams) (*InboundEvent, error) {
row := q.db.QueryRowContext(ctx, insertInboundEvent,
arg.AccountID,
arg.SwtSid,
arg.SeqID,
arg.Kind,
arg.SwtEventKey,
arg.EventSubtype,
arg.OpName,
arg.Text,
arg.SwtTimestamp,
arg.RawLine,
arg.NormalizedPayload,
arg.MappingStrategy,
arg.DeliveryStatus,
)
var i InboundEvent
err := row.Scan(
&i.ID,
&i.AccountID,
&i.SwtSid,
&i.SeqID,
&i.Kind,
&i.SwtEventKey,
&i.EventSubtype,
&i.OpName,
&i.Text,
&i.SwtTimestamp,
&i.RawLine,
&i.NormalizedPayload,
&i.MappingStrategy,
&i.DeliveryStatus,
&i.Attempts,
&i.NextAttemptAt,
&i.GochatMessageID,
&i.LastError,
&i.CreatedAt,
&i.UpdatedAt,
)
return &i, err
}
const insertMessageMap = `-- name: InsertMessageMap :exec
INSERT INTO message_maps (
account_id, swt_sid, swt_message_id, swt_seq_id, kind, child_index,
direction, gochat_message_id, gochat_source_id, content_fingerprint
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(account_id, swt_sid, kind, swt_seq_id, child_index) DO UPDATE SET
swt_message_id = COALESCE(excluded.swt_message_id, message_maps.swt_message_id),
gochat_message_id = excluded.gochat_message_id,
gochat_source_id = COALESCE(excluded.gochat_source_id, message_maps.gochat_source_id),
content_fingerprint = COALESCE(excluded.content_fingerprint, message_maps.content_fingerprint),
updated_at = CURRENT_TIMESTAMP
`
type InsertMessageMapParams struct {
AccountID int64 `json:"account_id"`
SwtSid string `json:"swt_sid"`
SwtMessageID *string `json:"swt_message_id"`
SwtSeqID int64 `json:"swt_seq_id"`
Kind int64 `json:"kind"`
ChildIndex int64 `json:"child_index"`
Direction string `json:"direction"`
GochatMessageID int64 `json:"gochat_message_id"`
GochatSourceID *string `json:"gochat_source_id"`
ContentFingerprint *string `json:"content_fingerprint"`
}
func (q *Queries) InsertMessageMap(ctx context.Context, arg InsertMessageMapParams) error {
_, err := q.db.ExecContext(ctx, insertMessageMap,
arg.AccountID,
arg.SwtSid,
arg.SwtMessageID,
arg.SwtSeqID,
arg.Kind,
arg.ChildIndex,
arg.Direction,
arg.GochatMessageID,
arg.GochatSourceID,
arg.ContentFingerprint,
)
return err
}
const markMessageMapRetracted = `-- name: MarkMessageMapRetracted :exec
UPDATE message_maps SET
retracted_at = CURRENT_TIMESTAMP,
updated_at = CURRENT_TIMESTAMP
WHERE account_id = ? AND swt_sid = ? AND swt_message_id = ?
`
type MarkMessageMapRetractedParams struct {
AccountID int64 `json:"account_id"`
SwtSid string `json:"swt_sid"`
SwtMessageID *string `json:"swt_message_id"`
}
func (q *Queries) MarkMessageMapRetracted(ctx context.Context, arg MarkMessageMapRetractedParams) error {
_, err := q.db.ExecContext(ctx, markMessageMapRetracted, arg.AccountID, arg.SwtSid, arg.SwtMessageID)
return err
}
const recoverInboundDeliveries = `-- name: RecoverInboundDeliveries :execrows
UPDATE inbound_events SET
delivery_status = 'pending',
updated_at = CURRENT_TIMESTAMP
WHERE delivery_status = 'delivering'
`
func (q *Queries) RecoverInboundDeliveries(ctx context.Context) (int64, error) {
result, err := q.db.ExecContext(ctx, recoverInboundDeliveries)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
const retryInboundEvent = `-- name: RetryInboundEvent :exec
UPDATE inbound_events SET
delivery_status = 'pending',
next_attempt_at = ?,
last_error = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND delivery_status = 'delivering'
`
type RetryInboundEventParams struct {
NextAttemptAt *time.Time `json:"next_attempt_at"`
LastError *string `json:"last_error"`
ID int64 `json:"id"`
}
func (q *Queries) RetryInboundEvent(ctx context.Context, arg RetryInboundEventParams) error {
_, err := q.db.ExecContext(ctx, retryInboundEvent, arg.NextAttemptAt, arg.LastError, arg.ID)
return err
}
const upsertConversationMap = `-- name: UpsertConversationMap :one
INSERT INTO conversation_maps (
account_id, swt_sid, gochat_contact_source_id, gochat_contact_id,
gochat_conversation_id, gochat_display_id, swt_assignee_name
) VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(account_id, swt_sid) DO UPDATE SET
gochat_contact_source_id = excluded.gochat_contact_source_id,
gochat_contact_id = COALESCE(excluded.gochat_contact_id, conversation_maps.gochat_contact_id),
gochat_conversation_id = COALESCE(excluded.gochat_conversation_id, conversation_maps.gochat_conversation_id),
gochat_display_id = COALESCE(excluded.gochat_display_id, conversation_maps.gochat_display_id),
swt_assignee_name = COALESCE(excluded.swt_assignee_name, conversation_maps.swt_assignee_name),
updated_at = CURRENT_TIMESTAMP
RETURNING account_id, swt_sid, gochat_contact_source_id, gochat_contact_id, gochat_conversation_id, gochat_display_id, swt_assignee_name, created_at, updated_at
`
type UpsertConversationMapParams struct {
AccountID int64 `json:"account_id"`
SwtSid string `json:"swt_sid"`
GochatContactSourceID string `json:"gochat_contact_source_id"`
GochatContactID *int64 `json:"gochat_contact_id"`
GochatConversationID *int64 `json:"gochat_conversation_id"`
GochatDisplayID *int64 `json:"gochat_display_id"`
SwtAssigneeName *string `json:"swt_assignee_name"`
}
func (q *Queries) UpsertConversationMap(ctx context.Context, arg UpsertConversationMapParams) (*ConversationMap, error) {
row := q.db.QueryRowContext(ctx, upsertConversationMap,
arg.AccountID,
arg.SwtSid,
arg.GochatContactSourceID,
arg.GochatContactID,
arg.GochatConversationID,
arg.GochatDisplayID,
arg.SwtAssigneeName,
)
var i ConversationMap
err := row.Scan(
&i.AccountID,
&i.SwtSid,
&i.GochatContactSourceID,
&i.GochatContactID,
&i.GochatConversationID,
&i.GochatDisplayID,
&i.SwtAssigneeName,
&i.CreatedAt,
&i.UpdatedAt,
)
return &i, err
}