cooSql
Python implementation of a lightweight SQL database system for educational purposes. cooSql is designed to help understand database internals with a clean, modular architecture focusing on SQL parsing, query execution, and storage management.
项目结构
cooSql/
├── error.py # 错误定义
├── sql/ # SQL 模块
│ ├── parser/ # SQL 解析器
│ │ ├── ast.py # 抽象语法树定义
│ │ ├── lexer.py # 词法分析器
│ │ └── parser.py # 语法分析器
│ ├── types/ # 数据类型定义
│ │ └── data_types.py # 数据类型和值
│ └── schema.py # 表模式定义
├── storage/ # 存储模块
│ ├── engine.py # 存储引擎接口
│ └── memory.py # 内存存储引擎实现
└── tests/ # 测试
├── test_lexer.py # 词法分析器测试
└── test_parser.py # 语法分析器测试
安装
# 从源码安装
git clone https://github.com/yourusername/cooSql.git
cd cooSql
pip install -e .
快速开始
from storage.disk import DiskEngine
from sql.engine.kv import KVEngine
from sql.session import Session
# 创建数据库连接
storage_engine = DiskEngine("test.db")
kv_engine = KVEngine(storage_engine)
transaction = kv_engine.begin()
session = Session(transaction)
# 创建表
session.execute("""
CREATE TABLE users (
id INTEGER NOT NULL,
name STRING NOT NULL,
age INTEGER,
email STRING
);
""")
# 插入数据
session.execute("INSERT INTO users VALUES (1, 'Alice', 30, 'alice@example.com');")
# 查询数据
results = session.execute("SELECT * FROM users;")
for row in results:
print([col.value for col in row])
# 提交事务
transaction.commit()
# 关闭连接
storage_engine.close()
功能特性
目前支持的 SQL 语法:
- Create Table
CREATE TABLE table_name (
[ column_name data_type [ column_constraint [...] ] ]
[, ... ]
);
其中 data_type 可以是:
- BOOLEAN(BOOL): true | false
- FLOAT(DOUBLE)
- INTEGER(INT)
- STRING(TEXT, VARCHAR)
列约束可以是: [ NOT NULL | NULL | DEFAULT expr ]
- Insert Into
INSERT INTO table_name
[ ( column_name [, ...] ) ]
VALUES ( expr [, ...] );
- Select * From
SELECT * FROM table_name;
测试
运行测试:
# 单元测试
python -m tests.test_lexer
python -m tests.test_parser
# 或者使用pytest运行所有测试
pytest
示例
查看 examples/ 目录了解更多用法示例:
examples/basic_usage/: 基本使用方法示例,包括简单查询、事务处理等- 批量操作示例: 展示如何进行批量数据操作和压力测试
贡献
欢迎为cooSql做出贡献!请参考以下步骤:
- Fork项目
- 创建功能分支 (
git checkout -b feature/amazing-feature) - 提交更改 (
git commit -m 'Add some amazing feature') - 推送到分支 (
git push origin feature/amazing-feature) - 创建Pull Request
许可证
该项目采用MIT许可证 - 详细信息请参阅LICENSE文件
Metadata
Release files for cooSql 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| coosql-0.1.0.tar.gz | 252.1 kB | Without provenance | Details for coosql-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| coosql-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for coosql-0.1.0-py3-none-any.whl |
Total release size: 574.7 kB
Release files / coosql-0.1.0.tar.gz
| Download URL | coosql-0.1.0.tar.gz |
|---|---|
| Size | 252.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a63ff5c716c7748abf00a3260c4067ec206a430ab2fe80d5625374cfda4b05d7
|
|
BLAKE2b-256 checksum How to use checksums |
c79d364c5c4904bc84435e6c34f05c658e4b99976c08650eb5438ae35f1d9c6c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Release files / coosql-0.1.0-py3-none-any.whl
| Download URL | coosql-0.1.0-py3-none-any.whl |
|---|---|
| Size | 322.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c42687b956fb6ba7ebe9c78e054c620143157ff5e0b76f96c8edef18be3facd7
|
|
BLAKE2b-256 checksum How to use checksums |
20745989fc24b35ef83dc4e5c17e5c5dc80c089e0f9c435e9371293f136fec14
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Security for cooSql 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.