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

import sqlalchemy
import sqlalchemy.ext.asyncio

from mcpuniverse.app.db.sqlc import models


CREATE_RELEASED_BENCHMARK = """-- name: create_released_benchmark \\:one
INSERT INTO "released_benchmark" (owner_id,
                                  name,
                                  tag,
                                  description)
VALUES (:p1, :p2, :p3, :p4)
RETURNING id, owner_id, name, tag, description, created_at
"""


DELETE_RELEASED_BENCHMARK = """-- name: delete_released_benchmark \\:exec
DELETE
FROM "released_benchmark"
WHERE owner_id = :p1 AND name = :p2 AND tag = :p3
"""


GET_RELEASED_BENCHMARK_BY_ID = """-- name: get_released_benchmark_by_id \\:one
SELECT id, owner_id, name, tag, description, created_at
FROM "released_benchmark"
WHERE id = :p1
LIMIT 1
"""


GET_RELEASED_BENCHMARK_BY_NAME = """-- name: get_released_benchmark_by_name \\:many
SELECT id, owner_id, name, tag, description, created_at
FROM "released_benchmark"
WHERE owner_id = :p1 AND name = :p2
"""


GET_RELEASED_BENCHMARK_BY_NAME_AND_TAG = """-- name: get_released_benchmark_by_name_and_tag \\:one
SELECT id, owner_id, name, tag, description, created_at
FROM "released_benchmark"
WHERE owner_id = :p1 AND name = :p2 AND tag = :p3
LIMIT 1
"""


GET_RELEASED_BENCHMARK_ID = """-- name: get_released_benchmark_id \\:one
SELECT id
FROM "released_benchmark"
WHERE owner_id = :p1 AND name = :p2 AND tag = :p3
LIMIT 1
"""


LIST_RELEASED_BENCHMARKS = """-- name: list_released_benchmarks \\:many
SELECT DISTINCT owner_id, name
FROM "released_benchmark"
LIMIT :p1
OFFSET :p2
"""


@dataclasses.dataclass()
class ListReleasedBenchmarksRow:
    owner_id: int
    name: str


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

    def create_released_benchmark(self, *, owner_id: int, name: str, tag: str, description: Optional[str]) -> Optional[models.ReleasedBenchmark]:
        row = self._conn.execute(sqlalchemy.text(CREATE_RELEASED_BENCHMARK), {
            "p1": owner_id,
            "p2": name,
            "p3": tag,
            "p4": description,
        }).first()
        if row is None:
            return None
        return models.ReleasedBenchmark(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            created_at=row[5],
        )

    def delete_released_benchmark(self, *, owner_id: int, name: str, tag: str) -> None:
        self._conn.execute(sqlalchemy.text(DELETE_RELEASED_BENCHMARK), {"p1": owner_id, "p2": name, "p3": tag})

    def get_released_benchmark_by_id(self, *, id: int) -> Optional[models.ReleasedBenchmark]:
        row = self._conn.execute(sqlalchemy.text(GET_RELEASED_BENCHMARK_BY_ID), {"p1": id}).first()
        if row is None:
            return None
        return models.ReleasedBenchmark(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            created_at=row[5],
        )

    def get_released_benchmark_by_name(self, *, owner_id: int, name: str) -> Iterator[models.ReleasedBenchmark]:
        result = self._conn.execute(sqlalchemy.text(GET_RELEASED_BENCHMARK_BY_NAME), {"p1": owner_id, "p2": name})
        for row in result:
            yield models.ReleasedBenchmark(
                id=row[0],
                owner_id=row[1],
                name=row[2],
                tag=row[3],
                description=row[4],
                created_at=row[5],
            )

    def get_released_benchmark_by_name_and_tag(self, *, owner_id: int, name: str, tag: str) -> Optional[models.ReleasedBenchmark]:
        row = self._conn.execute(sqlalchemy.text(GET_RELEASED_BENCHMARK_BY_NAME_AND_TAG), {"p1": owner_id, "p2": name, "p3": tag}).first()
        if row is None:
            return None
        return models.ReleasedBenchmark(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            created_at=row[5],
        )

    def get_released_benchmark_id(self, *, owner_id: int, name: str, tag: str) -> Optional[int]:
        row = self._conn.execute(sqlalchemy.text(GET_RELEASED_BENCHMARK_ID), {"p1": owner_id, "p2": name, "p3": tag}).first()
        if row is None:
            return None
        return row[0]

    def list_released_benchmarks(self, *, limit: int, offset: int) -> Iterator[ListReleasedBenchmarksRow]:
        result = self._conn.execute(sqlalchemy.text(LIST_RELEASED_BENCHMARKS), {"p1": limit, "p2": offset})
        for row in result:
            yield ListReleasedBenchmarksRow(
                owner_id=row[0],
                name=row[1],
            )


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

    async def create_released_benchmark(self, *, owner_id: int, name: str, tag: str, description: Optional[str]) -> Optional[models.ReleasedBenchmark]:
        row = (await self._conn.execute(sqlalchemy.text(CREATE_RELEASED_BENCHMARK), {
            "p1": owner_id,
            "p2": name,
            "p3": tag,
            "p4": description,
        })).first()
        if row is None:
            return None
        return models.ReleasedBenchmark(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            created_at=row[5],
        )

    async def delete_released_benchmark(self, *, owner_id: int, name: str, tag: str) -> None:
        await self._conn.execute(sqlalchemy.text(DELETE_RELEASED_BENCHMARK), {"p1": owner_id, "p2": name, "p3": tag})

    async def get_released_benchmark_by_id(self, *, id: int) -> Optional[models.ReleasedBenchmark]:
        row = (await self._conn.execute(sqlalchemy.text(GET_RELEASED_BENCHMARK_BY_ID), {"p1": id})).first()
        if row is None:
            return None
        return models.ReleasedBenchmark(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            created_at=row[5],
        )

    async def get_released_benchmark_by_name(self, *, owner_id: int, name: str) -> AsyncIterator[models.ReleasedBenchmark]:
        result = await self._conn.stream(sqlalchemy.text(GET_RELEASED_BENCHMARK_BY_NAME), {"p1": owner_id, "p2": name})
        async for row in result:
            yield models.ReleasedBenchmark(
                id=row[0],
                owner_id=row[1],
                name=row[2],
                tag=row[3],
                description=row[4],
                created_at=row[5],
            )

    async def get_released_benchmark_by_name_and_tag(self, *, owner_id: int, name: str, tag: str) -> Optional[models.ReleasedBenchmark]:
        row = (await self._conn.execute(sqlalchemy.text(GET_RELEASED_BENCHMARK_BY_NAME_AND_TAG), {"p1": owner_id, "p2": name, "p3": tag})).first()
        if row is None:
            return None
        return models.ReleasedBenchmark(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            created_at=row[5],
        )

    async def get_released_benchmark_id(self, *, owner_id: int, name: str, tag: str) -> Optional[int]:
        row = (await self._conn.execute(sqlalchemy.text(GET_RELEASED_BENCHMARK_ID), {"p1": owner_id, "p2": name, "p3": tag})).first()
        if row is None:
            return None
        return row[0]

    async def list_released_benchmarks(self, *, limit: int, offset: int) -> AsyncIterator[ListReleasedBenchmarksRow]:
        result = await self._conn.stream(sqlalchemy.text(LIST_RELEASED_BENCHMARKS), {"p1": limit, "p2": offset})
        async for row in result:
            yield ListReleasedBenchmarksRow(
                owner_id=row[0],
                name=row[1],
            )
