Add WHERE clause support

This commit is contained in:
Jeremy Dormitzer 2020-06-09 10:05:23 -04:00
parent 08a4f4e0e1
commit 67453f4c47

View File

@ -112,7 +112,33 @@
(defun structlog--get-query-fields (query) (defun structlog--get-query-fields (query)
"Returns the fields selected by `query'" "Returns the fields selected by `query'"
(plist-get query :select)) (vconcat (plist-get query :select)))
(defun structlog--parse-where-clause (where)
"Parses a single WHERE data clause"
(pcase where
(`(:and . ,clauses) (format "(%s)"
(s-join " AND "
(mapcar #'structlog--parse-where-clause
clauses))))
(`(:or . ,clauses) (format "(%s)"
(s-join " OR "
(mapcar #'structlog--parse-where-clause
clauses))))
(`(:not ,clause) (format "NOT %s"
(structlog--parse-where-clause clause)))
(`(:like ,sym ,str) (format "%s->>'%s' LIKE '%s'"
structlog-db-record-field
sym
str))
(sym (format "%s->'%s' IS NOT NULL"
structlog-db-record-field
where))))
(defun structlog--get-where-sql (where)
"Returns the SQL WHERE clause for the `where' data"
(let ((parsed (structlog--parse-where-clause where)))
(and parsed (format "WHERE %s" parsed))))
(defun structlog--get-query-sql (query) (defun structlog--get-query-sql (query)
"Returns the SQL query to run for `query'" "Returns the SQL query to run for `query'"
@ -122,8 +148,10 @@
(order-by (format "ORDER BY %s->'%s'" (order-by (format "ORDER BY %s->'%s'"
structlog-db-record-field structlog-db-record-field
structlog-time-field)) structlog-time-field))
(limit (plist-get query :limit))) (limit (plist-get query :limit))
(where (plist-get query :where)))
(concat base " " (concat base " "
(when where (structlog--get-where-sql where)) " "
order-by " " order-by " "
(when limit (when limit
(format "LIMIT %s" limit))))) (format "LIMIT %s" limit)))))
@ -143,7 +171,7 @@
(list log-alist (list log-alist
(cl-map 'vector (cl-map 'vector
(lambda (field) (lambda (field)
(or (alist-get field log-alist) "")) (or (format "%s" (alist-get field log-alist)) ""))
structlog-fields))) structlog-fields)))
structlog-logs))) structlog-logs)))