From 6b2545287caf1b412af266f846c987cce4d388e8 Mon Sep 17 00:00:00 2001 From: Chris Bartak Date: Thu, 23 Mar 2023 12:09:21 -0500 Subject: [PATCH 1/7] DBInterface integration --- .gitignore | 1 + Project.toml | 2 ++ docs/src/index.md | 13 +++++++++++++ src/LibPQ.jl | 2 ++ src/dbinterface.jl | 10 ++++++++++ test/runtests.jl | 26 ++++++++++++++++++++++++++ 6 files changed, 54 insertions(+) create mode 100644 src/dbinterface.jl diff --git a/.gitignore b/.gitignore index 1c5e26df..9514dfe0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store +.vscode *.jl.cov *.jl.*.cov *.jl.mem diff --git a/Project.toml b/Project.toml index ee3cd68d..4f3055af 100644 --- a/Project.toml +++ b/Project.toml @@ -5,6 +5,7 @@ version = "1.15.1" [deps] CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82" +DBInterface = "a10d1c49-ce27-4219-8d33-6db1a4562965" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Decimals = "abce61dc-4473-55a0-ba07-351d65e31d42" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" @@ -25,6 +26,7 @@ UTCDateTimes = "0f7cfa37-7abf-4834-b969-a8aa512401c2" [compat] CEnum = "0.2, 0.3, 0.4" DataFrames = "0.20, 0.21, 0.22, 1" +DBInterface = "2" Decimals = "0.4.1" DocStringExtensions = "0.8.0, 0.9.1" Infinity = "0.2" diff --git a/docs/src/index.md b/docs/src/index.md index 3d5c0121..5fbb6853 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -99,3 +99,16 @@ execute(conn, copyin) close(conn) ``` + +### `DBInterface Integration` + +LibPQ types can also be used with the generic [DBInterface.jl](https://github.com/JuliaDatabases/DBInterface.jl) +package to connect to and query Postgres databases. + +```julia +using LibPQ, DBInterface + +conn = DBInterface.connect(LibPQ.Connection, "dbname=postgres") +res = DBInterface.execute(con, "SELECT * FROM table") +DBInterface.close!(conn) +``` \ No newline at end of file diff --git a/src/LibPQ.jl b/src/LibPQ.jl index db7a17f2..7403cc21 100644 --- a/src/LibPQ.jl +++ b/src/LibPQ.jl @@ -29,6 +29,7 @@ using OffsetArrays using SQLStrings using TimeZones using UTCDateTimes +using DBInterface const Parameter = Union{String,Missing} const LOGGER = getlogger(@__MODULE__) @@ -95,6 +96,7 @@ include("exceptions.jl") include("parsing.jl") include("copy.jl") include("tables.jl") +include("dbinterface.jl") include("asyncresults.jl") diff --git a/src/dbinterface.jl b/src/dbinterface.jl new file mode 100644 index 00000000..512b5145 --- /dev/null +++ b/src/dbinterface.jl @@ -0,0 +1,10 @@ +DBInterface.connect(::Type{Connection}, args...; kws...) = + LibPQ.Connection(args...; kws...) + +DBInterface.prepare(conn::Connection, args...; kws...) = + LibPQ.prepare(conn, args...; kws...) + +DBInterface.execute(conn::Union{Connection, Statement}, args...; kws...) = + LibPQ.execute(conn, args...; kws...) + +DBInterface.close!(conn::Connection) = LibPQ.close(conn) diff --git a/test/runtests.jl b/test/runtests.jl index 37a3ba84..f153a605 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -11,6 +11,7 @@ using Memento using Memento.TestUtils using OffsetArrays using SQLStrings +using DBInterface using TimeZones using Tables using UTCDateTimes @@ -1893,6 +1894,31 @@ end close(conn) end + + @testset "DBInterface integration" begin + conn = DBInterface.connect("dbname=postgres user=$DATABASE_USER") + @test conn isa LibPQ.Connection + + result = DBInterface.execute( + conn, + "SELECT typname FROM pg_type WHERE oid = 16"; + ) + @test result isa LibPQ.Result + @test status(result) == LibPQ.libpq_c.PGRES_TUPLES_OK + @test isopen(result) + @test LibPQ.num_columns(result) == 1 + @test LibPQ.num_rows(result) == 1 + @test LibPQ.column_name(result, 1) == "typname" + @test LibPQ.column_number(result, "typname") == 1 + + data = columntable(result) + + @test data[:typname][1] == "bool" + + DBInterface.close!(result) + @test !isopen(result) + + end end end From a0cd896a161059e75c6e4ac94d62bdb7ce2a5a97 Mon Sep 17 00:00:00 2001 From: Chris Bartak Date: Thu, 23 Mar 2023 12:14:47 -0500 Subject: [PATCH 2/7] remove module prefix --- src/dbinterface.jl | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/dbinterface.jl b/src/dbinterface.jl index 512b5145..9f1824b6 100644 --- a/src/dbinterface.jl +++ b/src/dbinterface.jl @@ -1,10 +1,9 @@ -DBInterface.connect(::Type{Connection}, args...; kws...) = - LibPQ.Connection(args...; kws...) +DBInterface.connect(::Type{Connection}, args...; kws...) = + Connection(args...; kws...) -DBInterface.prepare(conn::Connection, args...; kws...) = - LibPQ.prepare(conn, args...; kws...) +DBInterface.prepare(conn::Connection, args...; kws...) = + prepare(conn, args...; kws...) -DBInterface.execute(conn::Union{Connection, Statement}, args...; kws...) = - LibPQ.execute(conn, args...; kws...) +DBInterface.execute(conn::Union{Connection, Statement}, args...; kws...) = execute(conn, args...; kws...) -DBInterface.close!(conn::Connection) = LibPQ.close(conn) +DBInterface.close!(conn::Connection) = close(conn) From 8d6a987c711ede0c9e5b86f795b8bc018d817dab Mon Sep 17 00:00:00 2001 From: Chris Bartak Date: Thu, 23 Mar 2023 12:16:31 -0500 Subject: [PATCH 3/7] fmt --- src/dbinterface.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/dbinterface.jl b/src/dbinterface.jl index 9f1824b6..0308b50e 100644 --- a/src/dbinterface.jl +++ b/src/dbinterface.jl @@ -1,8 +1,6 @@ -DBInterface.connect(::Type{Connection}, args...; kws...) = - Connection(args...; kws...) +DBInterface.connect(::Type{Connection}, args...; kws...) = Connection(args...; kws...) -DBInterface.prepare(conn::Connection, args...; kws...) = - prepare(conn, args...; kws...) +DBInterface.prepare(conn::Connection, args...; kws...) = prepare(conn, args...; kws...) DBInterface.execute(conn::Union{Connection, Statement}, args...; kws...) = execute(conn, args...; kws...) From 4f746fb471df29686ecb0d0db25703833674c3ce Mon Sep 17 00:00:00 2001 From: Chris Bartak Date: Thu, 23 Mar 2023 12:36:02 -0500 Subject: [PATCH 4/7] fmt again --- src/dbinterface.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/dbinterface.jl b/src/dbinterface.jl index 0308b50e..19a72666 100644 --- a/src/dbinterface.jl +++ b/src/dbinterface.jl @@ -2,6 +2,8 @@ DBInterface.connect(::Type{Connection}, args...; kws...) = Connection(args...; k DBInterface.prepare(conn::Connection, args...; kws...) = prepare(conn, args...; kws...) -DBInterface.execute(conn::Union{Connection, Statement}, args...; kws...) = execute(conn, args...; kws...) +function DBInterface.execute(conn::Union{Connection,Statement}, args...; kws...) + return execute(conn, args...; kws...) +end DBInterface.close!(conn::Connection) = close(conn) From 3429f49d881730f6f8137974b5b7517ad8dcf1ef Mon Sep 17 00:00:00 2001 From: Chris Bartak Date: Tue, 28 Mar 2023 12:16:43 -0500 Subject: [PATCH 5/7] add test for DBInterface.prepare --- test/runtests.jl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index f153a605..834f1868 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1910,11 +1910,24 @@ end @test LibPQ.num_rows(result) == 1 @test LibPQ.column_name(result, 1) == "typname" @test LibPQ.column_number(result, "typname") == 1 - data = columntable(result) - @test data[:typname][1] == "bool" + qstr = "SELECT \$1::double precision as foo, typname FROM pg_type WHERE oid = \$2" + stmt = DBInterface.prepare(conn, qstr) + result = DBInterface.execute( + conn, + qstr, + (1.0, 16); + ) + @test result isa LibPQ.Result + @test status(result) == LibPQ.libpq_c.PGRES_TUPLES_OK + @test isopen(result) + @test LibPQ.num_columns(result) == 2 + @test LibPQ.num_rows(result) == 1 + @test LibPQ.column_name(result, 1) == "foo" + @test LibPQ.column_name(result, 2) == "typname" + DBInterface.close!(result) @test !isopen(result) From 7f380dd953a0ee7f211cbca6c27f08338e05685e Mon Sep 17 00:00:00 2001 From: chris-b1 Date: Tue, 28 Mar 2023 12:24:21 -0500 Subject: [PATCH 6/7] Update test/runtests.jl Co-authored-by: Eric Davies --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 834f1868..da01e050 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1896,7 +1896,7 @@ end end @testset "DBInterface integration" begin - conn = DBInterface.connect("dbname=postgres user=$DATABASE_USER") + conn = DBInterface.connect(LibPQ.Connection, "dbname=postgres user=$DATABASE_USER") @test conn isa LibPQ.Connection result = DBInterface.execute( From 81e68da51880039509514a3875f8e9745dc31b3f Mon Sep 17 00:00:00 2001 From: chris-b1 Date: Wed, 29 Mar 2023 18:31:05 -0500 Subject: [PATCH 7/7] Update test/runtests.jl Co-authored-by: Eric Davies --- test/runtests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index da01e050..6a018f64 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1928,8 +1928,8 @@ end @test LibPQ.column_name(result, 1) == "foo" @test LibPQ.column_name(result, 2) == "typname" - DBInterface.close!(result) - @test !isopen(result) + DBInterface.close!(conn) + @test !isopen(conn) end end