# Code generated by sqlc. DO NOT EDIT.
# versions:
#   sqlc v1.29.0
# source: release_scheduler_lock.sql
import dataclasses
from typing import Optional

import sqlalchemy
import sqlalchemy.ext.asyncio

from . import models


RELEASE_SCHEDULER_LOCK = """-- name: release_scheduler_lock \\:one
UPDATE hyrex_scheduler_lock
SET is_active    = false,
    release_at   = now(),
    heartbeat_at = now()
WHERE lockid = 1
  AND worker_name = :p1
RETURNING lockid
"""


@dataclasses.dataclass()
class ReleaseSchedulerLockParams:
    worker_name: str


class Querier:
    def __init__(self, conn: sqlalchemy.engine.Connection):
        self._conn = conn

    def release_scheduler_lock(self, arg: ReleaseSchedulerLockParams) -> Optional[int]:
        row = self._conn.execute(sqlalchemy.text(RELEASE_SCHEDULER_LOCK), {"p1": arg.worker_name}).first()
        if row is None:
            return None
        return row[0]


class AsyncQuerier:
    def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
        self._conn = conn

    async def release_scheduler_lock(self, arg: ReleaseSchedulerLockParams) -> Optional[int]:
        row = (await self._conn.execute(sqlalchemy.text(RELEASE_SCHEDULER_LOCK), {"p1": arg.worker_name})).first()
        if row is None:
            return None
        return row[0]
