#minify #sql #macro #compression #compile-time #primary-key

minify_sql

提供最小化SQL查询功能的crate

4个版本

0.1.5 2024年6月13日
0.1.4 2024年4月16日
0.1.3 2024年4月16日
0.1.2 2024年4月16日

#473 in 数据库接口

Download history 356/week @ 2024-04-14 24/week @ 2024-04-21 7/week @ 2024-04-28 18/week @ 2024-05-19 8/week @ 2024-05-26 37/week @ 2024-06-02 142/week @ 2024-06-09 31/week @ 2024-06-16 8/week @ 2024-06-23 3/week @ 2024-06-30 6/week @ 2024-07-14 1/week @ 2024-07-21 73/week @ 2024-07-28

82 每月下载量
3 crate 中使用

GPL-3.0 许可证

14KB
79 代码行

SQL最小化工具

Build status Crates.io Documentation License: GPL v3

SQL最小化工具 提供了最小化SQL代码的方法和过程宏,可选在编译时进行。它删除了单行 -- 和多行 /* ... */ 注释,不必要的空白字符,并将SQL关键字如 INTEGER 缩短为 INT

安装

将以下内容添加到您的 Cargo.toml 文件中

[dependencies]
sql_minifier = "0.1.4"

或者使用以下命令

cargo add sql_minifier

示例

假设您有一个SQL字符串并且想要最小化它。您可以使用 minify_sql 函数

use sql_minifier::minify_sql;

let minified: String = minify_sql(
    "-- Your SQL goes here
CREATE TABLE IF NOT EXISTS taxa (
    -- The unique identifier for the taxon
    id UUID PRIMARY KEY,
    -- The scientific name of the taxon
    name TEXT NOT NULL,
    -- The NCBI Taxon ID is a unique identifier for a taxon in the NCBI Taxonomy database
    -- which may be NULL when this taxon is not present in the NCBI Taxonomy database.
    ncbi_taxon_id INTEGER
);"
);

assert_eq!(
    minified,
    "CREATE TABLE IF NOT EXISTS taxa(id UUID PRIMARY KEY,name TEXT NOT NULL,ncbi_taxon_id INT)"
);

如果要在编译时完成此操作,可以使用 minify_sql

use sql_minifier::macros::minify_sql;

const SQL_CONTENT: &str = minify_sql!(
    "-- Your SQL goes here
CREATE TABLE IF NOT EXISTS taxa (
    -- The unique identifier for the taxon
    id UUID PRIMARY KEY,
    -- The scientific name of the taxon
    name TEXT NOT NULL,
    -- The NCBI Taxon ID is a unique identifier for a taxon in the NCBI Taxonomy database
    -- which may be NULL when this taxon is not present in the NCBI Taxonomy database.
    ncbi_taxon_id INTEGER
);"
);

assert_eq!(
    SQL_CONTENT,
    "CREATE TABLE IF NOT EXISTS taxa(id UUID PRIMARY KEY,name TEXT NOT NULL,ncbi_taxon_id INT)"
);

更复杂的 SQL文件,例如

-- SQL defining the container_horizontal_rules table.
-- The container horizontal rules define whether an item type can be placed next to another item type.
-- For instance a acid product cannot be placed next to a base product. Generally speaking, most items
-- can be placed next to each other, but some items cannot be placed next to each other. These rules
-- are defined in the form of a deny-list, meaning that if a rule is not defined, then the item type
-- can be placed next to any other item type. The rules are defined by an admin user, and are used to
-- enforce the placement rules when creating or updating items. Some items may only be placed next to
-- items that are within a certain temperature, humidity, or pressure range. These constraints are also
-- defined in the container rules.
CREATE TABLE container_horizontal_rules (
    id UUID PRIMARY KEY REFERENCES describables(id) ON DELETE CASCADE,
    item_type_id UUID REFERENCES item_categories(id) ON
    DELETE
        CASCADE,
        other_item_type_id UUID REFERENCES item_categories(id) ON
    DELETE
        CASCADE,
        minimum_temperature FLOAT DEFAULT NULL,
        maximum_temperature FLOAT DEFAULT NULL,
        minimum_humidity FLOAT DEFAULT NULL,
        maximum_humidity FLOAT DEFAULT NULL,
        minimum_pressure FLOAT DEFAULT NULL,
        maximum_pressure FLOAT DEFAULT NULL,
        CHECK (
            minimum_temperature IS NULL
            OR maximum_temperature IS NULL
            OR minimum_temperature <= maximum_temperature
        ),
        /* The minimum humidity must be less than or
        equal to the maximum humidity. */
        CHECK (
            minimum_humidity IS NULL
            OR maximum_humidity IS NULL
            OR minimum_humidity <= maximum_humidity
        ),
        CHECK (
            minimum_pressure IS NULL
            OR maximum_pressure IS NULL
            OR minimum_pressure <= maximum_pressure
        )
);
/* and other multiline comment */

我们可以使用 load_sql 宏在编译时加载和最小化它

use sql_minifier::macros::load_sql;

const SQL_CONTENT: &str = load_sql!("tests/test_file_3.sql");

assert_eq!(
    SQL_CONTENT,
    "CREATE TABLE container_horizontal_rules(id UUID PRIMARY KEY REFERENCES describables(id)ON DELETE CASCADE,item_type_id UUID REFERENCES item_categories(id)ON DELETE CASCADE,other_item_type_id UUID REFERENCES item_categories(id)ON DELETE CASCADE,minimum_temperature FLOAT DEFAULT NULL,maximum_temperature FLOAT DEFAULT NULL,minimum_humidity FLOAT DEFAULT NULL,maximum_humidity FLOAT DEFAULT NULL,minimum_pressure FLOAT DEFAULT NULL,maximum_pressure FLOAT DEFAULT NULL,CHECK(minimum_temperature IS NULL OR maximum_temperature IS NULL OR minimum_temperature<=maximum_temperature),CHECK(minimum_humidity IS NULL OR maximum_humidity IS NULL OR minimum_humidity<=maximum_humidity),CHECK(minimum_pressure IS NULL OR maximum_pressure IS NULL OR minimum_pressure<=maximum_pressure))"
);

特性

我们支持以下特性

  • gluesql:当启用时,最小化器不会将 BOOLEAN 关键字最小化为 BOOL,因为 GlueSQL 不支持。

依赖关系

~2.2–3.5MB
~55K SLoC