From b9cad79408622cfdcf25e31c27af919b8300cbba Mon Sep 17 00:00:00 2001 From: ExpandingMan Date: Thu, 21 Nov 2019 15:45:05 -0500 Subject: [PATCH 1/3] added decimal type --- Manifest.toml | 108 +++++++++++++++++++++++++++++++++++++++++++++++ Project.toml | 5 ++- src/JDBC.jl | 37 +++++++++++++--- src/tables.jl | 4 +- test/runtests.jl | 14 ++++-- 5 files changed, 153 insertions(+), 15 deletions(-) create mode 100644 Manifest.toml diff --git a/Manifest.toml b/Manifest.toml new file mode 100644 index 0000000..9d8c942 --- /dev/null +++ b/Manifest.toml @@ -0,0 +1,108 @@ +# This file is machine-generated - editing it directly is not advised + +[[Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" + +[[DataAPI]] +git-tree-sha1 = "674b67f344687a88310213ddfa8a2b3c76cc4252" +uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" +version = "1.1.0" + +[[DataStructures]] +deps = ["InteractiveUtils", "OrderedCollections"] +git-tree-sha1 = "1fe8fad5fc84686dcbc674aa255bc867a64f8132" +uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +version = "0.17.5" + +[[DataValueInterfaces]] +git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6" +uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464" +version = "1.0.0" + +[[Dates]] +deps = ["Printf"] +uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" + +[[Decimals]] +deps = ["Test"] +git-tree-sha1 = "b374d464d64470e743c5f9172c57352e1a9404ac" +uuid = "abce61dc-4473-55a0-ba07-351d65e31d42" +version = "0.4.0" + +[[Distributed]] +deps = ["Random", "Serialization", "Sockets"] +uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" + +[[InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" + +[[IteratorInterfaceExtensions]] +git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856" +uuid = "82899510-4779-5014-852e-03e436cf321d" +version = "1.0.0" + +[[JavaCall]] +deps = ["DataStructures", "Dates", "Libdl", "Test", "WinReg"] +git-tree-sha1 = "cd1942b4201ed14923927f30514ef98112fe60c1" +uuid = "494afd89-becb-516b-aafa-70d2670c0337" +version = "0.7.2" + +[[Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[LinearAlgebra]] +deps = ["Libdl"] +uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + +[[Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" + +[[Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" + +[[OrderedCollections]] +deps = ["Random", "Serialization", "Test"] +git-tree-sha1 = "c4c13474d23c60d20a67b217f1d7f22a40edf8f1" +uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +version = "1.1.0" + +[[Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[[Random]] +deps = ["Serialization"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[[Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" + +[[Sockets]] +uuid = "6462fe0b-24de-5631-8697-dd941f90decc" + +[[TableTraits]] +deps = ["IteratorInterfaceExtensions"] +git-tree-sha1 = "b1ad568ba658d8cbb3b892ed5380a6f3e781a81e" +uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c" +version = "1.0.0" + +[[Tables]] +deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "TableTraits", "Test"] +git-tree-sha1 = "aaed7b3b00248ff6a794375ad6adf30f30ca5591" +uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" +version = "0.2.11" + +[[Test]] +deps = ["Distributed", "InteractiveUtils", "Logging", "Random"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[[Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" + +[[WinReg]] +deps = ["Test"] +git-tree-sha1 = "808380e0a0483e134081cc54150be4177959b5f4" +uuid = "1b915085-20d7-51cf-bf83-8f477d6f5128" +version = "0.3.1" diff --git a/Project.toml b/Project.toml index 7eed695..1e4eb25 100644 --- a/Project.toml +++ b/Project.toml @@ -1,15 +1,16 @@ name = "JDBC" uuid = "6042db11-3c3d-5e84-8dba-9cbf74c9ba48" -version = "0.5.0" +version = "0.5.1" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" +Decimals = "abce61dc-4473-55a0-ba07-351d65e31d42" JavaCall = "494afd89-becb-516b-aafa-70d2670c0337" Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" [compat] JavaCall = "≥ 0.7.0" -julia = "1.0" +julia = "1.3" [extras] DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" diff --git a/src/JDBC.jl b/src/JDBC.jl index 15da065..792fdb9 100644 --- a/src/JDBC.jl +++ b/src/JDBC.jl @@ -1,12 +1,12 @@ #This file is part of JDBC.jl. License is MIT. module JDBC using JavaCall -using Dates +using Dates, Decimals export DriverManager, createStatement, prepareStatement, prepareCall, executeQuery, setFetchSize, getInt, getFloat, getString, getShort, getByte, getTime, getTimestamp, getDate, - getBoolean, getNString, getURL, setInt, setFloat, setString, setShort, setByte, setBoolean, - getMetaData, getColumnCount, getColumnType, getColumnName, executeUpdate, execute, commit, + getBoolean, getNString, getURL, getBigDecimal, setInt, setFloat, setString, setShort, setByte, setBoolean, + setBigDecimal, getMetaData, getColumnCount, getColumnType, getColumnName, executeUpdate, execute, commit, rollback, setAutoCommit, getResultSet @@ -32,6 +32,7 @@ const JStatement = @jimport java.sql.Statement const JPreparedStatement = @jimport java.sql.PreparedStatement const JCallableStatement = @jimport java.sql.CallableStatement const JConnection = @jimport java.sql.Connection +const JBigDecimal = @jimport java.math.BigDecimal const COLUMN_NO_NULLS = 0 const COLUMN_NULLABLE = 1 @@ -40,6 +41,10 @@ const COLUMN_NULLABLE_UNKNOWN = 2 init() = JavaCall.init() destroy() = JavaCall.destroy() + +JBigDecimal(x::Decimal) = JBigDecimal((JString,), string(x)) +Decimals.Decimal(x::JBigDecimal) = decimal(jcall(x, "toPlainString", JString, ())) + """ ``` createStatement(connection::JConnection) @@ -283,8 +288,7 @@ for s in [("String", :JString), ("Float", :jfloat), ("Double", :jdouble), ("Byte", :jbyte), - ("URL", :(@jimport(java.net.URL))), - ("BigDecimal", :(@jimport(java.math.BigDecimal)))] + ("URL", :(@jimport(java.net.URL)))] m = Symbol(string("get", s[1])) n = Symbol(string("set", s[1])) v = quote @@ -295,6 +299,25 @@ for s in [("String", :JString), eval(v) end +# this requires a special conversion +function getBigDecimal_raw(rs::Union{JResultSet, JCallableStatement}, fld::AbstractString) + jcall(rs, "getBigDecimal", JBigDecimal, (JString,), fld) +end +function getBigDecimal_raw(rs::Union{JResultSet, JCallableStatement}, fld::Integer) + jcall(rs, "getBigDecimal", JBigDecimal, (jint,), fld) +end +function setBigDecimal_raw(stmt::Union{JPreparedStatement, JCallableStatement}, idx::Integer, v) + jcall(stmt, "setBigDecimal", Nothing, (jint, JBigDecimal), idx, v) +end + +getBigDecimal(rs::Union{JResultSet,JCallableStatement}, fld) = Decimal(getBigDecimal_raw(rs, fld)) +function setBigDecimal(stmt::Union{JPreparedStatement,JCallableStatement}, idx::Integer, v::AbstractString) + setBigDecimal_raw(stmt, idx, JBigDecimal((JString,), v)) +end +function setBigDecimal(stmt::Union{JPreparedStatement,JCallableStatement}, idx::Integer, v::Decimal) + setBigDecimal_raw(stmt, idx, JBigDecimal(v)) +end + """ ``` getDate(rs::Union{JResultSet, JCallableStatement}, fld::AbstractString) @@ -548,7 +571,7 @@ global const get_method_dict = Dict( # JDBC_COLTYPE_CLOB => 2005, # JDBC_COLTYPE_DATALINK => 70, JDBC_COLTYPE_DATE => getDate, - JDBC_COLTYPE_DECIMAL => getFloat, + JDBC_COLTYPE_DECIMAL => getBigDecimal, # JDBC_COLTYPE_DISTINCT => 2001, JDBC_COLTYPE_DOUBLE => getDouble, JDBC_COLTYPE_FLOAT => getFloat, @@ -560,7 +583,7 @@ global const get_method_dict = Dict( JDBC_COLTYPE_NCHAR => getNString, # JDBC_COLTYPE_NCLOB => 2011, # JDBC_COLTYPE_NULL => 0, - JDBC_COLTYPE_NUMERIC => getDouble, + JDBC_COLTYPE_NUMERIC => getBigDecimal, JDBC_COLTYPE_NVARCHAR => getNString, # JDBC_COLTYPE_OTHER => 1111, JDBC_COLTYPE_REAL => getFloat, diff --git a/src/tables.jl b/src/tables.jl index f2baa90..8360297 100644 --- a/src/tables.jl +++ b/src/tables.jl @@ -7,14 +7,14 @@ const column_types = Dict( JDBC_COLTYPE_BOOLEAN=>Bool, JDBC_COLTYPE_CHAR=>String, JDBC_COLTYPE_DATE=>Date, - JDBC_COLTYPE_DECIMAL=>Float64, + JDBC_COLTYPE_DECIMAL=>Decimal, JDBC_COLTYPE_DOUBLE=>Float64, JDBC_COLTYPE_FLOAT=>Float32, JDBC_COLTYPE_INTEGER=>Int32, JDBC_COLTYPE_LONGNVARCHAR=>String, JDBC_COLTYPE_LONGVARCHAR=>String, JDBC_COLTYPE_NCHAR=>String, - JDBC_COLTYPE_NUMERIC=>Float64, + JDBC_COLTYPE_NUMERIC=>Decimal, JDBC_COLTYPE_NVARCHAR=>String, JDBC_COLTYPE_REAL=>Float64, JDBC_COLTYPE_ROWID=>Int64, diff --git a/test/runtests.jl b/test/runtests.jl index 645d3a7..8a6fd7b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,6 +4,7 @@ using JDBC using DataFrames using Test using Dates +using Decimals import Pkg derby_driver_path = joinpath(dirname(pathof(JDBC)),"..","test","derby.jar") @@ -94,23 +95,28 @@ stmt = createStatement(conn) executeUpdate(stmt, "CREATE TABLE FIRSTTABLE (ID INT PRIMARY KEY, - NAME VARCHAR(12))") -ppstmt = prepareStatement(conn, "insert into firsttable values (?, ?)") + NAME VARCHAR(12), + VALUE DECIMAL(5,2))") +ppstmt = prepareStatement(conn, "insert into firsttable values (?, ?, ?)") setInt(ppstmt, 1,10) setString(ppstmt, 2,"TEN") +setBigDecimal(ppstmt, 3, decimal("10.38")) executeUpdate(ppstmt) setInt(ppstmt, 1,20) setString(ppstmt, 2,"TWENTY") +setBigDecimal(ppstmt, 3, decimal("-1.72e2")) executeUpdate(ppstmt) -rs=executeQuery(stmt, "select * from FIRSTTABLE") +rs = executeQuery(stmt, "select * from FIRSTTABLE") ft = JDBC.load(DataFrame, rs) @testset "Query3" begin - @test size(ft) == (2,2) + @test size(ft) == (2,3) @test ft[1, :ID] == 10 @test ft[1, :NAME] == "TEN" + @test ft[1, :VALUE] == decimal("10.38") @test ft[2, :ID] == 20 @test ft[2, :NAME] == "TWENTY" + @test ft[2, :VALUE] == decimal("-1.72e2") end close(rs) From c71ec64f027e72341dfce370e1aca18c83034688 Mon Sep 17 00:00:00 2001 From: ExpandingMan Date: Thu, 21 Nov 2019 15:46:58 -0500 Subject: [PATCH 2/3] deleted Manifest.toml --- Manifest.toml | 108 -------------------------------------------------- 1 file changed, 108 deletions(-) delete mode 100644 Manifest.toml diff --git a/Manifest.toml b/Manifest.toml deleted file mode 100644 index 9d8c942..0000000 --- a/Manifest.toml +++ /dev/null @@ -1,108 +0,0 @@ -# This file is machine-generated - editing it directly is not advised - -[[Base64]] -uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" - -[[DataAPI]] -git-tree-sha1 = "674b67f344687a88310213ddfa8a2b3c76cc4252" -uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" -version = "1.1.0" - -[[DataStructures]] -deps = ["InteractiveUtils", "OrderedCollections"] -git-tree-sha1 = "1fe8fad5fc84686dcbc674aa255bc867a64f8132" -uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" -version = "0.17.5" - -[[DataValueInterfaces]] -git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6" -uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464" -version = "1.0.0" - -[[Dates]] -deps = ["Printf"] -uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" - -[[Decimals]] -deps = ["Test"] -git-tree-sha1 = "b374d464d64470e743c5f9172c57352e1a9404ac" -uuid = "abce61dc-4473-55a0-ba07-351d65e31d42" -version = "0.4.0" - -[[Distributed]] -deps = ["Random", "Serialization", "Sockets"] -uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" - -[[InteractiveUtils]] -deps = ["Markdown"] -uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" - -[[IteratorInterfaceExtensions]] -git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856" -uuid = "82899510-4779-5014-852e-03e436cf321d" -version = "1.0.0" - -[[JavaCall]] -deps = ["DataStructures", "Dates", "Libdl", "Test", "WinReg"] -git-tree-sha1 = "cd1942b4201ed14923927f30514ef98112fe60c1" -uuid = "494afd89-becb-516b-aafa-70d2670c0337" -version = "0.7.2" - -[[Libdl]] -uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" - -[[LinearAlgebra]] -deps = ["Libdl"] -uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" - -[[Logging]] -uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" - -[[Markdown]] -deps = ["Base64"] -uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" - -[[OrderedCollections]] -deps = ["Random", "Serialization", "Test"] -git-tree-sha1 = "c4c13474d23c60d20a67b217f1d7f22a40edf8f1" -uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" -version = "1.1.0" - -[[Printf]] -deps = ["Unicode"] -uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" - -[[Random]] -deps = ["Serialization"] -uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" - -[[Serialization]] -uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" - -[[Sockets]] -uuid = "6462fe0b-24de-5631-8697-dd941f90decc" - -[[TableTraits]] -deps = ["IteratorInterfaceExtensions"] -git-tree-sha1 = "b1ad568ba658d8cbb3b892ed5380a6f3e781a81e" -uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c" -version = "1.0.0" - -[[Tables]] -deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "TableTraits", "Test"] -git-tree-sha1 = "aaed7b3b00248ff6a794375ad6adf30f30ca5591" -uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" -version = "0.2.11" - -[[Test]] -deps = ["Distributed", "InteractiveUtils", "Logging", "Random"] -uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[[Unicode]] -uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" - -[[WinReg]] -deps = ["Test"] -git-tree-sha1 = "808380e0a0483e134081cc54150be4177959b5f4" -uuid = "1b915085-20d7-51cf-bf83-8f477d6f5128" -version = "0.3.1" From 6cb2d9281b3b541c6ac875c057f2e49d5b8fdca8 Mon Sep 17 00:00:00 2001 From: ExpandingMan Date: Thu, 21 Nov 2019 19:19:48 -0500 Subject: [PATCH 3/3] reverted julia compat change --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1e4eb25..c114bd2 100644 --- a/Project.toml +++ b/Project.toml @@ -10,7 +10,7 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" [compat] JavaCall = "≥ 0.7.0" -julia = "1.3" +julia = "1.0" [extras] DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"