10 个版本
0.2.1 | 2024 年 3 月 12 日 |
---|---|
0.2.0 | 2024 年 3 月 11 日 |
0.1.8 | 2024 年 1 月 8 日 |
0.1.7 | 2023 年 7 月 13 日 |
#1246 在 数据库接口
每月 44 次下载
38KB
812 行
dml-tools
dml-tools 是一个用于从/到代码或 YAML 文件生成和序列化 DML(数据操作语言)的库
请参阅 示例 了解用法。
安装
# Cargo.toml
[dependencies]
dml-tools = "0.2"
用法
要获取此 DML(数据操作语言)定义
CREATE SCHEMA my_schema AUTHORIZATION rw_user;
CREATE TABLE my_schema.users (
workspace text NULL,
user_id text NULL,
user_name text NULL,
full_name text NULL,
"role" text NULL,
is_locked text NULL,
email text NULL,
is_locked_by_supervisor text NULL,
CONSTRAINT my_table_my_reftable_field1_field2_fk FOREIGN KEY (field1,field2) REFERENCES schema1.my_reftable (rfield1, rfield2) ON DELETE RESTRICT ON UPDATE CASCADE
);
可以从这个 YAML 文件加载它
- tag: Schema
name: my_schema
owner: rw_user
- tag: Table
path:
schema: my_schema
name: users
otype: Table
fields:
- name: workspace
attributes:
unique: true
primary_key: true
- name: user_id
attributes:
empty: false
primary_key: true
- name: user_name
attributes:
unique: true
- name: full_name
attributes: {}
- name: role
attributes:
empty: false
- name: is_locked
attributes:
type: bool
empty: false
- name: email
attributes: {}
- name: is_locked_by_supervisor
attributes:
type: bool
empty: false
defval: 'false'
- tag: ForeignKey
table:
schema: schema1
name: my_table
otype: Table
fields:
- field1
- field2
ref_table:
schema: schema1
name: my_reftable
otype: Table
ref_fields:
- rfield1
- rfield2
on_delete: Restrict
on_update: Cascade
使用以下代码
use dml_tools::Loader;
use dml_tools::Processor;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let loader = Loader::new("examples/my-dmls.yaml")?;
// by default, it generates PostgreSQL DML statements
let proc = Processor::new_with_objects(loader.objects(), None);
proc.write_to_sql_file("my-generated.sql")?;
Ok(())
}
或者,使用以下代码生成它
use dml_tools::sql::*;
use dml_tools::Processor;
use std::error::Error;
use dml_tools::macros::*;
pub struct MyRoles {
pub rw: String,
pub ro: String,
pub upd: String,
}
impl Default for MyRoles {
fn default() -> Self {
MyRoles {
rw: "rw_user".into(),
ro: "ro_user".into(),
upd: "upd_user".into(),
}
}
}
fn main() -> Result<(), Box<dyn Error>> {
let mut processor= Processor::new(Some(Box::new(dml_tools::type_writers::Mysql{})));
let roles = MyRoles::default();
let my_schema = String::from("my_schema");
let schema = Schema::new(&my_schema, &roles.rw);
processor.add(&schema);
let oschema = ObjectPath::new_table_only(&schema.name);
add_grant!(processor, GrantType::All, &roles.rw, &oschema);
add_grant!(processor, GrantType::Usage, &roles.upd, &oschema);
add_grant!(processor, GrantType::Usage, &roles.ro, &oschema);
let u_fields = vec![
Field::new("workspace", &FieldAttributes::new_uk_pk(FieldType::Txt)),
Field::new("is_archived", &FieldAttributes::new(FieldType::Bool)),
Field::new("user_name", &FieldAttributes::new_uk(FieldType::Txt)),
Field::new("full_name", &FieldAttributes::new(FieldType::Txt)),
Field::new("role", &FieldAttributes::new_nn(FieldType::Txt)),
Field::new("is_locked_by_supervisor", &FieldAttributes::new_nn_def(FieldType::Bool, "false")),
];
let t_users = Table::new(&ObjectPath::new_table(&my_schema, "users"), u_fields, None);
// println!("{}", t_users);
processor.add(&t_users);
grant_perms!(&mut processor, roles, &t_users.path);
processor.serialize_to_yaml_file("my-generated.sql")?;
Ok(())
}
许可证
在 MIT 许可证下许可(《LICENSE-MIT》或 http://opensource.org/licenses/MIT》)
依赖关系
~2.4–3.5MB
~71K SLoC