-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo-menu-query.el
52 lines (46 loc) · 1.58 KB
/
mongo-menu-query.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
(defun mongo-menu-get-database ()
"Returns current database info"
(cdr (assoc mongo-menu-current-database mongo-menu-databases))
)
(defun mongo-menu-requote-output (output)
"Adds quotes around ObjectId in OUTPUT.
When mongo outputs json, it has unquoted ObjectIds in it that
emacs cannot interpret as json. "
(let ((output (replace-regexp-in-string
"ObjectId(\"\\(.*?\\)\")"
"\"ObjectId(\\\\\"\\1\\\\\")\""
output)))
(replace-regexp-in-string
"ISODate(\"\\(.*?\\)\")"
"\"ISODate(\\\\\"\\1\\\\\")\""
output)
)
)
(defun mongo-menu-json-query (query)
(let ((output (mongo-menu-raw-query query)))
(json-read-from-string (mongo-menu-requote-output output))
)
)
(defun mongo-menu-raw-query (query)
(let* ((db (mongo-menu-get-database))
(host (elt db 0))
(user (elt db 1))
(password (elt db 2))
(cmd (format "%s -u %s -p %s %s --quiet --eval '%s'"
mongo-menu-client
user
password
host
query
)))
;; (message "Mongo-menu command: %s" cmd)
(shell-command-to-string cmd)
)
)
(defun mongo-menu-query-cursor (query)
"Runs the query as a mongodb cursor and returns the output as a string JSON list"
(let ((query (format "var cursor = %s; print(\"\[\"); while(cursor.hasNext()) { printjson(cursor.next()); if (cursor.hasNext()) {print(\",\");}} print(\"]\");" query)))
(mongo-menu-json-query query)
)
)
(provide 'mongo-menu-query)