Skip to content

Commit 96cc9c0

Browse files
committed
chore: sql-playground and sqls
Adding these to showcase how I use org-mode as an sql-playground in my personal projects.
1 parent 5dd8a6d commit 96cc9c0

File tree

5 files changed

+118
-20
lines changed

5 files changed

+118
-20
lines changed

.dir-locals.el

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
;;; Directory Local Variables
2+
;;; For more information see (info "(emacs) Directory Variables")
3+
4+
((org-mode . ((eval (lambda ()
5+
(defun org-babel-edit-prep:sql (babel-info)
6+
(setq-local buffer-file-name (->> babel-info caddr (alist-get :tangle)))
7+
(setq-local lsp-buffer-uri (->> babel-info caddr (alist-get :tangle) lsp--path-to-uri))
8+
(setq-local lsp-headerline-breadcrumb-enable nil)
9+
(lsp-deferred))))))
10+
(nil . ((eval (lambda ()
11+
(setq lsp-sqls-connections `(((driver . "postgresql")
12+
(dataSourceName . ,(getenv "DATABASE_URL"))))))))))

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/target
22
/result*
33
/dist/
4-
/storage
4+
/storage
5+
/test.sql

flake.lock

Lines changed: 19 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
inherit (import "${crate2nix}/tools.nix" { inherit pkgs; })
2020
generatedCargoNix;
2121

22+
sqls = pkgs.buildGoModule {
23+
name = "sqls";
24+
vendorSha256 = "sha256-sowzyhvNr7Ek3ex4BP415HhHSKnqPHy5EbnECDVZOGw=";
25+
26+
src = pkgs.fetchFromGitHub {
27+
owner = "lighttiger2505";
28+
repo = "sqls";
29+
rev = "v0.2.22";
30+
sha256 = "sha256-xtvm/NVL98dRzQL1id/WwT/NdsnB7qTRVR7jfrRsabY=";
31+
};
32+
};
33+
2234
project = import
2335
(generatedCargoNix {
2436
name = crateName;
@@ -49,7 +61,7 @@
4961
cargo-audit
5062
cargo-outdated
5163
cargo-make
52-
(rust-bin.stable.latest.default.override {
64+
(rust-bin.nightly.latest.default.override {
5365
extensions = [
5466
"rust-src"
5567
"cargo"
@@ -62,6 +74,7 @@
6274
})
6375
minio
6476
minio-client
77+
sqls
6578
];
6679

6780
entropy = project.rootCrate.build;

sql-playground.org

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#+PROPERTY: header-args:sql :tangle test.sql
2+
#+PROPERTY: header-args:sql :engine postgres
3+
4+
#+begin_src elisp
5+
#+end_src
6+
7+
#+name: groups
8+
#+begin_src sql
9+
SELECT groups.name, groups.slug, source, source_link FROM groups;
10+
#+end_src
11+
12+
#+RESULTS: groups
13+
| name | slug | source | source_link |
14+
|-------------+---------+--------+-------------|
15+
| Lol bro wtf | entropy | local | |
16+
17+
#+name: events
18+
#+begin_src sql
19+
SELECT events.title,
20+
events.slug,
21+
start_time,
22+
events.id,
23+
events.source,
24+
format('%s mins', extract(epoch from (events.end_time - events.start_time))/60)
25+
FROM events;
26+
#+end_src
27+
28+
#+RESULTS: events
29+
| title | slug | start_time | end_time | source | format |
30+
|--------------------+----------------------+---------------------+---------------------+--------+---------|
31+
| Lol bro what | lol-rofl-lmao | 2021-11-29 12:30:00 | 2021-11-29 13:30:00 | local | 60 mins |
32+
| Nested event title | nested-lol-rofl-lmao | 2021-10-29 12:30:00 | 2021-10-29 13:30:00 | local | 60 mins |
33+
34+
#+name: event_sections
35+
#+begin_src sql
36+
SELECT
37+
events.slug,
38+
logo,
39+
format('%s mins', extract(epoch from (event_sections.end_time - event_sections.start_time))/60) as duration
40+
FROM
41+
("event_sections" INNER JOIN "events" ON "events"."id" = "event_sections"."event_id");
42+
#+end_src
43+
44+
#+RESULTS: event_sections
45+
| slug | logo | duration |
46+
|---------------+---------------------------+----------|
47+
| lol-rofl-lmao | logos/dark/lightening.svg | 30 mins |
48+
49+
50+
#+name: initiatives
51+
#+begin_src sql :engine postgres
52+
SELECT initiatives.slug from initiatives left join goals on initiatives.slug = goals.initiative_slug where goals.slug is null;
53+
#+end_src
54+
55+
#+RESULTS: initiatives
56+
| slug |
57+
|--------------|
58+
| fear-of-cops |
59+
60+
#+name: goals
61+
#+begin_src sql :engine postgres
62+
SELECT
63+
title, slug, initiative_slug, target,
64+
justify_interval(iteration_interval) AS iteration_interval
65+
FROM goals;
66+
#+end_src
67+
68+
#+RESULTS: goals
69+
| title | slug | initiative_slug | target | iteration_interval |
70+
|-------------------------+--------------------------+---------------------+--------+--------------------|
71+
| 10 monthly active users | ten-monthly-active-users | meager-tech-culture | 10 | 7 days |

0 commit comments

Comments
 (0)