Skip to content

Commit

Permalink
Use prepared statements in user manual
Browse files Browse the repository at this point in the history
  • Loading branch information
emarsden committed Dec 14, 2023
1 parent b5df9ab commit cbda97a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
13 changes: 6 additions & 7 deletions doc/src/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ ELISP> (member "count_test" (pg-tables *pg*))
("count_test")
ELISP> (member "val" (pg-columns *pg* "count_test"))
("val")
ELISP> (cl-loop for i from 1 to 100
for sql = (format "INSERT INTO count_test VALUES(%s, %s)" i (* i i))
do (pg-exec *pg* sql))
ELISP> (dotimes (i 100)
(pg-exec-prepared *pg* "INSERT INTO count_test VALUES($1, $2)"
`((,i . "int4") (,(* i i) . "int4"))))
nil
ELISP> (let ((res (pg-exec *pg* "SELECT count(*) FROM count_test")))
(car (pg-result res :tuple 0)))
Expand Down Expand Up @@ -210,8 +210,9 @@ ELISP> (let* ((size 512)
(dotimes (i size)
(setf (aref random-octets i) (random 256)))
(setf (aref random-octets 0) 0)
(pg-exec *pg* (format "INSERT INTO bt VALUES (decode('%s', 'base64'), 42)"
(base64-encode-string random-octets)))
(pg-exec-prepared *pg*
"INSERT INTO bt VALUES (decode($1, 'base64'), 42)"
`((,(base64-encode-string random-octets) . "text")))
(equal random-octets (car (pg-result (pg-exec *pg* "SELECT blob FROM bt WHERE tag=42") :tuple 0))))
t
ELISP> (let* ((res (pg-exec *pg* "SELECT sha256('foobles'::bytea)"))
Expand Down Expand Up @@ -241,8 +242,6 @@ t
## HSTORE
## PREPARE / EXECUTE
## Special pg-el features
Expand Down
4 changes: 2 additions & 2 deletions pg.el
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ Returns a pgresult structure (see function `pg-result')."
(message "Received unexpected message type %s in pg-fetch" c))))))

;; Do a PARSE/BIND/EXECUTE sequence, using the Extended Query message flow.
(cl-defun pg-exec/prepared (con query typed-arguments &key (max-rows 0))
(cl-defun pg-exec-prepared (con query typed-arguments &key (max-rows 0))
"Execute SQL QUERY using TYPED-ARGUMENTS on database connection CON.
Query can contain numbered parameters ($1, $2 etc.) that are
bound to the values in TYPED-ARGUMENTS, which is a list of the
Expand All @@ -891,7 +891,7 @@ are available, they can later be retrieved with `pg-fetch'."
(pg-fetch con portal-name :max-rows max-rows)))

(defun pg-close-portal (con portal-name)
"Close the portal named PORTAL-NAME that was opened by pg-exec/prepared."
"Close the portal named PORTAL-NAME that was opened by pg-exec-prepared."
(let ((len (+ 4 1 (1+ (length portal-name)))))
;; send a Close message
(pg-send-char con ?C)
Expand Down
10 changes: 5 additions & 5 deletions test/test-pg.el
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@


(defun pg-test-prepared (con)
(cl-labels ((row (query args) (pg-result (pg-exec/prepared con query args) :tuple 0))
(cl-labels ((row (query args) (pg-result (pg-exec-prepared con query args) :tuple 0))
(scalar (query args) (car (row query args)))
(approx= (x y) (< (/ (abs (- x y)) (max (abs x) (abs y))) 1e-5)))
(should (equal (make-bool-vector 1 nil) (scalar "SELECT $1::bit" '(("0" . "bit")))))
Expand Down Expand Up @@ -174,15 +174,15 @@


(cl-defun pg-test-prepared/multifetch (con &optional (rows 1000))
(cl-labels ((row (query args) (pg-result (pg-exec/prepared con query args) :tuple 0))
(cl-labels ((row (query args) (pg-result (pg-exec-prepared con query args) :tuple 0))
(scalar (query args) (car (row query args))))
(message "Running multiple fetch/suspended portal test")
(let* ((res (pg-exec/prepared con "SELECT generate_series(1, $1)"
(let* ((res (pg-exec-prepared con "SELECT generate_series(1, $1)"
`((,rows . "int4"))
:max-rows 10))
(portal (pgresult-portal res))
(counter 0))
;; check the results from the initial pg-exec/prepared
;; check the results from the initial pg-exec-prepared
(dolist (tuple (pg-result res :tuples))
(should (eql (cl-first tuple) (cl-incf counter))))
;; keep fetching and checking more rows until the portal is complete
Expand Down Expand Up @@ -239,7 +239,7 @@
(should (member "count_test" (pg-tables con)))
(should (member "val" (pg-columns con "count_test")))
(dotimes (i count)
(pg-exec/prepared con "INSERT INTO count_test VALUES($1, $2)"
(pg-exec-prepared con "INSERT INTO count_test VALUES($1, $2)"
`((,i . "int4") (,(* i i) . "int4"))))
(should (eql count (scalar "SELECT count(*) FROM count_test")))
(should (eql (/ (* (1- count) count) 2) (scalar "SELECT sum(key) FROM count_test")))
Expand Down

0 comments on commit cbda97a

Please sign in to comment.