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

import sqlalchemy
import sqlalchemy.ext.asyncio

from mcpuniverse.app.db.sqlc import models


CREATE_BENCHMARK = """-- name: create_benchmark \\:one
INSERT INTO "benchmark" (owner_id,
                         name,
                         description)
VALUES (:p1, :p2, :p3)
RETURNING id, owner_id, name, description, created_at, updated_at
"""


DELETE_BENCHMARK = """-- name: delete_benchmark \\:exec
DELETE
FROM "benchmark"
WHERE owner_id = :p1 AND name = :p2
"""


GET_BENCHMARK_BY_ID = """-- name: get_benchmark_by_id \\:one
SELECT id, owner_id, name, description, created_at, updated_at
FROM "benchmark"
WHERE id = :p1
LIMIT 1
"""


GET_BENCHMARK_BY_NAME = """-- name: get_benchmark_by_name \\:one
SELECT id, owner_id, name, description, created_at, updated_at
FROM "benchmark"
WHERE owner_id = :p1 AND name = :p2
LIMIT 1
"""


GET_BENCHMARK_ID = """-- name: get_benchmark_id \\:one
SELECT id
FROM "benchmark"
WHERE owner_id = :p1 AND name = :p2
LIMIT 1
"""


UPDATE_BENCHMARK = """-- name: update_benchmark \\:one
UPDATE "benchmark"
SET description   = COALESCE(:p1, description),
    updated_at    = COALESCE(:p2, updated_at)
WHERE owner_id = :p3 AND name = :p4
RETURNING id, owner_id, name, description, created_at, updated_at
"""


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

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

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

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

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

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

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


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

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

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

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

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

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

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