forked from kostafey/ejc-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ejc-interaction.el
277 lines (228 loc) · 8.93 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
;;; ejc-interaction.el -- ejc-sql interact with Clojure. -*- lexical-binding: t -*-
;;; Copyright © 2013-2019 - 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 '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-resolve-dependencies
resolve-dependencies
:lib-name "ejc-sql"
:namespace cemerick.pomegranate.aether)
(clomacs-defun ejc-require
clojure.core/require
:lib-name "ejc-sql")
(clomacs-defun ejc-import
clojure.core/import
:lib-name "ejc-sql")
(defun ejc-connect-to-db (conn-struct)
(ejc-add-classpath (alist-get :classpath conn-struct))
(if-let ((classname (alist-get :classname conn-struct)))
(ejc-import (read classname)))
(ejc-sql-set-db conn-struct)
(setq-local ejc-connection-struct 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--eval-sql-get-column
eval-sql-internal-get-column
:lib-name "ejc-sql"
:return-type :list
:namespace ejc-sql.connect
:doc "Get `sql', return list.")
(defalias 'ejc--eval-get-list 'ejc--eval-sql-get-column)
(clomacs-defun ejc--describe-table
describe-table
:lib-name "ejc-sql"
:namespace ejc-sql.structure)
(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-rows-limit
set-rows-limit
: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-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.structure
: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-connection-struct))
(clomacs-defun ejc-get-cache
get-cache
:lib-name "ejc-sql"
:namespace ejc-sql.structure)
(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