-
-
Save ayato-p/c8c2fbfc0efdd0dd56dc85fd694007fc to your computer and use it in GitHub Desktop.
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
(require '[clojure.java.jdbc :as jdbc]) | |
(def db-spec | |
{:dbtype "postgresql" | |
:dbname "jdbctest" | |
:user "your_pg_user" | |
:password "your_pg_password"}) | |
(doc jdbc/execute!) | |
;; => ------------------------- | |
;; => clojure.java.jdbc/execute! | |
;; => ([db sql-params] [db sql-params opts]) | |
;; => Given a database connection and a vector containing SQL (or PreparedStatement) | |
;; followed by optional parameters, perform a general (non-select) SQL operation. | |
;; The :transaction? option specifies whether to run the operation in a | |
;; transaction or not (default true). | |
;; If the :multi? option is false (the default), the SQL statement should be | |
;; followed by the parameters for that statement. | |
;; If the :multi? option is true, the SQL statement should be followed by one or | |
;; more vectors of parameters, one for each application of the SQL statement. | |
;; If :return-keys is provided, db-do-prepared-return-keys will be called | |
;; instead of db-do-prepared, and the result will be a sequence of maps | |
;; containing the generated keys. | |
;; If there are no parameters specified, executeUpdate will be used, otherwise | |
;; executeBatch will be used. This may affect what SQL you can run via execute! | |
(jdbc/execute! | |
db-spec | |
["insert into foo (x, y) values (?, ?), (?, ?)" "x1" "y1" "x2" "y2"] | |
{:return-keys true}) | |
;; => {:x "x1", :y "y1"} | |
(jdbc/execute! | |
db-spec | |
["insert into foo (x, y) values (?, ?)" ["x1" "y1"] ["x2" "y2"]] | |
{:return-keys true :multi? true}) | |
;; => ({:x "x1", :y "y1"} {:x "x2", :y "y2"}) | |
;;; My expectation is | |
(jdbc/execute! | |
db-spec | |
["insert into foo (x, y) values (?, ?), (?, ?)" "x1" "y1" "x2" "y2"] | |
{:return-keys true}) | |
;; => ({:x "x1", :y "y1"} {:x "x2", :y "y2"}) |
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
(jdbc/execute! db-spec ["insert into foo (x, y) values (?, ?)" "x1" "y1"] {:return-keys ["x"]}) | |
;; => {:x "x1", :y "y1"} | |
;;; My expectation is | |
(jdbc/with-db-connection [db db-spec] | |
(let [stmt (-> (jdbc/db-find-connection db) | |
(jdbc/prepare-statement "insert into foo (x, y) values (?, ?), (?, ?)" {:return-keys ["x"]}))] | |
(#'jdbc/dft-set-parameters stmt ["x1" "y1" "x2" "y2"]) | |
(.executeUpdate stmt) | |
(jdbc/result-set-seq (.getGeneratedKeys stmt)))) | |
;; => ({:x "x1"} {:x "x2"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment