forked from babashka/babashka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hsqldb_unused_vars.clj
executable file
·88 lines (73 loc) · 3.51 KB
/
hsqldb_unused_vars.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env bb
;; $ examples/hsqldb_unused_vars.clj src
;; | :VARS/NS | :VARS/NAME | :VARS/FILENAME | :VARS/ROW | :VARS/COL |
;; |----------------------------+--------------------------+------------------------------------+-----------+-----------|
;; | babashka.impl.bencode.core | read-netstring | src/babashka/impl/bencode/core.clj | 162 | 1 |
;; | babashka.impl.bencode.core | write-netstring | src/babashka/impl/bencode/core.clj | 201 | 1 |
;; | babashka.impl.classes | generate-reflection-file | src/babashka/impl/classes.clj | 230 | 1 |
;; | babashka.impl.classpath | ->DirectoryResolver | src/babashka/impl/classpath.clj | 12 | 1 |
;; | babashka.impl.classpath | ->JarFileResolver | src/babashka/impl/classpath.clj | 37 | 1 |
;; | babashka.impl.classpath | ->Loader | src/babashka/impl/classpath.clj | 47 | 1 |
;; | babashka.impl.clojure.test | file-position | src/babashka/impl/clojure/test.clj | 286 | 1 |
;; | babashka.impl.nrepl-server | stop-server! | src/babashka/impl/nrepl_server.clj | 179 | 1 |
;; | babashka.main | -main | src/babashka/main.clj | 485 | 1 |
(ns hsqldb-unused-vars
(:require
[babashka.pods :as pods]
[clojure.pprint :refer [print-table]]))
(pods/load-pod "pod-babashka-hsqldb")
(require '[pod.babashka.hsqldb :as jdbc]
'[pod.babashka.hsqldb.sql :as sql])
(pods/load-pod "clj-kondo")
(require '[pod.borkdude.clj-kondo :as clj-kondo])
(def db "jdbc:hsqldb:mem:testdb;sql.syntax_mys=true")
(defn query [q]
(jdbc/execute! db [q]))
(defn create-db! []
(query "create table vars (
ns text
, name text
, filename text
, row int
, col int )")
(query "create table var_usages (
\"from\" text
, \"to\" text
, name text
, filename text
, row int
, col int )"))
(defn parse-int [^String x]
(when x
(Integer. x)))
(defn insert-vars! [var-definitions]
(sql/insert-multi! db :vars [:ns :name :filename :row :col]
(map (juxt (comp str :ns)
(comp str :name)
:filename
(comp parse-int :row)
(comp parse-int :col))
var-definitions)))
(defn insert-var-usages! [var-usages]
(sql/insert-multi! db :var_usages ["\"from\"" "\"to\"" :name :filename :row :col]
(map (juxt (comp str :from)
(comp str :to)
(comp str :name)
:filename
(comp parse-int :row)
(comp parse-int :col))
var-usages)))
(defn analysis->db [paths]
(let [results (clj-kondo/run! {:lint paths
:config {:output {:analysis true}}})
analysis (:analysis results)
{:keys [:var-definitions :var-usages]} analysis]
(insert-vars! var-definitions)
(insert-var-usages! var-usages)))
(create-db!)
(analysis->db *command-line-args*)
(def unused-vars "
select distinct * from vars v
where (v.ns, v.name) not in
(select vu.\"to\", vu.name from var_usages vu)")
(print-table (query unused-vars))