Skip to content

Commit

Permalink
Merge pull request #9 from Vastxiao/dev
Browse files Browse the repository at this point in the history
更新到v0.2.0
  • Loading branch information
Vastxiao authored Oct 15, 2024
2 parents c49a28a + 56a5a48 commit 4bae067
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 30 deletions.
47 changes: 37 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
# Change Log

## [0.2.0] - 2024.10.14

### Features

1. Fixed the handling of the `echo` parameter in `aiomysql`.
2. Added the `echo_sql_log` parameter to the `AsMysql` class, used to control whether `aiomysql` outputs the executed SQL statements (default is False).

```python
from asmysql import AsMysql

class TestAsMysql(AsMysql):
# This allows controlling whether the executed SQL statements in aiomysql
# are output to Logging.logger.
echo_sql_log = True


# Of course, the `echo_sql_log` parameter can also be passed when instantiating AsMysql.
async def main():
async with TestAsMysql(echo_sql_log=True) as mysql:
result = await mysql.client.execute('select user, authentication_string, host from mysql.user')
if result.err:
print(result.err)
else:
async for item in result.iterate():
print(item)
```

## [0.1.4] - 2024.08.15

### Features

#### 1.AsMysql支持异步上下文管理器。
#### 1. `AsMysql` supports asynchronous context manager.

```python
import asyncio
from asmysql import AsMysql

class TestAsMysql(AsMysql):
async def get_users(self):
result = await self.client.execute('select user,authentication_string,host from mysql.user')
result = await self.client.execute('select user, authentication_string, host from mysql.user')
if result.err:
print(result.err)
else:
Expand All @@ -27,26 +54,26 @@ if __name__ == '__main__':
asyncio.run(main())
```

#### 2.在connection中的异常抛出时,使用ConnectionError替代。
#### 2. Replaced exceptions in `connection` with `ConnectionError`.

## [0.1.1] - 2023.07.25

### Features

> 新增 Result.err_msg 返回exception错误的详情字符串。
> Added `Result.err_msg` to return a detailed string of the exception error.
## [0.1.0] - 2023.07.16

### Breaking Changes

### Features

> 1. asmysql是对aiomysql封装的简易使用库。
> 2. 支持自动管理mysql连接池,和重连机制。
> 3. 全局自动捕获处理MysqlError错误。
> 4. 分离执行语句和数据获取。
> 5. 直接集成AsMysql类进行逻辑开发。
> 1. `asmysql` is a simplified wrapper library around `aiomysql`.
> 2. Supports automatic management of MySQL connection pools and reconnection mechanism.
> 3. Automatically captures and handles `MysqlError` globally.
> 4. Separates statement execution and data retrieval.
> 5. Directly integrates the `AsMysql` class for logical development.
### Internal

> 初始化项目,开发环境使用poetry进行管理。
> Initialized the project, with the development environment managed using `poetry`.
78 changes: 78 additions & 0 deletions CHANGELOG_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Change Log

## [0.2.0] - 2024.10.14

### Features

1. 修复aiomysql对echo参数的处理。
2. 对AsMysql类新增echo_sql_log参数,用于控制aiomysql是否输出执行的sql语句(默认False)。

```python
from asmysql import AsMysql

class TestAsMysql(AsMysql):
# 这样就可以控制aiomysql库是否在Logging.logger输出执行的sql语句。
echo_sql_log = True


# 当然,echo_sql_log参数也可以在实例化AsMysql时传入。
async def main():
async with TestAsMysql(echo_sql_log=True) as mysql:
result = await mysql.client.execute('select user,authentication_string,host from mysql.user')
if result.err:
print(result.err)
else:
async for item in result.iterate():
print(item)
```

## [0.1.4] - 2024.08.15

### Features

#### 1.AsMysql支持异步上下文管理器。

```python
import asyncio
from asmysql import AsMysql

class TestAsMysql(AsMysql):
async def get_users(self):
result = await self.client.execute('select user,authentication_string,host from mysql.user')
if result.err:
print(result.err)
else:
async for item in result.iterate():
print(item)

async def main():
async with TestAsMysql() as mysql:
await mysql.get_users()

if __name__ == '__main__':
asyncio.run(main())
```

#### 2.在connection中的异常抛出时,使用ConnectionError替代。

## [0.1.1] - 2023.07.25

### Features

> 新增 Result.err_msg 返回exception错误的详情字符串。
## [0.1.0] - 2023.07.16

### Breaking Changes

### Features

> 1. asmysql是对aiomysql封装的简易使用库。
> 2. 支持自动管理mysql连接池,和重连机制。
> 3. 全局自动捕获处理MysqlError错误。
> 4. 分离执行语句和数据获取。
> 5. 直接集成AsMysql类进行逻辑开发。
### Internal

> 初始化项目,开发环境使用poetry进行管理。
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 渺小
Copyright (c) 2023 vastxiao (渺小)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 6 additions & 6 deletions asmysql/_asmysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ class AsMysql:
max_pool_size: int = 10
pool_recycle: float = -1 # 空闲TCP连接回收等待时间(秒)
connect_timeout: int = 5 # 连接超时时间(秒)
echo_sql_log: bool = False # 是否打印sql语句日志

@final
def __init__(self, host: str = None, port: int = None,
user: str = None, password: str = None,
database: str = None, charset: str = None,
min_pool_size: int = None, max_pool_size: int = None,
pool_recycle: float = None, connect_timeout: int = None):
pool_recycle: float = None, connect_timeout: int = None,
echo_sql_log: bool = None):
self.host: Final[str] = host or self.host
self.port: Final[int] = port or self.port
self.user: Final[str] = user or self.user
Expand All @@ -35,6 +37,7 @@ def __init__(self, host: str = None, port: int = None,
self.max_pool_size: Final[int] = max_pool_size if max_pool_size is not None else self.max_pool_size
self.pool_recycle: Final[float] = pool_recycle or self.pool_recycle
self.connect_timeout: Final[int] = connect_timeout or self.connect_timeout
self.echo_sql_log: Final[bool] = echo_sql_log if echo_sql_log is not None else self.echo_sql_log

self.url: Final[str] = f'mysql://{self.host}:{self.port}{"/" + self.database if self.database else ""}'
self.__pool: Optional[Pool] = None
Expand Down Expand Up @@ -63,9 +66,9 @@ async def connect(self):
db=self.database,
minsize=self.min_pool_size,
maxsize=self.max_pool_size,
echo=f'{self.url}',
pool_recycle=self.pool_recycle,
connect_timeout=self.connect_timeout,
echo=self.echo_sql_log,
)
self.__cursor_client = CursorClient(self.__pool)
except MySQLError as err:
Expand All @@ -90,10 +93,7 @@ async def release_connections(self):
@property
def is_connected(self):
"""数据库是否已连接"""
if self.__pool:
return True
else:
return False
return True if self.__pool else False

@final
@property
Expand Down
10 changes: 4 additions & 6 deletions asmysql/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


class Result:
def __init__(self, query: str, *, rows: int = None, cursor: Cursor = None, err: Exception = None):
def __init__(self, query: str, *, rows: int = None,
cursor: Cursor = None, err: Exception = None):
if bool(cursor) ^ bool(err):
self.query: Final[str] = query
self.rows: Final[int] = rows
Expand All @@ -23,10 +24,7 @@ def __repr__(self):
@property
@lru_cache
def err_msg(self):
if self.err:
return err_msg(self.err)
else:
return ""
return err_msg(self.err) if self.err else ""

async def fetch_one(self) -> Optional[tuple]:
"""获取一条记录"""
Expand All @@ -47,7 +45,7 @@ async def iterate(self) -> AsyncIterator[tuple]:
"""异步生成器遍历所有记录"""
if not self.err:
while True:
data = await self.fetch_one()
data = await self.__cursor.fetchone()
if data:
yield data
else:
Expand Down
14 changes: 7 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "asmysql"
version = "0.1.4"
version = "0.2.0"
description = "封装aiomysql的异步mysql客户端引擎。"
license = "MIT"
authors = ["vastxiao <vastxiao@gmail.com>"]
Expand All @@ -9,13 +9,15 @@ repository = "https://github.com/Vastxiao/asmysql"
documentation = "https://github.com/Vastxiao/asmysql/blob/main/README.md"
keywords = ["async", "asyncio", "mysql", "aiomysql"]
readme = "README.md"
include = ["CHANGELOG.md", "README_zh.md"]
include = ["CHANGELOG.md", "CHANGELOG_zh.md", "README_zh.md"]

[tool.poetry.urls]
"Readme-zh" = "https://github.com/Vastxiao/asmysql/blob/main/README_zh.md"
"Changelog-zh" = "https://github.com/Vastxiao/asmysql/blob/main/CHANGELOG_zh.md"
"Changelog" = "https://github.com/Vastxiao/asmysql/blob/main/CHANGELOG.md"
"Vastxiao Blog" = "https://vastxiao.github.io/"
"Gitee Repo" = "https://gitee.com/vastxiao/asmysql"
"readme-zh" = "https://github.com/Vastxiao/asmysql/blob/main/README_zh.md"
"changelog" = "https://github.com/Vastxiao/asmysql/blob/main/CHANGELOG.md"
"vastxiao blog" = "https://vastxiao.github.io/"


[tool.poetry.dependencies]
python = "^3.11"
Expand All @@ -40,8 +42,6 @@ name = "ali"
url = "https://mirrors.aliyun.com/pypi/simple/"
priority = "primary"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

0 comments on commit 4bae067

Please sign in to comment.