diff --git a/doc/src/usage.md b/doc/src/usage.md index f59b6ae..bad3970 100644 --- a/doc/src/usage.md +++ b/doc/src/usage.md @@ -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))) @@ -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)")) @@ -241,8 +242,6 @@ t ## HSTORE -## PREPARE / EXECUTE - ## Special pg-el features diff --git a/pg.el b/pg.el index 8757db0..b28ee99 100644 --- a/pg.el +++ b/pg.el @@ -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 @@ -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) diff --git a/test/test-pg.el b/test/test-pg.el index 778647b..1e69484 100644 --- a/test/test-pg.el +++ b/test/test-pg.el @@ -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"))))) @@ -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 @@ -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")))