baichun-framework-db 0.1.0

Database module for Baichun-Rust framework
Documentation
# baichun-framework-db

[![Crates.io](https://img.shields.io/crates/v/baichun-framework-db.svg)](https://crates.io/crates/baichun-framework-db)
[![Documentation](https://docs.rs/baichun-framework-db/badge.svg)](https://docs.rs/baichun-framework-db)
[![License](https://img.shields.io/crates/l/baichun-framework-db.svg)](LICENSE)

`baichun-framework-db` 是 Baichun Framework 的数据库模块,提供了一个简单而强大的数据库访问层。基于 SQLx,支持 MySQL、PostgreSQL 和 SQLite。

## 特性

- 连接池管理
  - 自动配置最佳连接数
  - 连接健康检查
  - 连接超时处理
- 多数据库支持
  - MySQL
  - PostgreSQL
  - SQLite
- 异步操作
  - 基于 tokio 的异步运行时
  - 非阻塞数据库操作
- 事务支持
  - 自动事务管理
  - 嵌套事务支持
- 错误处理
  - 详细的错误类型
  - 错误追踪和日志记录

## 安装

将以下内容添加到你的 `Cargo.toml` 文件中:

```toml
[dependencies]
baichun-framework-db = "0.1"
```

## 使用示例

```rust
use baichun_framework_db::{DbPool, DbConfig};

// 创建数据库配置
let config = DbConfig::new()
    .url("mysql://user:pass@localhost/db_name")
    .max_connections(10)
    .min_connections(5);

// 创建连接池
let pool = DbPool::new(config).await?;

// 执行查询
let rows = pool.execute("SELECT * FROM users WHERE id = ?", &[&1]).await?;

// 使用事务
let mut tx = pool.begin().await?;
tx.execute("INSERT INTO users (name) VALUES (?)", &[&"Alice"]).await?;
tx.execute("UPDATE users SET status = ? WHERE name = ?", &[&"active", &"Alice"]).await?;
tx.commit().await?;
```

## 配置示例

```rust
use baichun_framework_db::DbConfig;

// MySQL 配置
let mysql_config = DbConfig::new()
    .url("mysql://user:pass@localhost/db_name")
    .max_connections(20)
    .min_connections(5)
    .connect_timeout(Duration::from_secs(5))
    .idle_timeout(Duration::from_secs(300));

// PostgreSQL 配置
let pg_config = DbConfig::new()
    .url("postgresql://user:pass@localhost/db_name")
    .max_connections(20)
    .min_connections(5);

// SQLite 配置
let sqlite_config = DbConfig::new()
    .url("sqlite:///path/to/db.sqlite")
    .max_connections(1);  // SQLite 通常使用单连接
```

## 特性标记

- `mysql`:启用 MySQL 支持(默认)
- `postgres`:启用 PostgreSQL 支持
- `sqlite`:启用 SQLite 支持
- `all`:启用所有数据库支持

## 许可证

本项目采用 MIT 许可证。详见 [LICENSE](LICENSE) 文件。