SQL minifier
SQL minifier provides methods and procedural macros to minify SQL code, optionally at compile time.
It removes both single-line --
and multi-line /* ... */
comments, unnecessary whitespaces, and shortens SQL keywords such as INTEGER
to INT
.
Installation
Add the following to your Cargo.toml
file:
[]
= "0.1.4"
or use the following command:
Examples
Suppose you have an SQL string and you want to minify it. You can use the minify_sql
function:
use minify_sql;
let minified: String = minify_sql;
assert_eq!;
If you want this to be done at compile time, you can use the minify_sql
macro:
use minify_sql;
const SQL_CONTENT: &str = minify_sql!;
assert_eq!;
A more complex SQL file such as:
-- 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.
(
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 */
We can load it and minify it at compile time using the load_sql
macro:
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))"
);
Features
We support the following features:
gluesql
: When enabled, the minifier will not minify BOOLEAN keywords to BOOL, as it is not supported by GlueSQL.