#sql-database #sql #proc-macro #define

sql-table

使SQL数据库的非ORM查询更简洁

4个版本

0.1.4 2023年7月30日
0.1.3 2023年7月30日
0.1.2 2023年4月7日
0.1.1 2023年4月6日
0.1.0 2023年4月6日

数据库接口中排名第1968

Download history 128/week @ 2024-04-14 134/week @ 2024-04-21 7/week @ 2024-04-28 17/week @ 2024-05-05 86/week @ 2024-05-12 115/week @ 2024-05-19 102/week @ 2024-05-26 136/week @ 2024-06-02 86/week @ 2024-06-09 90/week @ 2024-06-16 47/week @ 2024-06-23 52/week @ 2024-06-30 65/week @ 2024-07-07 93/week @ 2024-07-14 84/week @ 2024-07-21 96/week @ 2024-07-28

每月下载量348

MIT许可

16KB
266

sql-table

使SQL数据库的非ORM查询更简洁。

Cargo Documentation License

示例

基本用法

use sql_table::{
    table, inject, 
    ForeignKeyName, IndexName, Qualified, Table, TableColumn, Unquote,
};

table!(Person: "person" {
    Name: "name",
    Country: "country",
});

assert_eq!(
    inject!("
        SELECT #{Person::Name}#
        FROM #{Person}#
        WHERE #{Person::Country}# = 'United States'
    "),
    format!("
        SELECT name
        FROM person
        WHERE country = 'United States'
    ")
);

如果您需要更复杂的行为

use sql_table::{
    table, inject, 
    ForeignKeyName, IndexName, Qualified, Table, TableColumn, Unquote,
};

// If you want a specific table identifiers to be displayed by default as quoted
// just add `quote` parameter after table definition.
table!(SimCard: "sim card" {
    Owner: "owner",
    PhoneNumber: "phone number",
}, quote: "`");

table!(Person: "person" {
    Name: "name",
    Country: "country",
});

// Identifiers can also be unquoted using `.unquoted()` method,
// or, in case of field names, they can be converted into qualified form 
// using `.qualified()` method of trait `Qualified` provided by this library
// and implemented for all fields of each generated table.
// Said methods can also be chained `.unquoted().qualified()`.
assert_eq!(
    inject!("
        SELECT #{SimCard::PhoneNumber.qualified()}#
        FROM #{SimCard}#
        JOIN #{Person}# ON #{Person::Name.qualified()}# = #{SimCard::Owner.unquoted().qualified()}#
        WHERE #{Person::Country.qualified()}# = 'United States'
    "),
    format!("
        SELECT `sim card`.`phone number`
        FROM `sim card`
        JOIN person ON person.name = `sim card`.owner
        WHERE person.country = 'United States'
    ")
);

// Format: fk_<target table>_<target field>_<source table>_<source field>
// The whole name will get quoted if any of it's components (table/field names) are quoted.
// In this case quotation will be done using the first found quote
// of corresponding components listed in the key name.
assert_eq!(
    SimCard::Owner.foreign_key_name(Person::Name),
    "`fk_sim card_owner_person_name`"
);
assert_eq!(
    <SimCard as Table>::Unquoted::Owner.unquoted().foreign_key_name(Person::Name),
    "fk_sim card_owner_person_name"
);

// Format: ix_<table>_<field 1>_<field 2>_..._<field n>
// All fields must be either quoted or unquoted.
// The whole name will get quoted if either table or fields are quoted.
assert_eq!(
    Person::index_name(&[Person::Name]),
    "ix_person_name"
);
assert_eq!(
    SimCard::index_name(&[SimCard::Owner, SimCard::PhoneNumber]),
    "`ix_sim card_owner_phone number`"
);
assert_eq!(
    <SimCard as Table>::Unquoted::index_name(&[
        <SimCard as Table>::Unquoted::Owner.unquoted(), 
        <SimCard as Table>::Unquoted::PhoneNumber.unquoted()
    ]),
    "ix_sim card_owner_phone number"
);

// If you need a different naming format for foreign keys, indices or anything else
// you can implement custom `IndexName`, `ForeignKeyName`, etc. traits
// using provided by this library default implementations as a reference.

依赖项