|
| 1 | +--- |
| 2 | +prev: 服务器插件 |
| 3 | +next: Tarina |
| 4 | +--- |
| 5 | + |
| 6 | +# 数据库插件 |
| 7 | + |
| 8 | +`entari-plugin-database` 属于官方插件,允许你在插件中使用数据库进行数据存储和查询。 |
| 9 | + |
| 10 | +由于基于 [`SQLAlchemy`](https://www.sqlalchemy.org/),大部分情况下,你可以直接使用 SQLAlchemy 的 API 来操作数据库。 |
| 11 | + |
| 12 | +本插件只提供了 ORM 功能,没有数据库后端,也没有直接连接数据库后端的能力。 所以你需要另行安装数据库驱动和数据库后端,并且配置数据库连接信息。 |
| 13 | + |
| 14 | +## 安装 |
| 15 | + |
| 16 | +::: code-group |
| 17 | +```bash:no-line-numbers [pdm] |
| 18 | +pdm add entari-plugin-database |
| 19 | +``` |
| 20 | + |
| 21 | +```bash:no-line-numbers [uv] |
| 22 | +uv add entari-plugin-database |
| 23 | +``` |
| 24 | + |
| 25 | +```bash:no-line-numbers [pip] |
| 26 | +pip install entari-plugin-database |
| 27 | +``` |
| 28 | +::: |
| 29 | + |
| 30 | +## 配置连接 |
| 31 | +在配置文件中,你可以通过 `database` 字段来配置数据库连接: |
| 32 | + |
| 33 | +```yaml:no-line-numbers title=entari.yml |
| 34 | +basic: |
| 35 | + log: |
| 36 | + ignores: ["aiosqlite.core"] # 忽略 aiosqlite 的 DEBUG 日志 |
| 37 | +plugins: |
| 38 | + database: |
| 39 | + type: sqlite # 数据库类型, 可选值有 sqlite, mysql, postgresql, oracle 等 |
| 40 | + name: my_database.db # 数据库名称或文件目录 |
| 41 | + driver: aiosqlite # 数据库驱动, 根据数据库类型选择 |
| 42 | + ... |
| 43 | +``` |
| 44 | + |
| 45 | +`type` 与 `driver` 的支持列表详见 [Dialects](https://docs.sqlalchemy.org/en/21/dialects/#included-dialects)。 |
| 46 | + |
| 47 | +其余的配置项包括: |
| 48 | +- `host`: 数据库主机地址 (仅在使用 MySQL/PostgreSQL 等远程数据库时需要) |
| 49 | +- `port`: 数据库端口号 (仅在使用 MySQL/PostgreSQL 等远程数据库时需要) |
| 50 | +- `username`: 数据库用户名 (仅在使用 MySQL/PostgreSQL 等远程数据库时需要) |
| 51 | +- `password`: 数据库密码 (仅在使用 MySQL/PostgreSQL 等远程数据库时需要) |
| 52 | +- `query`: 数据库连接参数 (仅在使用 MySQL/PostgreSQL 等远程数据库时需要) |
| 53 | +- `options`: SQLAlchemy 的其他选项。参见 [Engine Creation API](https://docs.sqlalchemy.org/en/21/core/engines.html#engine-creation-api) |
| 54 | + |
| 55 | +若不传入配置项,则默认使用 SQLite 数据库,并将数据库文件存储在当前目录下。 |
| 56 | + |
| 57 | +## 定义模型 |
| 58 | + |
| 59 | +`database` 插件使用 SQLAlchemy 的 ORM 功能来定义模型。你可以通过继承 `database.Base` 类来定义你的模型类。 |
| 60 | + |
| 61 | +```python title=my_plugin.py |
| 62 | +from entari_plugin_database import Base, Mapped, mapped_column |
| 63 | + |
| 64 | +class Weather(Base): |
| 65 | + __tablename__ = "weather" |
| 66 | + |
| 67 | + location: Mapped[str] = mapped_column(primary_key=True) |
| 68 | + weather: Mapped[str] |
| 69 | +``` |
| 70 | + |
| 71 | +我们可以用以下代码检查模型生成的数据库模式是否正确: |
| 72 | + |
| 73 | +```python |
| 74 | +from sqlalchemy.schema import CreateTable |
| 75 | + |
| 76 | +print(CreateTable(Weather.__table__)) |
| 77 | +``` |
| 78 | + |
| 79 | +```sql |
| 80 | +CREATE TABLE weather ( |
| 81 | + location VARCHAR NOT NULL, |
| 82 | + weather VARCHAR NOT NULL, |
| 83 | + CONSTRAINT pk_weather PRIMARY KEY (location) |
| 84 | +) |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | +## 使用会话 |
| 89 | + |
| 90 | +`database` 插件通过 `SqlalchemyService` 提供数据库会话服务。 |
| 91 | + |
| 92 | +你可以通过依赖注入的方式获取 `SqlalchemyService` 实例,并使用它来获取数据库会话。 |
| 93 | + |
| 94 | +:::code-group |
| 95 | + |
| 96 | +```python title=my_plugin.py [ORM] |
| 97 | +from arclet.entari import Session, command |
| 98 | +from entari_plugin_database import SqlalchemyService |
| 99 | +from sqlalchemy import select |
| 100 | + |
| 101 | +@command.on("get_weather {location}") |
| 102 | +async def on_message(location: str, session: Session, db: SqlalchemyService): |
| 103 | + async with db.get_session() as db_session: |
| 104 | + # 在这里使用 SQLAlchemy 的会话进行数据库操作 |
| 105 | + result = await db_session.scalars(select(Weather).where(Weather.location == location)) |
| 106 | + data = result.all() |
| 107 | + await session.send(f"Data: {data}") |
| 108 | +``` |
| 109 | + |
| 110 | + |
| 111 | +```python title=my_plugin.py [SQL语句] |
| 112 | +from arclet.entari import Session, command |
| 113 | +from entari_plugin_database import SqlalchemyService |
| 114 | +from sqlalchemy import text |
| 115 | + |
| 116 | +@command.on("get_weather {location}") |
| 117 | +async def on_message(location: str, session: Session, db: SqlalchemyService): |
| 118 | + async with db.get_session() as db_session: |
| 119 | + # 在这里使用 SQLAlchemy 的会话进行数据库操作 |
| 120 | + result = await db_session.execute(text("SELECT * FROM weather WHERE location=:location"), {"location": location}) |
| 121 | + data = result.fetchall() |
| 122 | + await session.send(f"Data: {data}") |
| 123 | +``` |
| 124 | + |
| 125 | +::: |
| 126 | + |
| 127 | +关于如何使用 SQLAlchemy 的 ORM 功能,你可以参考 [SQLAlchemy 官方文档](https://docs.sqlalchemy.org/en/21/orm/quickstart.html)。 |
0 commit comments