Created
July 5, 2017 12:25
-
-
Save schmir/6e03b3d649950d0108a06bf6fd653dec to your computer and use it in GitHub Desktop.
streaming responses in java.jdbc
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
(ns soka-trb.jdbc-streaming | |
"streaming results in clojure.java.jdbc, when {:use-streaming? true} is passed | |
as option | |
hooks into jdbc/prepare-statements and calls turn-on-streaming | |
Users need to extend the TurnOnStreaming protocol for their databases | |
" | |
(:require [clojure.java.jdbc :as jdbc] | |
[robert.hooke])) | |
(defprotocol TurnOnStreaming | |
(turn-on-streaming [con opts])) | |
(extend-protocol TurnOnStreaming | |
org.sqlite.SQLiteConnection | |
(turn-on-streaming [con opts] opts) | |
org.postgresql.jdbc.PgConnection | |
(turn-on-streaming [con opts] | |
(.setAutoCommit con false) | |
(if (pos? (get opts :fetch-size 0)) | |
opts | |
(assoc opts :fetch-size 1000))) | |
com.mysql.jdbc.JDBC4Connection | |
(turn-on-streaming [con opts] | |
(assoc opts :fetch-size Integer/MIN_VALUE))) | |
(defn prepare-statement | |
([f con sql] (prepare-statement f con sql {})) | |
([f | |
^java.sql.Connection con | |
^String sql | |
{:keys [return-keys result-type concurrency cursors | |
fetch-size max-rows timeout use-streaming?] :as opts}] | |
(if use-streaming? | |
(f con sql (turn-on-streaming con opts)) | |
(f con sql opts)))) | |
(defn add-hook | |
[] | |
(robert.hooke/add-hook #'jdbc/prepare-statement #'prepare-statement)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment