-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out JSON and HSTORE documentation into separate sections
- Loading branch information
Showing
13 changed files
with
162 additions
and
73 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# The HSTORE key-value type | ||
|
||
There is support for the PostgreSQL [HSTORE | ||
extension](https://www.postgresql.org/docs/current/hstore.html), which can store key/value pairs | ||
in a single PostgreSQL column. It's necessary to call `pg-hstore-setup` before using this | ||
functionality, to load the extension if necessary and to set up our parser support for the HSTORE | ||
type. | ||
|
||
~~~admonish example title="Using HSTORE values" | ||
```lisp | ||
ELISP> (pg-hstore-setup *pg*) | ||
ELISP> (defvar *hs* (car (pg-result (pg-exec *pg* "SELECT 'foo=>bar'::hstore") :tuple 0))) | ||
*hs* | ||
ELISP> (gethash "foo" *hs*) | ||
"bar" | ||
ELISP> (hash-table-count *hs*) | ||
1 (#o1, #x1, ?\C-a) | ||
;; There is no guarantee as to the value stored for the 'a' key (duplicate) | ||
ELISP> (setq *hs* (car (pg-result (pg-exec *pg* "SELECT 'a=>1,foobles=>2,a=>66'::hstore") :tuple 0))) | ||
#<hash-table equal 2/65 0x1574257479d9> | ||
ELISP> (hash-table-count *hs*) | ||
2 (#o2, #x2, ?\C-b) | ||
ELISP> (pg-result (pg-exec *pg* "SELECT akeys('biz=>NULL,baz=>42,boz=>66'::hstore)") :tuple 0) | ||
(["baz" "biz" "boz"]) | ||
``` | ||
~~~ | ||
|
||
|
||
|
||
~~~admonish example title="Serialization support for HSTORE values" | ||
```lisp | ||
ELISP> (pg-hstore-setup *pg*) | ||
``` | ||
~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# JSON and JSONB values | ||
|
||
PostgreSQL has quite a lot of [support for storing, saving and processing JSON and JSONB | ||
data](https://www.postgresql.org/docs/current/functions-json.html). pg-el is able to deserialize | ||
JSON and JSONB values into Emacs Lisp structures such as hashtables (for dicts), arrays, numbers, | ||
strings and so on. | ||
|
||
This library will parse and represent JSON/JSONB data either using the JSON support built into Emacs | ||
with libjansson (see function `json-available-p`, from version 28.1), or using the `json.el` | ||
library. There are some differences in the ways these methods handle dictionaries and specific | ||
values such as NULL, false, [] and {}. Our examples below use the builtin JSON support in Emacs. | ||
|
||
|
||
~~~admonish example title="Retrieving and manipulating JSON data" | ||
```lisp | ||
ELISP> (defun scalar (sql) (car (pg-result (pg-exec *pg* sql) :tuple 0))) | ||
scalar | ||
ELISP> (let ((json (scalar "SELECT '[5,7]'::json"))) | ||
(aref json 0)) | ||
5 (#o5, #x5, ?\C-e) | ||
ELISP> (let ((json (scalar "SELECT '[42.0,77.7]'::jsonb"))) | ||
(aref json 1)) | ||
77.7 | ||
ELISP> (scalar "SELECT '[]'::json") | ||
[] | ||
ELISP> (scalar "SELECT '{}'::json") | ||
#<hash-table equal 0/1 0x1586e6cc2813> | ||
ELISP> (let ((json (scalar "SELECT '{\"a\": 42, \"b\": \"foo\"}'::json"))) | ||
(gethash "b" json)) | ||
"foo" | ||
ELISP> (let ((json (scalar "SELECT '{\"a\": [0,1,2,null]}'::json"))) | ||
(gethash "a" json)) | ||
[0 1 2 :null] | ||
``` | ||
~~~ | ||
|
||
|
||
pg-el can also serialize Emacs Lisp structures into the PostgreSQL JSON format, for use in prepared | ||
statements. | ||
|
||
~~~admonish example title="Serializing objects to JSON / JSONB" | ||
```lisp | ||
ELISP> (let ((ht (make-hash-table))) | ||
(puthash "biz" 45 ht) | ||
(puthash "boz" -5.5 ht) | ||
(puthash "comment" "good stuff" ht) | ||
(pg-result (pg-exec-prepared *pg* "SELECT $1->'boz'" `((,ht . "json"))) :tuple 0)) | ||
(-5.5) | ||
ELISP> (let ((ht (make-hash-table))) | ||
(puthash "biz" 45 ht) | ||
(puthash "boz" -5.5 ht) | ||
(puthash "comment" "good stuff" ht) | ||
;; the '-' jsonb operator deletes a matching key/value mapping | ||
(let* ((res (pg-exec-prepared *pg* "SELECT $1 - 'boz'" `((,ht . "jsonb")))) | ||
(row (pg-result res :tuple 0))) | ||
(gethash "comment" (cl-first row) ))) | ||
"good stuff" | ||
``` | ||
~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters