internal.queries.badge

  1# Code generated by sqlc. DO NOT EDIT.
  2# versions:
  3#   sqlc v1.30.0
  4# source: badge.sql
  5import datetime
  6import pydantic
  7from typing import AsyncIterator, Optional
  8
  9import sqlalchemy
 10import sqlalchemy.ext.asyncio
 11
 12from internal.queries import models
 13
 14
 15ACQUIRE_BADGE = """-- name: acquire_badge \\:one
 16INSERT INTO badges_acquired (user_id, badge_id, level)
 17VALUES (:p1, :p2, :p3)
 18RETURNING user_id, badge_id, level, acquired_at
 19"""
 20
 21
 22class AcquireBadgeParams(pydantic.BaseModel):
 23    user_id: int
 24    badge_id: int
 25    level: int
 26
 27
 28GET_BADGE = """-- name: get_badge \\:one
 29SELECT badge_id, name, description
 30FROM badges
 31WHERE badge_id = :p1
 32LIMIT 1
 33"""
 34
 35
 36GET_BADGES = """-- name: get_badges \\:many
 37SELECT badge_id, name, description
 38FROM badges
 39"""
 40
 41
 42GET_CONSUMER_BADGES = """-- name: get_consumer_badges \\:many
 43SELECT b.badge_id, b.name, b.description, ba.level, ba.acquired_at
 44FROM badges b
 45INNER JOIN badges_acquired ba ON ba.badge_id = b.badge_id
 46WHERE ba.user_id = :p1
 47"""
 48
 49
 50class GetConsumerBadgesRow(pydantic.BaseModel):
 51    badge_id: int
 52    name: str
 53    description: str
 54    level: int
 55    acquired_at: datetime.datetime
 56
 57
 58UPDATE_BADGE = """-- name: update_badge \\:one
 59UPDATE badges
 60SET name = :p2, description = :p3
 61WHERE badge_id = :p1
 62RETURNING badge_id, name, description
 63"""
 64
 65
 66class UpdateBadgeParams(pydantic.BaseModel):
 67    badge_id: int
 68    name: str
 69    description: str
 70
 71
 72UPDATE_BADGE_LEVEL = """-- name: update_badge_level \\:one
 73UPDATE badges_acquired
 74SET level=:p1
 75WHERE user_id=:p2 AND badge_id=:p3
 76RETURNING user_id, badge_id, level, acquired_at
 77"""
 78
 79
 80class UpdateBadgeLevelParams(pydantic.BaseModel):
 81    level: int
 82    user_id: int
 83    badge_id: int
 84
 85
 86class AsyncQuerier:
 87    def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
 88        self._conn = conn
 89
 90    async def acquire_badge(self, arg: AcquireBadgeParams) -> Optional[models.BadgesAcquired]:
 91        row = (await self._conn.execute(sqlalchemy.text(ACQUIRE_BADGE), {"p1": arg.user_id, "p2": arg.badge_id, "p3": arg.level})).first()
 92        if row is None:
 93            return None
 94        return models.BadgesAcquired(
 95            user_id=row[0],
 96            badge_id=row[1],
 97            level=row[2],
 98            acquired_at=row[3],
 99        )
100
101    async def get_badge(self, *, badge_id: int) -> Optional[models.Badge]:
102        row = (await self._conn.execute(sqlalchemy.text(GET_BADGE), {"p1": badge_id})).first()
103        if row is None:
104            return None
105        return models.Badge(
106            badge_id=row[0],
107            name=row[1],
108            description=row[2],
109        )
110
111    async def get_badges(self) -> AsyncIterator[models.Badge]:
112        result = await self._conn.stream(sqlalchemy.text(GET_BADGES))
113        async for row in result:
114            yield models.Badge(
115                badge_id=row[0],
116                name=row[1],
117                description=row[2],
118            )
119
120    async def get_consumer_badges(self, *, user_id: int) -> AsyncIterator[GetConsumerBadgesRow]:
121        result = await self._conn.stream(sqlalchemy.text(GET_CONSUMER_BADGES), {"p1": user_id})
122        async for row in result:
123            yield GetConsumerBadgesRow(
124                badge_id=row[0],
125                name=row[1],
126                description=row[2],
127                level=row[3],
128                acquired_at=row[4],
129            )
130
131    async def update_badge(self, arg: UpdateBadgeParams) -> Optional[models.Badge]:
132        row = (await self._conn.execute(sqlalchemy.text(UPDATE_BADGE), {"p1": arg.badge_id, "p2": arg.name, "p3": arg.description})).first()
133        if row is None:
134            return None
135        return models.Badge(
136            badge_id=row[0],
137            name=row[1],
138            description=row[2],
139        )
140
141    async def update_badge_level(self, arg: UpdateBadgeLevelParams) -> Optional[models.BadgesAcquired]:
142        row = (await self._conn.execute(sqlalchemy.text(UPDATE_BADGE_LEVEL), {"p1": arg.level, "p2": arg.user_id, "p3": arg.badge_id})).first()
143        if row is None:
144            return None
145        return models.BadgesAcquired(
146            user_id=row[0],
147            badge_id=row[1],
148            level=row[2],
149            acquired_at=row[3],
150        )
ACQUIRE_BADGE = '-- name: acquire_badge \\:one\nINSERT INTO badges_acquired (user_id, badge_id, level)\nVALUES (:p1, :p2, :p3)\nRETURNING user_id, badge_id, level, acquired_at\n'
class AcquireBadgeParams(pydantic.main.BaseModel):
23class AcquireBadgeParams(pydantic.BaseModel):
24    user_id: int
25    badge_id: int
26    level: int

!!! abstract "Usage Documentation" Models

A base class for creating Pydantic models.

Attributes:
  • __class_vars__: The names of the class variables defined on the model.
  • __private_attributes__: Metadata about the private attributes of the model.
  • __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.
  • __pydantic_complete__: Whether model building is completed, or if there are still undefined fields.
  • __pydantic_core_schema__: The core schema of the model.
  • __pydantic_custom_init__: Whether the model has a custom __init__ function.
  • __pydantic_decorators__: Metadata containing the decorators defined on the model. This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.
  • __pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to __args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.
  • __pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models.
  • __pydantic_post_init__: The name of the post-init method for the model, if defined.
  • __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel].
  • __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model.
  • __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.
  • __pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects.
  • __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.
  • __pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra] is set to 'allow'.
  • __pydantic_fields_set__: The names of fields explicitly set during instantiation.
  • __pydantic_private__: Values of private attributes set on the model instance.
user_id: int = PydanticUndefined
badge_id: int = PydanticUndefined
level: int = PydanticUndefined
GET_BADGE = '-- name: get_badge \\:one\nSELECT badge_id, name, description\nFROM badges\nWHERE badge_id = :p1\nLIMIT 1\n'
GET_BADGES = '-- name: get_badges \\:many\nSELECT badge_id, name, description\nFROM badges\n'
GET_CONSUMER_BADGES = '-- name: get_consumer_badges \\:many\nSELECT b.badge_id, b.name, b.description, ba.level, ba.acquired_at\nFROM badges b\nINNER JOIN badges_acquired ba ON ba.badge_id = b.badge_id\nWHERE ba.user_id = :p1\n'
class GetConsumerBadgesRow(pydantic.main.BaseModel):
51class GetConsumerBadgesRow(pydantic.BaseModel):
52    badge_id: int
53    name: str
54    description: str
55    level: int
56    acquired_at: datetime.datetime

!!! abstract "Usage Documentation" Models

A base class for creating Pydantic models.

Attributes:
  • __class_vars__: The names of the class variables defined on the model.
  • __private_attributes__: Metadata about the private attributes of the model.
  • __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.
  • __pydantic_complete__: Whether model building is completed, or if there are still undefined fields.
  • __pydantic_core_schema__: The core schema of the model.
  • __pydantic_custom_init__: Whether the model has a custom __init__ function.
  • __pydantic_decorators__: Metadata containing the decorators defined on the model. This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.
  • __pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to __args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.
  • __pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models.
  • __pydantic_post_init__: The name of the post-init method for the model, if defined.
  • __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel].
  • __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model.
  • __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.
  • __pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects.
  • __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.
  • __pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra] is set to 'allow'.
  • __pydantic_fields_set__: The names of fields explicitly set during instantiation.
  • __pydantic_private__: Values of private attributes set on the model instance.
badge_id: int = PydanticUndefined
name: str = PydanticUndefined
description: str = PydanticUndefined
level: int = PydanticUndefined
acquired_at: datetime.datetime = PydanticUndefined
UPDATE_BADGE = '-- name: update_badge \\:one\nUPDATE badges\nSET name = :p2, description = :p3\nWHERE badge_id = :p1\nRETURNING badge_id, name, description\n'
class UpdateBadgeParams(pydantic.main.BaseModel):
67class UpdateBadgeParams(pydantic.BaseModel):
68    badge_id: int
69    name: str
70    description: str

!!! abstract "Usage Documentation" Models

A base class for creating Pydantic models.

Attributes:
  • __class_vars__: The names of the class variables defined on the model.
  • __private_attributes__: Metadata about the private attributes of the model.
  • __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.
  • __pydantic_complete__: Whether model building is completed, or if there are still undefined fields.
  • __pydantic_core_schema__: The core schema of the model.
  • __pydantic_custom_init__: Whether the model has a custom __init__ function.
  • __pydantic_decorators__: Metadata containing the decorators defined on the model. This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.
  • __pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to __args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.
  • __pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models.
  • __pydantic_post_init__: The name of the post-init method for the model, if defined.
  • __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel].
  • __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model.
  • __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.
  • __pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects.
  • __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.
  • __pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra] is set to 'allow'.
  • __pydantic_fields_set__: The names of fields explicitly set during instantiation.
  • __pydantic_private__: Values of private attributes set on the model instance.
badge_id: int = PydanticUndefined
name: str = PydanticUndefined
description: str = PydanticUndefined
UPDATE_BADGE_LEVEL = '-- name: update_badge_level \\:one\nUPDATE badges_acquired\nSET level=:p1\nWHERE user_id=:p2 AND badge_id=:p3\nRETURNING user_id, badge_id, level, acquired_at\n'
class UpdateBadgeLevelParams(pydantic.main.BaseModel):
81class UpdateBadgeLevelParams(pydantic.BaseModel):
82    level: int
83    user_id: int
84    badge_id: int

!!! abstract "Usage Documentation" Models

A base class for creating Pydantic models.

Attributes:
  • __class_vars__: The names of the class variables defined on the model.
  • __private_attributes__: Metadata about the private attributes of the model.
  • __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.
  • __pydantic_complete__: Whether model building is completed, or if there are still undefined fields.
  • __pydantic_core_schema__: The core schema of the model.
  • __pydantic_custom_init__: Whether the model has a custom __init__ function.
  • __pydantic_decorators__: Metadata containing the decorators defined on the model. This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.
  • __pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to __args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.
  • __pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models.
  • __pydantic_post_init__: The name of the post-init method for the model, if defined.
  • __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel].
  • __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model.
  • __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.
  • __pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects.
  • __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.
  • __pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra] is set to 'allow'.
  • __pydantic_fields_set__: The names of fields explicitly set during instantiation.
  • __pydantic_private__: Values of private attributes set on the model instance.
level: int = PydanticUndefined
user_id: int = PydanticUndefined
badge_id: int = PydanticUndefined
class AsyncQuerier:
 87class AsyncQuerier:
 88    def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
 89        self._conn = conn
 90
 91    async def acquire_badge(self, arg: AcquireBadgeParams) -> Optional[models.BadgesAcquired]:
 92        row = (await self._conn.execute(sqlalchemy.text(ACQUIRE_BADGE), {"p1": arg.user_id, "p2": arg.badge_id, "p3": arg.level})).first()
 93        if row is None:
 94            return None
 95        return models.BadgesAcquired(
 96            user_id=row[0],
 97            badge_id=row[1],
 98            level=row[2],
 99            acquired_at=row[3],
100        )
101
102    async def get_badge(self, *, badge_id: int) -> Optional[models.Badge]:
103        row = (await self._conn.execute(sqlalchemy.text(GET_BADGE), {"p1": badge_id})).first()
104        if row is None:
105            return None
106        return models.Badge(
107            badge_id=row[0],
108            name=row[1],
109            description=row[2],
110        )
111
112    async def get_badges(self) -> AsyncIterator[models.Badge]:
113        result = await self._conn.stream(sqlalchemy.text(GET_BADGES))
114        async for row in result:
115            yield models.Badge(
116                badge_id=row[0],
117                name=row[1],
118                description=row[2],
119            )
120
121    async def get_consumer_badges(self, *, user_id: int) -> AsyncIterator[GetConsumerBadgesRow]:
122        result = await self._conn.stream(sqlalchemy.text(GET_CONSUMER_BADGES), {"p1": user_id})
123        async for row in result:
124            yield GetConsumerBadgesRow(
125                badge_id=row[0],
126                name=row[1],
127                description=row[2],
128                level=row[3],
129                acquired_at=row[4],
130            )
131
132    async def update_badge(self, arg: UpdateBadgeParams) -> Optional[models.Badge]:
133        row = (await self._conn.execute(sqlalchemy.text(UPDATE_BADGE), {"p1": arg.badge_id, "p2": arg.name, "p3": arg.description})).first()
134        if row is None:
135            return None
136        return models.Badge(
137            badge_id=row[0],
138            name=row[1],
139            description=row[2],
140        )
141
142    async def update_badge_level(self, arg: UpdateBadgeLevelParams) -> Optional[models.BadgesAcquired]:
143        row = (await self._conn.execute(sqlalchemy.text(UPDATE_BADGE_LEVEL), {"p1": arg.level, "p2": arg.user_id, "p3": arg.badge_id})).first()
144        if row is None:
145            return None
146        return models.BadgesAcquired(
147            user_id=row[0],
148            badge_id=row[1],
149            level=row[2],
150            acquired_at=row[3],
151        )
AsyncQuerier(conn: sqlalchemy.ext.asyncio.engine.AsyncConnection)
88    def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
89        self._conn = conn
async def acquire_badge( self, arg: AcquireBadgeParams) -> internal.queries.models.BadgesAcquired | None:
 91    async def acquire_badge(self, arg: AcquireBadgeParams) -> Optional[models.BadgesAcquired]:
 92        row = (await self._conn.execute(sqlalchemy.text(ACQUIRE_BADGE), {"p1": arg.user_id, "p2": arg.badge_id, "p3": arg.level})).first()
 93        if row is None:
 94            return None
 95        return models.BadgesAcquired(
 96            user_id=row[0],
 97            badge_id=row[1],
 98            level=row[2],
 99            acquired_at=row[3],
100        )
async def get_badge(self, *, badge_id: int) -> internal.queries.models.Badge | None:
102    async def get_badge(self, *, badge_id: int) -> Optional[models.Badge]:
103        row = (await self._conn.execute(sqlalchemy.text(GET_BADGE), {"p1": badge_id})).first()
104        if row is None:
105            return None
106        return models.Badge(
107            badge_id=row[0],
108            name=row[1],
109            description=row[2],
110        )
async def get_badges(self) -> AsyncIterator[internal.queries.models.Badge]:
112    async def get_badges(self) -> AsyncIterator[models.Badge]:
113        result = await self._conn.stream(sqlalchemy.text(GET_BADGES))
114        async for row in result:
115            yield models.Badge(
116                badge_id=row[0],
117                name=row[1],
118                description=row[2],
119            )
async def get_consumer_badges( self, *, user_id: int) -> AsyncIterator[GetConsumerBadgesRow]:
121    async def get_consumer_badges(self, *, user_id: int) -> AsyncIterator[GetConsumerBadgesRow]:
122        result = await self._conn.stream(sqlalchemy.text(GET_CONSUMER_BADGES), {"p1": user_id})
123        async for row in result:
124            yield GetConsumerBadgesRow(
125                badge_id=row[0],
126                name=row[1],
127                description=row[2],
128                level=row[3],
129                acquired_at=row[4],
130            )
async def update_badge( self, arg: UpdateBadgeParams) -> internal.queries.models.Badge | None:
132    async def update_badge(self, arg: UpdateBadgeParams) -> Optional[models.Badge]:
133        row = (await self._conn.execute(sqlalchemy.text(UPDATE_BADGE), {"p1": arg.badge_id, "p2": arg.name, "p3": arg.description})).first()
134        if row is None:
135            return None
136        return models.Badge(
137            badge_id=row[0],
138            name=row[1],
139            description=row[2],
140        )
async def update_badge_level( self, arg: UpdateBadgeLevelParams) -> internal.queries.models.BadgesAcquired | None:
142    async def update_badge_level(self, arg: UpdateBadgeLevelParams) -> Optional[models.BadgesAcquired]:
143        row = (await self._conn.execute(sqlalchemy.text(UPDATE_BADGE_LEVEL), {"p1": arg.level, "p2": arg.user_id, "p3": arg.badge_id})).first()
144        if row is None:
145            return None
146        return models.BadgesAcquired(
147            user_id=row[0],
148            badge_id=row[1],
149            level=row[2],
150            acquired_at=row[3],
151        )