db-connector-tool 0.0.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- db_connector_tool-0.0.3/LICENSE +21 -0
- db_connector_tool-0.0.3/PKG-INFO +237 -0
- db_connector_tool-0.0.3/README.md +187 -0
- db_connector_tool-0.0.3/db_connector/__init__.py +122 -0
- db_connector_tool-0.0.3/db_connector/cli.py +1027 -0
- db_connector_tool-0.0.3/db_connector/core/__init__.py +99 -0
- db_connector_tool-0.0.3/db_connector/core/config.py +661 -0
- db_connector_tool-0.0.3/db_connector/core/connections.py +845 -0
- db_connector_tool-0.0.3/db_connector/core/crypto.py +356 -0
- db_connector_tool-0.0.3/db_connector/core/exceptions.py +651 -0
- db_connector_tool-0.0.3/db_connector/drivers/__init__.py +97 -0
- db_connector_tool-0.0.3/db_connector/drivers/sqlalchemy_driver.py +861 -0
- db_connector_tool-0.0.3/db_connector/utils/__init__.py +116 -0
- db_connector_tool-0.0.3/db_connector/utils/logging_utils.py +471 -0
- db_connector_tool-0.0.3/db_connector/utils/path_utils.py +428 -0
- db_connector_tool-0.0.3/db_connector_tool.egg-info/PKG-INFO +237 -0
- db_connector_tool-0.0.3/db_connector_tool.egg-info/SOURCES.txt +26 -0
- db_connector_tool-0.0.3/db_connector_tool.egg-info/dependency_links.txt +1 -0
- db_connector_tool-0.0.3/db_connector_tool.egg-info/entry_points.txt +2 -0
- db_connector_tool-0.0.3/db_connector_tool.egg-info/requires.txt +24 -0
- db_connector_tool-0.0.3/db_connector_tool.egg-info/top_level.txt +1 -0
- db_connector_tool-0.0.3/pyproject.toml +77 -0
- db_connector_tool-0.0.3/setup.cfg +4 -0
- db_connector_tool-0.0.3/tests/test_cli.py +85 -0
- db_connector_tool-0.0.3/tests/test_config.py +90 -0
- db_connector_tool-0.0.3/tests/test_crypto.py +66 -0
- db_connector_tool-0.0.3/tests/test_database.py +53 -0
- db_connector_tool-0.0.3/tests/test_path_helper.py +304 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 wangquanqing
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: db-connector-tool
|
|
3
|
+
Version: 0.0.3
|
|
4
|
+
Summary: 跨平台数据库连接管理工具
|
|
5
|
+
Author-email: wangquanqing <wangquanqing1636@sina.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/wangquanqing/db-connector-project
|
|
8
|
+
Project-URL: Repository, https://github.com/wangquanqing/db-connector-project
|
|
9
|
+
Project-URL: Documentation, https://db-connector-tool.readthedocs.io
|
|
10
|
+
Project-URL: Issue Tracker, https://github.com/wangquanqing/db-connector-project/issues
|
|
11
|
+
Keywords: database,connection,manager,orm,sqlalchemy,cli
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Database
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Requires-Python: >=3.8
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: cryptography>=41.0.0
|
|
29
|
+
Requires-Dist: tomli-w>=1.0.0
|
|
30
|
+
Requires-Dist: sqlalchemy>=2.0.0
|
|
31
|
+
Requires-Dist: oracledb>=2.0.0
|
|
32
|
+
Requires-Dist: psycopg>=3.0.0
|
|
33
|
+
Requires-Dist: pymysql>=1.1.0
|
|
34
|
+
Requires-Dist: pymssql>=2.2.0
|
|
35
|
+
Provides-Extra: dev
|
|
36
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
37
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
38
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
39
|
+
Requires-Dist: flake8>=6.0.0; extra == "dev"
|
|
40
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
41
|
+
Requires-Dist: twine>=4.0.0; extra == "dev"
|
|
42
|
+
Requires-Dist: build>=0.10.0; extra == "dev"
|
|
43
|
+
Provides-Extra: docs
|
|
44
|
+
Requires-Dist: sphinx>=7.0.0; extra == "docs"
|
|
45
|
+
Requires-Dist: sphinx-rtd-theme>=1.0.0; extra == "docs"
|
|
46
|
+
Provides-Extra: cli
|
|
47
|
+
Requires-Dist: click>=8.0.0; extra == "cli"
|
|
48
|
+
Requires-Dist: rich>=13.0.0; extra == "cli"
|
|
49
|
+
Dynamic: license-file
|
|
50
|
+
|
|
51
|
+
# DB Connector - 跨平台数据库连接管理模块
|
|
52
|
+
|
|
53
|
+
一个安全、跨平台的Python数据库连接管理模块,支持主流数据库并提供加密存储功能。
|
|
54
|
+
|
|
55
|
+
## 特性
|
|
56
|
+
|
|
57
|
+
- 🔐 **安全加密**: 使用 `cryptography.fernet` 加密敏感连接信息
|
|
58
|
+
- 📁 **配置管理**: 基于 TOML 的配置文件(读取:`tomllib`,写入:`tomli-w`)
|
|
59
|
+
- 🗄️ **多数据库支持**:
|
|
60
|
+
- Oracle (oracledb)
|
|
61
|
+
- PostgreSQL (psycopg2/psycopg3)
|
|
62
|
+
- SQL Server (pymssql)
|
|
63
|
+
- MySQL (pymysql)
|
|
64
|
+
- SQLite (sqlite3)
|
|
65
|
+
- 🏗️ **ORM 集成**: 基于 SQLAlchemy 2.0+
|
|
66
|
+
- 📊 **完整日志**: 使用 logging 模块记录操作日志
|
|
67
|
+
- 🌐 **跨平台**: 支持 Windows、Linux、macOS
|
|
68
|
+
- 🧪 **完整测试**: 包含单元测试和集成测试
|
|
69
|
+
|
|
70
|
+
## 安装
|
|
71
|
+
|
|
72
|
+
### 要求
|
|
73
|
+
|
|
74
|
+
- Python >= 3.8
|
|
75
|
+
- 参见 `requirements.txt` 了解依赖详情
|
|
76
|
+
|
|
77
|
+
### 从源码安装
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
git clone https://github.com/yourusername/db-connector.git
|
|
81
|
+
cd db-connector
|
|
82
|
+
pip install -e .
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 从 PyPI 安装(未来计划)
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pip install db-connector
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## 快速开始
|
|
92
|
+
|
|
93
|
+
### 基础用法
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from db_connector import DatabaseManager
|
|
97
|
+
|
|
98
|
+
# 创建数据库管理器
|
|
99
|
+
db_manager = DatabaseManager()
|
|
100
|
+
|
|
101
|
+
# 添加数据库连接
|
|
102
|
+
mysql_config = {
|
|
103
|
+
'type': 'mysql',
|
|
104
|
+
'host': 'localhost',
|
|
105
|
+
'port': '3306',
|
|
106
|
+
'username': 'your_username',
|
|
107
|
+
'password': 'your_password',
|
|
108
|
+
'database': 'your_database'
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
db_manager.create_connection('my_mysql', mysql_config)
|
|
112
|
+
|
|
113
|
+
# 执行查询
|
|
114
|
+
results = db_manager.execute_query('my_mysql', 'SELECT * FROM users LIMIT 10')
|
|
115
|
+
print(results)
|
|
116
|
+
|
|
117
|
+
# 关闭连接
|
|
118
|
+
db_manager.close_all_connections()
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 多数据库操作
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from db_connector import DatabaseManager
|
|
125
|
+
|
|
126
|
+
db_manager = DatabaseManager()
|
|
127
|
+
|
|
128
|
+
# 配置多个数据库
|
|
129
|
+
databases = {
|
|
130
|
+
'app_db': {
|
|
131
|
+
'type': 'postgresql',
|
|
132
|
+
'host': 'db.server.com',
|
|
133
|
+
'username': 'user',
|
|
134
|
+
'password': 'pass',
|
|
135
|
+
'database': 'application'
|
|
136
|
+
},
|
|
137
|
+
'log_db': {
|
|
138
|
+
'type': 'sqlite',
|
|
139
|
+
'database': '/path/to/logs.db'
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
for name, config in databases.items():
|
|
144
|
+
db_manager.create_connection(name, config)
|
|
145
|
+
|
|
146
|
+
# 跨数据库操作
|
|
147
|
+
users = db_manager.execute_query('app_db', 'SELECT * FROM users')
|
|
148
|
+
db_manager.execute_command('log_db', 'INSERT INTO access_log VALUES (?, ?)', (user_id, 'login'))
|
|
149
|
+
|
|
150
|
+
db_manager.close_all_connections()
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## 配置说明
|
|
154
|
+
|
|
155
|
+
### 连接配置格式
|
|
156
|
+
|
|
157
|
+
每个数据库连接支持以下配置:
|
|
158
|
+
|
|
159
|
+
**MySQL/PostgreSQL/SQL Server/Oracle:**
|
|
160
|
+
```python
|
|
161
|
+
{
|
|
162
|
+
'type': 'mysql', # mysql, postgresql, mssql, oracle
|
|
163
|
+
'host': 'localhost',
|
|
164
|
+
'port': '3306',
|
|
165
|
+
'username': 'your_username',
|
|
166
|
+
'password': 'your_password',
|
|
167
|
+
'database': 'your_database'
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**SQLite:**
|
|
172
|
+
```python
|
|
173
|
+
{
|
|
174
|
+
'type': 'sqlite',
|
|
175
|
+
'database': '/path/to/database.db' # 或 ':memory:' 用于内存数据库
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### 配置文件位置
|
|
180
|
+
|
|
181
|
+
- **配置文件**: `~/.config/db_connector/connections.toml`
|
|
182
|
+
- **日志文件**: `~/.config/db_connector/logs/db_connector.log`
|
|
183
|
+
- **加密密钥**: `~/.config/db_connector/encryption.key`
|
|
184
|
+
|
|
185
|
+
## API 参考
|
|
186
|
+
|
|
187
|
+
### DatabaseManager
|
|
188
|
+
|
|
189
|
+
主要管理类,提供以下方法:
|
|
190
|
+
|
|
191
|
+
- `create_connection(name, config)`: 创建连接配置
|
|
192
|
+
- `get_connection(name)`: 获取数据库连接
|
|
193
|
+
- `execute_query(connection_name, query, params)`: 执行查询
|
|
194
|
+
- `execute_command(connection_name, command, params)`: 执行命令
|
|
195
|
+
- `test_connection(name)`: 测试连接
|
|
196
|
+
- `list_connections()`: 列出所有连接
|
|
197
|
+
- `remove_connection(name)`: 删除连接
|
|
198
|
+
- `close_connection(name)`: 关闭连接
|
|
199
|
+
- `close_all_connections()`: 关闭所有连接
|
|
200
|
+
|
|
201
|
+
## 开发
|
|
202
|
+
|
|
203
|
+
### 运行测试
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
# 运行所有测试
|
|
207
|
+
pytest
|
|
208
|
+
|
|
209
|
+
# 运行特定测试模块
|
|
210
|
+
pytest tests/test_database.py
|
|
211
|
+
|
|
212
|
+
# 带覆盖率的测试
|
|
213
|
+
pytest --cov=db_connector tests/
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### 代码风格
|
|
217
|
+
|
|
218
|
+
本项目使用 Black 代码格式化工具:
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
black db_connector/ tests/ examples/
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## 许可证
|
|
225
|
+
|
|
226
|
+
MIT License - 详见 LICENSE 文件
|
|
227
|
+
|
|
228
|
+
## 贡献
|
|
229
|
+
|
|
230
|
+
欢迎提交 Issue 和 Pull Request!
|
|
231
|
+
|
|
232
|
+
## 支持
|
|
233
|
+
|
|
234
|
+
如有问题请:
|
|
235
|
+
1. 查看文档和示例
|
|
236
|
+
2. 提交 GitHub Issue
|
|
237
|
+
3. 联系维护者
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# DB Connector - 跨平台数据库连接管理模块
|
|
2
|
+
|
|
3
|
+
一个安全、跨平台的Python数据库连接管理模块,支持主流数据库并提供加密存储功能。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 🔐 **安全加密**: 使用 `cryptography.fernet` 加密敏感连接信息
|
|
8
|
+
- 📁 **配置管理**: 基于 TOML 的配置文件(读取:`tomllib`,写入:`tomli-w`)
|
|
9
|
+
- 🗄️ **多数据库支持**:
|
|
10
|
+
- Oracle (oracledb)
|
|
11
|
+
- PostgreSQL (psycopg2/psycopg3)
|
|
12
|
+
- SQL Server (pymssql)
|
|
13
|
+
- MySQL (pymysql)
|
|
14
|
+
- SQLite (sqlite3)
|
|
15
|
+
- 🏗️ **ORM 集成**: 基于 SQLAlchemy 2.0+
|
|
16
|
+
- 📊 **完整日志**: 使用 logging 模块记录操作日志
|
|
17
|
+
- 🌐 **跨平台**: 支持 Windows、Linux、macOS
|
|
18
|
+
- 🧪 **完整测试**: 包含单元测试和集成测试
|
|
19
|
+
|
|
20
|
+
## 安装
|
|
21
|
+
|
|
22
|
+
### 要求
|
|
23
|
+
|
|
24
|
+
- Python >= 3.8
|
|
25
|
+
- 参见 `requirements.txt` 了解依赖详情
|
|
26
|
+
|
|
27
|
+
### 从源码安装
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
git clone https://github.com/yourusername/db-connector.git
|
|
31
|
+
cd db-connector
|
|
32
|
+
pip install -e .
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 从 PyPI 安装(未来计划)
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install db-connector
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## 快速开始
|
|
42
|
+
|
|
43
|
+
### 基础用法
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from db_connector import DatabaseManager
|
|
47
|
+
|
|
48
|
+
# 创建数据库管理器
|
|
49
|
+
db_manager = DatabaseManager()
|
|
50
|
+
|
|
51
|
+
# 添加数据库连接
|
|
52
|
+
mysql_config = {
|
|
53
|
+
'type': 'mysql',
|
|
54
|
+
'host': 'localhost',
|
|
55
|
+
'port': '3306',
|
|
56
|
+
'username': 'your_username',
|
|
57
|
+
'password': 'your_password',
|
|
58
|
+
'database': 'your_database'
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
db_manager.create_connection('my_mysql', mysql_config)
|
|
62
|
+
|
|
63
|
+
# 执行查询
|
|
64
|
+
results = db_manager.execute_query('my_mysql', 'SELECT * FROM users LIMIT 10')
|
|
65
|
+
print(results)
|
|
66
|
+
|
|
67
|
+
# 关闭连接
|
|
68
|
+
db_manager.close_all_connections()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 多数据库操作
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from db_connector import DatabaseManager
|
|
75
|
+
|
|
76
|
+
db_manager = DatabaseManager()
|
|
77
|
+
|
|
78
|
+
# 配置多个数据库
|
|
79
|
+
databases = {
|
|
80
|
+
'app_db': {
|
|
81
|
+
'type': 'postgresql',
|
|
82
|
+
'host': 'db.server.com',
|
|
83
|
+
'username': 'user',
|
|
84
|
+
'password': 'pass',
|
|
85
|
+
'database': 'application'
|
|
86
|
+
},
|
|
87
|
+
'log_db': {
|
|
88
|
+
'type': 'sqlite',
|
|
89
|
+
'database': '/path/to/logs.db'
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
for name, config in databases.items():
|
|
94
|
+
db_manager.create_connection(name, config)
|
|
95
|
+
|
|
96
|
+
# 跨数据库操作
|
|
97
|
+
users = db_manager.execute_query('app_db', 'SELECT * FROM users')
|
|
98
|
+
db_manager.execute_command('log_db', 'INSERT INTO access_log VALUES (?, ?)', (user_id, 'login'))
|
|
99
|
+
|
|
100
|
+
db_manager.close_all_connections()
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## 配置说明
|
|
104
|
+
|
|
105
|
+
### 连接配置格式
|
|
106
|
+
|
|
107
|
+
每个数据库连接支持以下配置:
|
|
108
|
+
|
|
109
|
+
**MySQL/PostgreSQL/SQL Server/Oracle:**
|
|
110
|
+
```python
|
|
111
|
+
{
|
|
112
|
+
'type': 'mysql', # mysql, postgresql, mssql, oracle
|
|
113
|
+
'host': 'localhost',
|
|
114
|
+
'port': '3306',
|
|
115
|
+
'username': 'your_username',
|
|
116
|
+
'password': 'your_password',
|
|
117
|
+
'database': 'your_database'
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**SQLite:**
|
|
122
|
+
```python
|
|
123
|
+
{
|
|
124
|
+
'type': 'sqlite',
|
|
125
|
+
'database': '/path/to/database.db' # 或 ':memory:' 用于内存数据库
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### 配置文件位置
|
|
130
|
+
|
|
131
|
+
- **配置文件**: `~/.config/db_connector/connections.toml`
|
|
132
|
+
- **日志文件**: `~/.config/db_connector/logs/db_connector.log`
|
|
133
|
+
- **加密密钥**: `~/.config/db_connector/encryption.key`
|
|
134
|
+
|
|
135
|
+
## API 参考
|
|
136
|
+
|
|
137
|
+
### DatabaseManager
|
|
138
|
+
|
|
139
|
+
主要管理类,提供以下方法:
|
|
140
|
+
|
|
141
|
+
- `create_connection(name, config)`: 创建连接配置
|
|
142
|
+
- `get_connection(name)`: 获取数据库连接
|
|
143
|
+
- `execute_query(connection_name, query, params)`: 执行查询
|
|
144
|
+
- `execute_command(connection_name, command, params)`: 执行命令
|
|
145
|
+
- `test_connection(name)`: 测试连接
|
|
146
|
+
- `list_connections()`: 列出所有连接
|
|
147
|
+
- `remove_connection(name)`: 删除连接
|
|
148
|
+
- `close_connection(name)`: 关闭连接
|
|
149
|
+
- `close_all_connections()`: 关闭所有连接
|
|
150
|
+
|
|
151
|
+
## 开发
|
|
152
|
+
|
|
153
|
+
### 运行测试
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
# 运行所有测试
|
|
157
|
+
pytest
|
|
158
|
+
|
|
159
|
+
# 运行特定测试模块
|
|
160
|
+
pytest tests/test_database.py
|
|
161
|
+
|
|
162
|
+
# 带覆盖率的测试
|
|
163
|
+
pytest --cov=db_connector tests/
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 代码风格
|
|
167
|
+
|
|
168
|
+
本项目使用 Black 代码格式化工具:
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
black db_connector/ tests/ examples/
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## 许可证
|
|
175
|
+
|
|
176
|
+
MIT License - 详见 LICENSE 文件
|
|
177
|
+
|
|
178
|
+
## 贡献
|
|
179
|
+
|
|
180
|
+
欢迎提交 Issue 和 Pull Request!
|
|
181
|
+
|
|
182
|
+
## 支持
|
|
183
|
+
|
|
184
|
+
如有问题请:
|
|
185
|
+
1. 查看文档和示例
|
|
186
|
+
2. 提交 GitHub Issue
|
|
187
|
+
3. 联系维护者
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""
|
|
2
|
+
DB Connector - 跨平台数据库连接管理模块
|
|
3
|
+
======================================
|
|
4
|
+
|
|
5
|
+
一个功能强大的数据库连接管理工具,支持多种数据库类型。
|
|
6
|
+
|
|
7
|
+
特性:
|
|
8
|
+
- 支持 Oracle, PostgreSQL, SQL Server, MySQL, SQLite
|
|
9
|
+
- 统一的连接配置管理
|
|
10
|
+
- 安全的密码加密存储
|
|
11
|
+
- 命令行界面和API接口
|
|
12
|
+
- 跨平台兼容
|
|
13
|
+
|
|
14
|
+
版本: 1.0.0
|
|
15
|
+
作者: wangquanqing <wangquanqing1636@sina.com>
|
|
16
|
+
许可证: MIT
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
__version__ = "0.0.3"
|
|
20
|
+
__author__ = "wangquanqing <wangquanqing1636@sina.com>"
|
|
21
|
+
__license__ = "MIT"
|
|
22
|
+
|
|
23
|
+
# 核心模块导入
|
|
24
|
+
from .core.config import ConfigManager
|
|
25
|
+
from .core.connections import DatabaseManager
|
|
26
|
+
from .core.exceptions import (
|
|
27
|
+
ConfigError,
|
|
28
|
+
ConnectionError,
|
|
29
|
+
CryptoError,
|
|
30
|
+
DatabaseError,
|
|
31
|
+
DBConnectorError,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
# CLI模块导入(可选,安装时可能不可用)
|
|
35
|
+
try:
|
|
36
|
+
from .cli import DBConnectorCLI
|
|
37
|
+
from .cli import main as cli_main
|
|
38
|
+
except ImportError as e:
|
|
39
|
+
# 在安装过程中或缺少依赖时,CLI模块可能不可用。这不会影响核心功能的使用
|
|
40
|
+
DBConnectorCLI = None
|
|
41
|
+
cli = None
|
|
42
|
+
|
|
43
|
+
# 公共API导出列表
|
|
44
|
+
__all__ = [
|
|
45
|
+
# 核心管理器
|
|
46
|
+
"ConfigManager",
|
|
47
|
+
"DatabaseManager",
|
|
48
|
+
# 异常类
|
|
49
|
+
"DBConnectorError",
|
|
50
|
+
"ConfigError",
|
|
51
|
+
"CryptoError",
|
|
52
|
+
"DatabaseError",
|
|
53
|
+
"ConnectionError",
|
|
54
|
+
# CLI相关(可选)
|
|
55
|
+
"DBConnectorCLI",
|
|
56
|
+
"cli",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# 包级别便捷函数
|
|
61
|
+
def get_version() -> str:
|
|
62
|
+
"""
|
|
63
|
+
获取当前模块版本号
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
str: 版本号字符串,格式为 'x.y.z'
|
|
67
|
+
|
|
68
|
+
Example:
|
|
69
|
+
>>> from db_connector import get_version
|
|
70
|
+
>>> print(get_version())
|
|
71
|
+
'1.0.0'
|
|
72
|
+
"""
|
|
73
|
+
return __version__
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def get_supported_databases() -> list:
|
|
77
|
+
"""
|
|
78
|
+
获取支持的数据库类型列表
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
list: 支持的数据库类型名称列表
|
|
82
|
+
|
|
83
|
+
Example:
|
|
84
|
+
>>> from db_connector import get_supported_databases
|
|
85
|
+
>>> print(get_supported_databases())
|
|
86
|
+
['oracle', 'postgresql', 'sqlserver', 'mysql', 'sqlite']
|
|
87
|
+
"""
|
|
88
|
+
return ["oracle", "postgresql", "sqlserver", "mysql", "sqlite"]
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
# 包初始化检查
|
|
92
|
+
def _check_dependencies() -> None:
|
|
93
|
+
"""
|
|
94
|
+
检查必要的依赖是否可用
|
|
95
|
+
|
|
96
|
+
Raises:
|
|
97
|
+
ImportError: 如果核心依赖不可用
|
|
98
|
+
"""
|
|
99
|
+
try:
|
|
100
|
+
import sqlalchemy
|
|
101
|
+
except ImportError:
|
|
102
|
+
raise ImportError("SQLAlchemy是必需的依赖,请安装: pip install sqlalchemy")
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# 在导入时执行依赖检查
|
|
106
|
+
try:
|
|
107
|
+
_check_dependencies()
|
|
108
|
+
except ImportError as e:
|
|
109
|
+
# 记录警告但不阻止导入
|
|
110
|
+
import warnings
|
|
111
|
+
|
|
112
|
+
warnings.warn(f"依赖检查警告: {e}", ImportWarning)
|
|
113
|
+
|
|
114
|
+
# 包信息
|
|
115
|
+
__package_info__ = {
|
|
116
|
+
"name": "db_connector",
|
|
117
|
+
"version": __version__,
|
|
118
|
+
"author": __author__,
|
|
119
|
+
"license": __license__,
|
|
120
|
+
"description": "跨平台数据库连接管理工具",
|
|
121
|
+
"supported_databases": get_supported_databases(),
|
|
122
|
+
}
|