Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clojure/src/pgloader/ddl/common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
;; Pass-through: native PostgreSQL types emitted by non-MySQL sources
(= lower "uuid") "uuid"
(= lower "xml") "xml"
(= lower "bytea") "bytea"
;; Pass-through array types from PostgreSQL sources (#1371)
(str/ends-with? lower "[]") lower
(str/starts-with? upper "BIGSERIAL") "bigserial"
Expand Down
21 changes: 17 additions & 4 deletions clojure/src/pgloader/source/sqlite.clj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@
:quote-ids name
(str/lower-case name)))

(defn- jdbc-value->str
"Convert a raw JDBC value read from SQLite to a String for COPY TEXT.
BLOB values arrive as byte[]; str would give the JVM \"[B@hash\" form, so
they are emitted as PostgreSQL bytea hex (\\x...) instead (#1782)."
[v]
(when (some? v)
(if (bytes? v)
(let [ba ^bytes v
sb (StringBuilder. (+ 2 (* 2 (alength ba))))]
(.append sb "\\x")
(dotimes [i (alength ba)]
(.append sb (format "%02x" (bit-and (int (aget ba i)) 0xff))))
(.toString sb))
(str v))))

(deftype SQLiteSource
[^java.sql.Connection conn
^String db-path
Expand Down Expand Up @@ -219,8 +234,7 @@
(if (<= i n-cols)
(recur (unchecked-inc i)
(conj! result
(let [v (.getObject ^ResultSet rs i)]
(when (some? v) (str v)))))
(jdbc-value->str (.getObject ^ResultSet rs i))))
(persistent! result)))
(thisfn))))))))

Expand All @@ -243,8 +257,7 @@
(if (<= i n)
(recur (inc i)
(conj! result
(let [v (.getObject rs i)]
(when v (str v)))))
(jdbc-value->str (.getObject rs i))))
(persistent! result)))
(thisfn))))))})
(catch Exception e
Expand Down
10 changes: 10 additions & 0 deletions clojure/test/pgloader/ddl_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,13 @@
(is (= ["CREATE SCHEMA IF NOT EXISTS \"shopdb\";"
"CREATE SCHEMA IF NOT EXISTS \"public\";"]
(ddl/create-schemas-sql ["shopdb" "public" nil "shopdb"]))))

(deftest test-column-def-bytea-passthrough
;; SQLite catalog already yields PostgreSQL type names; "bytea" must not
;; fall through to the MySQL mapper's text default (#1782).
(let [col {:column-name "b"
:column-type "bytea"
:is-nullable true
:extra ""
:column-default nil}]
(is (= " \"b\" bytea" (ddl/column-def col)))))
13 changes: 13 additions & 0 deletions clojure/test/pgloader/source/sqlite_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,16 @@
(is (= 2 (count mvs)))
(is (nil? (:query (first mvs))))
(is (string? (:query (second mvs))))))))

(deftest jdbc-value->str-test
(testing "BLOB byte[] values become PostgreSQL bytea hex, not the JVM [B@ form (#1782)"
(is (= "\\xdeadbeef"
(#'sqlite/jdbc-value->str (byte-array [0xde 0xad 0xbe 0xef]))))
(is (= "\\x007f80ff"
(#'sqlite/jdbc-value->str (byte-array [0 127 -128 -1]))))
(is (= "\\x" (#'sqlite/jdbc-value->str (byte-array 0)))))

(testing "other values keep their string form; nil stays nil"
(is (= "42" (#'sqlite/jdbc-value->str 42)))
(is (= "abc" (#'sqlite/jdbc-value->str "abc")))
(is (nil? (#'sqlite/jdbc-value->str nil)))))