# Model/cache 手册
本手册是查阅入口。详细说明保留在旧路径:[Model/cache generation](../model-cache.md)。
## 能力概览
`rzcli model gen` 从 SQL schema 生成:
- entity struct
- repository trait
- 可选 SQLx repository skeleton
- cache key helper
- 可选 Redis cache skeleton 标记
## CLI
默认命令保持 MySQL/goctl SQL 子集和 SQLite SQLx skeleton:
```bash
cargo run -p rs-zero-cli -- model gen \
-s examples/model-cache/schema.sql \
-d target/generated \
--with-sqlx \
--with-redis-cache
```
显式选择 MySQL SQLx backend:
```bash
cargo run -p rs-zero-cli -- model gen \
-s examples/model-cache/schema.sql \
-d target/generated \
--with-sqlx \
--sqlx-backend mysql \
--with-redis-cache
```
PostgreSQL schema 使用显式方言。未指定 `--sqlx-backend` 时,`--dialect postgres --with-sqlx` 默认生成 `rs_zero::db::PostgresDatabasePool`:
```bash
cargo run -p rs-zero-cli -- model gen \
-s tests/fixtures/postgres/model/user.sql \
-d target/generated \
--dialect postgres \
--with-sqlx \
--with-redis-cache
```
也可以显式指定 SQLx 后端:
```bash
cargo run -p rs-zero-cli -- model gen \
-s tests/fixtures/postgres/model/user.sql \
-d target/generated \
--dialect postgres \
--sqlx-backend postgres \
--with-sqlx
```
## SQL parser 边界
支持常见 MySQL `CREATE TABLE` 子集,包含字段 metadata、table metadata、primary/unique/normal index 和复合索引。MySQL SQLx backend 需显式传 `--sqlx-backend mysql`,会生成 `rs_zero::db::MySqlDatabasePool` skeleton。
PostgreSQL 支持常见 `CREATE TABLE` 子集:双引号标识符、schema-qualified table name、`SERIAL` / `BIGSERIAL`、`GENERATED ... AS IDENTITY`、table-level constraint、`CREATE INDEX`、`COMMENT ON TABLE/COLUMN` 和常见字段类型映射。
完整 SQL grammar、数据库 introspection、迁移管理和 ORM 不是当前目标。
## 相关测试
- `cargo test -p rs-zero-cli --test model_codegen_integration`
- `cargo test -p rs-zero-cli --test model_adapter_codegen`
- `cargo test -p rs-zero-cli --test goctl_model_compat`
- `cargo test -p rs-zero-cli --test model_mysql_cli`
- `cargo test -p rs-zero-cli --test model_postgres_codegen`
- `cargo test -p rs-zero-cli --test model_postgres_cli`
- `cargo check -p rs-zero --no-default-features --features db-mysql`
- `cargo check -p rs-zero --no-default-features --features db-postgres`