sqlor/aidocs/aiomysqlor.md
2025-10-05 11:24:24 +08:00

105 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# AioMysqlor 技术文档
```markdown
# `AioMysqlor` 类文档
## 概述
`AioMysqlor` 是一个继承自 `MySqlor` 的类,用于标识或处理与异步 MySQL 驱动 `aiomysql` 相关的逻辑。该类通过类方法 `isMe` 判断传入的名称是否为 `'aiomysql'`,从而确定是否应由当前类处理。
---
## 模块依赖
```python
from .mysqlor import MySqlor
```
- 依赖于同级模块中的 `MySqlor` 基类。
- 使用相对导入方式加载父类。
---
## 类定义
### `class AioMysqlor(MySqlor)`
继承自 `MySqlor`,扩展了对特定驱动名称的识别能力。
---
## 类方法
### `@classmethod isMe(cls, name)`
#### 功能说明
判断传入的数据库驱动名称是否为 `'aiomysql'`,用于运行时类型识别或工厂模式中的类匹配。
#### 参数
| 参数名 | 类型 | 说明 |
|--------|--------|--------------------------|
| `name` | `str` | 待检测的数据库驱动名称。 |
#### 返回值
- **类型**: `bool`
- **说明**:
-`name == 'aiomysql'`,返回 `True`
- 否则返回 `False`
#### 示例
```python
print(AioMysqlor.isMe('aiomysql')) # 输出: True
print(AioMysqlor.isMe('pymysql')) # 输出: False
```
#### 装饰器
- 使用 `@classmethod` 装饰,表示这是一个类方法,无需实例化即可调用。
> ⚠️ 注意:方法定义中参数 `self` 实际上在类方法中应为 `cls`,建议修正以符合规范(见下方“注意事项”)。
---
## 使用场景
常用于数据库连接工厂中,根据配置的驱动名称动态选择对应的处理类:
```python
driver_name = config.get('db_driver')
if AioMysqlor.isMe(driver_name):
db = AioMysqlor(**config)
```
---
## 注意事项
1. **参数命名问题**
- 尽管使用了 `@classmethod`,方法签名仍写为 `def isMe(self, name)`,其中 `self` 应改为 `cls` 以符合 Python 社区惯例。
- 推荐修改为:
```python
@classmethod
def isMe(cls, name):
return name == 'aiomysql'
```
2. **字符串硬编码**
- `'aiomysql'` 可考虑提取为类常量,便于维护和扩展。
---
## 版本信息
- 创建时间YYYY-MM-DD请根据实际填写
- 作者:开发者团队 / 个人名称
- 适用版本Python 3.7+
- 依赖库:`aiomysql`(异步 MySQL 驱动)
---
```
> ✅ 提示:此文档可用于项目 Wiki、API 手册或代码仓库中的 `docs/` 目录,帮助团队成员理解 `AioMysqlor` 的用途和使用方式。