Sean Corfield <s...@corfield.org> writes:

>> The required steps to setup streaming are different from database to
>> database and I guess they may change with the driver version being used.
>
> This is really the crux of the problem here – I’m not sure what java.jdbc can 
> do generically to make this much easier.

Please take a look at the attachment or

  https://gist.github.com/schmir/6e03b3d649950d0108a06bf6fd653dec

This is using the robert.hooke library to hook into prepare-statement.
That hook calls into a protocol function if streaming is required. This
protocol function can be defined by users for the database they are
using.

I think it would be really nice for clojure.java.jdbc to provide this
functionality.

-- 
Cheers
Ralf

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
(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))

Reply via email to