Show HN: SQLYac – Structured SQL files you can run from the terminal

github.com

3 points by kalli 6 hours ago

Inspired by .http files and HTTPYac, SQLYac lets you write executable SQL documentation with variables, safety prompts, and per-query targeting.

Instead of scattered .sql files or copy-pasting into clients, write annotated queries once and pipe them to any sql database client. Variables get substituted, destructive queries can prompt for confirmation, and your schema stays documented alongside actual queries.

For example, if you have a `queries.sql` file like this:

  ---
  -- @name QueryWithVariables
  SELECT * 
  FROM orders o, users u
  WHERE u.id=@user_id 
  AND u.active=@active
  AND o.status=@status
  AND o.user_id=u.id
  LIMIT @lim;  

  -- some variables
  SET @user_id=2;
  SET @lim=10;
  SET @active=true;
  SET @status="completed";

  ---

You can execute a query in the terminal like this: `sqlyac queries.sql QueryWithVariables | mysql -u user -p database` (or `psql`, `sqlite3` or whatever)

Written in go, `go install github.com/kalli/sqlyac@latest` for a small-ish binary: https://github.com/kalli/sqlyac

Blogpost with more info: https://dev.karltryggvason.com/sqlyac-structure-and-tooling-...