# Path: irisett/sql.py
# class DBConnection:
#     def __init__(self, host: str, user: str, passwd: str, dbname: str, loop: asyncio.AbstractEventLoop=None) -> None:
#     async def initialize(self, *, only_init_tables: bool=False):
#     async def close(self) -> None:
#     async def _create_db(self) -> None:
#     async def _init_db(self, only_init_tables: bool) -> None:
#     async def _check_db_exists(self) -> bool:
#     async def _check_db_initialized(self) -> bool:
#     async def _upgrade_db(self) -> None:
#     async def _get_db_version(self) -> int:
#     async def _set_db_version(self, version: int):
#     async def fetch_all(self, query: str, args: Optional[Iterable]=None) -> List:
#     async def fetch_row(self, query: str, args: Optional[Iterable]=None) -> List:
#     async def fetch_single(self, query: str, args: Optional[Iterable]=None) -> Any:
#     async def count_rows(self, query: str, args: Optional[Iterable]=None) -> float:
#     async def operation(self, query: str, args: Optional[Iterable]=None) -> Any:
#     async def transact(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
# 
# Path: irisett/object_models.py
# def insert_values(object: Any) -> Tuple:
# def list_asdict(in_list: Iterable[Any]) -> List[Any]:
# def insert_filter(attribute: Any, value: Any) -> bool:
# class Contact:
# class ContactGroup:
# class ActiveMonitor:
# class ActiveMonitorArg:
# class ActiveMonitorAlert:
# class ActiveMonitorDef:
# class ActiveMonitorDefArg:
# class ObjectMetadata:
# class ObjectBindata:
# class MonitorGroup:


from typing import Dict, Iterable, Optional, Tuple
from irisett.sql import DBConnection, Cursor
from irisett import object_models

    If keys is given, only delete the specified keys, otherwise delete all
    metadata for the object.
    """

    async def _run(cur: Cursor) -> None:
        if keys:
            # noinspection PyTypeChecker
            for key in keys:
                q = """delete from object_metadata where object_type=%s and object_id=%s and `key`=%s"""
                q_args = (object_type, object_id, key)  # type: Tuple
                await cur.execute(q, q_args)
        else:
            q = """delete from object_metadata where object_type=%s and object_id=%s"""
            q_args = (object_type, object_id)
            await cur.execute(q, q_args)

    await dbcon.transact(_run)


async def get_metadata_for_object(
        dbcon: DBConnection, object_type: str, object_id: int) -> Iterable[object_models.ObjectMetadata]:
    """Get ObjectMetadata for a single object (type, id)

    Ie. contact, [ID]
    """
    q = """select metadata.object_type, metadata.object_id, metadata.key, metadata.value
        from object_metadata as metadata
        where metadata.object_type=%s and metadata.object_id=%s"""
    q_args = (object_type, object_id)

# Path: irisett/sql.py
# class DBConnection:
#     def __init__(self, host: str, user: str, passwd: str, dbname: str, loop: asyncio.AbstractEventLoop=None) -> None:
#     async def initialize(self, *, only_init_tables: bool=False):
#     async def close(self) -> None:
#     async def _create_db(self) -> None:
#     async def _init_db(self, only_init_tables: bool) -> None:
#     async def _check_db_exists(self) -> bool:
#     async def _check_db_initialized(self) -> bool:
#     async def _upgrade_db(self) -> None:
#     async def _get_db_version(self) -> int:
#     async def _set_db_version(self, version: int):
#     async def fetch_all(self, query: str, args: Optional[Iterable]=None) -> List:
#     async def fetch_row(self, query: str, args: Optional[Iterable]=None) -> List:
#     async def fetch_single(self, query: str, args: Optional[Iterable]=None) -> Any:
#     async def count_rows(self, query: str, args: Optional[Iterable]=None) -> float:
#     async def operation(self, query: str, args: Optional[Iterable]=None) -> Any:
#     async def transact(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
# 
# Path: irisett/object_models.py
# def insert_values(object: Any) -> Tuple:
# def list_asdict(in_list: Iterable[Any]) -> List[Any]:
# def insert_filter(attribute: Any, value: Any) -> bool:
# class Contact:
# class ContactGroup:
# class ActiveMonitor:
# class ActiveMonitorArg:
# class ActiveMonitorAlert:
# class ActiveMonitorDef:
# class ActiveMonitorDefArg:
# class ObjectMetadata:
# class ObjectBindata:
# class MonitorGroup:


# Path: irisett/metadata.py
from typing import Dict, Iterable, Optional, Tuple
from irisett.sql import DBConnection, Cursor
from irisett import object_models


    If keys is given, only delete the specified keys, otherwise delete all
    metadata for the object.
    """

    async def _run(cur: Cursor) -> None:
        if keys:
            # noinspection PyTypeChecker
            for key in keys:
                q = """delete from object_metadata where object_type=%s and object_id=%s and `key`=%s"""
                q_args = (object_type, object_id, key)  # type: Tuple
                await cur.execute(q, q_args)
        else:
            q = """delete from object_metadata where object_type=%s and object_id=%s"""
            q_args = (object_type, object_id)
            await cur.execute(q, q_args)

    await dbcon.transact(_run)


async def get_metadata_for_object(
        dbcon: DBConnection, object_type: str, object_id: int) -> Iterable[object_models.ObjectMetadata]:
    """Get ObjectMetadata for a single object (type, id)

    Ie. contact, [ID]
    """
    q = """select metadata.object_type, metadata.object_id, metadata.key, metadata.value
        from object_metadata as metadata
        where metadata.object_type=%s and metadata.object_id=%s"""
    q_args = (object_type, object_id)

    return [object_models.ObjectMetadata(*row) for row in await dbcon.fetch_all(q, q_args)]