Skip to content

Commit a39e199

Browse files
committed
📝 replace Weather with Record
1 parent 1c6e5ea commit a39e199

1 file changed

Lines changed: 39 additions & 35 deletions

File tree

tutorial/entari/database.md

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,37 @@ plugins:
7474
7575
`database` 插件使用 SQLAlchemy 的 ORM 功能来定义模型。你可以通过继承 `database.Base` 类来定义你的模型类。
7676
77-
假设我们要定义一个存储天气信息的模型
77+
假设我们要定义一个存储调用插件记录的模型
7878
7979
```python title=my_plugin.py
8080
from entari_plugin_database import Base, Mapped, mapped_column
8181
82-
class Weather(Base):
83-
location: Mapped[str] = mapped_column(primary_key=True)
84-
weather: Mapped[str]
82+
class Record(Base):
83+
plg_name: Mapped[str] = mapped_column(primary_key=True)
84+
user_id: Mapped[int]
85+
user_name: Mapped[str]
8586
```
8687

87-
其中,`primary_key=True` 意味着此列 (location) 是主键,即内容是唯一的且非空的。 每一个模型必须有至少一个主键。
88+
其中,`primary_key=True` 意味着此列 (plg_name) 是主键,即内容是唯一的且非空的。 每一个模型必须有至少一个主键。
8889

8990
我们可以用以下代码检查模型生成的数据库模式是否正确:
9091

9192
```python
9293
from sqlalchemy.schema import CreateTable
9394

94-
print(CreateTable(Weather.__table__))
95+
print(CreateTable(Record.__table__))
9596
```
9697

9798
```sql
98-
CREATE TABLE my_plugin_weather (
99-
location VARCHAR NOT NULL,
100-
weather VARCHAR NOT NULL,
101-
CONSTRAINT pk_my_plugin_weather PRIMARY KEY (location)
99+
CREATE TABLE my_plugin_record (
100+
plg_name VARCHAR NOT NULL,
101+
user_id INTEGER NOT NULL,
102+
user_name VARCHAR NOT NULL,
103+
CONSTRAINT pk_my_plugin_record PRIMARY KEY (plg_name)
102104
)
103105
```
104106

105-
可以注意到表名是 `my_plugin_weather` 而不是 `Weather` 或者 `weather`。 这是因为数据库插件会自动为模型生成一个表名,规则是:`<插件模块名>_<类名小写>`
107+
可以注意到表名是 `my_plugin_record` 而不是 `Record` 或者 `record`。 这是因为数据库插件会自动为模型生成一个表名,规则是:`<插件模块名>_<类名小写>`
106108

107109
你也可以通过指定 `__tablename__` 属性,或传入关键字来自定义表名:
108110

@@ -111,18 +113,20 @@ CREATE TABLE my_plugin_weather (
111113
```python title=my_plugin.py [指定 __tablename__]
112114
from entari_plugin_database import Base, Mapped, mapped_column
113115

114-
class Weather(Base):
115-
__tablename__ = "custom_weather" # 自定义表名
116-
location: Mapped[str] = mapped_column(primary_key=True)
117-
weather: Mapped[str]
116+
class Record(Base):
117+
__tablename__ = "record" # 自定义表名
118+
plg_name: Mapped[str] = mapped_column(primary_key=True)
119+
user_id: Mapped[int]
120+
user_name: Mapped[str]
118121
```
119122

120123
```python title=my_plugin.py [传入关键字]
121124
from entari_plugin_database import Base, Mapped, mapped_column
122125

123-
class Weather(Base, tablename="custom_weather"): # 自定义表名
124-
location: Mapped[str] = mapped_column(primary_key=True)
125-
weather: Mapped[str]
126+
class Record(Base, tablename="record"): # 自定义表名
127+
plg_name: Mapped[str] = mapped_column(primary_key=True)
128+
user_id: Mapped[int]
129+
user_name: Mapped[str]
126130
```
127131
:::
128132

@@ -139,11 +143,11 @@ class Weather(Base, tablename="custom_weather"): # 自定义表名
139143
from arclet.entari import command
140144
from entari_plugin_database import SqlalchemyService, select
141145

142-
@command.on("get_weather {location}")
143-
async def on_message(location: str, db: SqlalchemyService):
146+
@command.on("check {name}")
147+
async def on_message(name: str, db: SqlalchemyService):
144148
async with db.get_session() as db_session:
145149
# 在这里使用 SQLAlchemy 的会话进行数据库操作
146-
result = await db_session.scalars(select(Weather).where(Weather.location == location))
150+
result = await db_session.scalars(select(Record).where(Record.plg_name == name))
147151
data = result.all()
148152
return f"Data: {data}"
149153
```
@@ -153,11 +157,11 @@ from arclet.entari import command
153157
from entari_plugin_database import SqlalchemyService
154158
from sqlalchemy import text
155159

156-
@command.on("get_weather {location}")
157-
async def on_message(location: str, db: SqlalchemyService):
160+
@command.on("check {name}")
161+
async def on_message(name: str, db: SqlalchemyService):
158162
async with db.get_session() as db_session:
159163
# 在这里使用 SQLAlchemy 的会话进行数据库操作
160-
result = await db_session.execute(text("SELECT * FROM weather WHERE location=:location"), {"location": location})
164+
result = await db_session.execute(text("SELECT * FROM my_plugin_record WHERE plg_name=:name"), {"name": name})
161165
data = result.fetchall()
162166
return f"Data: {data}"
163167
```
@@ -171,10 +175,10 @@ async def on_message(location: str, db: SqlalchemyService):
171175
from arclet.entari import command
172176
from entari_plugin_database import AsyncSession, select
173177

174-
@command.on("get_weather {location}")
175-
async def on_message(location: str, db_session: AsyncSession):
178+
@command.on("check {name}")
179+
async def on_message(name: str, db_session: AsyncSession):
176180
# 在这里使用 SQLAlchemy 的会话进行数据库操作
177-
result = await db_session.scalars(select(Weather).where(Weather.location == location))
181+
result = await db_session.scalars(select(Record).where(Record.plg_name == name))
178182
data = result.all()
179183
return f"Data: {data}"
180184
```
@@ -195,13 +199,13 @@ async def on_message(location: str, db_session: AsyncSession):
195199
from arclet.entari import Param, command
196200
from entari_plugin_database import SQLDepends, select
197201

198-
@command.command("get_weather <location:str>")
202+
@command.command("check <name:str>")
199203
async def on_message(
200-
weather: Weather = SQLDepends(
201-
select(Weather).where(Weather.location == Param("location"))
204+
record: Record = SQLDepends(
205+
select(Record).where(Record.plg_name == Param("name"))
202206
),
203207
):
204-
return f"Data: {weather}"
208+
return f"Data: {record}"
205209
```
206210

207211
其中,SQLDepends 是一个特殊的依赖注入,它会根据类型标注和 SQL 语句提供数据,SQL 语句中也可以有子依赖。
@@ -214,13 +218,13 @@ from collections.abc import Sequence
214218
from arclet.entari import Param, command
215219
from entari_plugin_database import SQLDepends, select
216220

217-
@command.command("get_weather <location:str>")
221+
@command.command("check <name:str>")
218222
async def on_message(
219-
weathers: Sequence[Weather] = SQLDepends(
220-
select(Weather).where(Weather.location == Param("location"))
223+
records: Sequence[Record] = SQLDepends(
224+
select(Record).where(Record.plg_name == Param("name"))
221225
),
222226
):
223-
return "Data\n" + "\n".join(f"- {weather}" for weather in weathers)
227+
return "Data\n" + "\n".join(f"- {rec}" for rec in records)
224228
```
225229

226230
:::tip

0 commit comments

Comments
 (0)