forked from ndwarshuis/org-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg-sql-doc.el
212 lines (181 loc) · 8.37 KB
/
org-sql-doc.el
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
;;; org-sql-doc.el --- Build documentation for org-sql -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Nathan Dwarshuis
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; These are functions to build the ERDs and table descriptions
;;; Code:
(require 'package)
(require 'dash)
(require 's)
(require 'f)
(require 'org-sql)
(defconst org-sql-doc-dir "doc"
"The location of the doc files")
;;; entity relationship diagrams
(defun org-sql-er-format-column (pks fks config column)
(-let* (((name . (&plist :type :constraints)) column)
(type* (if (eq type 'enum)
(org-sql--case-mode config
((mysql postgres) "ENUM")
((sqlite sqlserver)
(org-sql--format-create-tables-type config name column)))
(org-sql--format-create-tables-type config name column)))
(constraints* (-some->> constraints
(org-sql--format-column-constraints)))
(name* (--> (org-sql--format-column-name name)
(if (memq name pks) (format "*%s" it) it)
(if (memq name fks) (format "+%s" it) it))))
(format "%s {label: \"%s\"}" name* (s-join ", " (-non-nil (list type* constraints*))))))
(defun org-sql-er-format-table (config table)
(-let* (((name . (&alist 'columns 'constraints)) table)
(pks (plist-get (alist-get 'primary constraints) :keys))
(fks (->> (--filter (eq (car it) 'foreign) constraints)
(--mapcat (plist-get (cdr it) :keys))
(-uniq)))
(columns* (->> columns
(--map (org-sql-er-format-column pks fks config it))
(s-join "\n"))))
(format "[%s]\n%s" name columns*)))
(defun org-sql-er-get-tables (config)
(->> org-sql--table-alist
(--map (org-sql-er-format-table config it))
(s-join "\n\n")))
(defun org-sql-er-format-table-relationships (table)
(-let* (((name . (&alist 'constraints)) table)
(fks (--filter (eq 'foreign (car it)) constraints)))
(--map (-let* (((&plist :ref :cardinality :keys) (cdr it))
(rel (pcase cardinality
(`nil (error "Need cardinality for %s" name))
(`one-to-one "1--1")
(`one-or-none-to-one "?--1")
(`many-to-one "+--1")
(`many-or-none-to-one "*--1")
(e (error "Unknown cardinality: %s" e))))
(parent (plist-get (cdr it) :ref)))
(if (< 1 (length fks))
(->> (-map #'org-sql--format-column-name keys)
(s-join ", ")
(format "%s %s %s {label: \"%s\"}" name rel parent))
(format "%s %s %s" name rel parent)))
fks)))
(defun org-sql-er-get-relationships ()
(->> org-sql--table-alist
(-mapcat #'org-sql-er-format-table-relationships)
(-non-nil)
(s-join "\n")))
(defun org-sql-format-er-file (config)
(let* ((db-name (org-sql--case-mode config
(mysql "MySQL/MariaDB")
(postgres "PostgreSQL")
(sqlite "SQLite")
(sqlserver "SQL Server")))
(title (format "title {label: \"Org-SQL ERD (%s)\"}" db-name))
(tables (org-sql-er-get-tables config))
(relationships (org-sql-er-get-relationships)))
(s-join "\n\n" (list title tables relationships))))
(defun org-sql-write-erd (config)
(if (not (executable-find "erd"))
(error "Install erd to complete documentation")
(let ((er (org-sql-format-er-file config))
(inpath (f-join (temporary-file-directory) "org-sql-erd.er"))
(outpath (->> (org-sql--case-mode config
(mysql "mysql")
(postgres "postgres")
(sqlite "sqlite")
(sqlserver "sqlserver"))
(format "erd-%s.png")
(f-join org-sql-doc-dir "erd"))))
(f-write-text er 'utf-8 inpath)
(call-process "erd" nil nil nil "-i" inpath "-o" outpath)
(f-delete inpath t))))
(defun org-sql-create-all-erds ()
(-each '((mysql) (postgres) (sqlite) (sqlserver)) #'org-sql-write-erd))
;;; table descriptions
(defun org-sql-get-package-version ()
"Get version of om package."
(with-current-buffer (find-file-noselect "org-sql.el")
(->> (package-buffer-info)
(package-desc-version)
(-map 'number-to-string)
(s-join version-separator))))
(defun org-sql-doc-format-quotes (s)
(s-replace-regexp "`\\([^[:space:]]+\\)'" "`\\1`" s))
(defun org-sql-doc-format-table-row (members)
(format "| %s |" (s-join " | " members)))
(defun org-sql-doc-format-allowed-values (members)
(when members
(-let* ((members* (--map (format "`%s`" it) members))
((rest last) (-split-at (1- (length members*)) members*))
(rest* (s-join ", " rest)))
(format "%s, or %s" rest* (car last)))))
;; TODO this is a good idea but could be clearer, perhaps a separate column?
;; (defun org-sql-doc-format-org-element-props (desc props)
;; (cl-flet
;; ((join-with-and
;; (xs)
;; (format "%s and %s" (s-join ", " (-drop-last 1 xs)) (-last-item xs))))
;; (cond
;; ((= 0 (length props))
;; desc)
;; ((= 1 (length props))
;; (->> (car props)
;; (format "%s (corresponds to the org-element `%s` property)" desc )))
;; ((< 1 (length props))
;; (->> (--map (format "`%s`" it) props)
;; (join-with-and)
;; (format "%s (corresponds to the org-element %s properties)" desc))))))
(defun org-sql-doc-format-column (column-meta constraints-meta)
(-let* (((column-name . (&plist :desc :type :properties :constraints :allowed))
column-meta)
((&alist 'primary) constraints-meta)
(primary-keys (plist-get primary :keys))
(column-name* (org-sql--format-column-name column-name))
(allowed* (org-sql-doc-format-allowed-values allowed))
(desc* (-> (if (consp desc) (s-join " " desc) desc)))
;; (org-sql-doc-format-org-element-props properties)))
;; (org-sql-doc-format-quotes it)
;; (if property
;; (format "%s (corresponds to the org-element `%s` property)"
;; it property)
;; it)))
(desc-full (if allowed* (format "%s (%s)" desc* allowed*) desc*)))
(->> (list column-name* desc-full)
(org-sql-doc-format-table-row))))
(defun org-sql-doc-format-schema (table-meta)
(-let* (((table-name . meta) table-meta)
((&alist 'desc 'columns 'constraints) meta)
(header (->> (symbol-name table-name)
(format "## %s")))
(table-headers (list "Column" "Description"))
(table-line (->> (-repeat (length table-headers) " - ")
(org-sql-doc-format-table-row)))
(table-headers* (org-sql-doc-format-table-row table-headers))
(table-rows (->> columns
(--map (org-sql-doc-format-column it constraints))
(s-join "\n"))))
(->> (list header
""
(s-join " " desc)
""
table-headers*
table-line
table-rows)
(s-join "\n"))))
(defun create-docs-file ()
(with-temp-file (f-join org-sql-doc-dir "table-descriptions.md")
(insert "# Table Descriptions\n\n")
(insert (->> org-sql--table-alist
(-map #'org-sql-doc-format-schema)
(s-join "\n\n")))
(insert "\n\n")
(insert (format "Version: %s" (org-sql-get-package-version)))))
;;; org-sql-doc.el ends here