like httpyac but for sql files
SQLYac lets you write multiple sql queries in a single file and execute them individually from the command line. Write your queries in an organized file, then pipe specific ones to your database tools of choice.
Ever find yourself with a bunch of sql snippets scattered across files, copying/pasting queries from your editor to the terminal or sql shell? SQLYac lets you:
- Organize related queries in one file
- Run specific queries by name
- Pipe results directly to mysql, sqlite3, psql, etc.
- Maintain your sql in version control with proper organization
- Add confirmation before running potentially destructive queries (see the config section below)
go install github.com/kalli/sqlyac
# or just `go run main.go` if you're feeling it# list all available queries
sqlyac example.sql
# run a specific query
sqlyac example.sql QueryName | mysql -u user -p database
# with flags (same thing)
sqlyac --file example.sql --name QueryName | sqlite3 db.sqliteUse three dashes (---) as separators between queries, annotate your queries with @name. Example:
---
-- @name GetActiveUsers
SELECT user_id, username, last_login
FROM Users
WHERE active = 1
ORDER BY last_login DESC;
---
---
-- @name GetLargeOrders
SELECT order_id, customer_id, total_amount
FROM Orders
WHERE total_amount > 1000
AND created_at > DATE_SUB(NOW(), INTERVAL 30 DAY);
---SQLYac supports variables for reusable values across queries. Define variables using SET @variable_name="value" syntax anywhere in your file, then reference them in queries using @variable_name. Here's an example:
-- @name QueryWithVariables
SELECT * FROM orders o, users u
WHERE u.id=@user_id
AND u.active=@active
AND o.status=@status
LIMIT @lim;
SET @user_id=2;
SET @lim=10;
SET @active=true;
SET @status="completed";When you run sqlyac file.sql QueryWithVariables, the output will be:
SELECT * FROM orders o, users u
WHERE u.id=2
AND u.active=true
AND o.status="completed"
LIMIT 10;Explore what's available
$ sqlyac example.sql
available queries:
CreateUsersTable
CreateOrdersTable
InsertSampleUsers
InsertSampleOrders
GetAllUsers
GetActiveUsers
GetLargeOrders
GetUserOrderSummary
GetRecentOrders
CountOrdersByStatus
CleanupTestDataRun a query:
$ sqlyac analytics.sql GetActiveUsers | mysql -u admin -p ecommerce_db --table
Enter password:
+--------+----------+---------------------+
| user_id| username | last_login |
+--------+----------+---------------------+
| 1234 | alice | 2024-03-15 14:30:22 |
| 5678 | bob | 2024-03-14 09:15:11 |
+--------+----------+---------------------+
Pipe to file:
$ sqlyac analytics.sql GetLargeOrders | mysql -u admin -p ecommerce_db > large_orders.csv- Write your queries in
.sqlfiles, separate them by dashes (---) and annotate with@name - Version control your sql alongside your code
- Run queries directly from terminal
- Pipe results to any database tool or file
You can save a configuration file in ~/.sqlyac/config.json with the following settings:
confirm- Ask for confirmation on all queries.confirm_schema_changes- Ask for confirmation on any queries that change the database schema (i.e.drop table,alter tableetc).confirm_updatesboolean - Ask for confirmation on any queries that create, update or delete rows.
Here's an example that would ask for confirmation on all updates, inserts and schema changes:
{
"confirm": false,
"confirm_schema_changes": true,
"confirm_updates": true
}Running any commands with the --confirm toggle overrides your config and asks for confirmation every time.
- only parses
.sqlfiles - ignores comment lines (except
@nameannotations) - strips leading/trailing whitespace from queries
- pretty forgiving with whitespace in
@nameannotations
Run tests like so:
go test -v