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 d6802f10..cd30bef3 100644 --- a/Project.toml +++ b/Project.toml @@ -5,6 +5,7 @@ version = "1.16.0" [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 2155643c..cf3a977b 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..19a72666 --- /dev/null +++ b/src/dbinterface.jl @@ -0,0 +1,9 @@ +DBInterface.connect(::Type{Connection}, args...; kws...) = Connection(args...; kws...) + +DBInterface.prepare(conn::Connection, args...; kws...) = prepare(conn, args...; kws...) + +function DBInterface.execute(conn::Union{Connection,Statement}, args...; kws...) + return execute(conn, args...; kws...) +end + +DBInterface.close!(conn::Connection) = close(conn) diff --git a/test/runtests.jl b/test/runtests.jl index 37a3ba84..6a018f64 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,44 @@ end close(conn) end + + @testset "DBInterface integration" begin + conn = DBInterface.connect(LibPQ.Connection, "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" + + 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!(conn) + @test !isopen(conn) + + end end end