forked from kostafey/ejc-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ejc-interaction.el
342 lines (286 loc) · 11.5 KB
/
ejc-interaction.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
;;; ejc-interaction.el -- ejc-sql interact with Clojure. -*- lexical-binding: t -*-
;;; Copyright © 2013-2020 - Kostafey <[email protected]>
;;; 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 2, 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, write to the Free Software Foundation,
;;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
;;; Code:
(require 'dash)
(require 'ejc-lib)
(require 'ejc-format)
(require 'clomacs)
(clomacs-create-httpd-start ejc-httpd-start
:lib-name "ejc-sql")
(clomacs-create-httpd-stop ejc-httpd-stop
:lib-name "ejc-sql")
(clomacs-defun ejc-sql-set-db
set-db
:lib-name "ejc-sql"
:namespace ejc-sql.connect
:doc "Define ejc-sql.connect/db var."
:httpd-starter 'ejc-httpd-start)
(clomacs-defun ejc-add-classpath
add-classpath
:lib-name "ejc-sql"
:namespace ejc-sql.classpath)
(clomacs-defun ejc-require
clojure.core/require
:lib-name "ejc-sql")
(clomacs-defun ejc-import
clojure.core/import
:lib-name "ejc-sql")
(clomacs-defun ejc-class-for-name
Class/forName
:lib-name "ejc-sql")
(clomacs-defun ejc-get-dependeces-files-list
get-dependeces-files-list
:lib-name "ejc-sql"
:namespace ejc-sql.deps-resolver
:return-type :list)
(clomacs-defun ejc-resolve-dependencies
resolve-dependencies
:lib-name "ejc-sql"
:namespace cemerick.pomegranate.aether)
(defun ejc-connect-to-db (conn-struct)
(let* ((jars-to-load
(if-let (dependencies (alist-get :dependencies conn-struct))
;; Resolve dependencies and prepare jars paths list to load.
(ejc-get-dependeces-files-list dependencies)
(let* ((classpath (alist-get :classpath conn-struct))
(dependency-jars (-map
(lambda (jar-path)
(ejc-get-dependeces-files-list
(ejc-path-to-lein-artifact jar-path)))
classpath)))
;; Join jar files paths from `:classpath' vector of
;; `ejc-create-connection' and dependencies for this jars.
(delq nil
(delete-dups
(append
(-flatten dependency-jars)
(append classpath nil))))))))
(-map 'ejc-add-classpath jars-to-load))
(when-let ((classname (alist-get :classname conn-struct)))
(ejc-class-for-name classname)
(ejc-import (read classname)))
(ejc-sql-set-db conn-struct))
(clomacs-defun ejc--eval-sql-and-log-print
eval-sql-and-log-print
:lib-name "ejc-sql"
:namespace ejc-sql.connect
:return-type :string
:doc "Core function to evaluate SQL queries.
Prepare SQL string, evaluate SQL script and write them to log file")
(clomacs-defun ejc--is-query-running-p
is-query-running?
:lib-name "ejc-sql"
:namespace ejc-sql.connect
:return-type :boolean)
(clomacs-defun ejc--cancel-query
cancel-query
:lib-name "ejc-sql"
:namespace ejc-sql.connect
:doc "Cancel current query."
:interactive t
:return-type :list)
(clomacs-defun ejc--describe-table
describe-table
:lib-name "ejc-sql"
:namespace ejc-sql.structure)
(clomacs-defun ejc-get-entity-type
get-entity-type-fmt
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :eval)
(clomacs-defun ejc-get-parameters
get-parameters
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :list)
(clomacs-defun ejc-get-entity-description
get-entity-description
:lib-name "ejc-sql"
:namespace ejc-sql.structure)
(clomacs-defun ejc--get-result-file-path
get-result-file-path
:lib-name "ejc-sql"
:namespace ejc-sql.output)
(clomacs-defun ejc-print
clojure.core/print
:lib-name "ejc-sql"
:return-type :string
:return-value :stdout)
(clomacs-defun ejc-write-result-file
write-result-file
:namespace ejc-sql.output
:lib-name "ejc-sql"
:return-type :string)
(clomacs-defun ejc-clear-result-file
clear-result-file
:namespace ejc-sql.output
:lib-name "ejc-sql"
:return-type :string)
(clomacs-defun ejc-get-log-file-path
print-log-file-path
:lib-name "ejc-sql"
:namespace ejc-sql.output
:return-value :stdout)
(clomacs-defun ejc-format-by-hibernate
format-sql-print
:lib-name "ejc-sql"
:namespace ejc-sql.output
:return-value :stdout)
(clomacs-defun ejc-set-fetch-size
set-fetch-size
:lib-name "ejc-sql"
:namespace ejc-sql.output
:doc (concat "Set limit for number of records to output. "
"When nil no limit."))
(clomacs-defun ejc-set-show-too-many-rows-message
set-show-too-many-rows-message
:lib-name "ejc-sql"
:namespace ejc-sql.output
:doc (concat "Show 'Too many rows...' message in case"
"of ResultSet is bigger than `fetch-size'."))
(clomacs-defun ejc-set-max-rows
set-max-rows
:lib-name "ejc-sql"
:namespace ejc-sql.output
:doc (concat "Set limit for number of records to contain "
"in ResultSet. When nil no limit."))
(clomacs-defun ejc-set-column-width-limit
set-column-width-limit
:lib-name "ejc-sql"
:namespace ejc-sql.output
:doc (concat "Set limit for number of chars per column to "
"output to output. When nil no limit."))
(clomacs-defun ejc-set-use-unicode
set-use-unicode
:lib-name "ejc-sql"
:namespace ejc-sql.output
:doc "Set using unicode for grid borders.")
(clomacs-defun ejc-get-stucture
get-stucture
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :list)
(clomacs-defun ejc-get-owners-candidates
get-owners-candidates
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :list)
(clomacs-defun ejc-get-tables-candidates
get-tables-candidates
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :list)
(clomacs-defun ejc-get-views-candidates
get-views-candidates
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :list)
(clomacs-defun ejc-get-packages-candidates
get-packages-candidates
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :list)
(clomacs-defun ejc-get-colomns-candidates
get-colomns-candidates
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :list)
(clomacs-defun ejc-get-cached-owners-list
get-owners
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :list)
(clomacs-defun ejc-get-cached-tables-list
get-tables
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :list
:doc "Return cached tables list.
Requires `ejc-db' buffer local variable as parameter.")
(clomacs-defun ejc-get-cached-colomns-list
get-colomns
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :list)
(clomacs-defun ejc-get-keywords-inner
get-keywords
:lib-name "ejc-sql"
:namespace ejc-sql.structure
:return-type :list)
(clomacs-defun ejc-invalidate-cache-inner
invalidate-cache
:lib-name "ejc-sql"
:namespace ejc-sql.cache
:doc (concat "Clean your current connection cache "
"(database owners and tables list)."))
(clomacs-defun ejc-validate-connection
validate-connection
:lib-name "ejc-sql"
:namespace ejc-sql.connect
:return-type :eval)
(defun ejc-invalidate-cache ()
"Clean current connection cache (database owners and tables list)."
(interactive)
(ejc-invalidate-cache-inner ejc-db))
(clomacs-defun ejc-output-cache
output-cache
:lib-name "ejc-sql"
:namespace ejc-sql.cache
:return-type :eval)
(defun ejc-print-cache ()
"Print current database connection cache when run from connected SQL buffer.
Print all connections cache otherwise."
(interactive)
(pprint (ejc-output-cache ejc-db)))
(clomacs-defun ejc-select-db-meta-script
select-db-meta-script
:lib-name "ejc-sql"
:namespace ejc-sql.structure)
(clomacs-defun ejc-get-db-name
get-db-name
:lib-name "ejc-sql"
:namespace ejc-sql.structure)
(clomacs-defun ejc-get-this-owner
get-this-owner
:lib-name "ejc-sql"
:namespace ejc-sql.structure)
(defun ejc-unset-mode-name ()
(if (derived-mode-p 'org-mode)
(setq mode-name "Org")
(if (derived-mode-p 'sql-mode)
(setq mode-name "SQL"))))
(defun ejc-quit-connection ()
"Stop nREPL process, mark ejc-sql-mode buffers disconnected."
(interactive)
(when (y-or-n-p "Are you sure you want to close all jdbc connections?")
(ejc-httpd-stop)
(cider--close-connection (clomacs-get-connection "ejc-sql"))
;; Update modeline of ejc-sql-mode buffers - mark as disconnected.
(let ((buffers (buffer-list)))
(while buffers
(with-current-buffer (car buffers)
(when (member 'ejc-sql-mode minor-mode-list)
(ejc-unset-mode-name)
(spinner-stop)))
(setq buffers (cdr buffers)))))
(unless (cider-connected-p)
(cider-close-ancillary-buffers)))
(defalias 'ejc-close-connection 'ejc-quit-connection)
(defun ejc-buffer-connected-p ()
"Check if current buffer is connected to database."
(and (clomacs-get-connection "ejc-sql") (boundp 'ejc-db) ejc-db))
(provide 'ejc-interaction)
;;; ejc-interaction.el ends here