-- name: InsertEvent :exec INSERT INTO events ( source_event_id, sport_id, match_name, home_team, away_team, home_team_id, away_team_id, home_kit_image, away_kit_image, league_id, league_name, start_time, is_live, status, source, default_winning_upper_limit ) VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16 ) ON CONFLICT (source_event_id, source) DO UPDATE SET sport_id = EXCLUDED.sport_id, match_name = EXCLUDED.match_name, home_team = EXCLUDED.home_team, away_team = EXCLUDED.away_team, home_team_id = EXCLUDED.home_team_id, away_team_id = EXCLUDED.away_team_id, home_kit_image = EXCLUDED.home_kit_image, away_kit_image = EXCLUDED.away_kit_image, league_id = EXCLUDED.league_id, league_name = EXCLUDED.league_name, start_time = EXCLUDED.start_time, score = EXCLUDED.score, match_minute = EXCLUDED.match_minute, timer_status = EXCLUDED.timer_status, added_time = EXCLUDED.added_time, match_period = EXCLUDED.match_period, is_live = EXCLUDED.is_live, source = EXCLUDED.source, default_winning_upper_limit = EXCLUDED.default_winning_upper_limit, fetched_at = now(); -- name: SaveTenantEventSettings :exec INSERT INTO company_event_settings ( company_id, event_id, is_active, is_featured, winning_upper_limit ) VALUES ($1, $2, $3, $4, $5) ON CONFLICT(company_id, event_id) DO UPDATE SET is_active = EXCLUDED.is_active, is_featured = EXCLUDED.is_featured, winning_upper_limit = EXCLUDED.winning_upper_limit; -- name: ListLiveEvents :many SELECT id FROM event_with_country WHERE is_live = true; -- name: GetAllEvents :many SELECT * FROM event_with_country WHERE ( is_live = sqlc.narg('is_live') OR sqlc.narg('is_live') IS NULL ) AND ( status = sqlc.narg('status') OR sqlc.narg('status') IS NULL ) AND ( league_id = sqlc.narg('league_id') OR sqlc.narg('league_id') IS NULL ) AND ( sport_id = sqlc.narg('sport_id') OR sqlc.narg('sport_id') IS NULL ) AND ( match_name ILIKE '%' || sqlc.narg('query') || '%' OR league_name ILIKE '%' || sqlc.narg('query') || '%' OR sqlc.narg('query') IS NULL ) AND ( start_time < sqlc.narg('last_start_time') OR sqlc.narg('last_start_time') IS NULL ) AND ( start_time > sqlc.narg('first_start_time') OR sqlc.narg('first_start_time') IS NULL ) AND ( league_cc = sqlc.narg('country_code') OR sqlc.narg('country_code') IS NULL ) AND ( source = sqlc.narg('source') OR sqlc.narg('source') IS NULL ) ORDER BY start_time ASC LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset'); -- name: GetTotalEvents :one SELECT COUNT(*) FROM event_with_country WHERE ( is_live = sqlc.narg('is_live') OR sqlc.narg('is_live') IS NULL ) AND ( status = sqlc.narg('status') OR sqlc.narg('status') IS NULL ) AND ( league_id = sqlc.narg('league_id') OR sqlc.narg('league_id') IS NULL ) AND ( sport_id = sqlc.narg('sport_id') OR sqlc.narg('sport_id') IS NULL ) AND ( match_name ILIKE '%' || sqlc.narg('query') || '%' OR league_name ILIKE '%' || sqlc.narg('query') || '%' OR sqlc.narg('query') IS NULL ) AND ( start_time < sqlc.narg('last_start_time') OR sqlc.narg('last_start_time') IS NULL ) AND ( start_time > sqlc.narg('first_start_time') OR sqlc.narg('first_start_time') IS NULL ) AND ( league_cc = sqlc.narg('country_code') OR sqlc.narg('country_code') IS NULL ) AND ( source = sqlc.narg('source') OR sqlc.narg('source') IS NULL ); -- name: GetTotalCompanyEvents :one SELECT COUNT(*) FROM events e LEFT JOIN company_event_settings ces ON e.id = ces.event_id AND ces.company_id = $1 JOIN leagues l ON l.id = e.league_id WHERE ( is_live = sqlc.narg('is_live') OR sqlc.narg('is_live') IS NULL ) AND ( status = sqlc.narg('status') OR sqlc.narg('status') IS NULL ) AND( league_id = sqlc.narg('league_id') OR sqlc.narg('league_id') IS NULL ) AND ( e.sport_id = sqlc.narg('sport_id') OR sqlc.narg('sport_id') IS NULL ) AND ( match_name ILIKE '%' || sqlc.narg('query') || '%' OR league_name ILIKE '%' || sqlc.narg('query') || '%' OR sqlc.narg('query') IS NULL ) AND ( start_time < sqlc.narg('last_start_time') OR sqlc.narg('last_start_time') IS NULL ) AND ( start_time > sqlc.narg('first_start_time') OR sqlc.narg('first_start_time') IS NULL ) AND ( l.country_code = sqlc.narg('country_code') OR sqlc.narg('country_code') IS NULL ) AND ( ces.is_featured = sqlc.narg('is_featured') OR e.default_is_featured = sqlc.narg('is_featured') OR sqlc.narg('is_featured') IS NULL ) AND ( ces.is_active = sqlc.narg('is_active') OR e.default_is_active = sqlc.narg('is_active') OR sqlc.narg('is_active') IS NULL ) AND ( source = sqlc.narg('source') OR sqlc.narg('source') IS NULL ); -- name: GetEventsWithSettings :many SELECT e.*, ces.company_id, COALESCE(ces.is_active, e.default_is_active) AS is_active, COALESCE(ces.is_featured, e.default_is_featured) AS is_featured, COALESCE( ces.winning_upper_limit, e.default_winning_upper_limit ) AS winning_upper_limit, ces.updated_at, l.country_code as league_cc FROM events e LEFT JOIN company_event_settings ces ON e.id = ces.event_id AND ces.company_id = $1 JOIN leagues l ON l.id = e.league_id WHERE ( is_live = sqlc.narg('is_live') OR sqlc.narg('is_live') IS NULL ) AND ( status = sqlc.narg('status') OR sqlc.narg('status') IS NULL ) AND( league_id = sqlc.narg('league_id') OR sqlc.narg('league_id') IS NULL ) AND ( e.sport_id = sqlc.narg('sport_id') OR sqlc.narg('sport_id') IS NULL ) AND ( match_name ILIKE '%' || sqlc.narg('query') || '%' OR league_name ILIKE '%' || sqlc.narg('query') || '%' OR sqlc.narg('query') IS NULL ) AND ( start_time < sqlc.narg('last_start_time') OR sqlc.narg('last_start_time') IS NULL ) AND ( start_time > sqlc.narg('first_start_time') OR sqlc.narg('first_start_time') IS NULL ) AND ( l.country_code = sqlc.narg('country_code') OR sqlc.narg('country_code') IS NULL ) AND ( ces.is_featured = sqlc.narg('is_featured') OR e.default_is_featured = sqlc.narg('is_featured') OR sqlc.narg('is_featured') IS NULL ) AND ( ces.is_active = sqlc.narg('is_active') OR e.default_is_active = sqlc.narg('is_active') OR sqlc.narg('is_active') IS NULL ) AND ( source = sqlc.narg('source') OR sqlc.narg('source') IS NULL ) ORDER BY start_time ASC LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset'); -- name: GetEventByID :one SELECT * FROM event_with_country WHERE id = $1 LIMIT 1; -- name: GetEventBySourceID :one SELECT * FROM event_with_country WHERE source_event_id = $1 AND source = $2; -- name: GetEventWithSettingByID :one SELECT e.*, ces.company_id, COALESCE(ces.is_active, e.default_is_active) AS is_active, COALESCE(ces.is_featured, e.default_is_featured) AS is_featured, COALESCE( ces.winning_upper_limit, e.default_winning_upper_limit ) AS winning_upper_limit, ces.updated_at, l.country_code as league_cc FROM events e LEFT JOIN company_event_settings ces ON e.id = ces.event_id AND ces.company_id = $2 JOIN leagues l ON l.id = e.league_id WHERE e.id = $1 LIMIT 1; -- name: GetSportAndLeagueIDs :one SELECT sport_id, league_id FROM events WHERE id = $1; -- name: UpdateMatchResult :exec UPDATE events SET score = $1, status = $2 WHERE id = $3; -- name: IsEventMonitored :one SELECT is_monitored FROM events WHERE id = $1; -- name: UpdateEventMonitored :exec UPDATE events SET is_monitored = $1, updated_at = CURRENT_TIMESTAMP WHERE id = $2; -- name: UpdateGlobalEventSettings :exec UPDATE events SET default_is_active = COALESCE(sqlc.narg(default_is_active), default_is_active), default_is_featured = COALESCE( sqlc.narg(default_is_featured), default_is_featured ), default_winning_upper_limit = COALESCE( sqlc.narg(default_winning_upper_limit), default_winning_upper_limit ), updated_at = CURRENT_TIMESTAMP WHERE id = $1; -- name: DeleteEvent :exec DELETE FROM events WHERE id = $1;