Skip to content

Latest commit

 

History

History
109 lines (74 loc) · 3.06 KB

README.md

File metadata and controls

109 lines (74 loc) · 3.06 KB

zsqlite-minify

Minify SQL scripts.

Note: This is a quick hack to minify some SQLs I use at a personal project. It doesn't cover the entire SQL.

You can pass option minify_root_path to the dependency. This will automatically minify and embed all files within that directory. You can grab the SQL by using embedMinifiedSql.

Install

Add as a dependency:

zig fetch --save "https://github.com/thiago-negri/zsqlite-minify/archive/refs/heads/master.zip"

Add to your build:

// Add SQL Minify
const zsqlite_minify = b.dependency("zsqlite-minify", .{
    .target = target,
    .optimize = optimize,
    .minify_root_path = @as([]const u8, "./src/sqls") // Where to minify and SQL embed files from
    .minify_files_prefix = @as([]const u8, "sqls") // Prefix to all embedded files, good to enable 'gf' in VIM
});
const zsqlite_minify_module = zsqlite_minify.module("zsqlite-minify");
exe.root_module.addImport("zsqlite-minify", zsqlite_minify_module);

Use

const minify = @import("zsqlite-minify");

// runtime minification
const sql = "SELECT a, b FROM foo;";
const alloc = ...; // how the resulting SQL will be allocated
const minified_sql = try minify.minifySql(alloc, sql);
defer alloc.free(minified_sql);

// build time minification
const built_minified_sql = minify.embedMinifiedSql("sqls/foo/create.sql");

Comptime

Hopefully minifySql can be done at comptime at some point. Right now it's impossible because it uses an allocator. Check zig#1291.

What is possible is to use it during build and provide the minified SQLs as a module.

The downside of doing it at build time is that the SQL is not in the same source file that uses it.

Build time alternative 1

const minifySql = @import("zsqlite-minify").minifySql;

pub fn build(b: *std.Build) !void {
    const sqls: []const [:0]const u8 = &[_][:0]const u8{
        \\SELECT foo, bar
        \\ FROM spam
        \\ WHERE eggs > ?
        \\   AND eggs < ?;
        ,
        \\INSERT INTO spam ( foo, bar )
        \\ VALUES          (   ?,   ? );
    };
    const sqls_len = comptime sqls.len;
    var minified_sqls: [sqls_len][:0]const u8 = undefined;
    for (sqls, 0..) |sql, index| {
        minified_sqls[index] = try minifySql(b.allocator, sql);
    }

    const options = b.addOptions();
    options.addOption([]const [:0]const u8, "minified_sqls", &minified_sqls);

    your_exe.root_module.addImport("minified-sqls", options.createModule());
}

Then it's possible to import and use the minified SQLs:

const sqls = @import("minified-sqls").minified_sqls;

std.debug.print("Minified SQLs:\n", .{});
for (sqls) |sql| {
    std.debug.print("{s}\n", .{sql});
}

This is used by zsqlite-migrate to embed minified SQL migrations.

Build time alternative 2

Use the minify_root_path option and embedMinifySql function.

This is used by zsqlite-demo to embed minified SQL statements.

Also used as part of the tests in this project.